Modified code to support both Python 2.7 and 3.x
[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 os
17 import uuid
18
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
22
23 __author__ = 'spisarski'
24
25 logger = logging.getLogger('nova_utils_tests')
26
27
28 class NovaSmokeTests(OSComponentTestCase):
29     """
30     Tests to ensure that the nova client can communicate with the cloud
31     """
32
33     def test_nova_connect_success(self):
34         """
35         Tests to ensure that the proper credentials can connect.
36         """
37         nova = nova_utils.nova_client(self.os_creds)
38
39         # This should not throw an exception
40         nova.flavors.list()
41
42     def test_nova_connect_fail(self):
43         """
44         Tests to ensure that the improper credentials cannot connect.
45         """
46         from snaps.openstack.os_credentials import OSCreds
47
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))
51
52         # This should throw an exception
53         with self.assertRaises(Exception):
54             nova.flavors.list()
55
56
57 class NovaUtilsKeypairTests(OSComponentTestCase):
58     """
59     Test basic nova keypair functionality
60     """
61
62     def setUp(self):
63         """
64         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
65         within OpenStack
66         """
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'
70
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
75         self.keypair = None
76         self.floating_ip = None
77
78     def tearDown(self):
79         """
80         Cleans the image and downloaded image file
81         """
82         if self.keypair:
83             try:
84                 nova_utils.delete_keypair(self.nova, self.keypair)
85             except:
86                 pass
87
88         try:
89             os.remove(self.priv_key_file_path)
90         except:
91             pass
92
93         try:
94             os.remove(self.pub_key_file_path)
95         except:
96             pass
97
98         if self.floating_ip:
99             nova_utils.delete_floating_ip(self.nova, self.floating_ip)
100
101     def test_create_keypair(self):
102         """
103         Tests the creation of an OpenStack keypair that does not exist.
104         """
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)
110
111     def test_create_delete_keypair(self):
112         """
113         Tests the creation of an OpenStack keypair that does not exist.
114         """
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)
121
122     def test_create_key_from_file(self):
123         """
124         Tests that the generated RSA keys are properly saved to files
125         :return:
126         """
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)
131
132     def test_floating_ips(self):
133         """
134         Tests the creation of a floating IP
135         :return:
136         """
137         ips = nova_utils.get_floating_ips(self.nova)
138         self.assertIsNotNone(ips)
139
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)
143
144
145 class NovaUtilsFlavorTests(OSComponentTestCase):
146     """
147     Test basic nova flavor functionality
148     """
149
150     def setUp(self):
151         """
152         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
153         within OpenStack
154         """
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)
159         self.flavor = None
160
161     def tearDown(self):
162         """
163         Cleans the image and downloaded image file
164         """
165         if self.flavor:
166             try:
167                 nova_utils.delete_flavor(self.nova, self.flavor)
168             except:
169                 pass
170
171     def test_create_flavor(self):
172         """
173         Tests the creation of an OpenStack keypair that does not exist.
174         """
175         self.flavor = nova_utils.create_flavor(self.nova, self.flavor_settings)
176         self.validate_flavor()
177
178     def test_create_delete_flavor(self):
179         """
180         Tests the creation of an OpenStack keypair that does not exist.
181         """
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)
187
188     def validate_flavor(self):
189         """
190         Validates the flavor_settings against the OpenStack flavor object
191         """
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)
199
200         if self.flavor_settings.swap == 0:
201             self.assertEqual('', self.flavor.swap)
202         else:
203             self.assertEqual(self.flavor_settings.swap, self.flavor.swap)
204
205         self.assertEqual(self.flavor_settings.rxtx_factor, self.flavor.rxtx_factor)
206         self.assertEqual(self.flavor_settings.is_public, self.flavor.is_public)