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 mock |
17
|
|
|
|
18
|
|
|
import inquirer |
19
|
|
|
from st2actions.container import service |
20
|
|
|
from st2common.constants.action import LIVEACTION_STATUS_PENDING |
21
|
|
|
from st2common.constants.pack import SYSTEM_PACK_NAME |
22
|
|
|
from st2common.persistence.execution import ActionExecution |
23
|
|
|
from st2common.services import action as action_service |
24
|
|
|
from st2common.transport import reactor |
25
|
|
|
from st2common.util import action_db as action_utils |
26
|
|
|
from st2tests.base import RunnerTestCase |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
mock_exc_get = mock.Mock() |
30
|
|
|
mock_exc_get.id = 'abcdef' |
31
|
|
|
|
32
|
|
|
mock_inquiry_liveaction_db = mock.Mock() |
33
|
|
|
mock_inquiry_liveaction_db.result = {"response": {}} |
34
|
|
|
|
35
|
|
|
mock_action_utils = mock.Mock() |
36
|
|
|
mock_action_utils.return_value = mock_inquiry_liveaction_db |
37
|
|
|
|
38
|
|
|
test_parent = mock.Mock() |
39
|
|
|
test_parent.id = '1234567890' |
40
|
|
|
|
41
|
|
|
mock_get_root = mock.Mock() |
42
|
|
|
mock_get_root.return_value = test_parent |
43
|
|
|
|
44
|
|
|
mock_trigger_dispatcher = mock.Mock() |
45
|
|
|
mock_request_pause = mock.Mock() |
46
|
|
|
|
47
|
|
|
test_user = 'st2admin' |
48
|
|
|
|
49
|
|
|
runner_params = { |
50
|
|
|
"users": [], |
51
|
|
|
"roles": [], |
52
|
|
|
"route": "developers", |
53
|
|
|
"schema": {} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
@mock.patch('inquirer.TriggerDispatcher', mock_trigger_dispatcher) |
58
|
|
|
@mock.patch.object( |
59
|
|
|
reactor, |
60
|
|
|
'TriggerDispatcher', |
61
|
|
|
mock_action_utils) |
62
|
|
|
@mock.patch.object( |
63
|
|
|
action_utils, |
64
|
|
|
'get_liveaction_by_id', |
65
|
|
|
mock_action_utils) |
66
|
|
|
@mock.patch.object( |
67
|
|
|
action_service, |
68
|
|
|
'request_pause', |
69
|
|
|
mock_request_pause) |
70
|
|
|
@mock.patch.object( |
71
|
|
|
action_service, |
72
|
|
|
'get_root_liveaction', |
73
|
|
|
mock_get_root) |
74
|
|
|
@mock.patch.object( |
75
|
|
|
ActionExecution, |
76
|
|
|
'get', |
77
|
|
|
mock.MagicMock(return_value=mock_exc_get)) |
78
|
|
|
class InquiryTestCase(RunnerTestCase): |
79
|
|
|
|
80
|
|
|
def tearDown(self): |
81
|
|
|
mock_trigger_dispatcher.reset_mock() |
82
|
|
|
mock_action_utils.reset_mock() |
83
|
|
|
mock_get_root.reset_mock() |
84
|
|
|
mock_request_pause.reset_mock() |
85
|
|
|
|
86
|
|
|
def test_runner_creation(self): |
87
|
|
|
runner = inquirer.get_runner() |
88
|
|
|
self.assertTrue(runner is not None, 'Creation failed. No instance.') |
89
|
|
|
self.assertEqual(type(runner), inquirer.Inquirer, 'Creation failed. No instance.') |
90
|
|
|
|
91
|
|
|
def test_simple_inquiry(self): |
92
|
|
|
runner = inquirer.get_runner() |
93
|
|
|
runner.context = { |
94
|
|
|
'user': test_user |
95
|
|
|
} |
96
|
|
|
runner.action = self._get_mock_action_obj() |
97
|
|
|
runner.runner_parameters = runner_params |
98
|
|
|
runner.container_service = service.RunnerContainerService() |
99
|
|
|
runner.pre_run() |
100
|
|
|
mock_inquiry_liveaction_db.context = { |
101
|
|
|
"parent": test_parent.id |
102
|
|
|
} |
103
|
|
|
(status, output, _) = runner.run({}) |
104
|
|
|
self.assertEqual(status, LIVEACTION_STATUS_PENDING) |
105
|
|
|
self.assertEqual( |
106
|
|
|
output, |
107
|
|
|
{ |
108
|
|
|
'users': [], |
109
|
|
|
'roles': [], |
110
|
|
|
'route': "developers", |
111
|
|
|
'schema': {}, |
112
|
|
|
'ttl': 1440 |
113
|
|
|
} |
114
|
|
|
) |
115
|
|
|
mock_trigger_dispatcher.return_value.dispatch.assert_called_once_with( |
116
|
|
|
'core.st2.generic.inquiry', |
117
|
|
|
{ |
118
|
|
|
'id': mock_exc_get.id, |
119
|
|
|
'route': "developers" |
120
|
|
|
} |
121
|
|
|
) |
122
|
|
|
mock_request_pause.assert_called_once_with( |
123
|
|
|
test_parent, |
124
|
|
|
test_user |
125
|
|
|
) |
126
|
|
|
|
127
|
|
|
def test_inquiry_no_parent(self): |
128
|
|
|
"""Should behave like a regular execution, but without requesting a pause |
129
|
|
|
""" |
130
|
|
|
|
131
|
|
|
runner = inquirer.get_runner() |
132
|
|
|
runner.context = { |
133
|
|
|
'user': 'st2admin' |
134
|
|
|
} |
135
|
|
|
runner.action = self._get_mock_action_obj() |
136
|
|
|
runner.runner_parameters = runner_params |
137
|
|
|
runner.container_service = service.RunnerContainerService() |
138
|
|
|
runner.pre_run() |
139
|
|
|
mock_inquiry_liveaction_db.context = { |
140
|
|
|
"parent": None |
141
|
|
|
} |
142
|
|
|
(status, output, _) = runner.run({}) |
143
|
|
|
self.assertEqual(status, LIVEACTION_STATUS_PENDING) |
144
|
|
|
self.assertEqual( |
145
|
|
|
output, |
146
|
|
|
{ |
147
|
|
|
'users': [], |
148
|
|
|
'roles': [], |
149
|
|
|
'route': "developers", |
150
|
|
|
'schema': {}, |
151
|
|
|
'ttl': 1440 |
152
|
|
|
} |
153
|
|
|
) |
154
|
|
|
mock_trigger_dispatcher.return_value.dispatch.assert_called_once_with( |
155
|
|
|
'core.st2.generic.inquiry', |
156
|
|
|
{ |
157
|
|
|
'id': mock_exc_get.id, |
158
|
|
|
'route': "developers" |
159
|
|
|
} |
160
|
|
|
) |
161
|
|
|
mock_request_pause.assert_not_called() |
162
|
|
|
|
163
|
|
|
def _get_mock_action_obj(self): |
164
|
|
|
action = mock.Mock() |
165
|
|
|
action.pack = SYSTEM_PACK_NAME |
166
|
|
|
action.users = [] |
167
|
|
|
action.roles = [] |
168
|
|
|
return action |
169
|
|
|
|