Debian Codegraph @

mistralclient/tests/unit/v2/test_cli_action_execs.py

raw symbols SCIP
1 # Copyright 2014 - Mirantis, Inc.
2 # Copyright 2016 - Brocade Communications Systems, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # 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, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16
17 import copy
18 import io
19 import sys
20 from unittest import mock
21
22 from oslo_serialization import jsonutils
23
24 from mistralclient.api.v2 import action_executions as action_ex
25 from mistralclient.commands.v2 import action_executions as action_ex_cmd
26 from mistralclient.tests.unit import base
27
28 ACTION_EX_DICT = {
29 'id': '123',
30 'name': 'some',
31 'workflow_name': 'thing',
32 'workflow_namespace': '',
33 'task_name': 'task1',
34 'task_execution_id': "1-2-3-4",
35 'state': 'RUNNING',
36 'state_info': 'RUNNING somehow.',
37 'accepted': True,
38 'created_at': '1',
39 'updated_at': '1',
40 }
41
42 ACTION_EX_RESULT = {"test": "is", "passed": "successfully"}
43 ACTION_EX_INPUT = {"param1": "val1", "param2": 2}
44
45 ACTION_EX_WITH_OUTPUT_DICT = ACTION_EX_DICT.copy()
46 ACTION_EX_WITH_OUTPUT_DICT.update(
47 {'output': jsonutils.dumps(ACTION_EX_RESULT)})
48
49 ACTION_EX_WITH_INPUT_DICT = ACTION_EX_DICT.copy()
50 ACTION_EX_WITH_INPUT_DICT.update({'input': jsonutils.dumps(ACTION_EX_INPUT)})
51
52 ACTION_EX = action_ex.ActionExecution(mock, ACTION_EX_DICT)
53 ACTION_EX_WITH_OUTPUT = action_ex.ActionExecution(
54 mock,
55 ACTION_EX_WITH_OUTPUT_DICT
56 )
57 ACTION_EX_WITH_INPUT = action_ex.ActionExecution(
58 mock,
59 ACTION_EX_WITH_INPUT_DICT
60 )
61
62
63 class TestCLIActionExecutions(base.BaseCommandTest):
64 def test_create(self):
65 (self.client.action_executions.create.
66 return_value) = ACTION_EX_WITH_OUTPUT
67
68 self.call(
69 action_ex_cmd.Create,
70 app_args=['some', '{"output": "Hello!"}']
71 )
72
73 self.assertDictEqual(
74 ACTION_EX_RESULT,
75 jsonutils.loads(self.app.stdout.write.call_args[0][0])
76 )
77
78 def test_create_save_result(self):
79 (self.client.action_executions.create.
80 return_value) = ACTION_EX_WITH_OUTPUT
81
82 result = self.call(
83 action_ex_cmd.Create,
84 app_args=[
85 'some', '{"output": "Hello!"}', '--save-result'
86 ]
87 )
88
89 self.assertEqual(
90 ('123', 'some', 'thing', '', 'task1', '1-2-3-4', 'RUNNING',
91 'RUNNING somehow.', True, '1', '1'),
92 result[1]
93 )
94
95 def test_create_run_sync(self):
96 (self.client.action_executions.create.
97 return_value) = ACTION_EX_WITH_OUTPUT
98
99 self.call(
100 action_ex_cmd.Create,
101 app_args=[
102 'some', '{"output": "Hello!"}', '--run-sync'
103 ]
104 )
105
106 self.assertDictEqual(
107 ACTION_EX_RESULT,
108 jsonutils.loads(self.app.stdout.write.call_args[0][0])
109 )
110
111 def test_create_run_sync_and_save_result(self):
112 (self.client.action_executions.create.
113 return_value) = ACTION_EX_WITH_OUTPUT
114
115 self.call(
116 action_ex_cmd.Create,
117 app_args=[
118 'some', '{"output": "Hello!"}', '--save-result', '--run-sync'
119 ]
120 )
121
122 self.assertDictEqual(
123 ACTION_EX_RESULT,
124 jsonutils.loads(self.app.stdout.write.call_args[0][0])
125 )
126
127 def test_update(self):
128 states = ['PAUSED', 'RUNNING', 'SUCCESS', 'ERROR', 'CANCELLED']
129
130 for state in states:
131 action_ex_dict = copy.deepcopy(ACTION_EX_DICT)
132 action_ex_dict['state'] = state
133 action_ex_dict['state_info'] = 'testing update'
134 action_ex_obj = action_ex.ActionExecution(mock, action_ex_dict)
135 self.client.action_executions.update.return_value = action_ex_obj
136
137 result = self.call(
138 action_ex_cmd.Update,
139 app_args=['id', '--state', state]
140 )
141
142 expected_result = (
143 action_ex_dict['id'],
144 action_ex_dict['name'],
145 action_ex_dict['workflow_name'],
146 action_ex_dict['workflow_namespace'],
147 action_ex_dict['task_name'],
148 action_ex_dict['task_execution_id'],
149 action_ex_dict['state'],
150 action_ex_dict['state_info'],
151 action_ex_dict['accepted'],
152 action_ex_dict['created_at'],
153 action_ex_dict['updated_at']
154 )
155
156 self.assertEqual(expected_result, result[1])
157
158 def test_update_invalid_state(self):
159 states = ['IDLE', 'WAITING', 'DELAYED']
160
161 # Redirect the stderr so it doesn't show during tox
162 _stderr = sys.stderr
163 sys.stderr = io.StringIO()
164
165 for state in states:
166 self.assertRaises(
167 SystemExit,
168 self.call,
169 action_ex_cmd.Update,
170 app_args=['id', '--state', state]
171 )
172
173 # Stop the redirection
174 print(sys.stderr.getvalue())
175 sys.stderr = _stderr
176
177 def test_list(self):
178 self.client.action_executions.list.return_value = [ACTION_EX]
179
180 result = self.call(action_ex_cmd.List)
181
182 self.assertEqual(
183 [('123', 'some', 'thing', '', 'task1', '1-2-3-4', 'RUNNING', True,
184 '1', '1')],
185 result[1]
186 )
187
188 def test_get(self):
189 self.client.action_executions.get.return_value = ACTION_EX
190
191 result = self.call(action_ex_cmd.Get, app_args=['id'])
192
193 self.assertEqual(
194 ('123', 'some', 'thing', '', 'task1', '1-2-3-4', 'RUNNING',
195 'RUNNING somehow.', True, '1', '1'), result[1]
196 )
197
198 def test_get_output(self):
199 self.client.action_executions.get.return_value = ACTION_EX_WITH_OUTPUT
200
201 self.call(action_ex_cmd.GetOutput, app_args=['id'])
202
203 self.assertDictEqual(
204 ACTION_EX_RESULT,
205 jsonutils.loads(self.app.stdout.write.call_args[0][0])
206 )
207
208 def test_get_input(self):
209 self.client.action_executions.get.return_value = ACTION_EX_WITH_INPUT
210
211 self.call(action_ex_cmd.GetInput, app_args=['id'])
212
213 self.assertDictEqual(
214 ACTION_EX_INPUT,
215 jsonutils.loads(self.app.stdout.write.call_args[0][0])
216 )
217
218 def test_delete(self):
219 self.call(action_ex_cmd.Delete, app_args=['id'])
220
221 self.client.action_executions.delete.assert_called_once_with('id')
222
223 def test_delete_with_multi_names(self):
224 self.call(action_ex_cmd.Delete, app_args=['id1', 'id2'])
225
226 self.assertEqual(2, self.client.action_executions.delete.call_count)
227 self.assertEqual(
228 [mock.call('id1'), mock.call('id2')],
229 self.client.action_executions.delete.call_args_list
230 )