Debian Codegraph @

mistralclient/tests/unit/v2/test_tasks.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.v2.executions import Execution
19 from mistralclient.api.v2 import tasks
20 from mistralclient.tests.unit.v2 import base
21
22 # TODO(everyone): later we need additional tests verifying all the errors etc.
23
24 TASK = {
25 'id': "1",
26 'workflow_execution_id': '123',
27 'name': 'my_task',
28 'workflow_name': 'my_wf',
29 'state': 'RUNNING',
30 'tags': ['deployment', 'demo'],
31 'result': {'some': 'result'}
32 }
33
34 SUB_WF_EXEC = {
35 'id': "456",
36 'workflow_id': '123e4567-e89b-12d3-a456-426655440000',
37 'workflow_name': 'my_sub_wf',
38 'workflow_namespace': '',
39 'task_execution_id': "1",
40 'description': '',
41 'state': 'RUNNING',
42 'input': {}
43 }
44
45 URL_TEMPLATE = '/tasks'
46 URL_TEMPLATE_ID = '/tasks/%s'
47 URL_TEMPLATE_SUB_EXECUTIONS = '/tasks/%s/executions%s'
48
49
50 class TestTasksV2(base.BaseClientV2Test):
51 def test_list(self):
52 self.requests_mock.get(self.TEST_URL + URL_TEMPLATE,
53 json={'tasks': [TASK]})
54
55 task_list = self.tasks.list()
56
57 self.assertEqual(1, len(task_list))
58 task = task_list[0]
59
60 self.assertEqual(
61 tasks.Task(self.tasks, TASK).to_dict(),
62 task.to_dict()
63 )
64
65 def test_list_with_fields(self):
66 field_params = "?fields=id,name"
67
68 self.requests_mock.get(self.TEST_URL + URL_TEMPLATE + field_params,
69 json={'tasks': [TASK]})
70
71 self.tasks.list(fields=["id,name"])
72 self.assertTrue(self.requests_mock.called_once)
73
74 def test_list_with_no_limit(self):
75 self.requests_mock.get(self.TEST_URL + URL_TEMPLATE,
76 json={'tasks': [TASK]})
77
78 task_list = self.tasks.list(limit=-1)
79
80 self.assertEqual(1, len(task_list))
81
82 last_request = self.requests_mock.last_request
83
84 self.assertNotIn('limit', last_request.qs)
85
86 def test_get(self):
87 url = self.TEST_URL + URL_TEMPLATE_ID % TASK['id']
88 self.requests_mock.get(url, json=TASK)
89
90 task = self.tasks.get(TASK['id'])
91
92 self.assertEqual(
93 tasks.Task(self.tasks, TASK).to_dict(),
94 task.to_dict()
95 )
96
97 def test_rerun(self):
98 url = self.TEST_URL + URL_TEMPLATE_ID % TASK['id']
99 self.requests_mock.put(url, json=TASK)
100
101 task = self.tasks.rerun(TASK['id'])
102
103 self.assertDictEqual(
104 tasks.Task(self.tasks, TASK).to_dict(),
105 task.to_dict()
106 )
107
108 body = {
109 'reset': True,
110 'state': 'RUNNING',
111 'id': TASK['id']
112 }
113 self.assertDictEqual(body, self.requests_mock.last_request.json())
114
115 def test_rerun_no_reset(self):
116 url = self.TEST_URL + URL_TEMPLATE_ID % TASK['id']
117 self.requests_mock.put(url, json=TASK)
118
119 task = self.tasks.rerun(TASK['id'], reset=False)
120
121 self.assertDictEqual(
122 tasks.Task(self.tasks, TASK).to_dict(),
123 task.to_dict()
124 )
125
126 body = {
127 'reset': False,
128 'state': 'RUNNING',
129 'id': TASK['id']
130 }
131 self.assertDictEqual(body, self.requests_mock.last_request.json())
132
133 def test_rerun_update_env(self):
134 url = self.TEST_URL + URL_TEMPLATE_ID % TASK['id']
135 self.requests_mock.put(url, json=TASK)
136
137 task = self.tasks.rerun(TASK['id'], env={'k1': 'foobar'})
138
139 self.assertDictEqual(
140 tasks.Task(self.tasks, TASK).to_dict(),
141 task.to_dict()
142 )
143
144 body = {
145 'reset': True,
146 'state': 'RUNNING',
147 'id': TASK['id'],
148 'env': jsonutils.dumps({'k1': 'foobar'})
149 }
150 self.assertDictEqual(body, self.requests_mock.last_request.json())
151
152 def test_get_sub_executions(self):
153 url = self.TEST_URL + URL_TEMPLATE_SUB_EXECUTIONS \
154 % (TASK['id'], '?max_depth=-1&errors_only=')
155
156 self.requests_mock.get(url, json={'executions': [SUB_WF_EXEC]})
157
158 sub_execution_list = self.tasks.get_task_sub_executions(TASK['id'])
159
160 self.assertEqual(1, len(sub_execution_list))
161 self.assertDictEqual(
162 Execution(self.executions, SUB_WF_EXEC).to_dict(),
163 sub_execution_list[0].to_dict()
164 )