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(self.os_creds)
49 # This should not throw an exception
50 self.assertIsNotNone(magnum.clusters.list())
52 def test_nova_connect_fail(self):
54 Tests to ensure that the improper credentials cannot connect.
57 with self.assertRaises(RuntimeError):
58 magnum_utils.magnum_client(
59 OSCreds(username='user', password='pass',
60 auth_url=self.os_creds.auth_url,
61 project_name=self.os_creds.project_name,
62 proxy_settings=self.os_creds.proxy_settings))
65 class MagnumUtilsClusterTypeTests(OSComponentTestCase):
67 Tests individual functions within magnum_utils.py
71 self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
72 self.cluster_type_name = self.guid + '-cluster-type'
73 self.magnum = magnum_utils.magnum_client(self.os_creds)
75 metadata = self.image_metadata
78 if 'extra_properties' not in metadata:
79 metadata['extra_properties'] = dict()
80 metadata['extra_properties']['os_distro'] = 'cirros'
82 os_image_settings = openstack_tests.cirros_image_settings(
83 name=self.guid + '-image', image_metadata=metadata)
85 self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
87 self.flavor_creator = OpenStackFlavor(
88 self.os_creds, FlavorConfig(
89 name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))
91 keypair_priv_filepath = 'tmp/' + self.guid
92 keypair_pub_filepath = keypair_priv_filepath + '.pub'
94 self.keypair_creator = OpenStackKeypair(
95 self.os_creds, KeypairConfig(
96 name=self.guid + '-keypair',
97 public_filepath=keypair_pub_filepath,
98 private_filepath=keypair_priv_filepath))
100 self.cluster_template = None
103 self.image_creator.create()
104 self.flavor_creator.create()
105 self.keypair_creator.create()
111 if self.cluster_template:
113 magnum_utils.delete_cluster_template(
114 self.magnum, self.cluster_template.id)
117 if self.keypair_creator:
119 self.keypair_creator.clean()
122 if self.flavor_creator:
124 self.flavor_creator.clean()
127 if self.image_creator:
129 self.image_creator.clean()
133 def test_create_cluster_template_simple(self):
134 config = ClusterTemplateConfig(
135 name=self.cluster_type_name,
136 image=self.image_creator.image_settings.name,
137 keypair=self.keypair_creator.keypair_settings.name,
138 external_net=self.ext_net_name,
139 flavor=self.flavor_creator.flavor_settings.name)
141 self.cluster_template = magnum_utils.create_cluster_template(
143 self.assertIsNotNone(self.cluster_template)
145 validate_cluster_template(config, self.cluster_template))
147 template_by_name = magnum_utils.get_cluster_template(
148 self.magnum, template_name=config.name)
149 self.assertEqual(self.cluster_template, template_by_name)
150 template_by_id = magnum_utils.get_cluster_template_by_id(
151 self.magnum, self.cluster_template.id)
152 self.assertEqual(self.cluster_template, template_by_id)
154 def test_create_cluster_template_all(self):
155 config = ClusterTemplateConfig(
156 name=self.cluster_type_name,
157 image=self.image_creator.image_settings.name,
158 keypair=self.keypair_creator.keypair_settings.name,
159 network_driver='flannel', external_net=self.ext_net_name,
160 floating_ip_enabled=True, docker_volume_size=100,
161 server_type=ServerType.vm,
162 flavor=self.flavor_creator.flavor_settings.name,
163 master_flavor=self.flavor_creator.flavor_settings.name,
164 coe=ContainerOrchestrationEngine.kubernetes,
165 fixed_net='foo', fixed_subnet='bar',
166 registry_enabled=True, insecure_registry='localhost',
167 docker_storage_driver=DockerStorageDriver.overlay,
168 dns_nameserver='8.8.4.4', public=True, tls_disabled=True,
169 http_proxy=None, https_proxy=None, volume_driver='cinder',
170 master_lb_enabled=False, labels={'foo': 'bar'})
172 self.cluster_template = magnum_utils.create_cluster_template(
174 self.assertIsNotNone(self.cluster_template)
176 validate_cluster_template(config, self.cluster_template))
178 template_by_name = magnum_utils.get_cluster_template(
179 self.magnum, template_name=config.name)
180 self.assertEqual(self.cluster_template, template_by_name)
181 template_by_id = magnum_utils.get_cluster_template_by_id(
182 self.magnum, self.cluster_template.id)
183 self.assertEqual(self.cluster_template, template_by_id)
185 def test_create_cluster_template_bad_image(self):
186 config = ClusterTemplateConfig(
187 name=self.cluster_type_name,
189 keypair=self.keypair_creator.keypair_settings.name,
190 external_net=self.ext_net_name,
191 flavor=self.flavor_creator.flavor_settings.name)
193 with self.assertRaises(BadRequest):
194 self.cluster_template = magnum_utils.create_cluster_template(
197 def test_create_cluster_template_bad_ext_net(self):
198 config = ClusterTemplateConfig(
199 name=self.cluster_type_name,
200 image=self.image_creator.image_settings.name,
201 keypair=self.keypair_creator.keypair_settings.name,
203 flavor=self.flavor_creator.flavor_settings.name)
205 with self.assertRaises(BadRequest):
206 self.cluster_template = magnum_utils.create_cluster_template(
209 def test_create_cluster_template_bad_flavor(self):
210 config = ClusterTemplateConfig(
211 name=self.cluster_type_name,
212 image=self.image_creator.image_settings.name,
213 keypair=self.keypair_creator.keypair_settings.name,
214 external_net=self.ext_net_name,
217 with self.assertRaises(BadRequest):
218 self.cluster_template = magnum_utils.create_cluster_template(
221 def test_create_cluster_template_bad_master_flavor(self):
222 config = ClusterTemplateConfig(
223 name=self.cluster_type_name,
224 image=self.image_creator.image_settings.name,
225 keypair=self.keypair_creator.keypair_settings.name,
226 external_net=self.ext_net_name,
227 flavor=self.flavor_creator.flavor_settings.name,
230 with self.assertRaises(BadRequest):
231 self.cluster_template = magnum_utils.create_cluster_template(
234 def test_create_cluster_template_bad_network_driver(self):
235 config = ClusterTemplateConfig(
236 name=self.cluster_type_name,
237 image=self.image_creator.image_settings.name,
238 keypair=self.keypair_creator.keypair_settings.name,
239 external_net=self.ext_net_name,
240 network_driver='foo')
242 with self.assertRaises(BadRequest):
243 self.cluster_template = magnum_utils.create_cluster_template(
246 def test_create_cluster_template_bad_volume_driver(self):
247 config = ClusterTemplateConfig(
248 name=self.cluster_type_name,
249 image=self.image_creator.image_settings.name,
250 keypair=self.keypair_creator.keypair_settings.name,
251 external_net=self.ext_net_name,
254 with self.assertRaises(BadRequest):
255 self.cluster_template = magnum_utils.create_cluster_template(
259 def validate_cluster_template(tmplt_config, tmplt_obj):
261 Returns true if the configuration matches the ClusterTemplate object
262 :param tmplt_config: the ClusterTemplateConfig object
263 :param tmplt_obj: the ClusterTemplate domain object
266 if not tmplt_config.network_driver:
267 network_driver = 'flannel'
269 network_driver = tmplt_config.network_driver
272 tmplt_config.coe.value == tmplt_obj.coe and
273 tmplt_config.dns_nameserver == tmplt_obj.dns_nameserver and
274 tmplt_config.docker_storage_driver.value
275 == tmplt_obj.docker_storage_driver and
276 tmplt_config.docker_volume_size == tmplt_obj.docker_volume_size and
277 tmplt_config.external_net == tmplt_obj.external_net and
278 tmplt_config.fixed_net == tmplt_obj.fixed_net and
279 tmplt_config.fixed_subnet == tmplt_obj.fixed_subnet and
280 tmplt_config.flavor == tmplt_obj.flavor and
281 tmplt_config.floating_ip_enabled == tmplt_obj.floating_ip_enabled and
282 tmplt_config.http_proxy == tmplt_obj.http_proxy and
283 tmplt_config.https_proxy == tmplt_obj.https_proxy and
284 tmplt_config.no_proxy == tmplt_obj.no_proxy and
285 tmplt_config.image == tmplt_obj.image and
286 tmplt_config.insecure_registry == tmplt_obj.insecure_registry and
287 tmplt_config.keypair == tmplt_obj.keypair and
288 tmplt_config.labels == tmplt_obj.labels and
289 tmplt_config.master_flavor == tmplt_obj.master_flavor and
290 tmplt_config.master_lb_enabled == tmplt_obj.master_lb_enabled and
291 tmplt_config.name == tmplt_obj.name and
292 network_driver == tmplt_obj.network_driver and
293 tmplt_config.no_proxy == tmplt_obj.no_proxy and
294 tmplt_config.public == tmplt_obj.public and
295 tmplt_config.registry_enabled == tmplt_obj.registry_enabled and
296 tmplt_config.server_type.value == tmplt_obj.server_type and
297 tmplt_config.tls_disabled == tmplt_obj.tls_disabled and
298 tmplt_config.volume_driver == tmplt_obj.volume_driver