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 class TestDeploymentUnit(unittest.TestCase):
141 @mock.patch('experimental_framework.heat_manager.HeatManager',
142 side_effect=DummyHeatManager)
143 def test_constructor_for_sanity(self, mock_heat_manager):
144 du = mut.DeploymentUnit(dict())
145 self.assertTrue(isinstance(du.heat_manager, DummyHeatManager))
146 mock_heat_manager.assert_called_once_with(dict())
147 self.assertEqual(du.deployed_stacks, list())
149 @mock.patch('experimental_framework.heat_manager.HeatManager',
150 side_effect=DummyHeatManager)
151 @mock.patch('os.path.isfile')
152 def test_deploy_heat_template_for_failure(self, mock_os_is_file,
154 mock_os_is_file.return_value = False
155 du = mut.DeploymentUnit(dict())
159 self.assertRaises(ValueError, du.deploy_heat_template, template_file,
160 stack_name, parameters, 0)
162 @mock.patch('experimental_framework.heat_manager.HeatManager',
163 side_effect=DummyHeatManager)
164 @mock.patch('os.path.isfile')
165 def test_deploy_heat_template_for_success(self, mock_os_is_file,
167 mock_os_is_file.return_value = True
168 du = mut.DeploymentUnit(dict())
172 common.LOG = logging.getLogger()
173 output = du.deploy_heat_template(template_file, stack_name,
175 self.assertEqual(output, True)
177 @mock.patch('experimental_framework.heat_manager.HeatManager',
178 side_effect=DummyHeatManagerComplete)
179 @mock.patch('os.path.isfile')
180 def test_deploy_heat_template_2_for_success(self, mock_os_is_file,
182 mock_os_is_file.return_value = True
183 du = mut.DeploymentUnit(dict())
187 common.LOG = logging.getLogger()
188 output = du.deploy_heat_template(template_file, stack_name,
190 self.assertEqual(output, True)
192 @mock.patch('experimental_framework.heat_manager.HeatManager',
193 side_effect=DummyHeatManagerComplete)
194 @mock.patch('os.path.isfile')
195 @mock.patch('experimental_framework.deployment_unit.DeploymentUnit',
196 side_effect=DummyDeploymentUnit)
197 def test_deploy_heat_template_3_for_success(self, mock_dep_unit,
200 mock_os_is_file.return_value = True
201 du = mut.DeploymentUnit(dict())
205 common.LOG = logging.getLogger()
206 output = du.deploy_heat_template(template_file, stack_name,
208 self.assertEqual(output, True)
210 @mock.patch('experimental_framework.common.LOG')
211 @mock.patch('experimental_framework.heat_manager.HeatManager',
212 side_effect=DummyHeatManagerFailed)
213 @mock.patch('os.path.isfile')
214 def test_deploy_heat_template_for_success_2(self, mock_os_is_file,
215 mock_heat_manager, mock_log):
216 mock_os_is_file.return_value = True
217 du = DummyDeploymentUnit(dict())
221 output = du.deploy_heat_template(template_file, stack_name,
223 self.assertEqual(output, False)
225 @mock.patch('experimental_framework.heat_manager.HeatManager',
226 side_effect=DummyHeatManagerDestroy)
227 @mock.patch('experimental_framework.common.LOG')
228 def test_destroy_heat_template_for_success(self, mock_log,
230 openstack_credentials = dict()
231 du = mut.DeploymentUnit(openstack_credentials)
232 du.deployed_stacks = ['stack']
234 self.assertTrue(du.destroy_heat_template(stack_name))
235 self.assertEqual(du.heat_manager.delete_stack(None), 1)
237 @mock.patch('experimental_framework.heat_manager.HeatManager',
238 side_effect=DummyHeatManagerDestroyException)
239 @mock.patch('experimental_framework.common.LOG')
240 def test_destroy_heat_template_for_success_2(self, mock_log,
242 openstack_credentials = dict()
243 du = mut.DeploymentUnit(openstack_credentials)
244 du.deployed_stacks = ['stack']
246 self.assertFalse(du.destroy_heat_template(stack_name))
248 def test_destroy_all_deployed_stacks_for_success(self):
249 du = DeploymentUnitDestroy()
250 du.destroy_all_deployed_stacks()
251 self.assertTrue(du.destroy_heat_template())
253 @mock.patch('experimental_framework.heat_manager.HeatManager',
254 side_effect=DummyHeatManagerReiteration)
255 @mock.patch('os.path.isfile')
256 def test_deploy_heat_template_for_success_3(self, mock_os_is_file,
258 mock_os_is_file.return_value = True
259 du = mut.DeploymentUnit(dict())
260 template = 'template_reiteration'
261 stack = 'stack_reiteration'
262 parameters = 'parameters_reiteration'
263 output = du.deploy_heat_template(template, stack, parameters, 0)
264 self.assertFalse(output)
265 self.assertEqual(du.heat_manager.counts, 4)
268 class DeploymentUnitDestroy(mut.DeploymentUnit):
271 self.deployed_stacks = ['stack']
272 self.heat_manager = DummyHeatManagerDestroy(dict())
273 self.destroy_all_deployed_stacks_called_correctly = False
275 def destroy_heat_template(self, template_name=None):
276 if template_name == 'stack':
277 self.destroy_all_deployed_stacks_called_correctly = True
278 return self.destroy_all_deployed_stacks_called_correctly