Debian Codegraph @

mistralclient/tests/unit/v2/test_executions.py

raw symbols SCIP
1 # Copyright 2014 - Mirantis, Inc.
2 # Copyright 2015 - StackStorm, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # 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 from oslo_serialization import jsonutils
17
18 from mistralclient.api import base as api_base
19 from mistralclient.api.v2 import executions
20 from mistralclient.tests.unit.v2 import base
21
22 # TODO(everyone): Later we need additional tests verifying all the errors etc.
23
24 EXEC = {
25 'id': "123",
26 'workflow_id': '123e4567-e89b-12d3-a456-426655440000',
27 'workflow_name': 'my_wf',
28 'workflow_namespace': '',
29 'description': '',
30 'state': 'RUNNING',
31 'input': {
32 "person": {
33 "first_name": "John",
34 "last_name": "Doe"
35 }
36 }
37 }
38
39 SUB_WF_EXEC = {
40 'id': "456",
41 'workflow_id': '123e4567-e89b-12d3-a456-426655440000',
42 'workflow_name': 'my_sub_wf',
43 'workflow_namespace': '',
44 'task_execution_id': "abc",
45 'description': '',
46 'state': 'RUNNING',
47 'input': {
48 "person": {
49 "first_name": "John",
50 "last_name": "Doe"
51 }
52 }
53 }
54
55 ERROR_SUB_WF_EXEC = {
56 'id': "456",
57 'workflow_id': '123e4567-e89b-12d3-a456-426655440000',
58 'workflow_name': 'my_sub_wf',
59 'workflow_namespace': '',
60 'task_execution_id': "abc",
61 'description': '',
62 'state': 'ERROR',
63 'input': {}
64 }
65
66
67 SOURCE_EXEC = EXEC
68 SOURCE_EXEC['source_execution_id'] = EXEC['workflow_id']
69 URL_TEMPLATE = '/executions'
70 URL_TEMPLATE_ID = '/executions/%s'
71 URL_TEMPLATE_SUB_EXECUTIONS = '/executions/%s/executions%s'
72
73
74 class TestExecutionsV2(base.BaseClientV2Test):
75 def test_create(self):
76 self.requests_mock.post(self.TEST_URL + URL_TEMPLATE,
77 json=EXEC,
78 status_code=201)
79
80 body = {
81 'workflow_name': EXEC['workflow_name'],
82 'description': '',
83 'input': jsonutils.dumps(EXEC['input'])
84 }
85
86 ex = self.executions.create(
87 EXEC['workflow_name'],
88 EXEC['workflow_namespace'],
89 EXEC['input']
90 )
91
92 self.assertIsNotNone(ex)
93
94 self.assertDictEqual(
95 executions.Execution(self.executions, EXEC).to_dict(),
96 ex.to_dict()
97 )
98
99 self.assertDictEqual(body, self.requests_mock.last_request.json())
100
101 def test_create_with_workflow_id(self):
102 self.requests_mock.post(self.TEST_URL + URL_TEMPLATE,
103 json=EXEC,
104 status_code=201)
105
106 body = {
107 'workflow_id': EXEC['workflow_id'],
108 'description': '',
109 'input': jsonutils.dumps(EXEC['input'])
110 }
111
112 ex = self.executions.create(
113 EXEC['workflow_id'],
114 workflow_input=EXEC['input']
115 )
116
117 self.assertIsNotNone(ex)
118
119 self.assertDictEqual(
120 executions.Execution(self.executions, EXEC).to_dict(),
121 ex.to_dict()
122 )
123
124 self.assertDictEqual(body, self.requests_mock.last_request.json())
125
126 def test_create_with_source_execution_id(self):
127 self.requests_mock.post(self.TEST_URL + URL_TEMPLATE,
128 json=SOURCE_EXEC,
129 status_code=201)
130
131 body = {
132 'description': '',
133 'source_execution_id': SOURCE_EXEC['source_execution_id']
134 }
135
136 ex = self.executions.create(
137 source_execution_id=SOURCE_EXEC['source_execution_id']
138 )
139
140 self.assertIsNotNone(ex)
141
142 self.assertDictEqual(
143 executions.Execution(self.executions, SOURCE_EXEC).to_dict(),
144 ex.to_dict()
145 )
146
147 self.assertDictEqual(body, self.requests_mock.last_request.json())
148
149 def test_create_failure1(self):
150 self.requests_mock.post(self.TEST_URL + URL_TEMPLATE,
151 json=EXEC,
152 status_code=201)
153 self.assertRaises(api_base.APIException, self.executions.create, '')
154
155 def test_update(self):
156 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id']
157 self.requests_mock.put(url, json=EXEC)
158 body = {
159 'state': EXEC['state']
160 }
161
162 ex = self.executions.update(EXEC['id'], EXEC['state'])
163
164 self.assertIsNotNone(ex)
165
166 self.assertDictEqual(
167 executions.Execution(self.executions, EXEC).to_dict(),
168 ex.to_dict()
169 )
170
171 self.assertDictEqual(body, self.requests_mock.last_request.json())
172
173 def test_update_env(self):
174 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id']
175 self.requests_mock.put(url, json=EXEC)
176 body = {
177 'state': EXEC['state'],
178 'params': {
179 'env': {'k1': 'foobar'}
180 }
181 }
182
183 ex = self.executions.update(
184 EXEC['id'],
185 EXEC['state'],
186 env={'k1': 'foobar'}
187 )
188
189 self.assertIsNotNone(ex)
190
191 self.assertDictEqual(
192 executions.Execution(self.executions, EXEC).to_dict(),
193 ex.to_dict()
194 )
195
196 self.assertDictEqual(body, self.requests_mock.last_request.json())
197
198 def test_list(self):
199 self.requests_mock.get(self.TEST_URL + URL_TEMPLATE,
200 json={'executions': [EXEC, SUB_WF_EXEC]})
201
202 execution_list = self.executions.list()
203
204 self.assertEqual(2, len(execution_list))
205
206 self.assertDictEqual(
207 executions.Execution(self.executions, EXEC).to_dict(),
208 execution_list[0].to_dict()
209 )
210
211 self.assertDictEqual(
212 executions.Execution(self.executions, SUB_WF_EXEC).to_dict(),
213 execution_list[1].to_dict()
214 )
215
216 def test_list_with_pagination(self):
217 self.requests_mock.get(self.TEST_URL + URL_TEMPLATE,
218 json={'executions': [EXEC],
219 'next': '/executions?fake'})
220
221 execution_list = self.executions.list(
222 limit=1,
223 sort_keys='created_at',
224 sort_dirs='asc'
225 )
226
227 self.assertEqual(1, len(execution_list))
228
229 last_request = self.requests_mock.last_request
230
231 # The url param order is unpredictable.
232 self.assertEqual(['1'], last_request.qs['limit'])
233 self.assertEqual(['created_at'], last_request.qs['sort_keys'])
234 self.assertEqual(['asc'], last_request.qs['sort_dirs'])
235
236 def test_list_with_no_limit(self):
237 self.requests_mock.get(self.TEST_URL + URL_TEMPLATE,
238 json={'executions': [EXEC]})
239
240 execution_list = self.executions.list(limit=-1)
241
242 self.assertEqual(1, len(execution_list))
243
244 last_request = self.requests_mock.last_request
245
246 self.assertNotIn('limit', last_request.qs)
247
248 def test_get(self):
249 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id']
250 self.requests_mock.get(url, json=EXEC)
251
252 ex = self.executions.get(EXEC['id'])
253
254 self.assertDictEqual(
255 executions.Execution(self.executions, EXEC).to_dict(),
256 ex.to_dict()
257 )
258
259 def test_get_sub_wf_ex(self):
260 url = self.TEST_URL + URL_TEMPLATE_ID % SUB_WF_EXEC['id']
261 self.requests_mock.get(url, json=SUB_WF_EXEC)
262
263 ex = self.executions.get(SUB_WF_EXEC['id'])
264
265 self.assertDictEqual(
266 executions.Execution(self.executions, SUB_WF_EXEC).to_dict(),
267 ex.to_dict()
268 )
269
270 def test_delete_with_force(self):
271 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id']
272 self.requests_mock.delete(url, status_code=204)
273
274 self.executions.delete(EXEC['id'], force=True)
275
276 def test_delete(self):
277 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id']
278 self.requests_mock.delete(url, status_code=204)
279
280 self.executions.delete(EXEC['id'])
281
282 def test_report_statistics_only(self):
283 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id'] \
284 + '/report?statistics_only=True'
285
286 expected_json = {
287 'statistics': {}
288 }
289
290 self.requests_mock.get(url, json=expected_json)
291
292 report = self.executions.get_report(EXEC['id'], statistics_only=True)
293
294 self.assertDictEqual(expected_json, report)
295
296 def test_report(self):
297 url = self.TEST_URL + URL_TEMPLATE_ID % EXEC['id'] + '/report'
298
299 expected_json = {
300 'root_workflow_execution': {},
301 'statistics': {}
302 }
303
304 self.requests_mock.get(url, json=expected_json)
305
306 report = self.executions.get_report(EXEC['id'])
307
308 self.assertDictEqual(expected_json, report)
309
310 def test_get_sub_executions(self):
311 url = self.TEST_URL + URL_TEMPLATE_SUB_EXECUTIONS \
312 % (EXEC['id'], '?max_depth=-1&errors_only=')
313
314 self.requests_mock.get(url, json={'executions': [EXEC, SUB_WF_EXEC]})
315
316 sub_execution_list = self.executions.get_ex_sub_executions(EXEC['id'])
317
318 self.assertEqual(2, len(sub_execution_list))
319 self.assertDictEqual(
320 executions.Execution(self.executions, EXEC).to_dict(),
321 sub_execution_list[0].to_dict()
322 )
323 self.assertDictEqual(
324 executions.Execution(self.executions, SUB_WF_EXEC).to_dict(),
325 sub_execution_list[1].to_dict()
326 )