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, NotFound
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 def test_create_cluster_template_all(self):
148 config = ClusterTemplateConfig(
149 name=self.cluster_type_name,
150 image=self.image_creator.image_settings.name,
151 keypair=self.keypair_creator.keypair_settings.name,
152 network_driver='flannel', external_net=self.ext_net_name,
153 floating_ip_enabled=True, docker_volume_size=100,
154 server_type=ServerType.vm,
155 flavor=self.flavor_creator.flavor_settings.name,
156 master_flavor=self.flavor_creator.flavor_settings.name,
157 coe=ContainerOrchestrationEngine.kubernetes,
158 fixed_net='foo', fixed_subnet='bar',
159 registry_enabled=True, insecure_registry='localhost',
160 docker_storage_driver=DockerStorageDriver.overlay,
161 dns_nameserver='8.8.4.4', public=True, tls_disabled=True,
162 http_proxy=None, https_proxy=None, volume_driver='cinder',
163 master_lb_enabled=False, labels={'foo': 'bar'})
165 self.cluster_template = magnum_utils.create_cluster_template(
167 self.assertIsNotNone(self.cluster_template)
169 validate_cluster_template(config, self.cluster_template))
171 def test_create_cluster_template_bad_image(self):
172 config = ClusterTemplateConfig(
173 name=self.cluster_type_name,
175 keypair=self.keypair_creator.keypair_settings.name,
176 external_net=self.ext_net_name,
177 flavor=self.flavor_creator.flavor_settings.name)
179 with self.assertRaises(BadRequest):
180 self.cluster_template = magnum_utils.create_cluster_template(
183 def test_create_cluster_template_bad_kp(self):
184 config = ClusterTemplateConfig(
185 name=self.cluster_type_name,
186 image=self.image_creator.image_settings.name,
188 external_net=self.ext_net_name,
189 flavor=self.flavor_creator.flavor_settings.name)
191 with self.assertRaises(NotFound):
192 self.cluster_template = magnum_utils.create_cluster_template(
195 def test_create_cluster_template_bad_ext_net(self):
196 config = ClusterTemplateConfig(
197 name=self.cluster_type_name,
198 image=self.image_creator.image_settings.name,
199 keypair=self.keypair_creator.keypair_settings.name,
201 flavor=self.flavor_creator.flavor_settings.name)
203 with self.assertRaises(BadRequest):
204 self.cluster_template = magnum_utils.create_cluster_template(
207 def test_create_cluster_template_bad_flavor(self):
208 config = ClusterTemplateConfig(
209 name=self.cluster_type_name,
210 image=self.image_creator.image_settings.name,
211 keypair=self.keypair_creator.keypair_settings.name,
212 external_net=self.ext_net_name,
215 with self.assertRaises(BadRequest):
216 self.cluster_template = magnum_utils.create_cluster_template(
219 def test_create_cluster_template_bad_master_flavor(self):
220 config = ClusterTemplateConfig(
221 name=self.cluster_type_name,
222 image=self.image_creator.image_settings.name,
223 keypair=self.keypair_creator.keypair_settings.name,
224 external_net=self.ext_net_name,
225 flavor=self.flavor_creator.flavor_settings.name,
228 with self.assertRaises(BadRequest):
229 self.cluster_template = magnum_utils.create_cluster_template(
232 def test_create_cluster_template_bad_network_driver(self):
233 config = ClusterTemplateConfig(
234 name=self.cluster_type_name,
235 image=self.image_creator.image_settings.name,
236 keypair=self.keypair_creator.keypair_settings.name,
237 external_net=self.ext_net_name,
238 network_driver='foo')
240 with self.assertRaises(BadRequest):
241 self.cluster_template = magnum_utils.create_cluster_template(
244 def test_create_cluster_template_bad_volume_driver(self):
245 config = ClusterTemplateConfig(
246 name=self.cluster_type_name,
247 image=self.image_creator.image_settings.name,
248 keypair=self.keypair_creator.keypair_settings.name,
249 external_net=self.ext_net_name,
252 with self.assertRaises(BadRequest):
253 self.cluster_template = magnum_utils.create_cluster_template(
257 def validate_cluster_template(tmplt_config, tmplt_obj):
259 Returns true if the configuration matches the ClusterTemplate object
260 :param tmplt_config: the ClusterTemplateConfig object
261 :param tmplt_obj: the ClusterTemplate domain object
264 if not tmplt_config.network_driver:
265 network_driver = 'flannel'
267 network_driver = tmplt_config.network_driver
270 tmplt_config.coe.value == tmplt_obj.coe and
271 tmplt_config.dns_nameserver == tmplt_obj.dns_nameserver and
272 tmplt_config.docker_storage_driver.value
273 == tmplt_obj.docker_storage_driver and
274 tmplt_config.docker_volume_size == tmplt_obj.docker_volume_size and
275 tmplt_config.external_net == tmplt_obj.external_net and
276 tmplt_config.fixed_net == tmplt_obj.fixed_net and
277 tmplt_config.fixed_subnet == tmplt_obj.fixed_subnet and
278 tmplt_config.flavor == tmplt_obj.flavor and
279 tmplt_config.floating_ip_enabled == tmplt_obj.floating_ip_enabled and
280 tmplt_config.http_proxy == tmplt_obj.http_proxy and
281 tmplt_config.https_proxy == tmplt_obj.https_proxy and
282 tmplt_config.no_proxy == tmplt_obj.no_proxy and
283 tmplt_config.image == tmplt_obj.image and
284 tmplt_config.insecure_registry == tmplt_obj.insecure_registry and
285 tmplt_config.keypair == tmplt_obj.keypair and
286 tmplt_config.labels == tmplt_obj.labels and
287 tmplt_config.master_flavor == tmplt_obj.master_flavor and
288 tmplt_config.master_lb_enabled == tmplt_obj.master_lb_enabled and
289 tmplt_config.name == tmplt_obj.name and
290 network_driver == tmplt_obj.network_driver and
291 tmplt_config.no_proxy == tmplt_obj.no_proxy and
292 tmplt_config.public == tmplt_obj.public and
293 tmplt_config.registry_enabled == tmplt_obj.registry_enabled and
294 tmplt_config.server_type.value == tmplt_obj.server_type and
295 tmplt_config.tls_disabled == tmplt_obj.tls_disabled and
296 tmplt_config.volume_driver == tmplt_obj.volume_driver