| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
"""Test the top-level (root) GraphQL queries.""" |
| 18 |
|
| 19 |
from contextlib import suppress |
| 20 |
import pytest |
| 21 |
from typing import TYPE_CHECKING |
| 22 |
|
| 23 |
from graphql import parse, MiddlewareManager |
| 24 |
|
| 25 |
from cylc.flow.data_store_mgr import create_delta_store |
| 26 |
from cylc.flow.id import TaskTokens, Tokens |
| 27 |
from cylc.flow.network.client import WorkflowRuntimeClient |
| 28 |
from cylc.flow.network.schema import schema, SUB_RESOLVER_MAPPING |
| 29 |
from cylc.flow.network.graphql import ( |
| 30 |
CylcExecutionContext, |
| 31 |
IgnoreFieldMiddleware, |
| 32 |
instantiate_middleware, |
| 33 |
) |
| 34 |
from cylc.flow.network.graphql_subscribe import subscribe |
| 35 |
from cylc.flow.workflow_status import get_workflow_status |
| 36 |
|
| 37 |
if TYPE_CHECKING: |
| 38 |
from cylc.flow.scheduler import Scheduler |
| 39 |
|
| 40 |
|
| 41 |
# NOTE: These tests mutate the data store, so running them in isolation may |
| 42 |
# see failures when they actually pass if you run the whole file |
| 43 |
|
| 44 |
|
| 45 |
def job_config(schd): |
| 46 |
return { |
| 47 |
'owner': schd.owner, |
| 48 |
'submit_num': 1, |
| 49 |
'task_id': '1/foo', |
| 50 |
'job_runner_name': 'background', |
| 51 |
'env-script': None, |
| 52 |
'err-script': None, |
| 53 |
'exit-script': None, |
| 54 |
'execution_time_limit': None, |
| 55 |
'init-script': None, |
| 56 |
'post-script': None, |
| 57 |
'pre-script': None, |
| 58 |
'script': 'sleep 5; echo "I come in peace"', |
| 59 |
'work_d': None, |
| 60 |
'directives': {}, |
| 61 |
'environment': {}, |
| 62 |
'param_var': {}, |
| 63 |
'platform': {'name': 'platform'}, |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
def gather_subscription_args(schd, request_string): |
| 68 |
kwargs = { |
| 69 |
"variable_values": {}, |
| 70 |
"operation_name": None, |
| 71 |
"context_value": { |
| 72 |
'op_id': 1, |
| 73 |
'resolvers': schd.server.resolvers, |
| 74 |
'meta': {}, |
| 75 |
}, |
| 76 |
"subscribe_resolver_map": SUB_RESOLVER_MAPPING, |
| 77 |
"middleware": MiddlewareManager( |
| 78 |
*list( |
| 79 |
instantiate_middleware( |
| 80 |
[IgnoreFieldMiddleware] |
| 81 |
) |
| 82 |
) |
| 83 |
), |
| 84 |
"execution_context_class": CylcExecutionContext, |
| 85 |
} |
| 86 |
document = parse(request_string) |
| 87 |
return (document, kwargs) |
| 88 |
|
| 89 |
|
| 90 |
@pytest.fixture |
| 91 |
def job_db_row(): |
| 92 |
return [ |
| 93 |
'1', |
| 94 |
'foo', |
| 95 |
'running', |
| 96 |
4, |
| 97 |
'2020-04-03T13:40:18+13:00', |
| 98 |
'2020-04-03T13:40:20+13:00', |
| 99 |
'2020-04-03T13:40:30+13:00', |
| 100 |
'background', |
| 101 |
'20542', |
| 102 |
'localhost', |
| 103 |
] |
| 104 |
|
| 105 |
|
| 106 |
@pytest.fixture(scope='module') |
| 107 |
async def harness(mod_flow, mod_scheduler, mod_run): |
| 108 |
flow_def = { |
| 109 |
'scheduler': { |
| 110 |
'allow implicit tasks': True |
| 111 |
}, |
| 112 |
'scheduling': { |
| 113 |
'graph': { |
| 114 |
'R1': 'a => b & c => d' |
| 115 |
} |
| 116 |
}, |
| 117 |
'runtime': { |
| 118 |
'A': { |
| 119 |
}, |
| 120 |
'B': { |
| 121 |
'inherit': 'A', |
| 122 |
}, |
| 123 |
'b': { |
| 124 |
'inherit': 'B', |
| 125 |
}, |
| 126 |
}, |
| 127 |
} |
| 128 |
id_: str = mod_flow(flow_def) |
| 129 |
schd: 'Scheduler' = mod_scheduler(id_) |
| 130 |
async with mod_run(schd): |
| 131 |
client = WorkflowRuntimeClient(id_) |
| 132 |
schd.pool.hold_tasks({TaskTokens('*', 'root')}) |
| 133 |
schd.resume_workflow() |
| 134 |
# Think this is needed to save the data state at first start (?) |
| 135 |
# Fails without it.. and a test needs to overwrite schd data with this. |
| 136 |
# data = schd.data_store_mgr.data[schd.data_store_mgr.workflow_id] |
| 137 |
|
| 138 |
workflow_tokens = Tokens( |
| 139 |
user=schd.owner, |
| 140 |
workflow=schd.workflow, |
| 141 |
) |
| 142 |
|
| 143 |
yield schd, client, workflow_tokens |
| 144 |
|
| 145 |
|
| 146 |
async def test_workflows(harness): |
| 147 |
schd, client, w_tokens = harness |
| 148 |
ret = await client.async_request( |
| 149 |
'graphql', |
| 150 |
{'request_string': 'query { workflows { id } }'} |
| 151 |
) |
| 152 |
assert ret == { |
| 153 |
'workflows': [ |
| 154 |
{ |
| 155 |
'id': f'{w_tokens}' |
| 156 |
} |
| 157 |
] |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
async def test_tasks(harness): |
| 162 |
schd, client, w_tokens = harness |
| 163 |
|
| 164 |
# query "tasks" |
| 165 |
ret = await client.async_request( |
| 166 |
'graphql', |
| 167 |
{'request_string': 'query { tasks { id } }'} |
| 168 |
) |
| 169 |
ids = [ |
| 170 |
w_tokens.duplicate(cycle=f'$namespace|{namespace}').id |
| 171 |
for namespace in ('a', 'b', 'c', 'd') |
| 172 |
] |
| 173 |
ret['tasks'].sort(key=lambda x: x['id']) |
| 174 |
assert ret == { |
| 175 |
'tasks': [ |
| 176 |
{'id': id_} |
| 177 |
for id_ in ids |
| 178 |
] |
| 179 |
} |
| 180 |
|
| 181 |
# query "task" |
| 182 |
for id_ in ids: |
| 183 |
ret = await client.async_request( |
| 184 |
'graphql', |
| 185 |
{'request_string': 'query { task(id: "%s") { id } }' % id_} |
| 186 |
) |
| 187 |
assert ret == { |
| 188 |
'task': {'id': id_} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
async def test_families(harness): |
| 193 |
schd, client, w_tokens = harness |
| 194 |
|
| 195 |
# query "tasks" |
| 196 |
ret = await client.async_request( |
| 197 |
'graphql', |
| 198 |
{'request_string': 'query { families { id } }'} |
| 199 |
) |
| 200 |
ids = [ |
| 201 |
w_tokens.duplicate( |
| 202 |
cycle=f'$namespace|{namespace}' |
| 203 |
).id |
| 204 |
for namespace in ('A', 'B', 'root') |
| 205 |
] |
| 206 |
ret['families'].sort(key=lambda x: x['id']) |
| 207 |
assert ret == { |
| 208 |
'families': [ |
| 209 |
{'id': id_} |
| 210 |
for id_ in ids |
| 211 |
] |
| 212 |
} |
| 213 |
|
| 214 |
# query "task" |
| 215 |
for id_ in ids: |
| 216 |
ret = await client.async_request( |
| 217 |
'graphql', |
| 218 |
{'request_string': 'query { family(id: "%s") { id } }' % id_} |
| 219 |
) |
| 220 |
assert ret == { |
| 221 |
'family': {'id': id_} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
async def test_task_proxies(harness): |
| 226 |
schd, client, w_tokens = harness |
| 227 |
|
| 228 |
# query "tasks" |
| 229 |
ret = await client.async_request( |
| 230 |
'graphql', |
| 231 |
{'request_string': 'query { taskProxies { id } }'} |
| 232 |
) |
| 233 |
ids = [ |
| 234 |
w_tokens.duplicate( |
| 235 |
cycle='1', |
| 236 |
task=namespace, |
| 237 |
) |
| 238 |
# NOTE: task "d" is not in the n=1 window yet |
| 239 |
for namespace in ('a', 'b', 'c') |
| 240 |
] |
| 241 |
ret['taskProxies'].sort(key=lambda x: x['id']) |
| 242 |
assert ret == { |
| 243 |
'taskProxies': [ |
| 244 |
{'id': id_.id} |
| 245 |
for id_ in ids |
| 246 |
] |
| 247 |
} |
| 248 |
|
| 249 |
# query "task" |
| 250 |
ret = await client.async_request( |
| 251 |
'graphql', |
| 252 |
{'request_string': 'query { taskProxy(id: "%s") { id } }' % ids[0].id} |
| 253 |
) |
| 254 |
assert ret == { |
| 255 |
'taskProxy': {'id': ids[0].id} |
| 256 |
} |
| 257 |
|
| 258 |
# query "taskProxies" fragment with null stripping |
| 259 |
ret = await client.async_request( |
| 260 |
'graphql', |
| 261 |
{ |
| 262 |
'request_string': ''' |
| 263 |
fragment wf on Workflow { |
| 264 |
taskProxies (ids: ["%s"], stripNull: true) { id } |
| 265 |
} |
| 266 |
query { workflows (ids: ["%s"]) { ...wf } } |
| 267 |
''' % (ids[0].relative_id, ids[0].workflow_id) |
| 268 |
} |
| 269 |
) |
| 270 |
assert ret == {'workflows': [{'taskProxies': [{'id': ids[0].id}]}]} |
| 271 |
|
| 272 |
|
| 273 |
async def test_family_proxies(harness): |
| 274 |
schd, client, w_tokens = harness |
| 275 |
|
| 276 |
# query "familys" |
| 277 |
ret = await client.async_request( |
| 278 |
'graphql', |
| 279 |
{'request_string': 'query { familyProxies { id } }'} |
| 280 |
) |
| 281 |
ids = [ |
| 282 |
w_tokens.duplicate( |
| 283 |
cycle='1', |
| 284 |
task=namespace, |
| 285 |
).id |
| 286 |
# NOTE: family "d" is not in the n=1 window yet |
| 287 |
for namespace in ('A', 'B', 'root') |
| 288 |
] |
| 289 |
ret['familyProxies'].sort(key=lambda x: x['id']) |
| 290 |
assert ret == { |
| 291 |
'familyProxies': [ |
| 292 |
{'id': id_} |
| 293 |
for id_ in ids |
| 294 |
] |
| 295 |
} |
| 296 |
|
| 297 |
# query "family" |
| 298 |
for id_ in ids: |
| 299 |
ret = await client.async_request( |
| 300 |
'graphql', |
| 301 |
{'request_string': 'query { familyProxy(id: "%s") { id } }' % id_} |
| 302 |
) |
| 303 |
assert ret == { |
| 304 |
'familyProxy': {'id': id_} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
async def test_edges(harness): |
| 309 |
schd, client, w_tokens = harness |
| 310 |
|
| 311 |
t_tokens = [ |
| 312 |
w_tokens.duplicate( |
| 313 |
cycle='1', |
| 314 |
task=namespace, |
| 315 |
) |
| 316 |
# NOTE: task "d" is not in the n=1 window yet |
| 317 |
for namespace in ('a', 'b', 'c') |
| 318 |
] |
| 319 |
edges = [ |
| 320 |
(t_tokens[0], t_tokens[1]), |
| 321 |
(t_tokens[0], t_tokens[2]), |
| 322 |
] |
| 323 |
e_ids = sorted([ |
| 324 |
w_tokens.duplicate( |
| 325 |
cycle=( |
| 326 |
'$edge' |
| 327 |
f'|{left.relative_id}' |
| 328 |
f'|{right.relative_id}' |
| 329 |
) |
| 330 |
).id |
| 331 |
for left, right in edges |
| 332 |
]) |
| 333 |
|
| 334 |
# query "edges" |
| 335 |
ret = await client.async_request( |
| 336 |
'graphql', |
| 337 |
{'request_string': 'query { edges { id } }'} |
| 338 |
) |
| 339 |
ret['edges'].sort(key=lambda x: x['id']) |
| 340 |
assert ret == { |
| 341 |
'edges': [ |
| 342 |
{'id': id_} |
| 343 |
for id_ in e_ids |
| 344 |
] |
| 345 |
} |
| 346 |
|
| 347 |
# query "nodesEdges" |
| 348 |
ret = await client.async_request( |
| 349 |
'graphql', |
| 350 |
{'request_string': 'query { nodesEdges { nodes {id}\nedges {id} } }'} |
| 351 |
) |
| 352 |
ret['nodesEdges']['nodes'].sort(key=lambda x: x['id']) |
| 353 |
ret['nodesEdges']['edges'].sort(key=lambda x: x['id']) |
| 354 |
assert ret == { |
| 355 |
'nodesEdges': { |
| 356 |
'nodes': [ |
| 357 |
{'id': tokens.id} |
| 358 |
for tokens in t_tokens |
| 359 |
], |
| 360 |
'edges': [ |
| 361 |
{'id': id_} |
| 362 |
for id_ in e_ids |
| 363 |
], |
| 364 |
}, |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
async def test_jobs(harness): |
| 369 |
schd: Scheduler |
| 370 |
schd, client, w_tokens = harness |
| 371 |
|
| 372 |
# add a job |
| 373 |
itask = schd.pool._get_task_by_id('1/a') |
| 374 |
schd.data_store_mgr.insert_job(itask, 'submitted', job_config(schd)) |
| 375 |
schd.data_store_mgr.update_data_structure() |
| 376 |
j_tokens = w_tokens.duplicate( |
| 377 |
cycle='1', |
| 378 |
task='a', |
| 379 |
job='01', |
| 380 |
) |
| 381 |
j_id = j_tokens.id |
| 382 |
|
| 383 |
# query "jobs" |
| 384 |
ret = await client.async_request( |
| 385 |
'graphql', |
| 386 |
{'request_string': 'query { jobs { id } }'} |
| 387 |
) |
| 388 |
assert ret == { |
| 389 |
'jobs': [ |
| 390 |
{'id': f'{j_id}'} |
| 391 |
] |
| 392 |
} |
| 393 |
|
| 394 |
# query "job" |
| 395 |
ret = await client.async_request( |
| 396 |
'graphql', |
| 397 |
{'request_string': 'query { job(id: "%s") { id } }' % j_id} |
| 398 |
) |
| 399 |
assert ret == { |
| 400 |
'job': {'id': f'{j_id}'} |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # |
| 405 |
# Test the GraphQL subscription infrastructure. |
| 406 |
# (currently only used at UIS) |
| 407 |
@pytest.mark.asyncio(loop_scope="module") |
| 408 |
async def test_subscription_basic(harness): |
| 409 |
"""Test a basic subscription that uses the resolver's sub_resolver code.""" |
| 410 |
schd, _, w_tokens = harness |
| 411 |
document, kwargs = gather_subscription_args( |
| 412 |
schd, |
| 413 |
'subscription { workflows { id } }', |
| 414 |
) |
| 415 |
subscription = await subscribe( |
| 416 |
schema.graphql_schema, |
| 417 |
document, |
| 418 |
**kwargs |
| 419 |
) |
| 420 |
has_item = False |
| 421 |
with suppress(GeneratorExit): |
| 422 |
async for response in subscription: |
| 423 |
has_item = True |
| 424 |
assert response.data['workflows'][0]['id'] == w_tokens.id |
| 425 |
await subscription.aclose() |
| 426 |
assert has_item |
| 427 |
|
| 428 |
|
| 429 |
@pytest.mark.asyncio(loop_scope="module") |
| 430 |
async def test_subscription_deltas(one, start): |
| 431 |
"""Test the full subscription with null-stripping and delta handling.""" |
| 432 |
async with start(one): |
| 433 |
document, kwargs = gather_subscription_args( |
| 434 |
one, |
| 435 |
''' |
| 436 |
subscription { |
| 437 |
deltas (stripNull: true) { |
| 438 |
id |
| 439 |
added { |
| 440 |
workflow { |
| 441 |
id |
| 442 |
host |
| 443 |
status |
| 444 |
} |
| 445 |
} |
| 446 |
updated { |
| 447 |
workflow { |
| 448 |
id |
| 449 |
host |
| 450 |
status |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
''', |
| 456 |
) |
| 457 |
subscription = await subscribe( |
| 458 |
schema.graphql_schema, |
| 459 |
document, |
| 460 |
**kwargs |
| 461 |
) |
| 462 |
aitem = await subscription.__anext__() |
| 463 |
assert aitem.data['deltas']['added']['workflow'] == { |
| 464 |
'id': one.id, |
| 465 |
'host': one.host, |
| 466 |
'status': 'paused', |
| 467 |
} |
| 468 |
# Workflow one is paused on start |
| 469 |
await one.update_data_structure() |
| 470 |
assert ( |
| 471 |
one.data_store_mgr.data[one.id]['workflow'].status |
| 472 |
== get_workflow_status(one).value |
| 473 |
) |
| 474 |
# Get the all delta, process, then add it to the subscription queue. |
| 475 |
btopic, delta, _ = one.data_store_mgr.publish_deltas[-1] |
| 476 |
_, sub_queue = next( |
| 477 |
iter(one.data_store_mgr.delta_queues[one.id].items()) |
| 478 |
) |
| 479 |
sub_queue.put( |
| 480 |
( |
| 481 |
one.id, |
| 482 |
btopic.decode('utf-8'), |
| 483 |
create_delta_store(delta, one.id) |
| 484 |
) |
| 485 |
) |
| 486 |
aitem = await subscription.__anext__() |
| 487 |
assert aitem.data['deltas']['updated']['workflow'] == { |
| 488 |
'id': one.id, |
| 489 |
} |
| 490 |
with suppress(GeneratorExit): |
| 491 |
await subscription.aclose() |