Added cluster template creator/state machine class.
[snaps.git] / snaps / openstack / tests / cluster_template_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 from magnumclient.common.apiclient.exceptions import BadRequest, NotFound
16
17 from snaps.config.cluster_template import ClusterTemplateConfig
18 from snaps.config.flavor import FlavorConfig
19 from snaps.config.keypair import KeypairConfig
20 from snaps.openstack.cluster_template import OpenStackClusterTemplate
21 from snaps.openstack.create_flavor import OpenStackFlavor
22 from snaps.openstack.create_image import OpenStackImage
23 from snaps.openstack.create_keypairs import OpenStackKeypair
24 from snaps.openstack.tests import openstack_tests
25
26 try:
27     from urllib.request import URLError
28 except ImportError:
29     from urllib2 import URLError
30
31 import logging
32 import uuid
33
34 from snaps.openstack.tests.os_source_file_test import OSIntegrationTestCase
35 from snaps.openstack.utils import magnum_utils
36
37 __author__ = 'spisarski'
38
39 logger = logging.getLogger('cluster_template_tests')
40
41
42 class CreateClusterTemplateTests(OSIntegrationTestCase):
43     """
44     Test for the OpenStackClusterTemplate class defined in py
45     without any QoS Specs or Encryption
46     """
47
48     def setUp(self):
49         """
50         Instantiates the CreateClusterTemplate object that is responsible for
51         downloading and creating an OS template config file within OpenStack
52         """
53         super(self.__class__, self).__start__()
54
55         self.guid = self.__class__.__name__ + '-' + str(uuid.uuid4())
56         self.cluster_type_name = self.guid + '-cluster-type'
57         self.magnum = magnum_utils.magnum_client(self.os_creds)
58
59         metadata = self.image_metadata
60         if not metadata:
61             metadata = dict()
62         if 'extra_properties' not in metadata:
63             metadata['extra_properties'] = dict()
64         metadata['extra_properties']['os_distro'] = 'cirros'
65
66         os_image_settings = openstack_tests.cirros_image_settings(
67             name=self.guid + '-image', image_metadata=metadata)
68
69         self.image_creator = OpenStackImage(self.os_creds, os_image_settings)
70
71         self.flavor_creator = OpenStackFlavor(
72             self.os_creds, FlavorConfig(
73                 name=self.guid + '-flavor', ram=512, disk=10, vcpus=1))
74
75         keypair_priv_filepath = 'tmp/' + self.guid
76         keypair_pub_filepath = keypair_priv_filepath + '.pub'
77
78         self.keypair_creator = OpenStackKeypair(
79             self.os_creds, KeypairConfig(
80                 name=self.guid + '-keypair',
81                 public_filepath=keypair_pub_filepath,
82                 private_filepath=keypair_priv_filepath))
83
84         self.cluster_template_creator = None
85
86         self.cluster_template_config = ClusterTemplateConfig(
87             name=self.cluster_type_name,
88             image=self.image_creator.image_settings.name,
89             keypair=self.keypair_creator.keypair_settings.name,
90             external_net=self.ext_net_name,
91             flavor=self.flavor_creator.flavor_settings.name)
92
93         try:
94             self.image_creator.create()
95             self.flavor_creator.create()
96             self.keypair_creator.create()
97         except:
98             self.tearDown()
99             raise
100
101     def tearDown(self):
102         """
103         Cleans the template config
104         """
105         if self.cluster_template_creator:
106             try:
107                 self.cluster_template_creator.clean()
108             except:
109                 pass
110         if self.keypair_creator:
111             try:
112                 self.keypair_creator.clean()
113             except:
114                 pass
115         if self.flavor_creator:
116             try:
117                 self.flavor_creator.clean()
118             except:
119                 pass
120         if self.image_creator:
121             try:
122                 self.image_creator.clean()
123             except:
124                 pass
125
126         super(self.__class__, self).__clean__()
127
128     def test_create_cluster_template(self):
129         """
130         Tests the creation of an OpenStack cluster template.
131         """
132         # Create ClusterTemplate
133         self.cluster_template_creator = OpenStackClusterTemplate(
134             self.os_creds, self.cluster_template_config)
135         created_cluster_template = self.cluster_template_creator.create()
136         self.assertIsNotNone(created_cluster_template)
137         self.assertEqual(self.cluster_template_config.name,
138                          created_cluster_template.name)
139
140         retrieved_cluster_template1 = magnum_utils.get_cluster_template(
141             self.magnum, template_config=self.cluster_template_config)
142         self.assertIsNotNone(retrieved_cluster_template1)
143         self.assertEqual(created_cluster_template, retrieved_cluster_template1)
144
145         retrieved_cluster_template2 = magnum_utils.get_cluster_template_by_id(
146             self.magnum, created_cluster_template.id)
147         self.assertEqual(created_cluster_template, retrieved_cluster_template2)
148
149     def test_create_delete_cluster_template(self):
150         """
151         Tests the creation then deletion of an OpenStack template config to
152         ensure clean() does not raise an Exception.
153         """
154         # Create ClusterTemplate
155         self.cluster_template_creator = OpenStackClusterTemplate(
156             self.os_creds, self.cluster_template_config)
157         created_cluster_template = self.cluster_template_creator.create()
158         self.assertIsNotNone(created_cluster_template)
159
160         self.cluster_template_creator.clean()
161
162         tmplt = magnum_utils.get_cluster_template(
163             self.magnum, template_name=self.cluster_template_config.name)
164         self.assertIsNone(tmplt)
165
166     def test_create_same_cluster_template(self):
167         """
168         Tests the creation of an OpenStack cluster_template when one already
169         exists.
170         """
171         # Create ClusterTemplate
172         self.cluster_template_creator = OpenStackClusterTemplate(
173             self.os_creds, self.cluster_template_config)
174         cluster_template1 = self.cluster_template_creator.create()
175
176         retrieved_cluster_template = magnum_utils.get_cluster_template(
177             self.magnum, template_config=self.cluster_template_config)
178         self.assertEqual(cluster_template1, retrieved_cluster_template)
179
180         # Should be retrieving the instance data
181         os_cluster_template_2 = OpenStackClusterTemplate(
182             self.os_creds, self.cluster_template_config)
183         cluster_template2 = os_cluster_template_2.create()
184         self.assertEqual(cluster_template2, cluster_template2)
185
186     def test_create_cluster_template_bad_flavor(self):
187         """
188         Tests the creation of an OpenStack cluster template raises an
189         exception with an invalid flavor.
190         """
191         # Create ClusterTemplate
192         cluster_template_config = ClusterTemplateConfig(
193             name=self.cluster_type_name,
194             image=self.image_creator.image_settings.name,
195             keypair=self.keypair_creator.keypair_settings.name,
196             external_net=self.ext_net_name,
197             flavor='foo')
198
199         self.cluster_template_creator = OpenStackClusterTemplate(
200             self.os_creds, cluster_template_config)
201
202         with self.assertRaises(BadRequest):
203             self.cluster_template_creator.create()
204
205     def test_create_cluster_template_bad_master_flavor(self):
206         """
207         Tests the creation of an OpenStack cluster template raises an
208         exception with an invalid master flavor.
209         """
210         # Create ClusterTemplate
211         cluster_template_config = ClusterTemplateConfig(
212             name=self.cluster_type_name,
213             image=self.image_creator.image_settings.name,
214             keypair=self.keypair_creator.keypair_settings.name,
215             external_net=self.ext_net_name,
216             flavor=self.flavor_creator.flavor_settings.name,
217             master_flavor='foo')
218
219         self.cluster_template_creator = OpenStackClusterTemplate(
220             self.os_creds, cluster_template_config)
221
222         with self.assertRaises(BadRequest):
223             self.cluster_template_creator.create()
224
225     def test_create_cluster_template_bad_image(self):
226         """
227         Tests the creation of an OpenStack cluster template raises an
228         exception with an invalid image.
229         """
230         # Create ClusterTemplate
231         cluster_template_config = ClusterTemplateConfig(
232             name=self.cluster_type_name,
233             image='foo',
234             keypair=self.keypair_creator.keypair_settings.name,
235             external_net=self.ext_net_name,
236             flavor=self.flavor_creator.flavor_settings.name)
237
238         self.cluster_template_creator = OpenStackClusterTemplate(
239             self.os_creds, cluster_template_config)
240
241         with self.assertRaises(BadRequest):
242             self.cluster_template_creator.create()
243
244     def test_create_cluster_template_bad_keypair(self):
245         """
246         Tests the creation of an OpenStack cluster template raises an
247         exception with an invalid keypair.
248         """
249         # Create ClusterTemplate
250         cluster_template_config = ClusterTemplateConfig(
251             name=self.cluster_type_name,
252             image=self.image_creator.image_settings.name,
253             keypair='foo',
254             external_net=self.ext_net_name,
255             flavor=self.flavor_creator.flavor_settings.name)
256
257         self.cluster_template_creator = OpenStackClusterTemplate(
258             self.os_creds, cluster_template_config)
259
260         with self.assertRaises(NotFound):
261             self.cluster_template_creator.create()
262
263     def test_create_cluster_template_bad_network_driver(self):
264         """
265         Tests the creation of an OpenStack cluster template raises an
266         exception with an invalid keypair.
267         """
268         # Create ClusterTemplate
269         cluster_template_config = ClusterTemplateConfig(
270             name=self.cluster_type_name,
271             image=self.image_creator.image_settings.name,
272             keypair=self.keypair_creator.keypair_settings.name,
273             external_net=self.ext_net_name,
274             flavor=self.flavor_creator.flavor_settings.name,
275             network_driver='foo')
276
277         self.cluster_template_creator = OpenStackClusterTemplate(
278             self.os_creds, cluster_template_config)
279
280         with self.assertRaises(BadRequest):
281             self.cluster_template_creator.create()
282
283     def test_create_cluster_template_bad_volume_driver(self):
284         """
285         Tests the creation of an OpenStack cluster template raises an
286         exception with an invalid keypair.
287         """
288         # Create ClusterTemplate
289         cluster_template_config = ClusterTemplateConfig(
290             name=self.cluster_type_name,
291             image=self.image_creator.image_settings.name,
292             keypair=self.keypair_creator.keypair_settings.name,
293             external_net=self.ext_net_name,
294             flavor=self.flavor_creator.flavor_settings.name,
295             volume_driver='foo')
296
297         self.cluster_template_creator = OpenStackClusterTemplate(
298             self.os_creds, cluster_template_config)
299
300         with self.assertRaises(BadRequest):
301             self.cluster_template_creator.create()