1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 from snaps.openstack.utils import nova_utils
20 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
21 from snaps.openstack.create_flavor import FlavorSettings
23 __author__ = 'spisarski'
25 logger = logging.getLogger('nova_utils_tests')
28 class NovaSmokeTests(OSComponentTestCase):
30 Tests to ensure that the nova client can communicate with the cloud
33 def test_nova_connect_success(self):
35 Tests to ensure that the proper credentials can connect.
37 nova = nova_utils.nova_client(self.os_creds)
39 # This should not throw an exception
42 def test_nova_connect_fail(self):
44 Tests to ensure that the improper credentials cannot connect.
46 from snaps.openstack.os_credentials import OSCreds
48 nova = nova_utils.nova_client(
49 OSCreds(username='user', password='pass', auth_url=self.os_creds.auth_url,
50 project_name=self.os_creds.project_name, proxy_settings=self.os_creds.proxy_settings))
52 # This should throw an exception
53 with self.assertRaises(Exception):
57 class NovaUtilsKeypairTests(OSComponentTestCase):
59 Test basic nova keypair functionality
64 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
67 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
68 self.priv_key_file_path = 'tmp/' + guid
69 self.pub_key_file_path = self.priv_key_file_path + '.pub'
71 self.nova = nova_utils.nova_client(self.os_creds)
72 self.keys = nova_utils.create_keys()
73 self.public_key = nova_utils.public_key_openssh(self.keys)
74 self.keypair_name = guid
76 self.floating_ip = None
80 Cleans the image and downloaded image file
84 nova_utils.delete_keypair(self.nova, self.keypair)
89 os.chmod(self.priv_key_file_path, 0o777)
90 os.remove(self.priv_key_file_path)
95 os.chmod(self.pub_key_file_path, 0o777)
96 os.remove(self.pub_key_file_path)
101 nova_utils.delete_floating_ip(self.nova, self.floating_ip)
103 def test_create_keypair(self):
105 Tests the creation of an OpenStack keypair that does not exist.
107 self.keypair = nova_utils.upload_keypair(self.nova, self.keypair_name, self.public_key)
108 result = nova_utils.keypair_exists(self.nova, self.keypair)
109 self.assertEqual(self.keypair, result)
110 keypair = nova_utils.get_keypair_by_name(self.nova, self.keypair_name)
111 self.assertEqual(self.keypair, keypair)
113 def test_create_delete_keypair(self):
115 Tests the creation of an OpenStack keypair that does not exist.
117 self.keypair = nova_utils.upload_keypair(self.nova, self.keypair_name, self.public_key)
118 result = nova_utils.keypair_exists(self.nova, self.keypair)
119 self.assertEqual(self.keypair, result)
120 nova_utils.delete_keypair(self.nova, self.keypair)
121 result2 = nova_utils.keypair_exists(self.nova, self.keypair)
122 self.assertIsNone(result2)
124 def test_create_key_from_file(self):
126 Tests that the generated RSA keys are properly saved to files
129 nova_utils.save_keys_to_files(self.keys, self.pub_key_file_path, self.priv_key_file_path)
130 self.keypair = nova_utils.upload_keypair_file(self.nova, self.keypair_name, self.pub_key_file_path)
131 pub_key_file = open(os.path.expanduser(self.pub_key_file_path))
132 pub_key = pub_key_file.read()
134 self.assertEqual(self.keypair.public_key, pub_key)
136 def test_floating_ips(self):
138 Tests the creation of a floating IP
141 ips = nova_utils.get_floating_ips(self.nova)
142 self.assertIsNotNone(ips)
144 self.floating_ip = nova_utils.create_floating_ip(self.nova, self.ext_net_name)
145 returned = nova_utils.get_floating_ip(self.nova, self.floating_ip)
146 self.assertEqual(self.floating_ip, returned)
149 class NovaUtilsFlavorTests(OSComponentTestCase):
151 Test basic nova flavor functionality
156 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
159 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
160 self.flavor_settings = FlavorSettings(name=guid + '-name', flavor_id=guid + '-id', ram=1, disk=1, vcpus=1,
161 ephemeral=1, swap=2, rxtx_factor=3.0, is_public=False)
162 self.nova = nova_utils.nova_client(self.os_creds)
167 Cleans the image and downloaded image file
171 nova_utils.delete_flavor(self.nova, self.flavor)
175 def test_create_flavor(self):
177 Tests the creation of an OpenStack keypair that does not exist.
179 self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
180 self.validate_flavor()
182 def test_create_delete_flavor(self):
184 Tests the creation of an OpenStack keypair that does not exist.
186 self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
187 self.validate_flavor()
188 nova_utils.delete_flavor(self.nova, self.flavor)
189 flavor = nova_utils.get_flavor_by_name(self.nova, self.flavor_settings.name)
190 self.assertIsNone(flavor)
192 def validate_flavor(self):
194 Validates the flavor_settings against the OpenStack flavor object
196 self.assertIsNotNone(self.flavor)
197 self.assertEqual(self.flavor_settings.name, self.flavor.name)
198 self.assertEqual(self.flavor_settings.flavor_id, self.flavor.id)
199 self.assertEqual(self.flavor_settings.ram, self.flavor.ram)
200 self.assertEqual(self.flavor_settings.disk, self.flavor.disk)
201 self.assertEqual(self.flavor_settings.vcpus, self.flavor.vcpus)
202 self.assertEqual(self.flavor_settings.ephemeral, self.flavor.ephemeral)
204 if self.flavor_settings.swap == 0:
205 self.assertEqual('', self.flavor.swap)
207 self.assertEqual(self.flavor_settings.swap, self.flavor.swap)
209 self.assertEqual(self.flavor_settings.rxtx_factor, self.flavor.rxtx_factor)
210 self.assertEqual(self.flavor_settings.is_public, self.flavor.is_public)