fix: remove headers from generated files
[yardstick.git] / yardstick / vTC / apexlake / tests / deployment_unit_test.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from __future__ import absolute_import
16 import unittest
17 import logging
18 import mock
19 import experimental_framework.deployment_unit as mut
20 import experimental_framework.common as common
21
22 __author__ = 'vmriccox'
23
24
25 class DummyHeatManager:
26
27     def __init__(self, param):
28         self.counts = 0
29         pass
30
31     def validate_heat_template(self, template_file):
32         return True
33
34     def check_stack_status(self, stack_name):
35         # return 'CREATE_COMPLETE'
36         self.counts += 1
37         if self.counts >= 3:
38             return 'CREATE_COMPLETE'
39         else:
40             return 'CREATE_IN_PROGRESS'
41
42     def delete_stack(self, stack_name):
43         pass
44
45
46 class DummyHeatManagerFailed(DummyHeatManager):
47
48     def check_stack_status(self, stack_name):
49         return 'CREATE_FAILED'
50
51     def create_stack(self, template_file, stack_name, parameters):
52         pass
53
54
55 class DummyHeatManagerComplete(DummyHeatManager):
56
57     def check_stack_status(self, stack_name):
58         return 'CREATE_COMPLETE'
59
60     def create_stack(self, template_file, stack_name, parameters):
61         raise Exception()
62
63
64 class DummyHeatManagerFailedException(DummyHeatManagerFailed):
65
66     def create_stack(self, template_file, stack_name, parameters):
67         raise Exception
68
69     def check_stack_status(self, stack_name):
70         return ''
71
72
73 class DummyHeatManagerDestroy:
74
75     def __init__(self, credentials):
76         self.delete_stack_counter = 0
77         self.check_stack_status_counter = 0
78
79     def check_stack_status(self, stack_name):
80         if self.check_stack_status_counter < 2:
81             self.check_stack_status_counter += 1
82             return 'DELETE_IN_PROGRESS'
83         else:
84             return 'DELETE_COMPLETE'
85
86     def create_stack(self, template_file, stack_name, parameters):
87         pass
88
89     def delete_stack(self, stack_name=None):
90         if stack_name == 'stack':
91             self.delete_stack_counter += 1
92         else:
93             return self.delete_stack_counter
94
95     def is_stack_deployed(self, stack_name):
96         return True
97
98
99 class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):
100
101     def delete_stack(self, stack_name=None):
102         raise Exception
103
104
105 class DummyHeatManagerReiteration:
106
107     def __init__(self, param):
108         self.counts = 0
109
110     def validate_heat_template(self, template_file):
111         return True
112
113     def check_stack_status(self, stack_name):
114         return 'CREATE_FAILED'
115
116     def delete_stack(self, stack_name):
117         pass
118
119     def create_stack(self, template_file=None, stack_name=None,
120                      parameters=None):
121         if template_file == 'template_reiteration' and \
122             stack_name == 'stack_reiteration' and \
123                 parameters == 'parameters_reiteration':
124             self.counts += 1
125
126
127 class DummyDeploymentUnit(mut.DeploymentUnit):
128
129     def destroy_heat_template(self, stack_name):
130         raise Exception
131
132
133 class TestDeploymentUnit(unittest.TestCase):
134
135     def setUp(self):
136         pass
137
138     def tearDown(self):
139         pass
140
141     @mock.patch('experimental_framework.heat_manager.HeatManager',
142                 side_effect=DummyHeatManager)
143     def test_constructor_for_sanity(self, mock_heat_manager):
144         du = mut.DeploymentUnit(dict())
145         self.assertTrue(isinstance(du.heat_manager, DummyHeatManager))
146         mock_heat_manager.assert_called_once_with(dict())
147         self.assertEqual(du.deployed_stacks, list())
148
149     @mock.patch('experimental_framework.heat_manager.HeatManager',
150                 side_effect=DummyHeatManager)
151     @mock.patch('os.path.isfile')
152     def test_deploy_heat_template_for_failure(self, mock_os_is_file,
153                                               mock_heat_manager):
154         mock_os_is_file.return_value = False
155         du = mut.DeploymentUnit(dict())
156         template_file = ''
157         stack_name = ''
158         parameters = ''
159         self.assertRaises(ValueError, du.deploy_heat_template, template_file,
160                           stack_name, parameters, 0)
161
162     @mock.patch('experimental_framework.heat_manager.HeatManager',
163                 side_effect=DummyHeatManager)
164     @mock.patch('os.path.isfile')
165     def test_deploy_heat_template_for_success(self, mock_os_is_file,
166                                               mock_heat_manager):
167         mock_os_is_file.return_value = True
168         du = mut.DeploymentUnit(dict())
169         template_file = ''
170         stack_name = ''
171         parameters = ''
172         common.LOG = logging.getLogger()
173         output = du.deploy_heat_template(template_file, stack_name,
174                                          parameters, 0)
175         self.assertEqual(output, True)
176
177     @mock.patch('experimental_framework.heat_manager.HeatManager',
178                 side_effect=DummyHeatManagerComplete)
179     @mock.patch('os.path.isfile')
180     def test_deploy_heat_template_2_for_success(self, mock_os_is_file,
181                                                 mock_heat_manager):
182         mock_os_is_file.return_value = True
183         du = mut.DeploymentUnit(dict())
184         template_file = ''
185         stack_name = ''
186         parameters = ''
187         common.LOG = logging.getLogger()
188         output = du.deploy_heat_template(template_file, stack_name,
189                                          parameters, 0)
190         self.assertEqual(output, True)
191
192     @mock.patch('experimental_framework.heat_manager.HeatManager',
193                 side_effect=DummyHeatManagerComplete)
194     @mock.patch('os.path.isfile')
195     @mock.patch('experimental_framework.deployment_unit.DeploymentUnit',
196                 side_effect=DummyDeploymentUnit)
197     def test_deploy_heat_template_3_for_success(self, mock_dep_unit,
198                                                 mock_os_is_file,
199                                                 mock_heat_manager):
200         mock_os_is_file.return_value = True
201         du = mut.DeploymentUnit(dict())
202         template_file = ''
203         stack_name = ''
204         parameters = ''
205         common.LOG = logging.getLogger()
206         output = du.deploy_heat_template(template_file, stack_name,
207                                          parameters, 0)
208         self.assertEqual(output, True)
209
210     @mock.patch('experimental_framework.common.LOG')
211     @mock.patch('experimental_framework.heat_manager.HeatManager',
212                 side_effect=DummyHeatManagerFailed)
213     @mock.patch('os.path.isfile')
214     def test_deploy_heat_template_for_success_2(self, mock_os_is_file,
215                                                 mock_heat_manager, mock_log):
216         mock_os_is_file.return_value = True
217         du = DummyDeploymentUnit(dict())
218         template_file = ''
219         stack_name = ''
220         parameters = ''
221         output = du.deploy_heat_template(template_file, stack_name,
222                                          parameters, 0)
223         self.assertEqual(output, False)
224
225     @mock.patch('experimental_framework.heat_manager.HeatManager',
226                 side_effect=DummyHeatManagerDestroy)
227     @mock.patch('experimental_framework.common.LOG')
228     def test_destroy_heat_template_for_success(self, mock_log,
229                                                mock_heat_manager):
230         openstack_credentials = dict()
231         du = mut.DeploymentUnit(openstack_credentials)
232         du.deployed_stacks = ['stack']
233         stack_name = 'stack'
234         self.assertTrue(du.destroy_heat_template(stack_name))
235         self.assertEqual(du.heat_manager.delete_stack(None), 1)
236
237     @mock.patch('experimental_framework.heat_manager.HeatManager',
238                 side_effect=DummyHeatManagerDestroyException)
239     @mock.patch('experimental_framework.common.LOG')
240     def test_destroy_heat_template_for_success_2(self, mock_log,
241                                                  mock_heat_manager):
242         openstack_credentials = dict()
243         du = mut.DeploymentUnit(openstack_credentials)
244         du.deployed_stacks = ['stack']
245         stack_name = 'stack'
246         self.assertFalse(du.destroy_heat_template(stack_name))
247
248     def test_destroy_all_deployed_stacks_for_success(self):
249         du = DeploymentUnitDestroy()
250         du.destroy_all_deployed_stacks()
251         self.assertTrue(du.destroy_heat_template())
252
253     @mock.patch('experimental_framework.heat_manager.HeatManager',
254                 side_effect=DummyHeatManagerReiteration)
255     @mock.patch('os.path.isfile')
256     def test_deploy_heat_template_for_success_3(self, mock_os_is_file,
257                                                 mock_heat_manager):
258         mock_os_is_file.return_value = True
259         du = mut.DeploymentUnit(dict())
260         template = 'template_reiteration'
261         stack = 'stack_reiteration'
262         parameters = 'parameters_reiteration'
263         output = du.deploy_heat_template(template, stack, parameters, 0)
264         self.assertFalse(output)
265         self.assertEqual(du.heat_manager.counts, 4)
266
267
268 class DeploymentUnitDestroy(mut.DeploymentUnit):
269
270     def __init__(self):
271         self.deployed_stacks = ['stack']
272         self.heat_manager = DummyHeatManagerDestroy(dict())
273         self.destroy_all_deployed_stacks_called_correctly = False
274
275     def destroy_heat_template(self, template_name=None):
276         if template_name == 'stack':
277             self.destroy_all_deployed_stacks_called_correctly = True
278         return self.destroy_all_deployed_stacks_called_correctly