552ffc7abe57d995e48d5203a0a7cb06d587ac21
[snaps.git] / snaps / openstack / utils / tests / nova_utils_tests.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 import logging
16 import uuid
17
18 import os
19 from snaps.openstack.create_flavor import FlavorSettings, OpenStackFlavor
20 from snaps.openstack.create_image import OpenStackImage
21 from snaps.openstack.create_instance import VmInstanceSettings
22 from snaps.openstack.create_network import OpenStackNetwork, PortSettings
23 from snaps.openstack.tests import openstack_tests
24 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
25 from snaps.openstack.utils import nova_utils, neutron_utils, glance_utils
26
27 __author__ = 'spisarski'
28
29 logger = logging.getLogger('nova_utils_tests')
30
31
32 class NovaSmokeTests(OSComponentTestCase):
33     """
34     Tests to ensure that the nova client can communicate with the cloud
35     """
36
37     def test_nova_connect_success(self):
38         """
39         Tests to ensure that the proper credentials can connect.
40         """
41         nova = nova_utils.nova_client(self.os_creds)
42
43         # This should not throw an exception
44         nova.flavors.list()
45
46     def test_nova_connect_fail(self):
47         """
48         Tests to ensure that the improper credentials cannot connect.
49         """
50         from snaps.openstack.os_credentials import OSCreds
51
52         nova = nova_utils.nova_client(
53             OSCreds(username='user', password='pass',
54                     auth_url=self.os_creds.auth_url,
55                     project_name=self.os_creds.project_name,
56                     proxy_settings=self.os_creds.proxy_settings))
57
58         # This should throw an exception
59         with self.assertRaises(Exception):
60             nova.flavors.list()
61
62
63 class NovaUtilsKeypairTests(OSComponentTestCase):
64     """
65     Test basic nova keypair functionality
66     """
67
68     def setUp(self):
69         """
70         Instantiates the CreateImage object that is responsible for downloading
71         and creating an OS image file within OpenStack
72         """
73         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
74         self.priv_key_file_path = 'tmp/' + guid
75         self.pub_key_file_path = self.priv_key_file_path + '.pub'
76
77         self.nova = nova_utils.nova_client(self.os_creds)
78         self.keys = nova_utils.create_keys()
79         self.public_key = nova_utils.public_key_openssh(self.keys)
80         self.keypair_name = guid
81         self.keypair = None
82
83     def tearDown(self):
84         """
85         Cleans the image and downloaded image file
86         """
87         if self.keypair:
88             try:
89                 nova_utils.delete_keypair(self.nova, self.keypair)
90             except:
91                 pass
92
93         try:
94             os.chmod(self.priv_key_file_path, 0o777)
95             os.remove(self.priv_key_file_path)
96         except:
97             pass
98
99         try:
100             os.chmod(self.pub_key_file_path, 0o777)
101             os.remove(self.pub_key_file_path)
102         except:
103             pass
104
105     def test_create_keypair(self):
106         """
107         Tests the creation of an OpenStack keypair that does not exist.
108         """
109         self.keypair = nova_utils.upload_keypair(self.nova, self.keypair_name,
110                                                  self.public_key)
111         result = nova_utils.keypair_exists(self.nova, self.keypair)
112         self.assertEqual(self.keypair, result)
113         keypair = nova_utils.get_keypair_by_name(self.nova, self.keypair_name)
114         self.assertEqual(self.keypair, keypair)
115
116     def test_create_delete_keypair(self):
117         """
118         Tests the creation of an OpenStack keypair that does not exist.
119         """
120         self.keypair = nova_utils.upload_keypair(self.nova, self.keypair_name,
121                                                  self.public_key)
122         result = nova_utils.keypair_exists(self.nova, self.keypair)
123         self.assertEqual(self.keypair, result)
124         nova_utils.delete_keypair(self.nova, self.keypair)
125         result2 = nova_utils.keypair_exists(self.nova, self.keypair)
126         self.assertIsNone(result2)
127
128     def test_create_key_from_file(self):
129         """
130         Tests that the generated RSA keys are properly saved to files
131         :return:
132         """
133         nova_utils.save_keys_to_files(self.keys, self.pub_key_file_path,
134                                       self.priv_key_file_path)
135         self.keypair = nova_utils.upload_keypair_file(self.nova,
136                                                       self.keypair_name,
137                                                       self.pub_key_file_path)
138         pub_key_file = open(os.path.expanduser(self.pub_key_file_path))
139         pub_key = pub_key_file.read()
140         pub_key_file.close()
141         self.assertEqual(self.keypair.public_key, pub_key)
142
143
144 class NovaUtilsFlavorTests(OSComponentTestCase):
145     """
146     Test basic nova flavor functionality
147     """
148
149     def setUp(self):
150         """
151         Instantiates the CreateImage object that is responsible for downloading
152         and creating an OS image file within OpenStack
153         """
154         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
155         self.flavor_settings = FlavorSettings(name=guid + '-name',
156                                               flavor_id=guid + '-id', ram=1,
157                                               disk=1, vcpus=1,
158                                               ephemeral=1, swap=2,
159                                               rxtx_factor=3.0, is_public=False)
160         self.nova = nova_utils.nova_client(self.os_creds)
161         self.flavor = None
162
163     def tearDown(self):
164         """
165         Cleans the image and downloaded image file
166         """
167         if self.flavor:
168             try:
169                 nova_utils.delete_flavor(self.nova, self.flavor)
170             except:
171                 pass
172
173     def test_create_flavor(self):
174         """
175         Tests the creation of an OpenStack keypair that does not exist.
176         """
177         self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
178         self.validate_flavor()
179
180     def test_create_delete_flavor(self):
181         """
182         Tests the creation of an OpenStack keypair that does not exist.
183         """
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,
188                                                self.flavor_settings.name)
189         self.assertIsNone(flavor)
190
191     def validate_flavor(self):
192         """
193         Validates the flavor_settings against the OpenStack flavor object
194         """
195         self.assertIsNotNone(self.flavor)
196         self.assertEqual(self.flavor_settings.name, self.flavor.name)
197         self.assertEqual(self.flavor_settings.flavor_id, self.flavor.id)
198         self.assertEqual(self.flavor_settings.ram, self.flavor.ram)
199         self.assertEqual(self.flavor_settings.disk, self.flavor.disk)
200         self.assertEqual(self.flavor_settings.vcpus, self.flavor.vcpus)
201         self.assertEqual(self.flavor_settings.ephemeral, self.flavor.ephemeral)
202
203         if self.flavor_settings.swap == 0:
204             self.assertEqual('', self.flavor.swap)
205         else:
206             self.assertEqual(self.flavor_settings.swap, self.flavor.swap)
207
208         self.assertEqual(self.flavor_settings.rxtx_factor,
209                          self.flavor.rxtx_factor)
210         self.assertEqual(self.flavor_settings.is_public, self.flavor.is_public)
211
212
213 class NovaUtilsInstanceTests(OSComponentTestCase):
214     """
215     Tests the creation of VM instances via nova_utils.py
216     """
217
218     def setUp(self):
219         """
220         Setup objects required by VM instances
221         :return:
222         """
223
224         guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
225
226         self.nova = nova_utils.nova_client(self.os_creds)
227         self.neutron = neutron_utils.neutron_client(self.os_creds)
228         self.glance = glance_utils.glance_client(self.os_creds)
229
230         self.image_creator = None
231         self.network_creator = None
232         self.flavor_creator = None
233         self.port = None
234         self.vm_inst = None
235
236         try:
237             image_settings = openstack_tests.cirros_image_settings(
238                 name=guid + '-image', image_metadata=self.image_metadata)
239             self.image_creator = OpenStackImage(
240                 self.os_creds, image_settings=image_settings)
241             self.image_creator.create()
242
243             network_settings = openstack_tests.get_priv_net_config(
244                 guid + '-net', guid + '-subnet').network_settings
245             self.network_creator = OpenStackNetwork(
246                 self.os_creds, network_settings)
247             self.network_creator.create()
248
249             self.flavor_creator = OpenStackFlavor(
250                 self.os_creds,
251                 FlavorSettings(
252                     name=guid + '-flavor-name', ram=128, disk=10, vcpus=1))
253             self.flavor_creator.create()
254
255             port_settings = PortSettings(name=guid + '-port',
256                                          network_name=network_settings.name)
257             self.port = neutron_utils.create_port(
258                 self.neutron, self.os_creds, port_settings)
259
260             self.instance_settings = VmInstanceSettings(
261                 name=guid + '-vm_inst',
262                 flavor=self.flavor_creator.flavor_settings.name,
263                 port_settings=[port_settings])
264         except:
265             self.tearDown()
266             raise
267
268     def tearDown(self):
269         """
270         Cleanup deployed resources
271         :return:
272         """
273         if self.vm_inst:
274             try:
275                 nova_utils.delete_vm_instance(self.nova, self.vm_inst)
276             except:
277                 pass
278         if self.port:
279             try:
280                 neutron_utils.delete_port(self.neutron, self.port)
281             except:
282                 pass
283         if self.flavor_creator:
284             try:
285                 self.flavor_creator.clean()
286             except:
287                 pass
288         if self.network_creator:
289             try:
290                 self.network_creator.clean()
291             except:
292                 pass
293         if self.image_creator:
294             try:
295                 self.image_creator.clean()
296             except:
297                 pass
298
299     def test_create_instance(self):
300         """
301         Tests the nova_utils.create_server() method
302         :return:
303         """
304
305         self.vm_inst = nova_utils.create_server(
306             self.nova, self.neutron, self.glance, self.instance_settings,
307             self.image_creator.image_settings)
308
309         self.assertIsNotNone(self.vm_inst)
310
311         vm_inst = nova_utils.get_latest_server_object(self.nova, self.vm_inst)
312
313         self.assertEqual(self.vm_inst.name, vm_inst.name)
314         self.assertEqual(self.vm_inst.id, vm_inst.id)