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.
18 from magnumclient.common.apiclient.exceptions import BadRequest
20 from snaps.config.cluster_template import (
21 ClusterTemplateConfig, ServerType, ContainerOrchestrationEngine,
23 from snaps.config.flavor import FlavorConfig
24 from snaps.config.keypair import KeypairConfig
25 from snaps.openstack.create_flavor import OpenStackFlavor
26 from snaps.openstack.create_image import OpenStackImage
27 from snaps.openstack.create_keypairs import OpenStackKeypair
28 from snaps.openstack.os_credentials import OSCreds
29 from snaps.openstack.tests import openstack_tests
30 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
31 from snaps.openstack.utils import magnum_utils
33 __author__ = 'spisarski'
35 logger = logging.getLogger('magnum_utils_tests')
38 class MagnumSmokeTests(OSComponentTestCase):
40 Tests to ensure that the magnum client can communicate with the cloud
43 def test_connect_success(self):
45 Tests to ensure that the proper credentials can connect.
47 magnum = magnum_utils.magnum_client(
48 self.os_creds, self.os_session)
50 # This should not throw an exception
51 self.assertIsNotNone(magnum.clusters.list())
53 def test_nova_connect_fail(self):
55 Tests to ensure that the improper credentials cannot connect.
58 with self.assertRaises(RuntimeError):
59 magnum_utils.magnum_client(
60 OSCreds(username='user', password='pass',
61 auth_url=self.os_creds.auth_url,
62 project_name=self.os_creds.project_name,
63 proxy_settings=self.os_creds.proxy_settings))
66 class MagnumUtilsClusterTypeTests(OSComponentTestCase):
68 Tests individual functions within magnum_utils.py
72 self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
73 self.cluster_type_name = self.guid + '-cluster-type'
74 self.magnum = magnum_utils.magnum_client(
75 self.os_creds, self.os_session)
77 metadata = self.image_metadata
80 if 'extra_properties' not in metadata:
81 metadata['extra_properties'] = dict()
82 metadata['extra_properties']['os_distro'] = 'cirros'
84 os_image_settings = openstack_tests.cirros_image_settings(
85 name=self.guid + '-image', image_metadata=metadata)
87 self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
89 self.flavor_creator = OpenStackFlavor(
90 self.os_creds, FlavorConfig(
91 name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))
93 keypair_priv_filepath = 'tmp/' + self.guid
94 keypair_pub_filepath = keypair_priv_filepath + '.pub'
96 self.keypair_creator = OpenStackKeypair(
97 self.os_creds, KeypairConfig(
98 name=self.guid + '-keypair',
99 public_filepath=keypair_pub_filepath,
100 private_filepath=keypair_priv_filepath))
102 self.cluster_template = None
105 self.image_creator.create()
106 self.flavor_creator.create()
107 self.keypair_creator.create()
113 if self.cluster_template:
115 magnum_utils.delete_cluster_template(
116 self.magnum, self.cluster_template.id)
119 if self.keypair_creator:
121 self.keypair_creator.clean()
124 if self.flavor_creator:
126 self.flavor_creator.clean()
129 if self.image_creator:
131 self.image_creator.clean()
135 super(self.__class__, self).__clean__()
137 def test_create_cluster_template_simple(self):
138 config = ClusterTemplateConfig(
139 name=self.cluster_type_name,
140 image=self.image_creator.image_settings.name,
141 keypair=self.keypair_creator.keypair_settings.name,
142 external_net=self.ext_net_name,
143 flavor=self.flavor_creator.flavor_settings.name)
145 self.cluster_template = magnum_utils.create_cluster_template(
147 self.assertIsNotNone(self.cluster_template)
149 validate_cluster_template(config, self.cluster_template))
151 template_by_name = magnum_utils.get_cluster_template(
152 self.magnum, template_name=config.name)
153 self.assertEqual(self.cluster_template, template_by_name)
154 template_by_id = magnum_utils.get_cluster_template_by_id(
155 self.magnum, self.cluster_template.id)
156 self.assertEqual(self.cluster_template, template_by_id)
158 def test_create_cluster_template_all(self):
159 config = ClusterTemplateConfig(
160 name=self.cluster_type_name,
161 image=self.image_creator.image_settings.name,
162 keypair=self.keypair_creator.keypair_settings.name,
163 network_driver='flannel', external_net=self.ext_net_name,
164 floating_ip_enabled=True, docker_volume_size=100,
165 server_type=ServerType.vm,
166 flavor=self.flavor_creator.flavor_settings.name,
167 master_flavor=self.flavor_creator.flavor_settings.name,
168 coe=ContainerOrchestrationEngine.kubernetes,
169 fixed_net='foo', fixed_subnet='bar',
170 registry_enabled=True, insecure_registry='localhost',
171 docker_storage_driver=DockerStorageDriver.overlay,
172 dns_nameserver='8.8.4.4', public=True, tls_disabled=True,
173 http_proxy=None, https_proxy=None, volume_driver='cinder',
174 master_lb_enabled=False, labels={'foo': 'bar'})
176 self.cluster_template = magnum_utils.create_cluster_template(
178 self.assertIsNotNone(self.cluster_template)
180 validate_cluster_template(config, self.cluster_template))
182 template_by_name = magnum_utils.get_cluster_template(
183 self.magnum, template_name=config.name)
184 self.assertEqual(self.cluster_template, template_by_name)
185 template_by_id = magnum_utils.get_cluster_template_by_id(
186 self.magnum, self.cluster_template.id)
187 self.assertEqual(self.cluster_template, template_by_id)
189 def test_create_cluster_template_bad_image(self):
190 config = ClusterTemplateConfig(
191 name=self.cluster_type_name,
193 keypair=self.keypair_creator.keypair_settings.name,
194 external_net=self.ext_net_name,
195 flavor=self.flavor_creator.flavor_settings.name)
197 with self.assertRaises(BadRequest):
198 self.cluster_template = magnum_utils.create_cluster_template(
201 def test_create_cluster_template_bad_ext_net(self):
202 config = ClusterTemplateConfig(
203 name=self.cluster_type_name,
204 image=self.image_creator.image_settings.name,
205 keypair=self.keypair_creator.keypair_settings.name,
207 flavor=self.flavor_creator.flavor_settings.name)
209 with self.assertRaises(BadRequest):
210 self.cluster_template = magnum_utils.create_cluster_template(
213 def test_create_cluster_template_bad_flavor(self):
214 config = ClusterTemplateConfig(
215 name=self.cluster_type_name,
216 image=self.image_creator.image_settings.name,
217 keypair=self.keypair_creator.keypair_settings.name,
218 external_net=self.ext_net_name,
221 with self.assertRaises(BadRequest):
222 self.cluster_template = magnum_utils.create_cluster_template(
225 def test_create_cluster_template_bad_master_flavor(self):
226 config = ClusterTemplateConfig(
227 name=self.cluster_type_name,
228 image=self.image_creator.image_settings.name,
229 keypair=self.keypair_creator.keypair_settings.name,
230 external_net=self.ext_net_name,
231 flavor=self.flavor_creator.flavor_settings.name,
234 with self.assertRaises(BadRequest):
235 self.cluster_template = magnum_utils.create_cluster_template(
238 def test_create_cluster_template_bad_network_driver(self):
239 config = ClusterTemplateConfig(
240 name=self.cluster_type_name,
241 image=self.image_creator.image_settings.name,
242 keypair=self.keypair_creator.keypair_settings.name,
243 external_net=self.ext_net_name,
244 network_driver='foo')
246 with self.assertRaises(BadRequest):
247 self.cluster_template = magnum_utils.create_cluster_template(
250 def test_create_cluster_template_bad_volume_driver(self):
251 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,
258 with self.assertRaises(BadRequest):
259 self.cluster_template = magnum_utils.create_cluster_template(
263 def validate_cluster_template(tmplt_config, tmplt_obj):
265 Returns true if the configuration matches the ClusterTemplate object
266 :param tmplt_config: the ClusterTemplateConfig object
267 :param tmplt_obj: the ClusterTemplate domain object
270 if not tmplt_config.network_driver:
271 network_driver = 'flannel'
273 network_driver = tmplt_config.network_driver
276 tmplt_config.coe.value == tmplt_obj.coe and
277 tmplt_config.dns_nameserver == tmplt_obj.dns_nameserver and
278 tmplt_config.docker_storage_driver.value
279 == tmplt_obj.docker_storage_driver and
280 tmplt_config.docker_volume_size == tmplt_obj.docker_volume_size and
281 tmplt_config.external_net == tmplt_obj.external_net and
282 tmplt_config.fixed_net == tmplt_obj.fixed_net and
283 tmplt_config.fixed_subnet == tmplt_obj.fixed_subnet and
284 tmplt_config.flavor == tmplt_obj.flavor and
285 tmplt_config.floating_ip_enabled == tmplt_obj.floating_ip_enabled and
286 tmplt_config.http_proxy == tmplt_obj.http_proxy and
287 tmplt_config.https_proxy == tmplt_obj.https_proxy and
288 tmplt_config.no_proxy == tmplt_obj.no_proxy and
289 tmplt_config.image == tmplt_obj.image and
290 tmplt_config.insecure_registry == tmplt_obj.insecure_registry and
291 tmplt_config.keypair == tmplt_obj.keypair and
292 tmplt_config.labels == tmplt_obj.labels and
293 tmplt_config.master_flavor == tmplt_obj.master_flavor and
294 tmplt_config.master_lb_enabled == tmplt_obj.master_lb_enabled and
295 tmplt_config.name == tmplt_obj.name and
296 network_driver == tmplt_obj.network_driver and
297 tmplt_config.no_proxy == tmplt_obj.no_proxy and
298 tmplt_config.public == tmplt_obj.public and
299 tmplt_config.registry_enabled == tmplt_obj.registry_enabled and
300 tmplt_config.server_type.value == tmplt_obj.server_type and
301 tmplt_config.tls_disabled == tmplt_obj.tls_disabled and
302 tmplt_config.volume_driver == tmplt_obj.volume_driver