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.
18 import experimental_framework.deployment_unit as mut
19 import experimental_framework.common as common
21 __author__ = 'vmriccox'
24 class DummyHeatManager:
26 def __init__(self, param):
30 def validate_heat_template(self, template_file):
33 def check_stack_status(self, stack_name):
34 # return 'CREATE_COMPLETE'
37 return 'CREATE_COMPLETE'
39 return 'CREATE_IN_PROGRESS'
41 def delete_stack(self, stack_name):
45 class DummyHeatManagerFailed(DummyHeatManager):
47 def check_stack_status(self, stack_name):
48 return 'CREATE_FAILED'
50 def create_stack(self, template_file, stack_name, parameters):
54 class DummyHeatManagerComplete(DummyHeatManager):
56 def check_stack_status(self, stack_name):
57 return 'CREATE_COMPLETE'
59 def create_stack(self, template_file, stack_name, parameters):
63 class DummyHeatManagerFailedException(DummyHeatManagerFailed):
65 def create_stack(self, template_file, stack_name, parameters):
68 def check_stack_status(self, stack_name):
72 class DummyHeatManagerDestroy:
74 def __init__(self, credentials):
75 self.delete_stack_counter = 0
76 self.check_stack_status_counter = 0
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'
83 return 'DELETE_COMPLETE'
85 def create_stack(self, template_file, stack_name, parameters):
88 def delete_stack(self, stack_name=None):
89 if stack_name == 'stack':
90 self.delete_stack_counter += 1
92 return self.delete_stack_counter
94 def is_stack_deployed(self, stack_name):
98 class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):
100 def delete_stack(self, stack_name=None):
104 class DummyHeatManagerReiteration:
106 def __init__(self, param):
109 def validate_heat_template(self, template_file):
112 def check_stack_status(self, stack_name):
113 return 'CREATE_FAILED'
115 def delete_stack(self, stack_name):
118 def create_stack(self, template_file=None, stack_name=None,
120 if template_file == 'template_reiteration' and \
121 stack_name == 'stack_reiteration' and \
122 parameters == 'parameters_reiteration':
126 class DummyDeploymentUnit(mut.DeploymentUnit):
128 def destroy_heat_template(self, stack_name):
132 class TestDeploymentUnit(unittest.TestCase):
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())
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,
153 mock_os_is_file.return_value = False
154 du = mut.DeploymentUnit(dict())
158 self.assertRaises(ValueError, du.deploy_heat_template, template_file,
159 stack_name, parameters, 0)
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,
166 mock_os_is_file.return_value = True
167 du = mut.DeploymentUnit(dict())
171 common.LOG = logging.getLogger()
172 output = du.deploy_heat_template(template_file, stack_name,
174 self.assertEqual(output, True)
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,
181 mock_os_is_file.return_value = True
182 du = mut.DeploymentUnit(dict())
186 common.LOG = logging.getLogger()
187 output = du.deploy_heat_template(template_file, stack_name,
189 self.assertEqual(output, True)
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,
199 mock_os_is_file.return_value = True
200 du = mut.DeploymentUnit(dict())
204 common.LOG = logging.getLogger()
205 output = du.deploy_heat_template(template_file, stack_name,
207 self.assertEqual(output, True)
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())
220 output = du.deploy_heat_template(template_file, stack_name,
222 self.assertEqual(output, False)
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,
229 openstack_credentials = dict()
230 du = mut.DeploymentUnit(openstack_credentials)
231 du.deployed_stacks = ['stack']
233 self.assertTrue(du.destroy_heat_template(stack_name))
234 self.assertEqual(du.heat_manager.delete_stack(None), 1)
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,
241 openstack_credentials = dict()
242 du = mut.DeploymentUnit(openstack_credentials)
243 du.deployed_stacks = ['stack']
245 self.assertFalse(du.destroy_heat_template(stack_name))
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())
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,
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)
267 class DeploymentUnitDestroy(mut.DeploymentUnit):
270 self.deployed_stacks = ['stack']
271 self.heat_manager = DummyHeatManagerDestroy(dict())
272 self.destroy_all_deployed_stacks_called_correctly = False
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