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.
15 __author__ = 'vmriccox'
20 import experimental_framework.deployment_unit as mut
23 class DummyHeatManager:
25 def __init__(self, param):
29 def validate_heat_template(self, template_file):
32 def check_stack_status(self, stack_name):
33 # return 'CREATE_COMPLETE'
36 return 'CREATE_COMPLETE'
38 return 'CREATE_IN_PROGRESS'
40 def delete_stack(self, stack_name):
44 class DummyHeatManagerFailed(DummyHeatManager):
46 def check_stack_status(self, stack_name):
47 return 'CREATE_FAILED'
49 def create_stack(self, template_file, stack_name, parameters):
53 class DummyHeatManagerComplete(DummyHeatManager):
55 def check_stack_status(self, stack_name):
56 return 'CREATE_COMPLETE'
58 def create_stack(self, template_file, stack_name, parameters):
62 class DummyHeatManagerFailedException(DummyHeatManagerFailed):
64 def create_stack(self, template_file, stack_name, parameters):
67 def check_stack_status(self, stack_name):
71 class DummyHeatManagerDestroy:
73 def __init__(self, credentials):
74 self.delete_stack_counter = 0
75 self.check_stack_status_counter = 0
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'
82 return 'DELETE_COMPLETE'
84 def create_stack(self, template_file, stack_name, parameters):
87 def delete_stack(self, stack_name=None):
88 if stack_name == 'stack':
89 self.delete_stack_counter += 1
91 return self.delete_stack_counter
93 def is_stack_deployed(self, stack_name):
97 class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):
99 def delete_stack(self, stack_name=None):
103 class DummyHeatManagerReiteration:
105 def __init__(self, param):
108 def validate_heat_template(self, template_file):
111 def check_stack_status(self, stack_name):
112 return 'CREATE_FAILED'
114 def delete_stack(self, stack_name):
117 def create_stack(self, template_file=None, stack_name=None,
119 if template_file == 'template_reiteration' and \
120 stack_name == 'stack_reiteration' and \
121 parameters == 'parameters_reiteration':
125 class DummyDeploymentUnit(mut.DeploymentUnit):
127 def destroy_heat_template(self, stack_name):
131 class TestDeploymentUnit(unittest.TestCase):
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())
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,
152 mock_os_is_file.return_value = False
153 du = mut.DeploymentUnit(dict())
157 self.assertRaises(ValueError, du.deploy_heat_template, template_file,
158 stack_name, parameters, 0)
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,
165 mock_os_is_file.return_value = True
166 du = mut.DeploymentUnit(dict())
170 output = du.deploy_heat_template(template_file, stack_name,
172 self.assertEqual(output, True)
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,
179 mock_os_is_file.return_value = True
180 du = mut.DeploymentUnit(dict())
184 output = du.deploy_heat_template(template_file, stack_name,
186 self.assertEqual(output, True)
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,
196 mock_os_is_file.return_value = True
197 du = mut.DeploymentUnit(dict())
201 output = du.deploy_heat_template(template_file, stack_name,
203 self.assertEqual(output, True)
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())
216 output = du.deploy_heat_template(template_file, stack_name,
218 self.assertEqual(output, False)
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,
225 openstack_credentials = dict()
226 du = mut.DeploymentUnit(openstack_credentials)
227 du.deployed_stacks = ['stack']
229 self.assertTrue(du.destroy_heat_template(stack_name))
230 self.assertEqual(du.heat_manager.delete_stack(None), 1)
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,
237 openstack_credentials = dict()
238 du = mut.DeploymentUnit(openstack_credentials)
239 du.deployed_stacks = ['stack']
241 self.assertFalse(du.destroy_heat_template(stack_name))
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())
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,
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)
263 class DeploymentUnitDestroy(mut.DeploymentUnit):
266 self.deployed_stacks = ['stack']
267 self.heat_manager = DummyHeatManagerDestroy(dict())
268 self.destroy_all_deployed_stacks_called_correctly = False
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