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.remove(self.priv_key_file_path)
94 os.remove(self.pub_key_file_path)
99 nova_utils.delete_floating_ip(self.nova, self.floating_ip)
101 def test_create_keypair(self):
103 Tests the creation of an OpenStack keypair that does not exist.
105 self.keypair = nova_utils.upload_keypair(self.nova, self.keypair_name, self.public_key)
106 result = nova_utils.keypair_exists(self.nova, self.keypair)
107 self.assertEqual(self.keypair, result)
108 keypair = nova_utils.get_keypair_by_name(self.nova, self.keypair_name)
109 self.assertEqual(self.keypair, keypair)
111 def test_create_delete_keypair(self):
113 Tests the creation of an OpenStack keypair that does not exist.
115 self.keypair = nova_utils.upload_keypair(self.nova, self.keypair_name, self.public_key)
116 result = nova_utils.keypair_exists(self.nova, self.keypair)
117 self.assertEqual(self.keypair, result)
118 nova_utils.delete_keypair(self.nova, self.keypair)
119 result2 = nova_utils.keypair_exists(self.nova, self.keypair)
120 self.assertIsNone(result2)
122 def test_create_key_from_file(self):
124 Tests that the generated RSA keys are properly saved to files
127 nova_utils.save_keys_to_files(self.keys, self.pub_key_file_path, self.priv_key_file_path)
128 self.keypair = nova_utils.upload_keypair_file(self.nova, self.keypair_name, self.pub_key_file_path)
129 pub_key = open(os.path.expanduser(self.pub_key_file_path)).read()
130 self.assertEqual(self.keypair.public_key, pub_key)
132 def test_floating_ips(self):
134 Tests the creation of a floating IP
137 ips = nova_utils.get_floating_ips(self.nova)
138 self.assertIsNotNone(ips)
140 self.floating_ip = nova_utils.create_floating_ip(self.nova, self.ext_net_name)
141 returned = nova_utils.get_floating_ip(self.nova, self.floating_ip)
142 self.assertEqual(self.floating_ip, returned)
145 class NovaUtilsFlavorTests(OSComponentTestCase):
147 Test basic nova flavor functionality
152 Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
155 guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
156 self.flavor_settings = FlavorSettings(name=guid + '-name', flavor_id=guid + '-id', ram=1, disk=1, vcpus=1,
157 ephemeral=1, swap=2, rxtx_factor=3.0, is_public=False)
158 self.nova = nova_utils.nova_client(self.os_creds)
163 Cleans the image and downloaded image file
167 nova_utils.delete_flavor(self.nova, self.flavor)
171 def test_create_flavor(self):
173 Tests the creation of an OpenStack keypair that does not exist.
175 self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
176 self.validate_flavor()
178 def test_create_delete_flavor(self):
180 Tests the creation of an OpenStack keypair that does not exist.
182 self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
183 self.validate_flavor()
184 nova_utils.delete_flavor(self.nova, self.flavor)
185 flavor = nova_utils.get_flavor_by_name(self.nova, self.flavor_settings.name)
186 self.assertIsNone(flavor)
188 def validate_flavor(self):
190 Validates the flavor_settings against the OpenStack flavor object
192 self.assertIsNotNone(self.flavor)
193 self.assertEqual(self.flavor_settings.name, self.flavor.name)
194 self.assertEqual(self.flavor_settings.flavor_id, self.flavor.id)
195 self.assertEqual(self.flavor_settings.ram, self.flavor.ram)
196 self.assertEqual(self.flavor_settings.disk, self.flavor.disk)
197 self.assertEqual(self.flavor_settings.vcpus, self.flavor.vcpus)
198 self.assertEqual(self.flavor_settings.ephemeral, self.flavor.ephemeral)
200 if self.flavor_settings.swap == 0:
201 self.assertEqual('', self.flavor.swap)
203 self.assertEqual(self.flavor_settings.swap, self.flavor.swap)
205 self.assertEqual(self.flavor_settings.rxtx_factor, self.flavor.rxtx_factor)
206 self.assertEqual(self.flavor_settings.is_public, self.flavor.is_public)