Initial patch with all code from CableLabs repository.
[snaps.git] / snaps / openstack / utils / tests / nova_utils_tests.py
1 # Copyright (c) 2016 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 Crypto.PublicKey import RSA
20
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
24
25 __author__ = 'spisarski'
26
27 logger = logging.getLogger('nova_utils_tests')
28
29
30 class NovaSmokeTests(OSComponentTestCase):
31     """
32     Tests to ensure that the nova client can communicate with the cloud
33     """
34
35     def test_nova_connect_success(self):
36         """
37         Tests to ensure that the proper credentials can connect.
38         """
39         nova = nova_utils.nova_client(self.os_creds)
40
41         # This should not throw an exception
42         nova.flavors.list()
43
44     def test_nova_connect_fail(self):
45         """
46         Tests to ensure that the improper credentials cannot connect.
47         """
48         from snaps.openstack.os_credentials import OSCreds
49
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))
53
54         # This should throw an exception
55         with self.assertRaises(Exception):
56             nova.flavors.list()
57
58
59 class NovaUtilsKeypairTests(OSComponentTestCase):
60     """
61     Test basic nova keypair functionality
62     """
63
64     def setUp(self):
65         """
66         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
67         within OpenStack
68         """
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'
72
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
77         self.keypair = None
78         self.floating_ip = None
79
80     def tearDown(self):
81         """
82         Cleans the image and downloaded image file
83         """
84         if self.keypair:
85             try:
86                 nova_utils.delete_keypair(self.nova, self.keypair)
87             except:
88                 pass
89
90         try:
91             os.remove(self.priv_key_file_path)
92         except:
93             pass
94
95         try:
96             os.remove(self.pub_key_file_path)
97         except:
98             pass
99
100         if self.floating_ip:
101             nova_utils.delete_floating_ip(self.nova, self.floating_ip)
102
103     def test_create_keypair(self):
104         """
105         Tests the creation of an OpenStack keypair that does not exist.
106         """
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)
112
113     def test_create_delete_keypair(self):
114         """
115         Tests the creation of an OpenStack keypair that does not exist.
116         """
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)
123
124     def test_create_key_from_file(self):
125         """
126         Tests that the generated RSA keys are properly saved to files
127         :return:
128         """
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)
133
134     def test_floating_ips(self):
135         """
136         Tests the creation of a floating IP
137         :return:
138         """
139         ips = nova_utils.get_floating_ips(self.nova)
140         self.assertIsNotNone(ips)
141
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)
145
146
147 class NovaUtilsFlavorTests(OSComponentTestCase):
148     """
149     Test basic nova flavor functionality
150     """
151
152     def setUp(self):
153         """
154         Instantiates the CreateImage object that is responsible for downloading and creating an OS image file
155         within OpenStack
156         """
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)
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, self.flavor_settings.name)
188         self.assertIsNone(flavor)
189
190     def validate_flavor(self):
191         """
192         Validates the flavor_settings against the OpenStack flavor object
193         """
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)
201
202         if self.flavor_settings.swap == 0:
203             self.assertEquals('', self.flavor.swap)
204         else:
205             self.assertEquals(self.flavor_settings.swap, self.flavor.swap)
206
207         self.assertEquals(self.flavor_settings.rxtx_factor, self.flavor.rxtx_factor)
208         self.assertEquals(self.flavor_settings.is_public, self.flavor.is_public)