| 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 |
import logging |
| 18 |
from pathlib import Path |
| 19 |
import sqlite3 |
| 20 |
from textwrap import dedent |
| 21 |
from typing import Any |
| 22 |
import pytest |
| 23 |
|
| 24 |
from cylc.flow import commands |
| 25 |
from cylc.flow.cfgspec.glbl_cfg import glbl_cfg |
| 26 |
from cylc.flow.cfgspec.globalcfg import GlobalConfig |
| 27 |
from cylc.flow.exceptions import ( |
| 28 |
InputError, |
| 29 |
PointParsingError, |
| 30 |
ServiceFileError, |
| 31 |
WorkflowConfigError, |
| 32 |
XtriggerConfigError, |
| 33 |
) |
| 34 |
from cylc.flow.parsec.exceptions import ListValueError |
| 35 |
from cylc.flow.parsec.fileparse import read_and_proc |
| 36 |
from cylc.flow.pathutil import get_workflow_run_pub_db_path |
| 37 |
from cylc.flow.scheduler import Scheduler |
| 38 |
|
| 39 |
Fixture = Any |
| 40 |
param = pytest.param |
| 41 |
|
| 42 |
|
| 43 |
@pytest.mark.parametrize( |
| 44 |
'task_name,valid', [ |
| 45 |
# valid task names |
| 46 |
('a', True), |
| 47 |
('a-b', True), |
| 48 |
('a_b', True), |
| 49 |
('foo', True), |
| 50 |
('0aA-+%', True), |
| 51 |
# invalid task names |
| 52 |
('a b', False), |
| 53 |
('a£b', False), |
| 54 |
('+ab', False), |
| 55 |
('@ab', False), # not valid in [runtime] |
| 56 |
('_cylc', False), |
| 57 |
('_cylcy', False), |
| 58 |
] |
| 59 |
) |
| 60 |
def test_validate_task_name( |
| 61 |
flow, |
| 62 |
one_conf, |
| 63 |
validate, |
| 64 |
task_name: str, |
| 65 |
valid: bool |
| 66 |
): |
| 67 |
"""It should raise errors for invalid task names in the runtime section.""" |
| 68 |
id_ = flow({ |
| 69 |
**one_conf, |
| 70 |
'runtime': { |
| 71 |
task_name: {} |
| 72 |
} |
| 73 |
}) |
| 74 |
|
| 75 |
if valid: |
| 76 |
validate(id_) |
| 77 |
else: |
| 78 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 79 |
validate(id_) |
| 80 |
assert task_name in str(exc_ctx.value) |
| 81 |
|
| 82 |
|
| 83 |
@pytest.mark.parametrize( |
| 84 |
'task_name', |
| 85 |
[ |
| 86 |
'root', |
| 87 |
'_cylc', |
| 88 |
'_cylcy', |
| 89 |
] |
| 90 |
) |
| 91 |
def test_validate_implicit_task_name( |
| 92 |
flow, |
| 93 |
validate, |
| 94 |
task_name: str, |
| 95 |
): |
| 96 |
"""It should validate implicit task names in the graph. |
| 97 |
|
| 98 |
Note that most invalid task names get caught during graph parsing. |
| 99 |
Here we ensure that names which look like valid graph node names but which |
| 100 |
are blacklisted get caught and raise errors. |
| 101 |
""" |
| 102 |
id_ = flow({ |
| 103 |
'scheduling': { |
| 104 |
'graph': { |
| 105 |
'R1': task_name |
| 106 |
} |
| 107 |
}, |
| 108 |
'runtime': { |
| 109 |
# having one item in the runtime allows "root" to be expanded |
| 110 |
# which makes this test more thorough |
| 111 |
'whatever': {} |
| 112 |
} |
| 113 |
}) |
| 114 |
|
| 115 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 116 |
validate(id_) |
| 117 |
assert str(exc_ctx.value).splitlines()[0] == ( |
| 118 |
f'invalid task name "{task_name}"' |
| 119 |
) |
| 120 |
|
| 121 |
|
| 122 |
@pytest.mark.parametrize( |
| 123 |
'env_var,valid', [ |
| 124 |
('foo', True), |
| 125 |
('FOO', True), |
| 126 |
('+foo', False), |
| 127 |
] |
| 128 |
) |
| 129 |
def test_validate_env_vars(flow, one_conf, validate, env_var, valid): |
| 130 |
"""It should validate environment variable names.""" |
| 131 |
id_ = flow({ |
| 132 |
**one_conf, |
| 133 |
'runtime': { |
| 134 |
'foo': { |
| 135 |
'environment': { |
| 136 |
env_var: 'value' |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
}) |
| 141 |
if valid: |
| 142 |
validate(id_) |
| 143 |
else: |
| 144 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 145 |
validate(id_) |
| 146 |
assert env_var in str(exc_ctx.value) |
| 147 |
|
| 148 |
|
| 149 |
@pytest.mark.parametrize( |
| 150 |
'env_val', [ |
| 151 |
'%(x)s', # valid template but no such parameter x |
| 152 |
'%(a)123', # invalid template |
| 153 |
] |
| 154 |
) |
| 155 |
def test_validate_param_env_templ( |
| 156 |
flow, |
| 157 |
one_conf, |
| 158 |
validate, |
| 159 |
env_val, |
| 160 |
log_filter, |
| 161 |
): |
| 162 |
"""It should validate parameter environment templates.""" |
| 163 |
id_ = flow({ |
| 164 |
**one_conf, |
| 165 |
'runtime': { |
| 166 |
'foo': { |
| 167 |
'environment': { |
| 168 |
'foo': env_val |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
}) |
| 173 |
validate(id_) |
| 174 |
assert log_filter(contains='bad parameter environment template') |
| 175 |
assert log_filter(contains=env_val) |
| 176 |
|
| 177 |
|
| 178 |
def test_no_graph(flow, validate): |
| 179 |
"""It should fail for missing graph sections.""" |
| 180 |
id_ = flow({ |
| 181 |
'scheduling': {}, |
| 182 |
}) |
| 183 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 184 |
validate(id_) |
| 185 |
assert 'missing [scheduling][[graph]] section.' in str(exc_ctx.value) |
| 186 |
|
| 187 |
|
| 188 |
@pytest.mark.parametrize( |
| 189 |
'section', [ |
| 190 |
'external-trigger', |
| 191 |
'clock-trigger', |
| 192 |
'clock-expire', |
| 193 |
] |
| 194 |
) |
| 195 |
def test_parse_special_tasks_invalid(flow, validate, section): |
| 196 |
"""It should fail for invalid "special tasks".""" |
| 197 |
id_ = flow({ |
| 198 |
'scheduling': { |
| 199 |
'initial cycle point': 'now', |
| 200 |
'special tasks': { |
| 201 |
section: 'foo (', # missing closing bracket |
| 202 |
}, |
| 203 |
'graph': { |
| 204 |
'R1': 'foo', |
| 205 |
}, |
| 206 |
} |
| 207 |
}) |
| 208 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 209 |
validate(id_) |
| 210 |
assert f'Illegal {section} spec' in str(exc_ctx.value) |
| 211 |
assert 'foo' in str(exc_ctx.value) |
| 212 |
|
| 213 |
|
| 214 |
def test_parse_special_tasks_interval(flow, validate): |
| 215 |
"""It should fail for invalid durations in clock-triggers.""" |
| 216 |
id_ = flow({ |
| 217 |
'scheduling': { |
| 218 |
'initial cycle point': 'now', |
| 219 |
'special tasks': { |
| 220 |
'clock-trigger': 'foo(PT1Y)', # invalid ISO8601 duration |
| 221 |
}, |
| 222 |
'graph': { |
| 223 |
'R1': 'foo' |
| 224 |
} |
| 225 |
} |
| 226 |
}) |
| 227 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 228 |
validate(id_) |
| 229 |
assert 'Illegal clock-trigger spec' in str(exc_ctx.value) |
| 230 |
assert 'PT1Y' in str(exc_ctx.value) |
| 231 |
|
| 232 |
|
| 233 |
@pytest.mark.parametrize( |
| 234 |
'section', [ |
| 235 |
'external-trigger', |
| 236 |
'clock-trigger', |
| 237 |
'clock-expire', |
| 238 |
] |
| 239 |
) |
| 240 |
def test_parse_special_tasks_families(flow, scheduler, validate, section): |
| 241 |
"""It should expand families in special tasks.""" |
| 242 |
id_ = flow({ |
| 243 |
'scheduling': { |
| 244 |
'initial cycle point': 'now', |
| 245 |
'special tasks': { |
| 246 |
section: 'FOO(P1D)', |
| 247 |
}, |
| 248 |
'graph': { |
| 249 |
'R1': 'foo & foot', |
| 250 |
} |
| 251 |
}, |
| 252 |
'runtime': { |
| 253 |
# family |
| 254 |
'FOO': {}, |
| 255 |
# nested family |
| 256 |
'FOOT': { |
| 257 |
'inherit': 'FOO', |
| 258 |
}, |
| 259 |
'foo': { |
| 260 |
'inherit': 'FOO', |
| 261 |
}, |
| 262 |
'foot': { |
| 263 |
'inherit': 'FOOT', |
| 264 |
}, |
| 265 |
} |
| 266 |
}) |
| 267 |
if section == 'external-trigger': |
| 268 |
# external triggers cannot be used for multiple tasks so if family |
| 269 |
# expansion is completed correctly, validation should fail |
| 270 |
with pytest.raises(WorkflowConfigError) as exc_ctx: |
| 271 |
config = validate(id_) |
| 272 |
assert 'external triggers must be used only once' in str(exc_ctx.value) |
| 273 |
else: |
| 274 |
config = validate(id_) |
| 275 |
assert set(config.cfg['scheduling']['special tasks'][section]) == { |
| 276 |
# the family FOO has been expanded to the tasks foo, foot |
| 277 |
'foo(P1D)', |
| 278 |
'foot(P1D)' |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
def test_queue_treated_as_implicit(flow, validate, caplog, log_filter): |
| 283 |
"""Tasks in queues but not in runtime generate a warning. |
| 284 |
|
| 285 |
https://github.com/cylc/cylc-flow/issues/5260 |
| 286 |
""" |
| 287 |
id_ = flow( |
| 288 |
{ |
| 289 |
"scheduling": { |
| 290 |
"queues": {"my_queue": {"members": "task1, task2"}}, |
| 291 |
"graph": {"R1": "task2"}, |
| 292 |
}, |
| 293 |
"runtime": {"task2": {}}, |
| 294 |
} |
| 295 |
) |
| 296 |
validate(id_) |
| 297 |
assert log_filter(contains='Queues contain tasks not defined in runtime') |
| 298 |
|
| 299 |
|
| 300 |
def test_queue_treated_as_comma_separated(flow, validate): |
| 301 |
"""Tasks listed in queue should be separated with commas, not spaces. |
| 302 |
|
| 303 |
https://github.com/cylc/cylc-flow/issues/5260 |
| 304 |
""" |
| 305 |
id_ = flow( |
| 306 |
{ |
| 307 |
"scheduling": { |
| 308 |
"queues": {"my_queue": {"members": "task1 task2"}}, |
| 309 |
"graph": {"R1": "task2"}, |
| 310 |
}, |
| 311 |
"runtime": {"task1": {}, "task2": {}}, |
| 312 |
} |
| 313 |
) |
| 314 |
with pytest.raises(ListValueError, match="cannot contain a space"): |
| 315 |
validate(id_) |
| 316 |
|
| 317 |
|
| 318 |
def test_validate_incompatible_db(one_conf, flow, validate): |
| 319 |
"""Validation should fail for an incompatible DB due to not being able |
| 320 |
to load template vars.""" |
| 321 |
wid = flow(one_conf) |
| 322 |
# Create fake outdated DB |
| 323 |
db_file = Path(get_workflow_run_pub_db_path(wid)) |
| 324 |
db_file.parent.mkdir(parents=True, exist_ok=True) |
| 325 |
db_file.touch() |
| 326 |
conn = sqlite3.connect(db_file) |
| 327 |
try: |
| 328 |
conn.execute( |
| 329 |
'CREATE TABLE suite_params(key TEXT, value TEXT, PRIMARY KEY(key))' |
| 330 |
) |
| 331 |
conn.commit() |
| 332 |
finally: |
| 333 |
conn.close() |
| 334 |
|
| 335 |
with pytest.raises( |
| 336 |
ServiceFileError, match="Workflow database is incompatible" |
| 337 |
): |
| 338 |
validate(wid) |
| 339 |
|
| 340 |
# No tables should have been created |
| 341 |
stmt = "SELECT name FROM sqlite_master WHERE type='table'" |
| 342 |
conn = sqlite3.connect(db_file) |
| 343 |
try: |
| 344 |
tables = [i[0] for i in conn.execute(stmt)] |
| 345 |
finally: |
| 346 |
conn.close() |
| 347 |
assert tables == ['suite_params'] |
| 348 |
|
| 349 |
|
| 350 |
def test_xtrig_validation_wall_clock( |
| 351 |
flow: 'Fixture', |
| 352 |
validate: 'Fixture', |
| 353 |
): |
| 354 |
"""If an xtrigger module has a `validate()` function is called. |
| 355 |
|
| 356 |
https://github.com/cylc/cylc-flow/issues/5448 |
| 357 |
""" |
| 358 |
id_ = flow({ |
| 359 |
'scheduling': { |
| 360 |
'initial cycle point': '1012', |
| 361 |
'xtriggers': {'myxt': 'wall_clock(offset=PT7MH)'}, |
| 362 |
'graph': {'R1': '@myxt => foo'}, |
| 363 |
} |
| 364 |
}) |
| 365 |
with pytest.raises(WorkflowConfigError, match=( |
| 366 |
r'\[@myxt\] wall_clock\(offset=PT7MH\)\n' |
| 367 |
r'Invalid offset: PT7MH' |
| 368 |
)): |
| 369 |
validate(id_) |
| 370 |
|
| 371 |
|
| 372 |
def test_xtrig_implicit_wall_clock(flow: Fixture, validate: Fixture): |
| 373 |
"""Test @wall_clock is allowed in graph without explicit |
| 374 |
xtrigger definition. |
| 375 |
""" |
| 376 |
wid = flow({ |
| 377 |
'scheduling': { |
| 378 |
'initial cycle point': '2024', |
| 379 |
'graph': {'R1': '@wall_clock => foo'}, |
| 380 |
} |
| 381 |
}) |
| 382 |
validate(wid) |
| 383 |
|
| 384 |
|
| 385 |
def test_xtrig_validation_echo( |
| 386 |
flow: 'Fixture', |
| 387 |
validate: 'Fixture', |
| 388 |
): |
| 389 |
"""If an xtrigger module has a `validate()` function is called. |
| 390 |
|
| 391 |
https://github.com/cylc/cylc-flow/issues/5448 |
| 392 |
""" |
| 393 |
id_ = flow({ |
| 394 |
'scheduling': { |
| 395 |
'xtriggers': {'myxt': 'echo()'}, |
| 396 |
'graph': {'R1': '@myxt => foo'}, |
| 397 |
} |
| 398 |
}) |
| 399 |
with pytest.raises( |
| 400 |
WorkflowConfigError, |
| 401 |
match=r'Requires \'succeed=True/False\' arg' |
| 402 |
): |
| 403 |
validate(id_) |
| 404 |
|
| 405 |
|
| 406 |
def test_xtrig_validation_xrandom( |
| 407 |
flow: 'Fixture', |
| 408 |
validate: 'Fixture', |
| 409 |
): |
| 410 |
"""If an xtrigger module has a `validate()` function it is called. |
| 411 |
|
| 412 |
https://github.com/cylc/cylc-flow/issues/5448 |
| 413 |
""" |
| 414 |
id_ = flow({ |
| 415 |
'scheduling': { |
| 416 |
'xtriggers': {'myxt': 'xrandom(200)'}, |
| 417 |
'graph': {'R1': '@myxt => foo'}, |
| 418 |
} |
| 419 |
}) |
| 420 |
with pytest.raises( |
| 421 |
XtriggerConfigError, |
| 422 |
match=r"'percent' should be a float between 0 and 100" |
| 423 |
): |
| 424 |
validate(id_) |
| 425 |
|
| 426 |
|
| 427 |
def test_xtrig_validation_custom( |
| 428 |
flow: 'Fixture', |
| 429 |
validate: 'Fixture', |
| 430 |
monkeypatch: 'Fixture', |
| 431 |
): |
| 432 |
"""If an xtrigger module has a `validate()` function |
| 433 |
an exception is raised if that validate function fails. |
| 434 |
|
| 435 |
https://github.com/cylc/cylc-flow/issues/5448 |
| 436 |
""" |
| 437 |
# Rather than create our own xtrigger module on disk |
| 438 |
# and attempt to trigger a validation failure we |
| 439 |
# mock our own exception, xtrigger and xtrigger |
| 440 |
# validation functions and inject these into the |
| 441 |
# appropriate locations: |
| 442 |
def kustom_xt(feature): |
| 443 |
return True, {} |
| 444 |
|
| 445 |
def kustom_validate(args): |
| 446 |
raise Exception('This is only a test.') |
| 447 |
|
| 448 |
# Patch xtrigger func & its validate func |
| 449 |
monkeypatch.setattr( |
| 450 |
'cylc.flow.xtrigger_mgr.get_xtrig_func', |
| 451 |
lambda *args: kustom_validate if "validate" in args else kustom_xt |
| 452 |
) |
| 453 |
|
| 454 |
id_ = flow({ |
| 455 |
'scheduling': { |
| 456 |
'initial cycle point': '1012', |
| 457 |
'xtriggers': {'myxt': 'kustom_xt(feature=42)'}, |
| 458 |
'graph': {'R1': '@myxt => foo'}, |
| 459 |
} |
| 460 |
}) |
| 461 |
|
| 462 |
Path(id_) |
| 463 |
with pytest.raises(XtriggerConfigError, match=r'This is only a test.'): |
| 464 |
validate(id_) |
| 465 |
|
| 466 |
|
| 467 |
@pytest.mark.parametrize('xtrig_call, expected_msg', [ |
| 468 |
pytest.param( |
| 469 |
'xrandom()', |
| 470 |
r"missing a required argument: 'percent'", |
| 471 |
id="missing-arg" |
| 472 |
), |
| 473 |
pytest.param( |
| 474 |
'wall_clock(alan_grant=1)', |
| 475 |
r"unexpected keyword argument 'alan_grant'", |
| 476 |
id="unexpected-arg" |
| 477 |
), |
| 478 |
]) |
| 479 |
def test_xtrig_signature_validation( |
| 480 |
flow: 'Fixture', validate: 'Fixture', |
| 481 |
xtrig_call: str, expected_msg: str |
| 482 |
): |
| 483 |
"""Test automatic xtrigger function signature validation.""" |
| 484 |
id_ = flow({ |
| 485 |
'scheduling': { |
| 486 |
'initial cycle point': '2024', |
| 487 |
'xtriggers': {'myxt': xtrig_call}, |
| 488 |
'graph': {'R1': '@myxt => foo'}, |
| 489 |
} |
| 490 |
}) |
| 491 |
with pytest.raises(XtriggerConfigError, match=expected_msg): |
| 492 |
validate(id_) |
| 493 |
|
| 494 |
|
| 495 |
@pytest.mark.parametrize( |
| 496 |
'left', |
| 497 |
( |
| 498 |
param('@xrandom | @echo', id='xtrig-or-xtrig'), |
| 499 |
param('@xrandom | task', id='xtrig-or-task'), |
| 500 |
param('task | @echo', id='task-or-xtrig'), |
| 501 |
param('@xrandom | foo & bar', id='xtrig-or-complex'), |
| 502 |
param('@xrandom & bar | foo', id='complex-or-xtrig'), |
| 503 |
) |
| 504 |
) |
| 505 |
def test_xtrig_or_fails_validation( |
| 506 |
flow: "Fixture", |
| 507 |
validate: "Fixture", |
| 508 |
left: str |
| 509 |
): |
| 510 |
"""Xtriggers cannot be chained with the 'or' |
| 511 |
|
| 512 |
https://github.com/cylc/cylc-flow/issues/6771 |
| 513 |
https://github.com/cylc/cylc-flow/issues/2712 |
| 514 |
""" |
| 515 |
id_ = flow( |
| 516 |
{ |
| 517 |
"scheduling": { |
| 518 |
"initial cycle point": "2024", |
| 519 |
"xtriggers": { |
| 520 |
"xrandom": "xrandom(100)", |
| 521 |
"echo": "echo(succeed=True)" |
| 522 |
}, |
| 523 |
"graph": {"R1": f"{left} => fin"}, |
| 524 |
} |
| 525 |
} |
| 526 |
) |
| 527 |
expected_msg = ( |
| 528 |
"Xtriggers cannot be used in conditional graph expressions:\n") |
| 529 |
with pytest.raises(WorkflowConfigError, match=expected_msg): |
| 530 |
validate(id_) |
| 531 |
|
| 532 |
|
| 533 |
def test_special_task_non_word_names(flow: Fixture, validate: Fixture): |
| 534 |
"""Test validation of special tasks names with non-word characters""" |
| 535 |
wid = flow({ |
| 536 |
'scheduling': { |
| 537 |
'initial cycle point': '2020', |
| 538 |
'special tasks': { |
| 539 |
'clock-trigger': 't-1, t+1, t%1, t@1', |
| 540 |
}, |
| 541 |
'graph': { |
| 542 |
'P1D': 't-1 & t+1 & t%1 & t@1', |
| 543 |
}, |
| 544 |
}, |
| 545 |
'runtime': { |
| 546 |
't-1, t+1, t%1, t@1': {'script': True}, |
| 547 |
}, |
| 548 |
}) |
| 549 |
validate(wid) |
| 550 |
|
| 551 |
|
| 552 |
async def test_glbl_cfg(monkeypatch, tmp_path, caplog): |
| 553 |
"""Test accessing the global config via the glbl_cfg wrapper. |
| 554 |
|
| 555 |
Test the "cached" and "reload" kwargs to glbl_cfg. |
| 556 |
|
| 557 |
Also assert that accessing the global config during a reload operation does |
| 558 |
not cause issues. See https://github.com/cylc/cylc-flow/issues/6244 |
| 559 |
""" |
| 560 |
# wipe any previously cached config |
| 561 |
monkeypatch.setattr( |
| 562 |
'cylc.flow.cfgspec.globalcfg.GlobalConfig._DEFAULT', None |
| 563 |
) |
| 564 |
# load the global config from the test tmp directory |
| 565 |
monkeypatch.setenv('CYLC_CONF_PATH', str(tmp_path)) |
| 566 |
|
| 567 |
def write_global_config(cfg_str): |
| 568 |
"""Write the global.cylc file.""" |
| 569 |
Path(tmp_path, 'global.cylc').write_text(cfg_str) |
| 570 |
|
| 571 |
def get_platforms(cfg_obj): |
| 572 |
"""Return the platforms defined in the provided config instance.""" |
| 573 |
return set(cfg_obj.get(['platforms']).keys()) |
| 574 |
|
| 575 |
def expect_platforms_during_reload(platforms): |
| 576 |
"""Test the platforms defined in glbl_cfg() during reload. |
| 577 |
|
| 578 |
Assert that the platforms defined in glbl_cfg() match the expected |
| 579 |
value, whilst the global config is in the process of being reloaded. |
| 580 |
|
| 581 |
In other words, this tests that the cached instance is not changed |
| 582 |
until after the reload has completed. |
| 583 |
|
| 584 |
See https://github.com/cylc/cylc-flow/issues/6244 |
| 585 |
""" |
| 586 |
caplog.set_level(logging.INFO) |
| 587 |
|
| 588 |
def _capture(fcn): |
| 589 |
def _inner(*args, **kwargs): |
| 590 |
cfg = glbl_cfg() |
| 591 |
assert get_platforms(cfg) == platforms |
| 592 |
logging.getLogger('test').info( |
| 593 |
'ran expect_platforms_during_reload test' |
| 594 |
) |
| 595 |
return fcn(*args, **kwargs) |
| 596 |
return _inner |
| 597 |
|
| 598 |
monkeypatch.setattr( |
| 599 |
'cylc.flow.cfgspec.globalcfg.GlobalConfig._load', |
| 600 |
_capture(GlobalConfig._load) |
| 601 |
) |
| 602 |
|
| 603 |
# write a global config |
| 604 |
write_global_config(''' |
| 605 |
[platforms] |
| 606 |
[[foo]] |
| 607 |
''') |
| 608 |
|
| 609 |
# test the platforms defined in it |
| 610 |
assert get_platforms(glbl_cfg()) == {'localhost', 'foo'} |
| 611 |
|
| 612 |
# add a new platform the config |
| 613 |
write_global_config(''' |
| 614 |
[platforms] |
| 615 |
[[foo]] |
| 616 |
[[bar]] |
| 617 |
''') |
| 618 |
|
| 619 |
# the new platform should not appear (due to the cached instance) |
| 620 |
assert get_platforms(glbl_cfg()) == {'localhost', 'foo'} |
| 621 |
|
| 622 |
# if we request an uncached instance, the new platform should appear |
| 623 |
assert get_platforms(glbl_cfg(cached=False)) == {'localhost', 'foo', 'bar'} |
| 624 |
|
| 625 |
# however, this should not affect the cached instance |
| 626 |
assert get_platforms(glbl_cfg()) == {'localhost', 'foo'} |
| 627 |
|
| 628 |
# * if we reload the cached instance, the new platform should appear |
| 629 |
# * but during the reload itself, the old config should persist |
| 630 |
# see https://github.com/cylc/cylc-flow/issues/6244 |
| 631 |
expect_platforms_during_reload({'localhost', 'foo'}) |
| 632 |
assert get_platforms(glbl_cfg(reload=True)) == {'localhost', 'foo', 'bar'} |
| 633 |
assert 'ran expect_platforms_during_reload test' in caplog.messages |
| 634 |
|
| 635 |
# the cache should have been updated by the reload |
| 636 |
assert get_platforms(glbl_cfg()) == {'localhost', 'foo', 'bar'} |
| 637 |
|
| 638 |
|
| 639 |
def test_nonlive_mode_validation(flow, validate, caplog, log_filter): |
| 640 |
"""Nonlive tasks return a warning at validation. |
| 641 |
""" |
| 642 |
caplog.set_level(logging.INFO) |
| 643 |
msg1 = dedent( |
| 644 |
'The following tasks are set to run in skip mode:\n * skip' |
| 645 |
) |
| 646 |
|
| 647 |
wid = flow({ |
| 648 |
'scheduling': { |
| 649 |
'graph': { |
| 650 |
'R1': 'live => skip => simulation => dummy => default' |
| 651 |
} |
| 652 |
}, |
| 653 |
'runtime': { |
| 654 |
'default': {}, |
| 655 |
'live': {'run mode': 'live'}, |
| 656 |
'skip': { |
| 657 |
'run mode': 'skip', |
| 658 |
'skip': {'outputs': 'started, submitted'} |
| 659 |
}, |
| 660 |
}, |
| 661 |
}) |
| 662 |
|
| 663 |
validate(wid) |
| 664 |
assert log_filter(contains=msg1) |
| 665 |
|
| 666 |
|
| 667 |
def test_skip_forbidden_as_output(flow, validate): |
| 668 |
"""Run mode names are forbidden as task output names.""" |
| 669 |
wid = flow({ |
| 670 |
'scheduling': {'graph': {'R1': 'task'}}, |
| 671 |
'runtime': {'task': {'outputs': {'skip': 'message for skip'}}} |
| 672 |
}) |
| 673 |
with pytest.raises( |
| 674 |
WorkflowConfigError, match='Invalid task output .* cannot be: `skip`' |
| 675 |
): |
| 676 |
validate(wid) |
| 677 |
|
| 678 |
|
| 679 |
def test_validate_workflow_run_mode( |
| 680 |
flow: Fixture, validate: Fixture, caplog: Fixture |
| 681 |
): |
| 682 |
"""Test that Cylc validate will only check simulation mode settings |
| 683 |
if validate --mode simulation or dummy. |
| 684 |
Discovered in: |
| 685 |
https://github.com/cylc/cylc-flow/pull/6213#issuecomment-2225365825 |
| 686 |
""" |
| 687 |
wid = flow( |
| 688 |
{ |
| 689 |
'scheduling': {'graph': {'R1': 'mytask'}}, |
| 690 |
'runtime': { |
| 691 |
'mytask': { |
| 692 |
'simulation': {'fail cycle points': 'invalid'}, |
| 693 |
} |
| 694 |
}, |
| 695 |
} |
| 696 |
) |
| 697 |
|
| 698 |
validate(wid) |
| 699 |
|
| 700 |
# It fails with run mode simulation: |
| 701 |
with pytest.raises(PointParsingError, match='Incompatible value'): |
| 702 |
validate(wid, run_mode='simulation') |
| 703 |
|
| 704 |
# It fails with run mode dummy: |
| 705 |
with pytest.raises(PointParsingError, match='Incompatible value'): |
| 706 |
validate(wid, run_mode='dummy') |
| 707 |
|
| 708 |
|
| 709 |
async def test_invalid_starttask(one_conf, flow, scheduler, start): |
| 710 |
"""It should reject invalid starttask arguments.""" |
| 711 |
id_ = flow(one_conf) |
| 712 |
schd = scheduler(id_, starttask=['a///b']) |
| 713 |
with pytest.raises(InputError, match='a///b'): |
| 714 |
async with start(schd): |
| 715 |
pass |
| 716 |
|
| 717 |
|
| 718 |
async def test_CYLC_WORKFLOW_SRC_DIR_correctly_set(tmp_path, install, run_dir): |
| 719 |
"""CYLC_WORKFLOW_SRC_DIR is set correctly: |
| 720 |
|
| 721 |
* In source dir |
| 722 |
* In installed dir (Not testing different permutations of installed |
| 723 |
dir as these are covered by testing of `get_workflow_source_dir`) |
| 724 |
* Created directly in the run dir. |
| 725 |
|
| 726 |
""" |
| 727 |
def process_file(target): |
| 728 |
"""Run config through read_and_proc (~= cylc view --process) |
| 729 |
""" |
| 730 |
return read_and_proc( |
| 731 |
target, |
| 732 |
viewcfg={ |
| 733 |
'mark': False, |
| 734 |
'single': False, |
| 735 |
'label': False, |
| 736 |
'jinja2': True, |
| 737 |
'contin': True, |
| 738 |
'inline': True, |
| 739 |
}, |
| 740 |
) |
| 741 |
|
| 742 |
# Setup a source directory: |
| 743 |
(tmp_path / 'flow.cylc').write_text( |
| 744 |
'#!jinja2\n{{ CYLC_WORKFLOW_SRC_DIR }}' |
| 745 |
) |
| 746 |
|
| 747 |
# Check that the CYLC_SRC_DIRECTORY |
| 748 |
# points to the source directory (tmp_path): |
| 749 |
processed = process_file(tmp_path / 'flow.cylc') |
| 750 |
assert processed[0] == str(tmp_path) |
| 751 |
|
| 752 |
# After installation the CYLC_WORKFLOW_SRC_DIR |
| 753 |
# *still* points back to tmp_path: |
| 754 |
wid = await install(tmp_path) |
| 755 |
processed = process_file(run_dir / wid / 'flow.cylc') |
| 756 |
assert processed[0] == str(tmp_path) |
| 757 |
|
| 758 |
|
| 759 |
async def test_task_event_bad_custom_template( |
| 760 |
flow, validate, scheduler, start, log_filter |
| 761 |
): |
| 762 |
"""Validation fails if task event handler has a bad custom template. |
| 763 |
""" |
| 764 |
exception = ( |
| 765 |
r"bad task event handler template t1: echo %\(rubbish\)s:" |
| 766 |
r" KeyError\('rubbish'\)" |
| 767 |
) |
| 768 |
events = { |
| 769 |
'handlers': 'echo %(rubbish)s', |
| 770 |
'handler events': 'succeeded' |
| 771 |
} |
| 772 |
wid = flow({ |
| 773 |
'scheduling': {'graph': {'R1': 't1'}}, |
| 774 |
'runtime': {'t1': {'events': events}}, |
| 775 |
}) |
| 776 |
with pytest.raises(WorkflowConfigError, match=exception): |
| 777 |
validate(wid) |
| 778 |
|
| 779 |
schd = scheduler(wid) |
| 780 |
with pytest.raises(WorkflowConfigError, match=exception): |
| 781 |
async with start(schd): |
| 782 |
pass |
| 783 |
|
| 784 |
|
| 785 |
async def test_icp_now_reload( |
| 786 |
flow, scheduler, start, monkeypatch: pytest.MonkeyPatch, log_filter |
| 787 |
): |
| 788 |
"""initial cycle point = 'now' should not change from original value on |
| 789 |
reload/restart, and sequences should remain intact. |
| 790 |
|
| 791 |
https://github.com/cylc/cylc-flow/issues/7047 |
| 792 |
""" |
| 793 |
def set_time(value): |
| 794 |
monkeypatch.setattr( |
| 795 |
'cylc.flow.config.get_current_time_string', |
| 796 |
lambda *a, **k: f"2005-01-01T{value}Z", |
| 797 |
) |
| 798 |
|
| 799 |
wid = flow({ |
| 800 |
'scheduling': { |
| 801 |
'initial cycle point': 'now', |
| 802 |
'graph': { |
| 803 |
'R1': 'cold => foo', |
| 804 |
'PT15M': 'foo[-PT15M] => foo', |
| 805 |
}, |
| 806 |
}, |
| 807 |
}) |
| 808 |
schd: Scheduler = scheduler(wid) |
| 809 |
|
| 810 |
def main_check(icp): |
| 811 |
assert str(schd.config.initial_point) == icp |
| 812 |
assert schd.pool.get_task_ids() == { |
| 813 |
f'{icp}/cold', |
| 814 |
} |
| 815 |
assert {str(seq) for seq in schd.config.sequences} == { |
| 816 |
f'R1/{icp}/P0Y', |
| 817 |
f'R/{icp}/PT15M', |
| 818 |
} |
| 819 |
|
| 820 |
set_time('06:00') |
| 821 |
async with start(schd): |
| 822 |
expected_icp = '20050101T0600Z' |
| 823 |
main_check(expected_icp) |
| 824 |
|
| 825 |
set_time('06:03') |
| 826 |
await commands.run_cmd(commands.reload_workflow(schd)) |
| 827 |
|
| 828 |
main_check(expected_icp) |
| 829 |
|
| 830 |
await commands.run_cmd( |
| 831 |
commands.set_prereqs_and_outputs( |
| 832 |
schd, [f'{expected_icp}/cold'], [] |
| 833 |
) |
| 834 |
) |
| 835 |
# Downstream task should have spawned on sequence: |
| 836 |
assert schd.pool.get_task_ids() == { |
| 837 |
f'{expected_icp}/foo', |
| 838 |
} |
| 839 |
assert not log_filter(level=logging.WARNING) |