1 # Copyright (c) 2016 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 Crypto.PublicKey import RSA
21 from snaps.openstack.utils import nova_utils
22 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
23 from snaps.openstack.create_flavor import FlavorSettings
25 __author__ = 'spisarski'
27 logger = logging.getLogger('nova_utils_tests')
30 class NovaSmokeTests(OSComponentTestCase):
32 Tests to ensure that the nova client can communicate with the cloud
35 def test_nova_connect_success(self):
37 Tests to ensure that the proper credentials can connect.
39 nova = nova_utils.nova_client(self.os_creds)
41 # This should not throw an exception
44 def test_nova_connect_fail(self):
46 Tests to ensure that the improper credentials cannot connect.
48 from snaps.openstack.os_credentials import OSCreds
50 nova = nova_utils.nova_client(
51 OSCreds(username='user', password='pass', auth_url=self.os_creds.auth_url,
52 project_name=self.os_creds.project_name, proxy_settings=self.os_creds.proxy_settings))
54 # This should throw an exception
55 with self.assertRaises(Exception):
59 class NovaUtilsKeypairTests(OSComponentTestCase):
61 Test basic nova keypair functionality
66 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
69 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
70 self.priv_key_file_path = 'tmp/' + guid
71 self.pub_key_file_path = self.priv_key_file_path + '.pub'
73 self.nova = nova_utils.nova_client(self.os_creds)
74 self.keys = RSA.generate(1024)
75 self.public_key = self.keys.publickey().exportKey('OpenSSH')
76 self.keypair_name = guid
78 self.floating_ip = None
82 Cleans the image and downloaded image file
86 nova_utils.delete_keypair(self.nova, self.keypair)
91 os.remove(self.priv_key_file_path)
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.assertEquals(self.keypair, result)
110 keypair = nova_utils.get_keypair_by_name(self.nova, self.keypair_name)
111 self.assertEquals(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.assertEquals(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 = open(os.path.expanduser(self.pub_key_file_path)).read()
132 self.assertEquals(self.keypair.public_key, pub_key)
134 def test_floating_ips(self):
136 Tests the creation of a floating IP
139 ips = nova_utils.get_floating_ips(self.nova)
140 self.assertIsNotNone(ips)
142 self.floating_ip = nova_utils.create_floating_ip(self.nova, self.ext_net_name)
143 returned = nova_utils.get_floating_ip(self.nova, self.floating_ip)
144 self.assertEquals(self.floating_ip, returned)
147 class NovaUtilsFlavorTests(OSComponentTestCase):
149 Test basic nova flavor functionality
154 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
157 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
158 self.flavor_settings = FlavorSettings(name=guid + '-name', flavor_id=guid + '-id', ram=1, disk=1, vcpus=1,
159 ephemeral=1, swap=2, rxtx_factor=3.0, is_public=False)
160 self.nova = nova_utils.nova_client(self.os_creds)
165 Cleans the image and downloaded image file
169 nova_utils.delete_flavor(self.nova, self.flavor)
173 def test_create_flavor(self):
175 Tests the creation of an OpenStack keypair that does not exist.
177 self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
178 self.validate_flavor()
180 def test_create_delete_flavor(self):
182 Tests the creation of an OpenStack keypair that does not exist.
184 self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
185 self.validate_flavor()
186 nova_utils.delete_flavor(self.nova, self.flavor)
187 flavor = nova_utils.get_flavor_by_name(self.nova, self.flavor_settings.name)
188 self.assertIsNone(flavor)
190 def validate_flavor(self):
192 Validates the flavor_settings against the OpenStack flavor object
194 self.assertIsNotNone(self.flavor)
195 self.assertEquals(self.flavor_settings.name, self.flavor.name)
196 self.assertEquals(self.flavor_settings.flavor_id, self.flavor.id)
197 self.assertEquals(self.flavor_settings.ram, self.flavor.ram)
198 self.assertEquals(self.flavor_settings.disk, self.flavor.disk)
199 self.assertEquals(self.flavor_settings.vcpus, self.flavor.vcpus)
200 self.assertEquals(self.flavor_settings.ephemeral, self.flavor.ephemeral)
202 if self.flavor_settings.swap == 0:
203 self.assertEquals('', self.flavor.swap)
205 self.assertEquals(self.flavor_settings.swap, self.flavor.swap)
207 self.assertEquals(self.flavor_settings.rxtx_factor, self.flavor.rxtx_factor)
208 self.assertEquals(self.flavor_settings.is_public, self.flavor.is_public)