1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# a copy of the License at
5
# http://www.apache.org/licenses/LICENSE-2.0
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
14
from oslo_messaging.rpc import dispatcher
16
from heat.common import exception
17
from heat.common import template_format
18
from heat.engine import service
19
from heat.engine import stack
20
from heat.engine import template as templatem
21
from heat.objects import stack as stack_object
22
from heat.tests import common
23
from heat.tests.engine import tools
24
from heat.tests import utils
27
class StackServiceActionsTest(common.HeatTestCase):
30
super(StackServiceActionsTest, self).setUp()
31
self.ctx = utils.dummy_context()
32
self.man = service.EngineService('a-host', 'a-topic')
33
self.man.create_periodic_tasks()
35
@mock.patch.object(stack.Stack, 'load')
36
@mock.patch.object(service.ThreadGroupManager, 'start')
37
def test_stack_suspend(self, mock_start, mock_load):
38
stack_name = 'service_suspend_test_stack'
39
t = template_format.parse(tools.wp_template)
40
stk = utils.parse_stack(t, stack_name=stack_name)
41
s = stack_object.Stack.get_by_id(self.ctx, stk.id)
43
mock_load.return_value = stk
44
thread = mock.MagicMock()
45
mock_link = self.patchobject(thread, 'link')
46
mock_start.return_value = thread
48
result = self.man.stack_suspend(self.ctx, stk.identifier())
49
self.assertIsNone(result)
50
mock_load.assert_called_once_with(self.ctx, stack=s)
51
mock_link.assert_called_once_with(mock.ANY)
52
mock_start.assert_called_once_with(stk.id, mock.ANY, stk)
56
@mock.patch.object(stack.Stack, 'load')
57
@mock.patch.object(service.ThreadGroupManager, 'start')
58
def test_stack_resume(self, mock_start, mock_load):
59
stack_name = 'service_resume_test_stack'
60
t = template_format.parse(tools.wp_template)
61
stk = utils.parse_stack(t, stack_name=stack_name)
63
mock_load.return_value = stk
64
thread = mock.MagicMock()
65
mock_link = self.patchobject(thread, 'link')
66
mock_start.return_value = thread
68
result = self.man.stack_resume(self.ctx, stk.identifier())
69
self.assertIsNone(result)
71
mock_load.assert_called_once_with(self.ctx, stack=mock.ANY)
72
mock_link.assert_called_once_with(mock.ANY)
73
mock_start.assert_called_once_with(stk.id, mock.ANY, stk)
77
def test_stack_suspend_nonexist(self):
78
stack_name = 'service_suspend_nonexist_test_stack'
79
t = template_format.parse(tools.wp_template)
80
tmpl = templatem.Template(t)
81
stk = stack.Stack(self.ctx, stack_name, tmpl)
83
ex = self.assertRaises(dispatcher.ExpectedException,
84
self.man.stack_suspend, self.ctx,
86
self.assertEqual(exception.StackNotFound, ex.exc_info[0])
88
def test_stack_resume_nonexist(self):
89
stack_name = 'service_resume_nonexist_test_stack'
90
t = template_format.parse(tools.wp_template)
91
tmpl = templatem.Template(t)
92
stk = stack.Stack(self.ctx, stack_name, tmpl)
94
ex = self.assertRaises(dispatcher.ExpectedException,
95
self.man.stack_resume, self.ctx,
97
self.assertEqual(exception.StackNotFound, ex.exc_info[0])
99
def _mock_thread_start(self, stack_id, func, *args, **kwargs):
100
func(*args, **kwargs)
103
@mock.patch.object(service.ThreadGroupManager, 'start')
104
@mock.patch.object(stack.Stack, 'load')
105
def test_stack_check(self, mock_load, mock_start):
106
stack_name = 'service_check_test_stack'
107
t = template_format.parse(tools.wp_template)
108
stk = utils.parse_stack(t, stack_name=stack_name)
110
stk.check = mock.Mock()
111
mock_load.return_value = stk
112
mock_start.side_effect = self._mock_thread_start
114
self.man.stack_check(self.ctx, stk.identifier())
115
self.assertTrue(stk.check.called)
120
class StackServiceUpdateActionsNotSupportedTest(common.HeatTestCase):
123
('suspend_in_progress', dict(action='SUSPEND', status='IN_PROGRESS')),
124
('suspend_complete', dict(action='SUSPEND', status='COMPLETE')),
125
('suspend_failed', dict(action='SUSPEND', status='FAILED')),
126
('delete_in_progress', dict(action='DELETE', status='IN_PROGRESS')),
127
('delete_complete', dict(action='DELETE', status='COMPLETE')),
128
('delete_failed', dict(action='DELETE', status='FAILED')),
132
super(StackServiceUpdateActionsNotSupportedTest, self).setUp()
133
self.ctx = utils.dummy_context()
134
self.man = service.EngineService('a-host', 'a-topic')
136
@mock.patch.object(stack.Stack, 'load')
137
def test_stack_update_actions_not_supported(self, mock_load):
138
stack_name = '%s-%s' % (self.action, self.status)
139
t = template_format.parse(tools.wp_template)
140
old_stack = utils.parse_stack(t, stack_name=stack_name)
142
old_stack.action = self.action
143
old_stack.status = self.status
145
sid = old_stack.store()
146
s = stack_object.Stack.get_by_id(self.ctx, sid)
148
mock_load.return_value = old_stack
150
params = {'foo': 'bar'}
151
template = '{ "Resources": {} }'
152
ex = self.assertRaises(dispatcher.ExpectedException,
153
self.man.update_stack,
154
self.ctx, old_stack.identifier(), template,
156
self.assertEqual(exception.NotSupported, ex.exc_info[0])
157
mock_load.assert_called_once_with(self.ctx, stack=s)