Patch to make flavor configuration for tests more flexible.
[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         flavor_config = openstack_tests.get_flavor_config(
73             name=self.guid + '-flavor', ram=512, disk=10,
74             vcpus=1, metadata=self.flavor_metadata)
75         self.flavor_creator = OpenStackFlavor(self.os_creds, flavor_config)
76
77         keypair_priv_filepath = 'tmp/' + self.guid
78         keypair_pub_filepath = keypair_priv_filepath + '.pub'
79
80         self.keypair_creator = OpenStackKeypair(
81             self.os_creds, KeypairConfig(
82                 name=self.guid + '-keypair',
83                 public_filepath=keypair_pub_filepath,
84                 private_filepath=keypair_priv_filepath))
85
86         self.cluster_template_creator = None
87
88         self.cluster_template_config = ClusterTemplateConfig(
89             name=self.cluster_type_name,
90             image=self.image_creator.image_settings.name,
91             keypair=self.keypair_creator.keypair_settings.name,
92             external_net=self.ext_net_name,
93             flavor=self.flavor_creator.flavor_settings.name)
94
95         try:
96             self.image_creator.create()
97             self.flavor_creator.create()
98             self.keypair_creator.create()
99         except:
100             self.tearDown()
101             raise
102
103     def tearDown(self):
104         """
105         Cleans the template config
106         """
107         if self.cluster_template_creator:
108             try:
109                 self.cluster_template_creator.clean()
110             except:
111                 pass
112         if self.keypair_creator:
113             try:
114                 self.keypair_creator.clean()
115             except:
116                 pass
117         if self.flavor_creator:
118             try:
119                 self.flavor_creator.clean()
120             except:
121                 pass
122         if self.image_creator:
123             try:
124                 self.image_creator.clean()
125             except:
126                 pass
127
128         super(self.__class__, self).__clean__()
129
130     def test_create_cluster_template(self):
131         """
132         Tests the creation of an OpenStack cluster template.
133         """
134         # Create ClusterTemplate
135         self.cluster_template_creator = OpenStackClusterTemplate(
136             self.os_creds, self.cluster_template_config)
137         created_cluster_template = self.cluster_template_creator.create()
138         self.assertIsNotNone(created_cluster_template)
139         self.assertEqual(self.cluster_template_config.name,
140                          created_cluster_template.name)
141
142         retrieved_cluster_template1 = magnum_utils.get_cluster_template(
143             self.magnum, template_config=self.cluster_template_config)
144         self.assertIsNotNone(retrieved_cluster_template1)
145         self.assertEqual(created_cluster_template, retrieved_cluster_template1)
146
147         retrieved_cluster_template2 = magnum_utils.get_cluster_template_by_id(
148             self.magnum, created_cluster_template.id)
149         self.assertEqual(created_cluster_template, retrieved_cluster_template2)
150
151     def test_create_delete_cluster_template(self):
152         """
153         Tests the creation then deletion of an OpenStack template config to
154         ensure clean() does not raise an Exception.
155         """
156         # Create ClusterTemplate
157         self.cluster_template_creator = OpenStackClusterTemplate(
158             self.os_creds, self.cluster_template_config)
159         created_cluster_template = self.cluster_template_creator.create()
160         self.assertIsNotNone(created_cluster_template)
161
162         self.cluster_template_creator.clean()
163
164         tmplt = magnum_utils.get_cluster_template(
165             self.magnum, template_name=self.cluster_template_config.name)
166         self.assertIsNone(tmplt)
167
168     def test_create_same_cluster_template(self):
169         """
170         Tests the creation of an OpenStack cluster_template when one already
171         exists.
172         """
173         # Create ClusterTemplate
174         self.cluster_template_creator = OpenStackClusterTemplate(
175             self.os_creds, self.cluster_template_config)
176         cluster_template1 = self.cluster_template_creator.create()
177
178         retrieved_cluster_template = magnum_utils.get_cluster_template(
179             self.magnum, template_config=self.cluster_template_config)
180         self.assertEqual(cluster_template1, retrieved_cluster_template)
181
182         # Should be retrieving the instance data
183         os_cluster_template_2 = OpenStackClusterTemplate(
184             self.os_creds, self.cluster_template_config)
185         cluster_template2 = os_cluster_template_2.create()
186         self.assertEqual(cluster_template2, cluster_template2)
187
188     def test_create_cluster_template_bad_flavor(self):
189         """
190         Tests the creation of an OpenStack cluster template raises an
191         exception with an invalid flavor.
192         """
193         # Create ClusterTemplate
194         cluster_template_config = ClusterTemplateConfig(
195             name=self.cluster_type_name,
196             image=self.image_creator.image_settings.name,
197             keypair=self.keypair_creator.keypair_settings.name,
198             external_net=self.ext_net_name,
199             flavor='foo')
200
201         self.cluster_template_creator = OpenStackClusterTemplate(
202             self.os_creds, cluster_template_config)
203
204         with self.assertRaises(BadRequest):
205             self.cluster_template_creator.create()
206
207     def test_create_cluster_template_bad_master_flavor(self):
208         """
209         Tests the creation of an OpenStack cluster template raises an
210         exception with an invalid master flavor.
211         """
212         # Create ClusterTemplate
213         cluster_template_config = ClusterTemplateConfig(
214             name=self.cluster_type_name,
215             image=self.image_creator.image_settings.name,
216             keypair=self.keypair_creator.keypair_settings.name,
217             external_net=self.ext_net_name,
218             flavor=self.flavor_creator.flavor_settings.name,
219             master_flavor='foo')
220
221         self.cluster_template_creator = OpenStackClusterTemplate(
222             self.os_creds, cluster_template_config)
223
224         with self.assertRaises(BadRequest):
225             self.cluster_template_creator.create()
226
227     def test_create_cluster_template_bad_image(self):
228         """
229         Tests the creation of an OpenStack cluster template raises an
230         exception with an invalid image.
231         """
232         # Create ClusterTemplate
233         cluster_template_config = ClusterTemplateConfig(
234             name=self.cluster_type_name,
235             image='foo',
236             keypair=self.keypair_creator.keypair_settings.name,
237             external_net=self.ext_net_name,
238             flavor=self.flavor_creator.flavor_settings.name)
239
240         self.cluster_template_creator = OpenStackClusterTemplate(
241             self.os_creds, cluster_template_config)
242
243         with self.assertRaises(BadRequest):
244             self.cluster_template_creator.create()
245
246     def test_create_cluster_template_bad_network_driver(self):
247         """
248         Tests the creation of an OpenStack cluster template raises an
249         exception with an invalid keypair.
250         """
251         # Create ClusterTemplate
252         cluster_template_config = ClusterTemplateConfig(
253             name=self.cluster_type_name,
254             image=self.image_creator.image_settings.name,
255             keypair=self.keypair_creator.keypair_settings.name,
256             external_net=self.ext_net_name,
257             flavor=self.flavor_creator.flavor_settings.name,
258             network_driver='foo')
259
260         self.cluster_template_creator = OpenStackClusterTemplate(
261             self.os_creds, cluster_template_config)
262
263         with self.assertRaises(BadRequest):
264             self.cluster_template_creator.create()
265
266     def test_create_cluster_template_bad_volume_driver(self):
267         """
268         Tests the creation of an OpenStack cluster template raises an
269         exception with an invalid keypair.
270         """
271         # Create ClusterTemplate
272         cluster_template_config = ClusterTemplateConfig(
273             name=self.cluster_type_name,
274             image=self.image_creator.image_settings.name,
275             keypair=self.keypair_creator.keypair_settings.name,
276             external_net=self.ext_net_name,
277             flavor=self.flavor_creator.flavor_settings.name,
278             volume_driver='foo')
279
280         self.cluster_template_creator = OpenStackClusterTemplate(
281             self.os_creds, cluster_template_config)
282
283         with self.assertRaises(BadRequest):
284             self.cluster_template_creator.create()