forked from cylc/cylc-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_graphql.py
More file actions
350 lines (308 loc) · 8.62 KB
/
test_graphql.py
File metadata and controls
350 lines (308 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test the top-level (root) GraphQL queries."""
import pytest
from typing import TYPE_CHECKING
from cylc.flow.id import Tokens
from cylc.flow.network.client import WorkflowRuntimeClient
if TYPE_CHECKING:
from cylc.flow.scheduler import Scheduler
# NOTE: These tests mutate the data store, so running them in isolation may
# see failures when they actually pass if you run the whole file
def job_config(schd):
return {
'owner': schd.owner,
'submit_num': 1,
'task_id': '1/foo',
'job_runner_name': 'background',
'env-script': None,
'err-script': None,
'exit-script': None,
'execution_time_limit': None,
'init-script': None,
'post-script': None,
'pre-script': None,
'script': 'sleep 5; echo "I come in peace"',
'work_d': None,
'directives': {},
'environment': {},
'param_var': {},
'platform': {'name': 'platform'},
}
@pytest.fixture
def job_db_row():
return [
'1',
'foo',
'running',
4,
'2020-04-03T13:40:18+13:00',
'2020-04-03T13:40:20+13:00',
'2020-04-03T13:40:30+13:00',
'background',
'20542',
'localhost',
]
@pytest.fixture(scope='module')
async def harness(mod_flow, mod_scheduler, mod_run):
flow_def = {
'scheduler': {
'allow implicit tasks': True
},
'scheduling': {
'graph': {
'R1': 'a => b & c => d'
}
},
'runtime': {
'A': {
},
'B': {
'inherit': 'A',
},
'b': {
'inherit': 'B',
},
},
}
id_: str = mod_flow(flow_def)
schd: 'Scheduler' = mod_scheduler(id_)
async with mod_run(schd):
client = WorkflowRuntimeClient(id_)
schd.pool.hold_tasks(['*'])
schd.resume_workflow()
# Think this is needed to save the data state at first start (?)
# Fails without it.. and a test needs to overwrite schd data with this.
# data = schd.data_store_mgr.data[schd.data_store_mgr.workflow_id]
workflow_tokens = Tokens(
user=schd.owner,
workflow=schd.workflow,
)
yield schd, client, workflow_tokens
async def test_workflows(harness):
schd, client, w_tokens = harness
ret = await client.async_request(
'graphql',
{'request_string': 'query { workflows { id } }'}
)
assert ret == {
'workflows': [
{
'id': f'{w_tokens}'
}
]
}
async def test_tasks(harness):
schd, client, w_tokens = harness
# query "tasks"
ret = await client.async_request(
'graphql',
{'request_string': 'query { tasks { id } }'}
)
ids = [
w_tokens.duplicate(cycle=f'$namespace|{namespace}').id
for namespace in ('a', 'b', 'c', 'd')
]
ret['tasks'].sort(key=lambda x: x['id'])
assert ret == {
'tasks': [
{'id': id_}
for id_ in ids
]
}
# query "task"
for id_ in ids:
ret = await client.async_request(
'graphql',
{'request_string': 'query { task(id: "%s") { id } }' % id_}
)
assert ret == {
'task': {'id': id_}
}
async def test_families(harness):
schd, client, w_tokens = harness
# query "tasks"
ret = await client.async_request(
'graphql',
{'request_string': 'query { families { id } }'}
)
ids = [
w_tokens.duplicate(
cycle=f'$namespace|{namespace}'
).id
for namespace in ('A', 'B', 'root')
]
ret['families'].sort(key=lambda x: x['id'])
assert ret == {
'families': [
{'id': id_}
for id_ in ids
]
}
# query "task"
for id_ in ids:
ret = await client.async_request(
'graphql',
{'request_string': 'query { family(id: "%s") { id } }' % id_}
)
assert ret == {
'family': {'id': id_}
}
async def test_task_proxies(harness):
schd, client, w_tokens = harness
# query "tasks"
ret = await client.async_request(
'graphql',
{'request_string': 'query { taskProxies { id } }'}
)
ids = [
w_tokens.duplicate(
cycle='1',
task=namespace,
).id
# NOTE: task "d" is not in the n=1 window yet
for namespace in ('a', 'b', 'c')
]
ret['taskProxies'].sort(key=lambda x: x['id'])
assert ret == {
'taskProxies': [
{'id': id_}
for id_ in ids
]
}
# query "task"
ret = await client.async_request(
'graphql',
{'request_string': 'query { taskProxy(id: "%s") { id } }' % ids[0]}
)
assert ret == {
'taskProxy': {'id': ids[0]}
}
async def test_family_proxies(harness):
schd, client, w_tokens = harness
# query "familys"
ret = await client.async_request(
'graphql',
{'request_string': 'query { familyProxies { id } }'}
)
ids = [
w_tokens.duplicate(
cycle='1',
task=namespace,
).id
# NOTE: family "d" is not in the n=1 window yet
for namespace in ('A', 'B', 'root')
]
ret['familyProxies'].sort(key=lambda x: x['id'])
assert ret == {
'familyProxies': [
{'id': id_}
for id_ in ids
]
}
# query "family"
for id_ in ids:
ret = await client.async_request(
'graphql',
{'request_string': 'query { familyProxy(id: "%s") { id } }' % id_}
)
assert ret == {
'familyProxy': {'id': id_}
}
async def test_edges(harness):
schd, client, w_tokens = harness
t_tokens = [
w_tokens.duplicate(
cycle='1',
task=namespace,
)
# NOTE: task "d" is not in the n=1 window yet
for namespace in ('a', 'b', 'c')
]
edges = [
(t_tokens[0], t_tokens[1]),
(t_tokens[0], t_tokens[2]),
]
e_ids = sorted([
w_tokens.duplicate(
cycle=(
'$edge'
f'|{left.relative_id}'
f'|{right.relative_id}'
)
).id
for left, right in edges
])
# query "edges"
ret = await client.async_request(
'graphql',
{'request_string': 'query { edges { id } }'}
)
ret['edges'].sort(key=lambda x: x['id'])
assert ret == {
'edges': [
{'id': id_}
for id_ in e_ids
]
}
# query "nodesEdges"
ret = await client.async_request(
'graphql',
{'request_string': 'query { nodesEdges { nodes {id}\nedges {id} } }'}
)
ret['nodesEdges']['nodes'].sort(key=lambda x: x['id'])
ret['nodesEdges']['edges'].sort(key=lambda x: x['id'])
assert ret == {
'nodesEdges': {
'nodes': [
{'id': tokens.id}
for tokens in t_tokens
],
'edges': [
{'id': id_}
for id_ in e_ids
],
},
}
async def test_jobs(harness):
schd, client, w_tokens = harness
# add a job
schd.data_store_mgr.insert_job('a', '1', 'submitted', job_config(schd))
schd.data_store_mgr.update_data_structure()
j_tokens = w_tokens.duplicate(
cycle='1',
task='a',
job='01',
)
j_id = j_tokens.id
# query "jobs"
ret = await client.async_request(
'graphql',
{'request_string': 'query { jobs { id } }'}
)
assert ret == {
'jobs': [
{'id': f'{j_id}'}
]
}
# query "job"
ret = await client.async_request(
'graphql',
{'request_string': 'query { job(id: "%s") { id } }' % j_id}
)
assert ret == {
'job': {'id': f'{j_id}'}
}