Implemented the ability to create Magnum Cluster Type objects.
[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 snaps.config.cluster_template import ClusterTemplateConfig
19 from snaps.config.keypair import KeypairConfig
20 from snaps.openstack.create_image import OpenStackImage
21 from snaps.openstack.create_keypairs import OpenStackKeypair
22 from snaps.openstack.os_credentials import OSCreds
23 from snaps.openstack.tests import openstack_tests
24 from snaps.openstack.tests.os_source_file_test import OSComponentTestCase
25 from snaps.openstack.utils import magnum_utils
26
27 __author__ = 'spisarski'
28
29 logger = logging.getLogger('magnum_utils_tests')
30
31
32 class MagnumSmokeTests(OSComponentTestCase):
33     """
34     Tests to ensure that the magnum client can communicate with the cloud
35     """
36
37     def test_connect_success(self):
38         """
39         Tests to ensure that the proper credentials can connect.
40         """
41         magnum = magnum_utils.magnum_client(self.os_creds)
42
43         # This should not throw an exception
44         self.assertIsNotNone(magnum.clusters.list())
45
46     def test_nova_connect_fail(self):
47         """
48         Tests to ensure that the improper credentials cannot connect.
49         """
50
51         with self.assertRaises(RuntimeError):
52             magnum_utils.magnum_client(
53                 OSCreds(username='user', password='pass',
54                         auth_url=self.os_creds.auth_url,
55                         project_name=self.os_creds.project_name,
56                         proxy_settings=self.os_creds.proxy_settings))
57
58
59 class MagnumUtilsTests(OSComponentTestCase):
60     """
61     Tests individual functions within magnum_utils.py
62     """
63
64     def setUp(self):
65         self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
66         self.cluster_type_name = self.guid + '-cluster-type'
67         self.magnum = magnum_utils.magnum_client(self.os_creds)
68
69         metadata = self.image_metadata
70         if not metadata:
71             metadata = dict()
72         if 'extra_properties' not in metadata:
73             metadata['extra_properties'] = dict()
74         metadata['extra_properties']['os_distro'] = 'cirros'
75
76         os_image_settings = openstack_tests.cirros_image_settings(
77             name=self.guid + '-image', image_metadata=metadata)
78
79         self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
80
81         keypair_priv_filepath = 'tmp/' + self.guid
82         keypair_pub_filepath = keypair_priv_filepath + '.pub'
83
84         self.keypair_creator = OpenStackKeypair(
85             self.os_creds, KeypairConfig(
86                 name=self.guid + '-keypair',
87                 public_filepath=keypair_pub_filepath,
88                 private_filepath=keypair_priv_filepath))
89
90         self.cluster_template = None
91
92         try:
93             self.image_creator.create()
94             self.keypair_creator.create()
95         except:
96             self.tearDown()
97             raise
98
99     def tearDown(self):
100         if self.cluster_template:
101             try:
102                 magnum_utils.delete_cluster_template(
103                     self.magnum, self.cluster_template.id)
104             except:
105                 pass
106         if self.keypair_creator:
107             try:
108                 self.keypair_creator.clean()
109             except:
110                 pass
111         if self.image_creator:
112             try:
113                 self.image_creator.clean()
114             except:
115                 pass
116
117     def test_create_cluster_template_simple(self):
118         config = ClusterTemplateConfig(
119             name=self.cluster_type_name,
120             image=self.image_creator.image_settings.name,
121             keypair=self.keypair_creator.keypair_settings.name,
122             external_net=self.ext_net_name)
123
124         self.cluster_template = magnum_utils.create_cluster_template(
125             self.magnum, config)
126         self.assertIsNotNone(self.cluster_template)
127         self.assertTrue(
128             validate_cluster_template(config, self.cluster_template))
129
130
131 def validate_cluster_template(tmplt_config, tmplt_obj):
132     """
133     Returns true if the configuration matches the ClusterTemplate object
134     :param tmplt_config: the ClusterTemplateConfig object
135     :param tmplt_obj: the ClusterTemplate domain object
136     :return: T/F
137     """
138     if not tmplt_config.network_driver:
139         network_driver = 'flannel'
140     else:
141         network_driver = tmplt_config.network_driver
142
143     return (
144         tmplt_config.coe.value == tmplt_obj.coe and
145         tmplt_config.dns_nameserver == tmplt_obj.dns_nameserver and
146         tmplt_config.docker_storage_driver.value
147             == tmplt_obj.docker_storage_driver and
148         tmplt_config.docker_volume_size == tmplt_obj.docker_volume_size and
149         tmplt_config.external_net == tmplt_obj.external_net and
150         tmplt_config.fixed_net == tmplt_obj.fixed_net and
151         tmplt_config.fixed_subnet == tmplt_obj.fixed_subnet and
152         tmplt_config.flavor == tmplt_obj.flavor and
153         tmplt_config.floating_ip_enabled == tmplt_obj.floating_ip_enabled and
154         tmplt_config.http_proxy == tmplt_obj.http_proxy and
155         tmplt_config.https_proxy == tmplt_obj.https_proxy and
156         tmplt_config.no_proxy == tmplt_obj.no_proxy and
157         tmplt_config.image == tmplt_obj.image and
158         tmplt_config.insecure_registry == tmplt_obj.insecure_registry and
159         tmplt_config.keypair == tmplt_obj.keypair and
160         tmplt_config.labels == tmplt_obj.labels and
161         tmplt_config.master_flavor == tmplt_obj.master_flavor and
162         tmplt_config.master_lb_enabled == tmplt_obj.master_lb_enabled and
163         tmplt_config.name == tmplt_obj.name and
164         network_driver == tmplt_obj.network_driver and
165         tmplt_config.no_proxy == tmplt_obj.no_proxy and
166         tmplt_config.public == tmplt_obj.public and
167         tmplt_config.registry_enabled == tmplt_obj.registry_enabled and
168         tmplt_config.server_type.value == tmplt_obj.server_type and
169         tmplt_config.tls_disabled == tmplt_obj.tls_disabled and
170         tmplt_config.volume_driver == tmplt_obj.volume_driver
171     )
172     # def test_create_cluster_simple(self):
173     #     cluster = magnum_utils.create_cluster(self.magnum, 'foo')
174     #     self.assertIsNotNone(cluster)