Closing keystone sessions after done with them.
[snaps.git] / snaps / openstack / tests / cluster_template_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 from magnumclient.common.apiclient.exceptions import BadRequest
16
17 from snaps.config.cluster_template import ClusterTemplateConfig
18 from snaps.config.flavor import FlavorConfig
19 from snaps.config.keypair import KeypairConfig
20 from snaps.openstack.cluster_template import OpenStackClusterTemplate
21 from snaps.openstack.create_flavor import OpenStackFlavor
22 from snaps.openstack.create_image import OpenStackImage
23 from snaps.openstack.create_keypairs import OpenStackKeypair
24 from snaps.openstack.tests import openstack_tests
25
26 try:
27     from urllib.request import URLError
28 except ImportError:
29     from urllib2 import URLError
30
31 import logging
32 import uuid
33
34 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
35 from snaps.openstack.utils import magnum_utils
36
37 __author__ = 'spisarski'
38
39 logger = logging.getLogger('cluster_template_tests')
40
41
42 class CreateClusterTemplateTests(OSIntegrationTestCase):
43     """
44     Test for the OpenStackClusterTemplate class defined in py
45     without any QoS Specs or Encryption
46     """
47
48     def setUp(self):
49         """
50         Instantiates the CreateClusterTemplate object that is responsible for
51         downloading and creating an OS template config file within OpenStack
52         """
53         super(self.__class__, self).__start__()
54
55         self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
56         self.cluster_type_name = self.guid + '-cluster-type'
57         self.magnum = magnum_utils.magnum_client(
58             self.os_creds, self.os_session)
59
60         metadata = self.image_metadata
61         if not metadata:
62             metadata = dict()
63         if 'extra_properties' not in metadata:
64             metadata['extra_properties'] = dict()
65         metadata['extra_properties']['os_distro'] = 'cirros'
66
67         os_image_settings = openstack_tests.cirros_image_settings(
68             name=self.guid + '-image', image_metadata=metadata)
69
70         self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
71
72         self.flavor_creator = OpenStackFlavor(
73             self.os_creds, FlavorConfig(
74                 name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))
75
76         keypair_priv_filepath = 'tmp/' + self.guid
77         keypair_pub_filepath = keypair_priv_filepath + '.pub'
78
79         self.keypair_creator = OpenStackKeypair(
80             self.os_creds, KeypairConfig(
81                 name=self.guid + '-keypair',
82                 public_filepath=keypair_pub_filepath,
83                 private_filepath=keypair_priv_filepath))
84
85         self.cluster_template_creator = None
86
87         self.cluster_template_config = ClusterTemplateConfig(
88             name=self.cluster_type_name,
89             image=self.image_creator.image_settings.name,
90             keypair=self.keypair_creator.keypair_settings.name,
91             external_net=self.ext_net_name,
92             flavor=self.flavor_creator.flavor_settings.name)
93
94         try:
95             self.image_creator.create()
96             self.flavor_creator.create()
97             self.keypair_creator.create()
98         except:
99             self.tearDown()
100             raise
101
102     def tearDown(self):
103         """
104         Cleans the template config
105         """
106         if self.cluster_template_creator:
107             try:
108                 self.cluster_template_creator.clean()
109             except:
110                 pass
111         if self.keypair_creator:
112             try:
113                 self.keypair_creator.clean()
114             except:
115                 pass
116         if self.flavor_creator:
117             try:
118                 self.flavor_creator.clean()
119             except:
120                 pass
121         if self.image_creator:
122             try:
123                 self.image_creator.clean()
124             except:
125                 pass
126
127         super(self.__class__, self).__clean__()
128
129     def test_create_cluster_template(self):
130         """
131         Tests the creation of an OpenStack cluster template.
132         """
133         # Create ClusterTemplate
134         self.cluster_template_creator = OpenStackClusterTemplate(
135             self.os_creds, self.cluster_template_config)
136         created_cluster_template = self.cluster_template_creator.create()
137         self.assertIsNotNone(created_cluster_template)
138         self.assertEqual(self.cluster_template_config.name,
139                          created_cluster_template.name)
140
141         retrieved_cluster_template1 = magnum_utils.get_cluster_template(
142             self.magnum, template_config=self.cluster_template_config)
143         self.assertIsNotNone(retrieved_cluster_template1)
144         self.assertEqual(created_cluster_template, retrieved_cluster_template1)
145
146         retrieved_cluster_template2 = magnum_utils.get_cluster_template_by_id(
147             self.magnum, created_cluster_template.id)
148         self.assertEqual(created_cluster_template, retrieved_cluster_template2)
149
150     def test_create_delete_cluster_template(self):
151         """
152         Tests the creation then deletion of an OpenStack template config to
153         ensure clean() does not raise an Exception.
154         """
155         # Create ClusterTemplate
156         self.cluster_template_creator = OpenStackClusterTemplate(
157             self.os_creds, self.cluster_template_config)
158         created_cluster_template = self.cluster_template_creator.create()
159         self.assertIsNotNone(created_cluster_template)
160
161         self.cluster_template_creator.clean()
162
163         tmplt = magnum_utils.get_cluster_template(
164             self.magnum, template_name=self.cluster_template_config.name)
165         self.assertIsNone(tmplt)
166
167     def test_create_same_cluster_template(self):
168         """
169         Tests the creation of an OpenStack cluster_template when one already
170         exists.
171         """
172         # Create ClusterTemplate
173         self.cluster_template_creator = OpenStackClusterTemplate(
174             self.os_creds, self.cluster_template_config)
175         cluster_template1 = self.cluster_template_creator.create()
176
177         retrieved_cluster_template = magnum_utils.get_cluster_template(
178             self.magnum, template_config=self.cluster_template_config)
179         self.assertEqual(cluster_template1, retrieved_cluster_template)
180
181         # Should be retrieving the instance data
182         os_cluster_template_2 = OpenStackClusterTemplate(
183             self.os_creds, self.cluster_template_config)
184         cluster_template2 = os_cluster_template_2.create()
185         self.assertEqual(cluster_template2, cluster_template2)
186
187     def test_create_cluster_template_bad_flavor(self):
188         """
189         Tests the creation of an OpenStack cluster template raises an
190         exception with an invalid flavor.
191         """
192         # Create ClusterTemplate
193         cluster_template_config = ClusterTemplateConfig(
194             name=self.cluster_type_name,
195             image=self.image_creator.image_settings.name,
196             keypair=self.keypair_creator.keypair_settings.name,
197             external_net=self.ext_net_name,
198             flavor='foo')
199
200         self.cluster_template_creator = OpenStackClusterTemplate(
201             self.os_creds, cluster_template_config)
202
203         with self.assertRaises(BadRequest):
204             self.cluster_template_creator.create()
205
206     def test_create_cluster_template_bad_master_flavor(self):
207         """
208         Tests the creation of an OpenStack cluster template raises an
209         exception with an invalid master flavor.
210         """
211         # Create ClusterTemplate
212         cluster_template_config = ClusterTemplateConfig(
213             name=self.cluster_type_name,
214             image=self.image_creator.image_settings.name,
215             keypair=self.keypair_creator.keypair_settings.name,
216             external_net=self.ext_net_name,
217             flavor=self.flavor_creator.flavor_settings.name,
218             master_flavor='foo')
219
220         self.cluster_template_creator = OpenStackClusterTemplate(
221             self.os_creds, cluster_template_config)
222
223         with self.assertRaises(BadRequest):
224             self.cluster_template_creator.create()
225
226     def test_create_cluster_template_bad_image(self):
227         """
228         Tests the creation of an OpenStack cluster template raises an
229         exception with an invalid image.
230         """
231         # Create ClusterTemplate
232         cluster_template_config = ClusterTemplateConfig(
233             name=self.cluster_type_name,
234             image='foo',
235             keypair=self.keypair_creator.keypair_settings.name,
236             external_net=self.ext_net_name,
237             flavor=self.flavor_creator.flavor_settings.name)
238
239         self.cluster_template_creator = OpenStackClusterTemplate(
240             self.os_creds, cluster_template_config)
241
242         with self.assertRaises(BadRequest):
243             self.cluster_template_creator.create()
244
245     def test_create_cluster_template_bad_network_driver(self):
246         """
247         Tests the creation of an OpenStack cluster template raises an
248         exception with an invalid keypair.
249         """
250         # Create ClusterTemplate
251         cluster_template_config = ClusterTemplateConfig(
252             name=self.cluster_type_name,
253             image=self.image_creator.image_settings.name,
254             keypair=self.keypair_creator.keypair_settings.name,
255             external_net=self.ext_net_name,
256             flavor=self.flavor_creator.flavor_settings.name,
257             network_driver='foo')
258
259         self.cluster_template_creator = OpenStackClusterTemplate(
260             self.os_creds, cluster_template_config)
261
262         with self.assertRaises(BadRequest):
263             self.cluster_template_creator.create()
264
265     def test_create_cluster_template_bad_volume_driver(self):
266         """
267         Tests the creation of an OpenStack cluster template raises an
268         exception with an invalid keypair.
269         """
270         # Create ClusterTemplate
271         cluster_template_config = ClusterTemplateConfig(
272             name=self.cluster_type_name,
273             image=self.image_creator.image_settings.name,
274             keypair=self.keypair_creator.keypair_settings.name,
275             external_net=self.ext_net_name,
276             flavor=self.flavor_creator.flavor_settings.name,
277             volume_driver='foo')
278
279         self.cluster_template_creator = OpenStackClusterTemplate(
280             self.os_creds, cluster_template_config)
281
282         with self.assertRaises(BadRequest):
283             self.cluster_template_creator.create()