Add Deployment Unit to ApexLake
[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.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):
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