Closing keystone sessions after done with them.
[snaps.git] / snaps / openstack / utils / tests / magnum_utils_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 import logging
16 import uuid
17
18 from magnumclient.common.apiclient.exceptions import BadRequest
19
20 from snaps.config.cluster_template import (
21     ClusterTemplateConfig, ServerType,  ContainerOrchestrationEngine,
22     DockerStorageDriver)
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
32
33 __author__ = 'spisarski'
34
35 logger = logging.getLogger('magnum_utils_tests')
36
37
38 class MagnumSmokeTests(OSComponentTestCase):
39     """
40     Tests to ensure that the magnum client can communicate with the cloud
41     """
42
43     def test_connect_success(self):
44         """
45         Tests to ensure that the proper credentials can connect.
46         """
47         magnum = magnum_utils.magnum_client(
48             self.os_creds, self.os_session)
49
50         # This should not throw an exception
51         self.assertIsNotNone(magnum.clusters.list())
52
53     def test_nova_connect_fail(self):
54         """
55         Tests to ensure that the improper credentials cannot connect.
56         """
57
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))
64
65
66 class MagnumUtilsClusterTypeTests(OSComponentTestCase):
67     """
68     Tests individual functions within magnum_utils.py
69     """
70
71     def setUp(self):
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)
76
77         metadata = self.image_metadata
78         if not metadata:
79             metadata = dict()
80         if 'extra_properties' not in metadata:
81             metadata['extra_properties'] = dict()
82         metadata['extra_properties']['os_distro'] = 'cirros'
83
84         os_image_settings = openstack_tests.cirros_image_settings(
85             name=self.guid + '-image', image_metadata=metadata)
86
87         self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
88
89         self.flavor_creator = OpenStackFlavor(
90             self.os_creds, FlavorConfig(
91                 name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))
92
93         keypair_priv_filepath = 'tmp/' + self.guid
94         keypair_pub_filepath = keypair_priv_filepath + '.pub'
95
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))
101
102         self.cluster_template = None
103
104         try:
105             self.image_creator.create()
106             self.flavor_creator.create()
107             self.keypair_creator.create()
108         except:
109             self.tearDown()
110             raise
111
112     def tearDown(self):
113         if self.cluster_template:
114             try:
115                 magnum_utils.delete_cluster_template(
116                     self.magnum, self.cluster_template.id)
117             except:
118                 pass
119         if self.keypair_creator:
120             try:
121                 self.keypair_creator.clean()
122             except:
123                 pass
124         if self.flavor_creator:
125             try:
126                 self.flavor_creator.clean()
127             except:
128                 pass
129         if self.image_creator:
130             try:
131                 self.image_creator.clean()
132             except:
133                 pass
134
135         super(self.__class__, self).__clean__()
136
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)
144
145         self.cluster_template = magnum_utils.create_cluster_template(
146             self.magnum, config)
147         self.assertIsNotNone(self.cluster_template)
148         self.assertTrue(
149             validate_cluster_template(config, self.cluster_template))
150
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)
157
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'})
175
176         self.cluster_template = magnum_utils.create_cluster_template(
177             self.magnum, config)
178         self.assertIsNotNone(self.cluster_template)
179         self.assertTrue(
180             validate_cluster_template(config, self.cluster_template))
181
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)
188
189     def test_create_cluster_template_bad_image(self):
190         config = ClusterTemplateConfig(
191             name=self.cluster_type_name,
192             image='foo',
193             keypair=self.keypair_creator.keypair_settings.name,
194             external_net=self.ext_net_name,
195             flavor=self.flavor_creator.flavor_settings.name)
196
197         with self.assertRaises(BadRequest):
198             self.cluster_template = magnum_utils.create_cluster_template(
199                 self.magnum, config)
200
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,
206             external_net='foo',
207             flavor=self.flavor_creator.flavor_settings.name)
208
209         with self.assertRaises(BadRequest):
210             self.cluster_template = magnum_utils.create_cluster_template(
211                 self.magnum, config)
212
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,
219             flavor='foo')
220
221         with self.assertRaises(BadRequest):
222             self.cluster_template = magnum_utils.create_cluster_template(
223                 self.magnum, config)
224
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,
232             master_flavor='foo')
233
234         with self.assertRaises(BadRequest):
235             self.cluster_template = magnum_utils.create_cluster_template(
236                 self.magnum, config)
237
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')
245
246         with self.assertRaises(BadRequest):
247             self.cluster_template = magnum_utils.create_cluster_template(
248                 self.magnum, config)
249
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,
256             volume_driver='foo')
257
258         with self.assertRaises(BadRequest):
259             self.cluster_template = magnum_utils.create_cluster_template(
260                 self.magnum, config)
261
262
263 def validate_cluster_template(tmplt_config, tmplt_obj):
264     """
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
268     :return: T/F
269     """
270     if not tmplt_config.network_driver:
271         network_driver = 'flannel'
272     else:
273         network_driver = tmplt_config.network_driver
274
275     return (
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
303     )