| 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 |
from pathlib import Path |
| 18 |
import sqlite3 |
| 19 |
from typing import Any, Callable |
| 20 |
from shutil import copytree, rmtree |
| 21 |
|
| 22 |
import pytest |
| 23 |
|
| 24 |
from cylc.flow.dbstatecheck import output_fallback_msg |
| 25 |
from cylc.flow.exceptions import WorkflowConfigError, InputError |
| 26 |
from cylc.flow.rundb import CylcWorkflowDAO |
| 27 |
from cylc.flow.workflow_files import WorkflowFiles |
| 28 |
from cylc.flow.xtriggers.workflow_state import ( |
| 29 |
_workflow_state_backcompat, |
| 30 |
workflow_state, |
| 31 |
validate, |
| 32 |
) |
| 33 |
from cylc.flow.xtriggers.suite_state import suite_state |
| 34 |
|
| 35 |
|
| 36 |
def test_inferred_run(tmp_run_dir: 'Callable', capsys: pytest.CaptureFixture): |
| 37 |
"""Test that the workflow_state xtrigger infers the run number. |
| 38 |
|
| 39 |
Method: the faked run-dir has no DB to connect to, but the WorkflowPoller |
| 40 |
prints inferred ID to stderr if the run-dir exists. |
| 41 |
|
| 42 |
""" |
| 43 |
id_ = 'isildur' |
| 44 |
expected_workflow_id = f'{id_}/run1' |
| 45 |
cylc_run_dir = str(tmp_run_dir()) |
| 46 |
tmp_run_dir(expected_workflow_id, installed=True, named=True) |
| 47 |
workflow_state(id_ + '//3000/precious') |
| 48 |
assert expected_workflow_id in capsys.readouterr().err |
| 49 |
|
| 50 |
# Now test we can see workflows in alternate cylc-run directories |
| 51 |
# e.g. for `cylc workflow-state` or xtriggers targetting another user. |
| 52 |
alt_cylc_run_dir = cylc_run_dir + "_alt" |
| 53 |
|
| 54 |
# copy the cylc-run dir to alt location and delete the original. |
| 55 |
copytree(cylc_run_dir, alt_cylc_run_dir, symlinks=True) |
| 56 |
rmtree(cylc_run_dir) |
| 57 |
|
| 58 |
# It can no longer parse IDs in the original cylc-run location. |
| 59 |
workflow_state(id_) |
| 60 |
assert expected_workflow_id not in capsys.readouterr().err |
| 61 |
|
| 62 |
# But it can via an explicit alternate run directory. |
| 63 |
workflow_state(id_, alt_cylc_run_dir=alt_cylc_run_dir) |
| 64 |
assert expected_workflow_id in capsys.readouterr().err |
| 65 |
|
| 66 |
|
| 67 |
def test_c7_db_back_compat(tmp_run_dir: 'Callable'): |
| 68 |
"""Test workflow_state xtrigger backwards compatibility with Cylc 7 |
| 69 |
database.""" |
| 70 |
id_ = 'celebrimbor' |
| 71 |
c7_run_dir: Path = tmp_run_dir(id_) |
| 72 |
(c7_run_dir / WorkflowFiles.FLOW_FILE).rename( |
| 73 |
c7_run_dir / WorkflowFiles.SUITE_RC |
| 74 |
) |
| 75 |
db_file = c7_run_dir / 'log' / 'db' |
| 76 |
db_file.parent.mkdir(exist_ok=True) |
| 77 |
# Note: cannot use CylcWorkflowDAO here as creating outdated DB |
| 78 |
conn = sqlite3.connect(str(db_file)) |
| 79 |
try: |
| 80 |
conn.execute(r""" |
| 81 |
CREATE TABLE suite_params(key TEXT, value TEXT, PRIMARY KEY(key)); |
| 82 |
""") |
| 83 |
conn.execute(r""" |
| 84 |
CREATE TABLE task_states( |
| 85 |
name TEXT, cycle TEXT, time_created TEXT, time_updated TEXT, |
| 86 |
submit_num INTEGER, status TEXT, PRIMARY KEY(name, cycle) |
| 87 |
); |
| 88 |
""") |
| 89 |
conn.execute(r""" |
| 90 |
CREATE TABLE task_outputs( |
| 91 |
cycle TEXT, name TEXT, outputs TEXT, PRIMARY KEY(cycle, name) |
| 92 |
); |
| 93 |
""") |
| 94 |
conn.executemany( |
| 95 |
r'INSERT INTO "suite_params" VALUES(?,?);', |
| 96 |
[('cylc_version', '7.8.12'), |
| 97 |
('cycle_point_format', '%Y'), |
| 98 |
('cycle_point_tz', 'Z')] |
| 99 |
) |
| 100 |
conn.execute(r""" |
| 101 |
INSERT INTO "task_states" VALUES( |
| 102 |
'mithril','2012','2023-01-30T18:19:15Z','2023-01-30T18:19:15Z', |
| 103 |
0,'succeeded' |
| 104 |
); |
| 105 |
""") |
| 106 |
conn.execute(r""" |
| 107 |
INSERT INTO "task_outputs" VALUES( |
| 108 |
'2012','mithril','{"frodo": "bag end"}' |
| 109 |
); |
| 110 |
""") |
| 111 |
conn.commit() |
| 112 |
finally: |
| 113 |
conn.close() |
| 114 |
|
| 115 |
# Test workflow_state function |
| 116 |
satisfied, _ = workflow_state(f'{id_}//2012/mithril') |
| 117 |
assert satisfied |
| 118 |
satisfied, _ = workflow_state(f'{id_}//2012/mithril:succeeded') |
| 119 |
assert satisfied |
| 120 |
satisfied, _ = workflow_state( |
| 121 |
f'{id_}//2012/mithril:frodo', is_trigger=True |
| 122 |
) |
| 123 |
assert satisfied |
| 124 |
satisfied, _ = workflow_state( |
| 125 |
f'{id_}//2012/mithril:"bag end"', is_message=True |
| 126 |
) |
| 127 |
assert satisfied |
| 128 |
|
| 129 |
with pytest.raises(InputError, match='No such task state "pippin"'): |
| 130 |
workflow_state(f'{id_}//2012/mithril:pippin') |
| 131 |
|
| 132 |
satisfied, _ = workflow_state(id_ + '//2012/arkenstone') |
| 133 |
assert not satisfied |
| 134 |
|
| 135 |
# Test back-compat (old suite_state function) |
| 136 |
satisfied, _ = suite_state(suite=id_, task='mithril', point='2012') |
| 137 |
assert satisfied |
| 138 |
satisfied, _ = suite_state( |
| 139 |
suite=id_, task='mithril', point='2012', status='succeeded' |
| 140 |
) |
| 141 |
assert satisfied |
| 142 |
satisfied, _ = suite_state( |
| 143 |
suite=id_, task='mithril', point='2012', message='bag end' |
| 144 |
) |
| 145 |
assert satisfied |
| 146 |
satisfied, _ = suite_state(suite=id_, task='arkenstone', point='2012') |
| 147 |
assert not satisfied |
| 148 |
|
| 149 |
|
| 150 |
def test_c8_db_back_compat( |
| 151 |
tmp_run_dir: 'Callable', |
| 152 |
capsys: pytest.CaptureFixture, |
| 153 |
): |
| 154 |
"""Test workflow_state xtrigger backwards compatibility with Cylc < 8.3.0 |
| 155 |
database.""" |
| 156 |
id_ = 'nazgul' |
| 157 |
run_dir: Path = tmp_run_dir(id_) |
| 158 |
db_file = run_dir / 'log' / 'db' |
| 159 |
db_file.parent.mkdir(exist_ok=True) |
| 160 |
# Note: don't use CylcWorkflowDAO here as DB should be frozen |
| 161 |
conn = sqlite3.connect(str(db_file)) |
| 162 |
try: |
| 163 |
conn.execute(r""" |
| 164 |
CREATE TABLE workflow_params( |
| 165 |
key TEXT, value TEXT, PRIMARY KEY(key) |
| 166 |
); |
| 167 |
""") |
| 168 |
conn.execute(r""" |
| 169 |
CREATE TABLE task_states( |
| 170 |
name TEXT, cycle TEXT, flow_nums TEXT, time_created TEXT, |
| 171 |
time_updated TEXT, submit_num INTEGER, status TEXT, |
| 172 |
flow_wait INTEGER, is_manual_submit INTEGER, |
| 173 |
PRIMARY KEY(name, cycle, flow_nums) |
| 174 |
); |
| 175 |
""") |
| 176 |
conn.execute(r""" |
| 177 |
CREATE TABLE task_outputs( |
| 178 |
cycle TEXT, name TEXT, flow_nums TEXT, outputs TEXT, |
| 179 |
PRIMARY KEY(cycle, name, flow_nums) |
| 180 |
); |
| 181 |
""") |
| 182 |
conn.executemany( |
| 183 |
r'INSERT INTO "workflow_params" VALUES(?,?);', |
| 184 |
[('cylc_version', '8.2.7'), |
| 185 |
('cycle_point_format', '%Y'), |
| 186 |
('cycle_point_tz', 'Z')] |
| 187 |
) |
| 188 |
conn.execute(r""" |
| 189 |
INSERT INTO "task_states" VALUES( |
| 190 |
'gimli','2012','[1]','2023-01-30T18:19:15Z', |
| 191 |
'2023-01-30T18:19:15Z',1,'succeeded',0,0 |
| 192 |
); |
| 193 |
""") |
| 194 |
conn.execute(r""" |
| 195 |
INSERT INTO "task_outputs" VALUES( |
| 196 |
'2012','gimli','[1]', |
| 197 |
'["submitted", "started", "succeeded", "axe"]' |
| 198 |
); |
| 199 |
""") |
| 200 |
conn.commit() |
| 201 |
finally: |
| 202 |
conn.close() |
| 203 |
|
| 204 |
gimli = f'{id_}//2012/gimli' |
| 205 |
|
| 206 |
satisfied, _ = workflow_state(gimli) |
| 207 |
assert satisfied |
| 208 |
satisfied, _ = workflow_state(f'{gimli}:succeeded') |
| 209 |
assert satisfied |
| 210 |
satisfied, _ = workflow_state(f'{gimli}:axe', is_message=True) |
| 211 |
assert satisfied |
| 212 |
_, err = capsys.readouterr() |
| 213 |
assert not err |
| 214 |
# Output label selector falls back to message |
| 215 |
# (won't work if messsage != output label) |
| 216 |
satisfied, _ = workflow_state(f'{gimli}:axe', is_trigger=True) |
| 217 |
assert satisfied |
| 218 |
_, err = capsys.readouterr() |
| 219 |
assert output_fallback_msg in err |
| 220 |
|
| 221 |
|
| 222 |
def test__workflow_state_backcompat(tmp_run_dir: 'Callable'): |
| 223 |
"""Test the _workflow_state_backcompat & suite_state functions on a |
| 224 |
*current* Cylc database.""" |
| 225 |
id_ = 'dune' |
| 226 |
run_dir: Path = tmp_run_dir(id_) |
| 227 |
db_file = run_dir / 'log' / 'db' |
| 228 |
db_file.parent.mkdir(exist_ok=True) |
| 229 |
with CylcWorkflowDAO(db_file, create_tables=True) as dao: |
| 230 |
conn = dao.connect() |
| 231 |
conn.executemany( |
| 232 |
r'INSERT INTO "workflow_params" VALUES(?,?);', |
| 233 |
[('cylc_version', '8.3.0'), |
| 234 |
('cycle_point_format', '%Y'), |
| 235 |
('cycle_point_tz', 'Z')] |
| 236 |
) |
| 237 |
conn.execute(r""" |
| 238 |
INSERT INTO "task_states" VALUES( |
| 239 |
'arrakis','2012','[1]','2023-01-30T18:19:15Z', |
| 240 |
'2023-01-30T18:19:15Z',1,'succeeded',0,0 |
| 241 |
); |
| 242 |
""") |
| 243 |
conn.execute(r""" |
| 244 |
INSERT INTO "task_outputs" VALUES( |
| 245 |
'2012','arrakis','[1]', |
| 246 |
'{ |
| 247 |
"submitted": "submitted", |
| 248 |
"started": "started", |
| 249 |
"succeeded": "succeeded", |
| 250 |
"paul": "lisan al-gaib" |
| 251 |
}' |
| 252 |
); |
| 253 |
""") |
| 254 |
conn.commit() |
| 255 |
|
| 256 |
func: Any |
| 257 |
for func in (_workflow_state_backcompat, suite_state): |
| 258 |
satisfied, _ = func(id_, 'arrakis', '2012') |
| 259 |
assert satisfied |
| 260 |
satisfied, _ = func(id_, 'arrakis', '2012', status='succeeded') |
| 261 |
assert satisfied |
| 262 |
# Both output label and message work |
| 263 |
satisfied, _ = func(id_, 'arrakis', '2012', message='paul') |
| 264 |
assert satisfied |
| 265 |
satisfied, _ = func(id_, 'arrakis', '2012', message='lisan al-gaib') |
| 266 |
assert satisfied |
| 267 |
|
| 268 |
|
| 269 |
def test_validate_ok(): |
| 270 |
"""Validate returns ok with valid args.""" |
| 271 |
validate({ |
| 272 |
'workflow_task_id': 'foo//1/bar', |
| 273 |
'is_trigger': False, |
| 274 |
'is_message': False, |
| 275 |
'offset': 'PT1H', |
| 276 |
'flow_num': 44, |
| 277 |
}) |
| 278 |
|
| 279 |
|
| 280 |
@pytest.mark.parametrize( |
| 281 |
'id_', (('foo//1'),) |
| 282 |
) |
| 283 |
def test_validate_fail_bad_id(id_): |
| 284 |
"""Validation failure for bad id""" |
| 285 |
with pytest.raises(WorkflowConfigError, match='Full ID needed'): |
| 286 |
validate({ |
| 287 |
'workflow_task_id': id_, |
| 288 |
'offset': 'PT1H', |
| 289 |
'flow_num': 44, |
| 290 |
}) |
| 291 |
|
| 292 |
|
| 293 |
@pytest.mark.parametrize( |
| 294 |
'flow_num', ((4.25260), ('Belguim')) |
| 295 |
) |
| 296 |
def test_validate_fail_non_int_flow(flow_num): |
| 297 |
"""Validate failure for non integer flow numbers.""" |
| 298 |
with pytest.raises(WorkflowConfigError, match='must be an integer'): |
| 299 |
validate({ |
| 300 |
'workflow_task_id': 'foo//1/bar', |
| 301 |
'offset': 'PT1H', |
| 302 |
'flow_num': flow_num, |
| 303 |
}) |
| 304 |
|
| 305 |
|
| 306 |
def test_validate_polling_config(): |
| 307 |
"""It should reject invalid or unreliable polling configurations. |
| 308 |
|
| 309 |
See https://github.com/cylc/cylc-flow/issues/6157 |
| 310 |
""" |
| 311 |
with pytest.raises(WorkflowConfigError, match='No such task state'): |
| 312 |
validate({ |
| 313 |
'workflow_task_id': 'foo//1/bar:elephant', |
| 314 |
'is_trigger': False, |
| 315 |
'is_message': False, |
| 316 |
'flow_num': 44, |
| 317 |
}) |
| 318 |
|
| 319 |
with pytest.raises(WorkflowConfigError, match='Cannot poll for'): |
| 320 |
validate({ |
| 321 |
'workflow_task_id': 'foo//1/bar:waiting', |
| 322 |
'is_trigger': False, |
| 323 |
'is_message': False, |
| 324 |
'flow_num': 44, |
| 325 |
}) |
| 326 |
|
| 327 |
with pytest.raises(WorkflowConfigError, match='is not reliable'): |
| 328 |
validate({ |
| 329 |
'workflow_task_id': 'foo//1/bar:submitted', |
| 330 |
'is_trigger': False, |
| 331 |
'is_message': False, |
| 332 |
'flow_num': 44, |
| 333 |
}) |