Cluster template cleanup
[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(self.os_creds)
48
49         # This should not throw an exception
50         self.assertIsNotNone(magnum.clusters.list())
51
52     def test_nova_connect_fail(self):
53         """
54         Tests to ensure that the improper credentials cannot connect.
55         """
56
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))
63
64
65 class MagnumUtilsClusterTypeTests(OSComponentTestCase):
66     """
67     Tests individual functions within magnum_utils.py
68     """
69
70     def setUp(self):
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)
74
75         metadata = self.image_metadata
76         if not metadata:
77             metadata = dict()
78         if 'extra_properties' not in metadata:
79             metadata['extra_properties'] = dict()
80         metadata['extra_properties']['os_distro'] = 'cirros'
81
82         os_image_settings = openstack_tests.cirros_image_settings(
83             name=self.guid + '-image', image_metadata=metadata)
84
85         self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
86
87         self.flavor_creator = OpenStackFlavor(
88             self.os_creds, FlavorConfig(
89                 name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))
90
91         keypair_priv_filepath = 'tmp/' + self.guid
92         keypair_pub_filepath = keypair_priv_filepath + '.pub'
93
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))
99
100         self.cluster_template = None
101
102         try:
103             self.image_creator.create()
104             self.flavor_creator.create()
105             self.keypair_creator.create()
106         except:
107             self.tearDown()
108             raise
109
110     def tearDown(self):
111         if self.cluster_template:
112             try:
113                 magnum_utils.delete_cluster_template(
114                     self.magnum, self.cluster_template.id)
115             except:
116                 pass
117         if self.keypair_creator:
118             try:
119                 self.keypair_creator.clean()
120             except:
121                 pass
122         if self.flavor_creator:
123             try:
124                 self.flavor_creator.clean()
125             except:
126                 pass
127         if self.image_creator:
128             try:
129                 self.image_creator.clean()
130             except:
131                 pass
132
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)
140
141         self.cluster_template = magnum_utils.create_cluster_template(
142             self.magnum, config)
143         self.assertIsNotNone(self.cluster_template)
144         self.assertTrue(
145             validate_cluster_template(config, self.cluster_template))
146
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)
153
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'})
171
172         self.cluster_template = magnum_utils.create_cluster_template(
173             self.magnum, config)
174         self.assertIsNotNone(self.cluster_template)
175         self.assertTrue(
176             validate_cluster_template(config, self.cluster_template))
177
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)
184
185     def test_create_cluster_template_bad_image(self):
186         config = ClusterTemplateConfig(
187             name=self.cluster_type_name,
188             image='foo',
189             keypair=self.keypair_creator.keypair_settings.name,
190             external_net=self.ext_net_name,
191             flavor=self.flavor_creator.flavor_settings.name)
192
193         with self.assertRaises(BadRequest):
194             self.cluster_template = magnum_utils.create_cluster_template(
195                 self.magnum, config)
196
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,
202             external_net='foo',
203             flavor=self.flavor_creator.flavor_settings.name)
204
205         with self.assertRaises(BadRequest):
206             self.cluster_template = magnum_utils.create_cluster_template(
207                 self.magnum, config)
208
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,
215             flavor='foo')
216
217         with self.assertRaises(BadRequest):
218             self.cluster_template = magnum_utils.create_cluster_template(
219                 self.magnum, config)
220
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,
228             master_flavor='foo')
229
230         with self.assertRaises(BadRequest):
231             self.cluster_template = magnum_utils.create_cluster_template(
232                 self.magnum, config)
233
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')
241
242         with self.assertRaises(BadRequest):
243             self.cluster_template = magnum_utils.create_cluster_template(
244                 self.magnum, config)
245
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,
252             volume_driver='foo')
253
254         with self.assertRaises(BadRequest):
255             self.cluster_template = magnum_utils.create_cluster_template(
256                 self.magnum, config)
257
258
259 def validate_cluster_template(tmplt_config, tmplt_obj):
260     """
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
264     :return: T/F
265     """
266     if not tmplt_config.network_driver:
267         network_driver = 'flannel'
268     else:
269         network_driver = tmplt_config.network_driver
270
271     return (
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
299     )