Merge "test_base: speedup unittest, wait for queue not empty"
[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 @mock.patch("experimental_framework.deployment_unit.time")
134 class TestDeploymentUnit(unittest.TestCase):
135
136     def setUp(self):
137         pass
138
139     def tearDown(self):
140         pass
141
142     @mock.patch('experimental_framework.heat_manager.HeatManager',
143                 side_effect=DummyHeatManager)
144     def test_constructor_for_sanity(self, mock_heat_manager, mock_time):
145         du = mut.DeploymentUnit(dict())
146         self.assertTrue(isinstance(du.heat_manager, DummyHeatManager))
147         mock_heat_manager.assert_called_once_with(dict())
148         self.assertEqual(du.deployed_stacks, list())
149
150     @mock.patch('experimental_framework.heat_manager.HeatManager',
151                 side_effect=DummyHeatManager)
152     @mock.patch('os.path.isfile')
153     def test_deploy_heat_template_for_failure(self, mock_os_is_file,
154                                               mock_heat_manager, mock_time):
155         mock_os_is_file.return_value = False
156         du = mut.DeploymentUnit(dict())
157         template_file = ''
158         stack_name = ''
159         parameters = ''
160         self.assertRaises(ValueError, du.deploy_heat_template, template_file,
161                           stack_name, parameters, 0)
162
163     @mock.patch('experimental_framework.heat_manager.HeatManager',
164                 side_effect=DummyHeatManager)
165     @mock.patch('os.path.isfile')
166     def test_deploy_heat_template_for_success(self, mock_os_is_file,
167                                               mock_heat_manager, mock_time):
168         mock_os_is_file.return_value = True
169         du = mut.DeploymentUnit(dict())
170         template_file = ''
171         stack_name = ''
172         parameters = ''
173         common.LOG = logging.getLogger()
174         output = du.deploy_heat_template(template_file, stack_name,
175                                          parameters, 0)
176         self.assertEqual(output, True)
177
178     @mock.patch('experimental_framework.heat_manager.HeatManager',
179                 side_effect=DummyHeatManagerComplete)
180     @mock.patch('os.path.isfile')
181     def test_deploy_heat_template_2_for_success(self, mock_os_is_file,
182                                                 mock_heat_manager, mock_time):
183         mock_os_is_file.return_value = True
184         du = mut.DeploymentUnit(dict())
185         template_file = ''
186         stack_name = ''
187         parameters = ''
188         common.LOG = logging.getLogger()
189         output = du.deploy_heat_template(template_file, stack_name,
190                                          parameters, 0)
191         self.assertEqual(output, True)
192
193     @mock.patch('experimental_framework.heat_manager.HeatManager',
194                 side_effect=DummyHeatManagerComplete)
195     @mock.patch('os.path.isfile')
196     @mock.patch('experimental_framework.deployment_unit.DeploymentUnit',
197                 side_effect=DummyDeploymentUnit)
198     def test_deploy_heat_template_3_for_success(self, mock_dep_unit,
199                                                 mock_os_is_file,
200                                                 mock_heat_manager, mock_time):
201         mock_os_is_file.return_value = True
202         du = mut.DeploymentUnit(dict())
203         template_file = ''
204         stack_name = ''
205         parameters = ''
206         common.LOG = logging.getLogger()
207         output = du.deploy_heat_template(template_file, stack_name,
208                                          parameters, 0)
209         self.assertEqual(output, True)
210
211     @mock.patch('experimental_framework.common.LOG')
212     @mock.patch('experimental_framework.heat_manager.HeatManager',
213                 side_effect=DummyHeatManagerFailed)
214     @mock.patch('os.path.isfile')
215     def test_deploy_heat_template_for_success_2(self, mock_os_is_file,
216                                                 mock_heat_manager, mock_log, mock_time):
217         mock_os_is_file.return_value = True
218         du = DummyDeploymentUnit(dict())
219         template_file = ''
220         stack_name = ''
221         parameters = ''
222         output = du.deploy_heat_template(template_file, stack_name,
223                                          parameters, 0)
224         self.assertEqual(output, False)
225
226     @mock.patch('experimental_framework.heat_manager.HeatManager',
227                 side_effect=DummyHeatManagerDestroy)
228     @mock.patch('experimental_framework.common.LOG')
229     def test_destroy_heat_template_for_success(self, mock_log,
230                                                mock_heat_manager, mock_time):
231         openstack_credentials = dict()
232         du = mut.DeploymentUnit(openstack_credentials)
233         du.deployed_stacks = ['stack']
234         stack_name = 'stack'
235         self.assertTrue(du.destroy_heat_template(stack_name))
236         self.assertEqual(du.heat_manager.delete_stack(None), 1)
237
238     @mock.patch('experimental_framework.heat_manager.HeatManager',
239                 side_effect=DummyHeatManagerDestroyException)
240     @mock.patch('experimental_framework.common.LOG')
241     def test_destroy_heat_template_for_success_2(self, mock_log,
242                                                  mock_heat_manager, mock_time):
243         openstack_credentials = dict()
244         du = mut.DeploymentUnit(openstack_credentials)
245         du.deployed_stacks = ['stack']
246         stack_name = 'stack'
247         self.assertFalse(du.destroy_heat_template(stack_name))
248
249     def test_destroy_all_deployed_stacks_for_success(self, mock_time):
250         du = DeploymentUnitDestroy()
251         du.destroy_all_deployed_stacks()
252         self.assertTrue(du.destroy_heat_template())
253
254     @mock.patch('experimental_framework.heat_manager.HeatManager',
255                 side_effect=DummyHeatManagerReiteration)
256     @mock.patch('os.path.isfile')
257     def test_deploy_heat_template_for_success_3(self, mock_os_is_file,
258                                                 mock_heat_manager, mock_time):
259         mock_os_is_file.return_value = True
260         du = mut.DeploymentUnit(dict())
261         template = 'template_reiteration'
262         stack = 'stack_reiteration'
263         parameters = 'parameters_reiteration'
264         output = du.deploy_heat_template(template, stack, parameters, 0)
265         self.assertFalse(output)
266         self.assertEqual(du.heat_manager.counts, 4)
267
268
269 class DeploymentUnitDestroy(mut.DeploymentUnit):
270
271     def __init__(self):
272         self.deployed_stacks = ['stack']
273         self.heat_manager = DummyHeatManagerDestroy(dict())
274         self.destroy_all_deployed_stacks_called_correctly = False
275
276     def destroy_heat_template(self, template_name=None):
277         if template_name == 'stack':
278             self.destroy_all_deployed_stacks_called_correctly = True
279         return self.destroy_all_deployed_stacks_called_correctly