~ubuntu-branches/ubuntu/wily/heat/wily

« back to all changes in this revision

Viewing changes to heat/tests/engine/service/test_stack_action.py

  • Committer: Package Import Robot
  • Author(s): Corey Bryant
  • Date: 2015-09-08 15:52:07 UTC
  • mfrom: (1.1.28)
  • Revision ID: package-import@ubuntu.com-20150908155207-zi2r1rckyrevr5u7
Tags: 1:5.0.0~b3-0ubuntu1
* New upstream milestone for OpenStack Liberty.
* d/control: Align (build-)depends with upstream.
* d/p/fix-dummy-resource-missing.patch: Dropped. Fixed in milestone.
* d/p/move-extensions.patch: Dropped. Fixed in milestone.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
#
 
5
#         http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
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
 
11
# under the License.
 
12
 
 
13
import mock
 
14
from oslo_messaging.rpc import dispatcher
 
15
 
 
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
 
25
 
 
26
 
 
27
class StackServiceActionsTest(common.HeatTestCase):
 
28
 
 
29
    def setUp(self):
 
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()
 
34
 
 
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)
 
42
 
 
43
        mock_load.return_value = stk
 
44
        thread = mock.MagicMock()
 
45
        mock_link = self.patchobject(thread, 'link')
 
46
        mock_start.return_value = thread
 
47
 
 
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)
 
53
 
 
54
        stk.delete()
 
55
 
 
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)
 
62
 
 
63
        mock_load.return_value = stk
 
64
        thread = mock.MagicMock()
 
65
        mock_link = self.patchobject(thread, 'link')
 
66
        mock_start.return_value = thread
 
67
 
 
68
        result = self.man.stack_resume(self.ctx, stk.identifier())
 
69
        self.assertIsNone(result)
 
70
 
 
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)
 
74
 
 
75
        stk.delete()
 
76
 
 
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)
 
82
 
 
83
        ex = self.assertRaises(dispatcher.ExpectedException,
 
84
                               self.man.stack_suspend, self.ctx,
 
85
                               stk.identifier())
 
86
        self.assertEqual(exception.StackNotFound, ex.exc_info[0])
 
87
 
 
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)
 
93
 
 
94
        ex = self.assertRaises(dispatcher.ExpectedException,
 
95
                               self.man.stack_resume, self.ctx,
 
96
                               stk.identifier())
 
97
        self.assertEqual(exception.StackNotFound, ex.exc_info[0])
 
98
 
 
99
    def _mock_thread_start(self, stack_id, func, *args, **kwargs):
 
100
        func(*args, **kwargs)
 
101
        return mock.Mock()
 
102
 
 
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)
 
109
 
 
110
        stk.check = mock.Mock()
 
111
        mock_load.return_value = stk
 
112
        mock_start.side_effect = self._mock_thread_start
 
113
 
 
114
        self.man.stack_check(self.ctx, stk.identifier())
 
115
        self.assertTrue(stk.check.called)
 
116
 
 
117
        stk.delete()
 
118
 
 
119
 
 
120
class StackServiceUpdateActionsNotSupportedTest(common.HeatTestCase):
 
121
 
 
122
    scenarios = [
 
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')),
 
129
    ]
 
130
 
 
131
    def setUp(self):
 
132
        super(StackServiceUpdateActionsNotSupportedTest, self).setUp()
 
133
        self.ctx = utils.dummy_context()
 
134
        self.man = service.EngineService('a-host', 'a-topic')
 
135
 
 
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)
 
141
 
 
142
        old_stack.action = self.action
 
143
        old_stack.status = self.status
 
144
 
 
145
        sid = old_stack.store()
 
146
        s = stack_object.Stack.get_by_id(self.ctx, sid)
 
147
 
 
148
        mock_load.return_value = old_stack
 
149
 
 
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,
 
155
                               params, None, {})
 
156
        self.assertEqual(exception.NotSupported, ex.exc_info[0])
 
157
        mock_load.assert_called_once_with(self.ctx, stack=s)
 
158
 
 
159
        old_stack.delete()