1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import copy |
17
|
|
|
import uuid |
18
|
|
|
|
19
|
|
|
import mock |
20
|
|
|
from mock import call |
21
|
|
|
import requests |
22
|
|
|
import yaml |
23
|
|
|
|
24
|
|
|
from mistralclient.api.v2 import executions |
25
|
|
|
from mistralclient.api.v2 import workflows |
26
|
|
|
from oslo_config import cfg |
27
|
|
|
|
28
|
|
|
# XXX: actionsensor import depends on config being setup. |
29
|
|
|
import st2tests.config as tests_config |
30
|
|
|
tests_config.parse_args() |
31
|
|
|
|
32
|
|
|
from mistral_v2 import MistralRunner |
33
|
|
|
from st2common.bootstrap import actionsregistrar |
34
|
|
|
from st2common.bootstrap import runnersregistrar |
35
|
|
|
from st2common.constants import action as action_constants |
36
|
|
|
from st2common.models.db.liveaction import LiveActionDB |
37
|
|
|
from st2common.persistence.liveaction import LiveAction |
38
|
|
|
from st2common.runners import base as runners |
39
|
|
|
from st2common.services import action as action_service |
40
|
|
|
from st2common.transport.liveaction import LiveActionPublisher |
41
|
|
|
from st2common.transport.publishers import CUDPublisher |
42
|
|
|
from st2common.util import loader |
43
|
|
|
from st2tests import DbTestCase |
44
|
|
|
from st2tests import fixturesloader |
45
|
|
|
from st2tests.mocks.liveaction import MockLiveActionPublisher |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
TEST_PACK = 'mistral_tests' |
49
|
|
|
TEST_PACK_PATH = fixturesloader.get_fixtures_packs_base_path() + '/' + TEST_PACK |
50
|
|
|
|
51
|
|
|
PACKS = [ |
52
|
|
|
TEST_PACK_PATH, |
53
|
|
|
fixturesloader.get_fixtures_packs_base_path() + '/core' |
54
|
|
|
] |
55
|
|
|
|
56
|
|
|
# Action executions requirements |
57
|
|
|
MISTRAL_EXECUTION = {'id': str(uuid.uuid4()), 'state': 'RUNNING', 'workflow_name': None} |
58
|
|
|
ACTION_PARAMS = {'friend': 'Rocky'} |
59
|
|
|
NON_EMPTY_RESULT = 'non-empty' |
60
|
|
|
|
61
|
|
|
# Non-workbook with a single workflow |
62
|
|
|
WF1_META_FILE_NAME = 'workflow_v2.yaml' |
63
|
|
|
WF1_META_FILE_PATH = TEST_PACK_PATH + '/actions/' + WF1_META_FILE_NAME |
64
|
|
|
WF1_META_CONTENT = loader.load_meta_file(WF1_META_FILE_PATH) |
65
|
|
|
WF1_NAME = WF1_META_CONTENT['pack'] + '.' + WF1_META_CONTENT['name'] |
66
|
|
|
WF1_ENTRY_POINT = TEST_PACK_PATH + '/actions/' + WF1_META_CONTENT['entry_point'] |
67
|
|
|
WF1_ENTRY_POINT_X = WF1_ENTRY_POINT.replace(WF1_META_FILE_NAME, 'xformed_' + WF1_META_FILE_NAME) |
68
|
|
|
WF1_SPEC = yaml.safe_load(MistralRunner.get_workflow_definition(WF1_ENTRY_POINT_X)) |
69
|
|
|
WF1_YAML = yaml.safe_dump(WF1_SPEC, default_flow_style=False) |
70
|
|
|
WF1 = workflows.Workflow(None, {'name': WF1_NAME, 'definition': WF1_YAML}) |
71
|
|
|
WF1_OLD = workflows.Workflow(None, {'name': WF1_NAME, 'definition': ''}) |
72
|
|
|
WF1_EXEC = copy.deepcopy(MISTRAL_EXECUTION) |
73
|
|
|
WF1_EXEC['workflow_name'] = WF1_NAME |
74
|
|
|
WF1_EXEC_PAUSED = copy.deepcopy(WF1_EXEC) |
75
|
|
|
WF1_EXEC_PAUSED['state'] = 'PAUSED' |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
@mock.patch.object( |
79
|
|
|
CUDPublisher, |
80
|
|
|
'publish_update', |
81
|
|
|
mock.MagicMock(return_value=None)) |
82
|
|
|
@mock.patch.object( |
83
|
|
|
CUDPublisher, |
84
|
|
|
'publish_create', |
85
|
|
|
mock.MagicMock(side_effect=MockLiveActionPublisher.publish_create)) |
86
|
|
|
@mock.patch.object( |
87
|
|
|
LiveActionPublisher, |
88
|
|
|
'publish_state', |
89
|
|
|
mock.MagicMock(side_effect=MockLiveActionPublisher.publish_state)) |
90
|
|
|
class MistralRunnerCancelTest(DbTestCase): |
91
|
|
|
|
92
|
|
|
@classmethod |
93
|
|
|
def setUpClass(cls): |
94
|
|
|
super(MistralRunnerCancelTest, cls).setUpClass() |
95
|
|
|
|
96
|
|
|
# Override the retry configuration here otherwise st2tests.config.parse_args |
97
|
|
|
# in DbTestCase.setUpClass will reset these overrides. |
98
|
|
|
cfg.CONF.set_override('retry_exp_msec', 100, group='mistral') |
99
|
|
|
cfg.CONF.set_override('retry_exp_max_msec', 200, group='mistral') |
100
|
|
|
cfg.CONF.set_override('retry_stop_max_msec', 200, group='mistral') |
101
|
|
|
cfg.CONF.set_override('api_url', 'http://0.0.0.0:9101', group='auth') |
102
|
|
|
|
103
|
|
|
# Register runners. |
104
|
|
|
runnersregistrar.register_runners() |
105
|
|
|
|
106
|
|
|
# Register test pack(s). |
107
|
|
|
actions_registrar = actionsregistrar.ActionsRegistrar( |
108
|
|
|
use_pack_cache=False, |
109
|
|
|
fail_on_failure=True |
110
|
|
|
) |
111
|
|
|
|
112
|
|
|
for pack in PACKS: |
113
|
|
|
actions_registrar.register_from_pack(pack) |
114
|
|
|
|
115
|
|
|
@classmethod |
116
|
|
|
def get_runner_class(cls, runner_name): |
117
|
|
|
return runners.get_runner(runner_name).__class__ |
118
|
|
|
|
119
|
|
|
@mock.patch.object( |
120
|
|
|
workflows.WorkflowManager, 'list', |
121
|
|
|
mock.MagicMock(return_value=[])) |
122
|
|
|
@mock.patch.object( |
123
|
|
|
workflows.WorkflowManager, 'get', |
124
|
|
|
mock.MagicMock(return_value=WF1)) |
125
|
|
|
@mock.patch.object( |
126
|
|
|
workflows.WorkflowManager, 'create', |
127
|
|
|
mock.MagicMock(return_value=[WF1])) |
128
|
|
|
@mock.patch.object( |
129
|
|
|
executions.ExecutionManager, 'create', |
130
|
|
|
mock.MagicMock(return_value=executions.Execution(None, WF1_EXEC))) |
131
|
|
|
@mock.patch.object( |
132
|
|
|
executions.ExecutionManager, 'update', |
133
|
|
|
mock.MagicMock(return_value=executions.Execution(None, WF1_EXEC_PAUSED))) |
134
|
|
|
def test_cancel(self): |
135
|
|
|
liveaction = LiveActionDB(action=WF1_NAME, parameters=ACTION_PARAMS) |
136
|
|
|
liveaction, execution = action_service.request(liveaction) |
137
|
|
|
liveaction = LiveAction.get_by_id(str(liveaction.id)) |
138
|
|
|
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_RUNNING) |
139
|
|
|
|
140
|
|
|
mistral_context = liveaction.context.get('mistral', None) |
141
|
|
|
self.assertIsNotNone(mistral_context) |
142
|
|
|
self.assertEqual(mistral_context['execution_id'], WF1_EXEC.get('id')) |
143
|
|
|
self.assertEqual(mistral_context['workflow_name'], WF1_EXEC.get('workflow_name')) |
144
|
|
|
|
145
|
|
|
requester = cfg.CONF.system_user.user |
146
|
|
|
liveaction, execution = action_service.request_cancellation(liveaction, requester) |
147
|
|
|
executions.ExecutionManager.update.assert_called_with(WF1_EXEC.get('id'), 'PAUSED') |
148
|
|
|
liveaction = LiveAction.get_by_id(str(liveaction.id)) |
149
|
|
|
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_CANCELED) |
150
|
|
|
|
151
|
|
|
@mock.patch.object( |
152
|
|
|
workflows.WorkflowManager, 'list', |
153
|
|
|
mock.MagicMock(return_value=[])) |
154
|
|
|
@mock.patch.object( |
155
|
|
|
workflows.WorkflowManager, 'get', |
156
|
|
|
mock.MagicMock(return_value=WF1)) |
157
|
|
|
@mock.patch.object( |
158
|
|
|
workflows.WorkflowManager, 'create', |
159
|
|
|
mock.MagicMock(return_value=[WF1])) |
160
|
|
|
@mock.patch.object( |
161
|
|
|
executions.ExecutionManager, 'create', |
162
|
|
|
mock.MagicMock(return_value=executions.Execution(None, WF1_EXEC))) |
163
|
|
|
@mock.patch.object( |
164
|
|
|
executions.ExecutionManager, 'update', |
165
|
|
|
mock.MagicMock(side_effect=[requests.exceptions.ConnectionError(), |
166
|
|
|
executions.Execution(None, WF1_EXEC_PAUSED)])) |
167
|
|
|
def test_cancel_retry(self): |
168
|
|
|
liveaction = LiveActionDB(action=WF1_NAME, parameters=ACTION_PARAMS) |
169
|
|
|
liveaction, execution = action_service.request(liveaction) |
170
|
|
|
liveaction = LiveAction.get_by_id(str(liveaction.id)) |
171
|
|
|
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_RUNNING) |
172
|
|
|
|
173
|
|
|
mistral_context = liveaction.context.get('mistral', None) |
174
|
|
|
self.assertIsNotNone(mistral_context) |
175
|
|
|
self.assertEqual(mistral_context['execution_id'], WF1_EXEC.get('id')) |
176
|
|
|
self.assertEqual(mistral_context['workflow_name'], WF1_EXEC.get('workflow_name')) |
177
|
|
|
|
178
|
|
|
requester = cfg.CONF.system_user.user |
179
|
|
|
liveaction, execution = action_service.request_cancellation(liveaction, requester) |
180
|
|
|
executions.ExecutionManager.update.assert_called_with(WF1_EXEC.get('id'), 'PAUSED') |
181
|
|
|
liveaction = LiveAction.get_by_id(str(liveaction.id)) |
182
|
|
|
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_CANCELED) |
183
|
|
|
|
184
|
|
|
@mock.patch.object( |
185
|
|
|
workflows.WorkflowManager, 'list', |
186
|
|
|
mock.MagicMock(return_value=[])) |
187
|
|
|
@mock.patch.object( |
188
|
|
|
workflows.WorkflowManager, 'get', |
189
|
|
|
mock.MagicMock(return_value=WF1)) |
190
|
|
|
@mock.patch.object( |
191
|
|
|
workflows.WorkflowManager, 'create', |
192
|
|
|
mock.MagicMock(return_value=[WF1])) |
193
|
|
|
@mock.patch.object( |
194
|
|
|
executions.ExecutionManager, 'create', |
195
|
|
|
mock.MagicMock(return_value=executions.Execution(None, WF1_EXEC))) |
196
|
|
|
@mock.patch.object( |
197
|
|
|
executions.ExecutionManager, 'update', |
198
|
|
|
mock.MagicMock(side_effect=requests.exceptions.ConnectionError('Connection refused'))) |
199
|
|
|
def test_cancel_retry_exhausted(self): |
200
|
|
|
liveaction = LiveActionDB(action=WF1_NAME, parameters=ACTION_PARAMS) |
201
|
|
|
liveaction, execution = action_service.request(liveaction) |
202
|
|
|
liveaction = LiveAction.get_by_id(str(liveaction.id)) |
203
|
|
|
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_RUNNING) |
204
|
|
|
|
205
|
|
|
mistral_context = liveaction.context.get('mistral', None) |
206
|
|
|
self.assertIsNotNone(mistral_context) |
207
|
|
|
self.assertEqual(mistral_context['execution_id'], WF1_EXEC.get('id')) |
208
|
|
|
self.assertEqual(mistral_context['workflow_name'], WF1_EXEC.get('workflow_name')) |
209
|
|
|
|
210
|
|
|
requester = cfg.CONF.system_user.user |
211
|
|
|
liveaction, execution = action_service.request_cancellation(liveaction, requester) |
212
|
|
|
|
213
|
|
|
calls = [call(WF1_EXEC.get('id'), 'PAUSED') for i in range(0, 2)] |
214
|
|
|
executions.ExecutionManager.update.assert_has_calls(calls) |
215
|
|
|
|
216
|
|
|
liveaction = LiveAction.get_by_id(str(liveaction.id)) |
217
|
|
|
self.assertEqual(liveaction.status, action_constants.LIVEACTION_STATUS_CANCELING) |
218
|
|
|
|