Debian Codegraph @

mistralclient/tests/unit/v2/test_cli_tasks.py

raw symbols SCIP
1 # Copyright 2014 - Mirantis, Inc.
2 # Copyright 2015 - StackStorm, Inc.
3 # All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16 #
17
18 from unittest import mock
19
20 from oslo_serialization import jsonutils
21
22
23 from mistralclient.api.v2.executions import Execution
24 from mistralclient.api.v2 import tasks
25 from mistralclient.commands.v2 import tasks as task_cmd
26 from mistralclient.tests.unit import base
27
28 TASK_DICT = {
29 'id': '123',
30 'name': 'some',
31 'workflow_name': 'thing',
32 'workflow_namespace': '',
33 'workflow_execution_id': '321',
34 'state': 'SUCCESS',
35 'state_info': None,
36 'created_at': '2020-02-07 08:10:32',
37 'started_at': '2020-02-07 08:10:32',
38 'finished_at': '2020-02-07 08:10:41'
39 }
40
41 TASK_SUB_WF_EXEC = Execution(
42 mock,
43 {
44 'id': '456',
45 'workflow_id': '123e4567-e89b-12d3-a456-426655440000',
46 'workflow_name': 'some_sub_wf',
47 'workflow_namespace': '',
48 'root_execution_id': 'ROOT_EXECUTION_ID',
49 'description': '',
50 'state': 'ERROR',
51 'state_info': None,
52 'created_at': '2020-02-07 08:10:32',
53 'updated_at': '2020-02-07 08:10:41',
54 'task_execution_id': '123'
55 }
56 )
57
58 TASK_SUB_WF_EX_RESULT = (
59 '456',
60 '123e4567-e89b-12d3-a456-426655440000',
61 'some_sub_wf',
62 '',
63 '',
64 '123',
65 'ROOT_EXECUTION_ID',
66 'ERROR',
67 None,
68 '2020-02-07 08:10:32',
69 '2020-02-07 08:10:41',
70 '0:00:09'
71 )
72
73 TASK_RESULT = {"test": "is", "passed": "successfully"}
74 TASK_PUBLISHED = {"bar1": "val1", "var2": 2}
75
76 TASK_WITH_RESULT_DICT = TASK_DICT.copy()
77 TASK_WITH_RESULT_DICT.update({'result': jsonutils.dumps(TASK_RESULT)})
78 TASK_WITH_PUBLISHED_DICT = TASK_DICT.copy()
79 TASK_WITH_PUBLISHED_DICT.update({'published': jsonutils.dumps(TASK_PUBLISHED)})
80
81 TASK = tasks.Task(mock, TASK_DICT)
82 TASK_WITH_RESULT = tasks.Task(mock, TASK_WITH_RESULT_DICT)
83 TASK_WITH_PUBLISHED = tasks.Task(mock, TASK_WITH_PUBLISHED_DICT)
84
85 EXPECTED_TASK_RESULT = (
86 '123', 'some', 'thing', '', '321', 'SUCCESS', None,
87 '2020-02-07 08:10:32', '2020-02-07 08:10:32',
88 '2020-02-07 08:10:41', '0:00:09'
89 )
90
91
92 class TestCLITasksV2(base.BaseCommandTest):
93 def test_list(self):
94 self.client.tasks.list.return_value = [TASK]
95
96 result = self.call(task_cmd.List)
97
98 self.assertEqual([EXPECTED_TASK_RESULT], result[1])
99 self.assertEqual(
100 self.client.tasks.list.call_args[1]["fields"],
101 task_cmd.TaskFormatter.fields()
102 )
103
104 def test_list_with_workflow_execution(self):
105 self.client.tasks.list.return_value = [TASK]
106
107 result = self.call(task_cmd.List, app_args=['workflow_execution'])
108
109 self.assertEqual([EXPECTED_TASK_RESULT], result[1])
110
111 def test_get(self):
112 self.client.tasks.get.return_value = TASK
113
114 result = self.call(task_cmd.Get, app_args=['id'])
115
116 self.assertEqual(EXPECTED_TASK_RESULT, result[1])
117
118 def test_get_result(self):
119 self.client.tasks.get.return_value = TASK_WITH_RESULT
120
121 self.call(task_cmd.GetResult, app_args=['id'])
122
123 self.assertDictEqual(
124 TASK_RESULT,
125 jsonutils.loads(self.app.stdout.write.call_args[0][0])
126 )
127
128 def test_get_published(self):
129 self.client.tasks.get.return_value = TASK_WITH_PUBLISHED
130
131 self.call(task_cmd.GetPublished, app_args=['id'])
132
133 self.assertDictEqual(
134 TASK_PUBLISHED,
135 jsonutils.loads(self.app.stdout.write.call_args[0][0])
136 )
137
138 def test_rerun(self):
139 self.client.tasks.rerun.return_value = TASK
140
141 result = self.call(task_cmd.Rerun, app_args=['id'])
142
143 self.assertEqual(EXPECTED_TASK_RESULT, result[1])
144
145 def test_rerun_no_reset(self):
146 self.client.tasks.rerun.return_value = TASK
147
148 result = self.call(task_cmd.Rerun, app_args=['id', '--resume'])
149
150 self.assertEqual(EXPECTED_TASK_RESULT, result[1])
151
152 def test_rerun_update_env(self):
153 self.client.tasks.rerun.return_value = TASK
154
155 result = self.call(
156 task_cmd.Rerun,
157 app_args=['id', '--env', '{"k1": "foobar"}']
158 )
159
160 self.assertEqual(EXPECTED_TASK_RESULT, result[1])
161
162 def test_rerun_no_reset_update_env(self):
163 self.client.tasks.rerun.return_value = TASK
164
165 result = self.call(
166 task_cmd.Rerun,
167 app_args=['id', '--resume', '--env', '{"k1": "foobar"}']
168 )
169
170 self.assertEqual(EXPECTED_TASK_RESULT, result[1])
171
172 def test_sub_executions(self):
173 self.client.tasks.get_task_sub_executions.return_value = \
174 TASK_SUB_WF_EXEC
175
176 result = self.call(
177 task_cmd.SubExecutionsLister,
178 app_args=[TASK_DICT['id']]
179 )
180
181 self.assertEqual([TASK_SUB_WF_EX_RESULT], result[1])
182 self.assertEqual(
183 1,
184 self.client.tasks.get_task_sub_executions.call_count
185 )
186 self.assertEqual(
187 [mock.call(TASK_DICT['id'], errors_only='', max_depth=-1)],
188 self.client.tasks.get_task_sub_executions.call_args_list
189 )
190
191 def test_sub_executions_errors_only(self):
192 self.client.tasks.get_task_sub_executions.return_value = \
193 TASK_SUB_WF_EXEC
194
195 self.call(
196 task_cmd.SubExecutionsLister,
197 app_args=[TASK_DICT['id'], '--errors-only']
198 )
199
200 self.assertEqual(
201 1,
202 self.client.tasks.get_task_sub_executions.call_count
203 )
204 self.assertEqual(
205 [mock.call(TASK_DICT['id'], errors_only=True, max_depth=-1)],
206 self.client.tasks.get_task_sub_executions.call_args_list
207 )
208
209 def test_sub_executions_with_max_depth(self):
210 self.client.tasks.get_task_sub_executions.return_value = \
211 TASK_SUB_WF_EXEC
212
213 self.call(
214 task_cmd.SubExecutionsLister,
215 app_args=[TASK_DICT['id'], '--max-depth', '3']
216 )
217
218 self.assertEqual(
219 1,
220 self.client.tasks.get_task_sub_executions.call_count
221 )
222 self.assertEqual(
223 [mock.call(TASK_DICT['id'], errors_only='', max_depth=3)],
224 self.client.tasks.get_task_sub_executions.call_args_list
225 )