1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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
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
27 from urllib.request import URLError
29 from urllib2 import URLError
34 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
35 from snaps.openstack.utils import magnum_utils
37 __author__ = 'spisarski'
39 logger = logging.getLogger('cluster_template_tests')
42 class CreateClusterTemplateTests(OSIntegrationTestCase):
44 Test for the OpenStackClusterTemplate class defined in py
45 without any QoS Specs or Encryption
50 Instantiates the CreateClusterTemplate object that is responsible for
51 downloading and creating an OS template config file within OpenStack
53 super(self.__class__, self).__start__()
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)
60 metadata = self.image_metadata
63 if 'extra_properties' not in metadata:
64 metadata['extra_properties'] = dict()
65 metadata['extra_properties']['os_distro'] = 'cirros'
67 os_image_settings = openstack_tests.cirros_image_settings(
68 name=self.guid + '-image', image_metadata=metadata)
70 self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
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)
77 keypair_priv_filepath = 'tmp/' + self.guid
78 keypair_pub_filepath = keypair_priv_filepath + '.pub'
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))
86 self.cluster_template_creator = None
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)
96 self.image_creator.create()
97 self.flavor_creator.create()
98 self.keypair_creator.create()
105 Cleans the template config
107 if self.cluster_template_creator:
109 self.cluster_template_creator.clean()
112 if self.keypair_creator:
114 self.keypair_creator.clean()
117 if self.flavor_creator:
119 self.flavor_creator.clean()
122 if self.image_creator:
124 self.image_creator.clean()
128 super(self.__class__, self).__clean__()
130 def test_create_cluster_template(self):
132 Tests the creation of an OpenStack cluster template.
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)
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)
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)
151 def test_create_delete_cluster_template(self):
153 Tests the creation then deletion of an OpenStack template config to
154 ensure clean() does not raise an Exception.
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)
162 self.cluster_template_creator.clean()
164 tmplt = magnum_utils.get_cluster_template(
165 self.magnum, template_name=self.cluster_template_config.name)
166 self.assertIsNone(tmplt)
168 def test_create_same_cluster_template(self):
170 Tests the creation of an OpenStack cluster_template when one already
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()
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)
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)
188 def test_create_cluster_template_bad_flavor(self):
190 Tests the creation of an OpenStack cluster template raises an
191 exception with an invalid flavor.
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,
201 self.cluster_template_creator = OpenStackClusterTemplate(
202 self.os_creds, cluster_template_config)
204 with self.assertRaises(BadRequest):
205 self.cluster_template_creator.create()
207 def test_create_cluster_template_bad_master_flavor(self):
209 Tests the creation of an OpenStack cluster template raises an
210 exception with an invalid master flavor.
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,
221 self.cluster_template_creator = OpenStackClusterTemplate(
222 self.os_creds, cluster_template_config)
224 with self.assertRaises(BadRequest):
225 self.cluster_template_creator.create()
227 def test_create_cluster_template_bad_image(self):
229 Tests the creation of an OpenStack cluster template raises an
230 exception with an invalid image.
232 # Create ClusterTemplate
233 cluster_template_config = ClusterTemplateConfig(
234 name=self.cluster_type_name,
236 keypair=self.keypair_creator.keypair_settings.name,
237 external_net=self.ext_net_name,
238 flavor=self.flavor_creator.flavor_settings.name)
240 self.cluster_template_creator = OpenStackClusterTemplate(
241 self.os_creds, cluster_template_config)
243 with self.assertRaises(BadRequest):
244 self.cluster_template_creator.create()
246 def test_create_cluster_template_bad_network_driver(self):
248 Tests the creation of an OpenStack cluster template raises an
249 exception with an invalid keypair.
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')
260 self.cluster_template_creator = OpenStackClusterTemplate(
261 self.os_creds, cluster_template_config)
263 with self.assertRaises(BadRequest):
264 self.cluster_template_creator.create()
266 def test_create_cluster_template_bad_volume_driver(self):
268 Tests the creation of an OpenStack cluster template raises an
269 exception with an invalid keypair.
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,
280 self.cluster_template_creator = OpenStackClusterTemplate(
281 self.os_creds, cluster_template_config)
283 with self.assertRaises(BadRequest):
284 self.cluster_template_creator.create()