81171670cd0fa82c371c06276f24a005eede315c
[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 import unittest
16 import mock
17 import experimental_framework.deployment_unit as mut
18
19 __author__ = 'vmriccox'
20
21
22 class DummyHeatManager:
23
24     def __init__(self, param):
25         self.counts = 0
26         pass
27
28     def validate_heat_template(self, template_file):
29         return True
30
31     def check_stack_status(self, stack_name):
32         # return 'CREATE_COMPLETE'
33         self.counts += 1
34         if self.counts >= 3:
35             return 'CREATE_COMPLETE'
36         else:
37             return 'CREATE_IN_PROGRESS'
38
39     def delete_stack(self, stack_name):
40         pass
41
42
43 class DummyHeatManagerFailed(DummyHeatManager):
44
45     def check_stack_status(self, stack_name):
46         return 'CREATE_FAILED'
47
48     def create_stack(self, template_file, stack_name, parameters):
49         pass
50
51
52 class DummyHeatManagerComplete(DummyHeatManager):
53
54     def check_stack_status(self, stack_name):
55         return 'CREATE_COMPLETE'
56
57     def create_stack(self, template_file, stack_name, parameters):
58         raise Exception()
59
60
61 class DummyHeatManagerFailedException(DummyHeatManagerFailed):
62
63     def create_stack(self, template_file, stack_name, parameters):
64         raise Exception
65
66     def check_stack_status(self, stack_name):
67         return ''
68
69
70 class DummyHeatManagerDestroy:
71
72     def __init__(self, credentials):
73         self.delete_stack_counter = 0
74         self.check_stack_status_counter = 0
75
76     def check_stack_status(self, stack_name):
77         if self.check_stack_status_counter < 2:
78             self.check_stack_status_counter += 1
79             return 'DELETE_IN_PROGRESS'
80         else:
81             return 'DELETE_COMPLETE'
82
83     def create_stack(self, template_file, stack_name, parameters):
84         pass
85
86     def delete_stack(self, stack_name=None):
87         if stack_name == 'stack':
88             self.delete_stack_counter += 1
89         else:
90             return self.delete_stack_counter
91
92     def is_stack_deployed(self, stack_name):
93         return True
94
95
96 class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):
97
98     def delete_stack(self, stack_name=None):
99         raise Exception
100
101
102 class DummyHeatManagerReiteration:
103
104     def __init__(self, param):
105         self.counts = 0
106
107     def validate_heat_template(self, template_file):
108         return True
109
110     def check_stack_status(self, stack_name):
111         return 'CREATE_FAILED'
112
113     def delete_stack(self, stack_name):
114         pass
115
116     def create_stack(self, template_file=None, stack_name=None,
117                      parameters=None):
118         if template_file == 'template_reiteration' and \
119             stack_name == 'stack_reiteration' and \
120                 parameters == 'parameters_reiteration':
121             self.counts += 1
122
123
124 class DummyDeploymentUnit(mut.DeploymentUnit):
125
126     def destroy_heat_template(self, stack_name):
127         raise Exception
128
129
130 class TestDeploymentUnit(unittest.TestCase):
131
132     def setUp(self):
133         pass
134
135     def tearDown(self):
136         pass
137
138     @mock.patch('experimental_framework.heat_manager.HeatManager',
139                 side_effect=DummyHeatManager)
140     def test_constructor_for_sanity(self, mock_heat_manager):
141         du = mut.DeploymentUnit(dict())
142         self.assertTrue(isinstance(du.heat_manager, DummyHeatManager))
143         mock_heat_manager.assert_called_once_with(dict())
144         self.assertEqual(du.deployed_stacks, list())
145
146     @mock.patch('experimental_framework.heat_manager.HeatManager',
147                 side_effect=DummyHeatManager)
148     @mock.patch('os.path.isfile')
149     def test_deploy_heat_template_for_failure(self, mock_os_is_file,
150                                               mock_heat_manager):
151         mock_os_is_file.return_value = False
152         du = mut.DeploymentUnit(dict())
153         template_file = ''
154         stack_name = ''
155         parameters = ''
156         self.assertRaises(ValueError, du.deploy_heat_template, template_file,
157                           stack_name, parameters, 0)
158
159     @mock.patch('experimental_framework.heat_manager.HeatManager',
160                 side_effect=DummyHeatManager)
161     @mock.patch('os.path.isfile')
162     def test_deploy_heat_template_for_success(self, mock_os_is_file,
163                                               mock_heat_manager):
164         mock_os_is_file.return_value = True
165         du = mut.DeploymentUnit(dict())
166         template_file = ''
167         stack_name = ''
168         parameters = ''
169         output = du.deploy_heat_template(template_file, stack_name,
170                                          parameters, 0)
171         self.assertEqual(output, True)
172
173     @mock.patch('experimental_framework.heat_manager.HeatManager',
174                 side_effect=DummyHeatManagerComplete)
175     @mock.patch('os.path.isfile')
176     def test_deploy_heat_template_2_for_success(self, mock_os_is_file,
177                                                 mock_heat_manager):
178         mock_os_is_file.return_value = True
179         du = mut.DeploymentUnit(dict())
180         template_file = ''
181         stack_name = ''
182         parameters = ''
183         output = du.deploy_heat_template(template_file, stack_name,
184                                          parameters, 0)
185         self.assertEqual(output, True)
186
187     @mock.patch('experimental_framework.heat_manager.HeatManager',
188                 side_effect=DummyHeatManagerComplete)
189     @mock.patch('os.path.isfile')
190     @mock.patch('experimental_framework.deployment_unit.DeploymentUnit',
191                 side_effect=DummyDeploymentUnit)
192     def test_deploy_heat_template_3_for_success(self, mock_dep_unit,
193                                                 mock_os_is_file,
194                                                 mock_heat_manager):
195         mock_os_is_file.return_value = True
196         du = mut.DeploymentUnit(dict())
197         template_file = ''
198         stack_name = ''
199         parameters = ''
200         output = du.deploy_heat_template(template_file, stack_name,
201                                          parameters, 0)
202         self.assertEqual(output, True)
203
204     @mock.patch('experimental_framework.common.LOG')
205     @mock.patch('experimental_framework.heat_manager.HeatManager',
206                 side_effect=DummyHeatManagerFailed)
207     @mock.patch('os.path.isfile')
208     def test_deploy_heat_template_for_success_2(self, mock_os_is_file,
209                                                 mock_heat_manager, mock_log):
210         mock_os_is_file.return_value = True
211         du = DummyDeploymentUnit(dict())
212         template_file = ''
213         stack_name = ''
214         parameters = ''
215         output = du.deploy_heat_template(template_file, stack_name,
216                                          parameters, 0)
217         self.assertEqual(output, False)
218
219     @mock.patch('experimental_framework.heat_manager.HeatManager',
220                 side_effect=DummyHeatManagerDestroy)
221     @mock.patch('experimental_framework.common.LOG')
222     def test_destroy_heat_template_for_success(self, mock_log,
223                                                mock_heat_manager):
224         openstack_credentials = dict()
225         du = mut.DeploymentUnit(openstack_credentials)
226         du.deployed_stacks = ['stack']
227         stack_name = 'stack'
228         self.assertTrue(du.destroy_heat_template(stack_name))
229         self.assertEqual(du.heat_manager.delete_stack(None), 1)
230
231     @mock.patch('experimental_framework.heat_manager.HeatManager',
232                 side_effect=DummyHeatManagerDestroyException)
233     @mock.patch('experimental_framework.common.LOG')
234     def test_destroy_heat_template_for_success_2(self, mock_log,
235                                                  mock_heat_manager):
236         openstack_credentials = dict()
237         du = mut.DeploymentUnit(openstack_credentials)
238         du.deployed_stacks = ['stack']
239         stack_name = 'stack'
240         self.assertFalse(du.destroy_heat_template(stack_name))
241
242     def test_destroy_all_deployed_stacks_for_success(self):
243         du = DeploymentUnitDestroy()
244         du.destroy_all_deployed_stacks()
245         self.assertTrue(du.destroy_heat_template())
246
247     @mock.patch('experimental_framework.heat_manager.HeatManager',
248                 side_effect=DummyHeatManagerReiteration)
249     @mock.patch('os.path.isfile')
250     def test_deploy_heat_template_for_success_3(self, mock_os_is_file,
251                                                 mock_heat_manager):
252         mock_os_is_file.return_value = True
253         du = mut.DeploymentUnit(dict())
254         template = 'template_reiteration'
255         stack = 'stack_reiteration'
256         parameters = 'parameters_reiteration'
257         output = du.deploy_heat_template(template, stack, parameters, 0)
258         self.assertFalse(output)
259         self.assertEqual(du.heat_manager.counts, 4)
260
261
262 class DeploymentUnitDestroy(mut.DeploymentUnit):
263
264     def __init__(self):
265         self.deployed_stacks = ['stack']
266         self.heat_manager = DummyHeatManagerDestroy(dict())
267         self.destroy_all_deployed_stacks_called_correctly = False
268
269     def destroy_heat_template(self, template_name=None):
270         if template_name == 'stack':
271             self.destroy_all_deployed_stacks_called_correctly = True
272         return self.destroy_all_deployed_stacks_called_correctly