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