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