1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 import experimental_framework.deployment_unit as mut
19 __author__ = 'vmriccox'
22 class DummyHeatManager:
24 def __init__(self, param):
28 def validate_heat_template(self, template_file):
31 def check_stack_status(self, stack_name):
32 # return 'CREATE_COMPLETE'
35 return 'CREATE_COMPLETE'
37 return 'CREATE_IN_PROGRESS'
39 def delete_stack(self, stack_name):
43 class DummyHeatManagerFailed(DummyHeatManager):
45 def check_stack_status(self, stack_name):
46 return 'CREATE_FAILED'
48 def create_stack(self, template_file, stack_name, parameters):
52 class DummyHeatManagerComplete(DummyHeatManager):
54 def check_stack_status(self, stack_name):
55 return 'CREATE_COMPLETE'
57 def create_stack(self, template_file, stack_name, parameters):
61 class DummyHeatManagerFailedException(DummyHeatManagerFailed):
63 def create_stack(self, template_file, stack_name, parameters):
66 def check_stack_status(self, stack_name):
70 class DummyHeatManagerDestroy:
72 def __init__(self, credentials):
73 self.delete_stack_counter = 0
74 self.check_stack_status_counter = 0
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'
81 return 'DELETE_COMPLETE'
83 def create_stack(self, template_file, stack_name, parameters):
86 def delete_stack(self, stack_name=None):
87 if stack_name == 'stack':
88 self.delete_stack_counter += 1
90 return self.delete_stack_counter
92 def is_stack_deployed(self, stack_name):
96 class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):
98 def delete_stack(self, stack_name=None):
102 class DummyHeatManagerReiteration:
104 def __init__(self, param):
107 def validate_heat_template(self, template_file):
110 def check_stack_status(self, stack_name):
111 return 'CREATE_FAILED'
113 def delete_stack(self, stack_name):
116 def create_stack(self, template_file=None, stack_name=None,
118 if template_file == 'template_reiteration' and \
119 stack_name == 'stack_reiteration' and \
120 parameters == 'parameters_reiteration':
124 class DummyDeploymentUnit(mut.DeploymentUnit):
126 def destroy_heat_template(self, stack_name):
130 class TestDeploymentUnit(unittest.TestCase):
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())
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,
151 mock_os_is_file.return_value = False
152 du = mut.DeploymentUnit(dict())
156 self.assertRaises(ValueError, du.deploy_heat_template, template_file,
157 stack_name, parameters, 0)
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,
164 mock_os_is_file.return_value = True
165 du = mut.DeploymentUnit(dict())
169 output = du.deploy_heat_template(template_file, stack_name,
171 self.assertEqual(output, True)
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,
178 mock_os_is_file.return_value = True
179 du = mut.DeploymentUnit(dict())
183 output = du.deploy_heat_template(template_file, stack_name,
185 self.assertEqual(output, True)
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,
195 mock_os_is_file.return_value = True
196 du = mut.DeploymentUnit(dict())
200 output = du.deploy_heat_template(template_file, stack_name,
202 self.assertEqual(output, True)
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())
215 output = du.deploy_heat_template(template_file, stack_name,
217 self.assertEqual(output, False)
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,
224 openstack_credentials = dict()
225 du = mut.DeploymentUnit(openstack_credentials)
226 du.deployed_stacks = ['stack']
228 self.assertTrue(du.destroy_heat_template(stack_name))
229 self.assertEqual(du.heat_manager.delete_stack(None), 1)
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,
236 openstack_credentials = dict()
237 du = mut.DeploymentUnit(openstack_credentials)
238 du.deployed_stacks = ['stack']
240 self.assertFalse(du.destroy_heat_template(stack_name))
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())
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,
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)
262 class DeploymentUnitDestroy(mut.DeploymentUnit):
265 self.deployed_stacks = ['stack']
266 self.heat_manager = DummyHeatManagerDestroy(dict())
267 self.destroy_all_deployed_stacks_called_correctly = False
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