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 from __future__ import absolute_import
19 import experimental_framework.deployment_unit as mut
20 import experimental_framework.common as common
22 __author__ = 'vmriccox'
25 class DummyHeatManager:
27 def __init__(self, param):
31 def validate_heat_template(self, template_file):
34 def check_stack_status(self, stack_name):
35 # return 'CREATE_COMPLETE'
38 return 'CREATE_COMPLETE'
40 return 'CREATE_IN_PROGRESS'
42 def delete_stack(self, stack_name):
46 class DummyHeatManagerFailed(DummyHeatManager):
48 def check_stack_status(self, stack_name):
49 return 'CREATE_FAILED'
51 def create_stack(self, template_file, stack_name, parameters):
55 class DummyHeatManagerComplete(DummyHeatManager):
57 def check_stack_status(self, stack_name):
58 return 'CREATE_COMPLETE'
60 def create_stack(self, template_file, stack_name, parameters):
64 class DummyHeatManagerFailedException(DummyHeatManagerFailed):
66 def create_stack(self, template_file, stack_name, parameters):
69 def check_stack_status(self, stack_name):
73 class DummyHeatManagerDestroy:
75 def __init__(self, credentials):
76 self.delete_stack_counter = 0
77 self.check_stack_status_counter = 0
79 def check_stack_status(self, stack_name):
80 if self.check_stack_status_counter < 2:
81 self.check_stack_status_counter += 1
82 return 'DELETE_IN_PROGRESS'
84 return 'DELETE_COMPLETE'
86 def create_stack(self, template_file, stack_name, parameters):
89 def delete_stack(self, stack_name=None):
90 if stack_name == 'stack':
91 self.delete_stack_counter += 1
93 return self.delete_stack_counter
95 def is_stack_deployed(self, stack_name):
99 class DummyHeatManagerDestroyException(DummyHeatManagerDestroy):
101 def delete_stack(self, stack_name=None):
105 class DummyHeatManagerReiteration:
107 def __init__(self, param):
110 def validate_heat_template(self, template_file):
113 def check_stack_status(self, stack_name):
114 return 'CREATE_FAILED'
116 def delete_stack(self, stack_name):
119 def create_stack(self, template_file=None, stack_name=None,
121 if template_file == 'template_reiteration' and \
122 stack_name == 'stack_reiteration' and \
123 parameters == 'parameters_reiteration':
127 class DummyDeploymentUnit(mut.DeploymentUnit):
129 def destroy_heat_template(self, stack_name):
133 @mock.patch("experimental_framework.deployment_unit.time")
134 class TestDeploymentUnit(unittest.TestCase):
142 @mock.patch('experimental_framework.heat_manager.HeatManager',
143 side_effect=DummyHeatManager)
144 def test_constructor_for_sanity(self, mock_heat_manager, mock_time):
145 du = mut.DeploymentUnit(dict())
146 self.assertTrue(isinstance(du.heat_manager, DummyHeatManager))
147 mock_heat_manager.assert_called_once_with(dict())
148 self.assertEqual(du.deployed_stacks, list())
150 @mock.patch('experimental_framework.heat_manager.HeatManager',
151 side_effect=DummyHeatManager)
152 @mock.patch('os.path.isfile')
153 def test_deploy_heat_template_for_failure(self, mock_os_is_file,
154 mock_heat_manager, mock_time):
155 mock_os_is_file.return_value = False
156 du = mut.DeploymentUnit(dict())
160 self.assertRaises(ValueError, du.deploy_heat_template, template_file,
161 stack_name, parameters, 0)
163 @mock.patch('experimental_framework.heat_manager.HeatManager',
164 side_effect=DummyHeatManager)
165 @mock.patch('os.path.isfile')
166 def test_deploy_heat_template_for_success(self, mock_os_is_file,
167 mock_heat_manager, mock_time):
168 mock_os_is_file.return_value = True
169 du = mut.DeploymentUnit(dict())
173 common.LOG = logging.getLogger()
174 output = du.deploy_heat_template(template_file, stack_name,
176 self.assertEqual(output, True)
178 @mock.patch('experimental_framework.heat_manager.HeatManager',
179 side_effect=DummyHeatManagerComplete)
180 @mock.patch('os.path.isfile')
181 def test_deploy_heat_template_2_for_success(self, mock_os_is_file,
182 mock_heat_manager, mock_time):
183 mock_os_is_file.return_value = True
184 du = mut.DeploymentUnit(dict())
188 common.LOG = logging.getLogger()
189 output = du.deploy_heat_template(template_file, stack_name,
191 self.assertEqual(output, True)
193 @mock.patch('experimental_framework.heat_manager.HeatManager',
194 side_effect=DummyHeatManagerComplete)
195 @mock.patch('os.path.isfile')
196 @mock.patch('experimental_framework.deployment_unit.DeploymentUnit',
197 side_effect=DummyDeploymentUnit)
198 def test_deploy_heat_template_3_for_success(self, mock_dep_unit,
200 mock_heat_manager, mock_time):
201 mock_os_is_file.return_value = True
202 du = mut.DeploymentUnit(dict())
206 common.LOG = logging.getLogger()
207 output = du.deploy_heat_template(template_file, stack_name,
209 self.assertEqual(output, True)
211 @mock.patch('experimental_framework.common.LOG')
212 @mock.patch('experimental_framework.heat_manager.HeatManager',
213 side_effect=DummyHeatManagerFailed)
214 @mock.patch('os.path.isfile')
215 def test_deploy_heat_template_for_success_2(self, mock_os_is_file,
216 mock_heat_manager, mock_log, mock_time):
217 mock_os_is_file.return_value = True
218 du = DummyDeploymentUnit(dict())
222 output = du.deploy_heat_template(template_file, stack_name,
224 self.assertEqual(output, False)
226 @mock.patch('experimental_framework.heat_manager.HeatManager',
227 side_effect=DummyHeatManagerDestroy)
228 @mock.patch('experimental_framework.common.LOG')
229 def test_destroy_heat_template_for_success(self, mock_log,
230 mock_heat_manager, mock_time):
231 openstack_credentials = dict()
232 du = mut.DeploymentUnit(openstack_credentials)
233 du.deployed_stacks = ['stack']
235 self.assertTrue(du.destroy_heat_template(stack_name))
236 self.assertEqual(du.heat_manager.delete_stack(None), 1)
238 @mock.patch('experimental_framework.heat_manager.HeatManager',
239 side_effect=DummyHeatManagerDestroyException)
240 @mock.patch('experimental_framework.common.LOG')
241 def test_destroy_heat_template_for_success_2(self, mock_log,
242 mock_heat_manager, mock_time):
243 openstack_credentials = dict()
244 du = mut.DeploymentUnit(openstack_credentials)
245 du.deployed_stacks = ['stack']
247 self.assertFalse(du.destroy_heat_template(stack_name))
249 def test_destroy_all_deployed_stacks_for_success(self, mock_time):
250 du = DeploymentUnitDestroy()
251 du.destroy_all_deployed_stacks()
252 self.assertTrue(du.destroy_heat_template())
254 @mock.patch('experimental_framework.heat_manager.HeatManager',
255 side_effect=DummyHeatManagerReiteration)
256 @mock.patch('os.path.isfile')
257 def test_deploy_heat_template_for_success_3(self, mock_os_is_file,
258 mock_heat_manager, mock_time):
259 mock_os_is_file.return_value = True
260 du = mut.DeploymentUnit(dict())
261 template = 'template_reiteration'
262 stack = 'stack_reiteration'
263 parameters = 'parameters_reiteration'
264 output = du.deploy_heat_template(template, stack, parameters, 0)
265 self.assertFalse(output)
266 self.assertEqual(du.heat_manager.counts, 4)
269 class DeploymentUnitDestroy(mut.DeploymentUnit):
272 self.deployed_stacks = ['stack']
273 self.heat_manager = DummyHeatManagerDestroy(dict())
274 self.destroy_all_deployed_stacks_called_correctly = False
276 def destroy_heat_template(self, template_name=None):
277 if template_name == 'stack':
278 self.destroy_all_deployed_stacks_called_correctly = True
279 return self.destroy_all_deployed_stacks_called_correctly