| 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 validation of the [runtime][<namespace>][outputs] section.""" |
| 18 |
|
| 19 |
from random import random |
| 20 |
|
| 21 |
import pytest |
| 22 |
|
| 23 |
from cylc.flow.exceptions import WorkflowConfigError |
| 24 |
from cylc.flow.unicode_rules import TaskOutputValidator, TaskMessageValidator |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
'outputs, valid', |
| 29 |
[ |
| 30 |
pytest.param( |
| 31 |
[ |
| 32 |
'foo', |
| 33 |
'foo-bar', |
| 34 |
'foo_bar', |
| 35 |
'0foo0', |
| 36 |
'123', |
| 37 |
], |
| 38 |
True, |
| 39 |
id='valid', |
| 40 |
), |
| 41 |
pytest.param( |
| 42 |
[ |
| 43 |
# special prefix |
| 44 |
'_cylc', |
| 45 |
# nasty chars |
| 46 |
'foo bar', |
| 47 |
'foo,bar', |
| 48 |
'foo/bar', |
| 49 |
'foo+bar', |
| 50 |
# keywords |
| 51 |
'required', |
| 52 |
'optional', |
| 53 |
'all', |
| 54 |
# built-in qualifiers |
| 55 |
'succeeded', |
| 56 |
'succeed-all', |
| 57 |
# alternative qualifiers |
| 58 |
'succeed', |
| 59 |
], |
| 60 |
False, |
| 61 |
id='invalid', |
| 62 |
), |
| 63 |
], |
| 64 |
) |
| 65 |
def test_outputs(outputs, valid, flow, validate): |
| 66 |
"""It should validate task outputs. |
| 67 |
|
| 68 |
Outputs i.e. the keys of the [outputs] section. |
| 69 |
|
| 70 |
We don't want users adding outputs that override built-in |
| 71 |
outputs (e.g. succeeded, failed) or qualifiers (e.g. succeed-all). |
| 72 |
|
| 73 |
We don't want users adding outputs that conflict with keywords e.g. |
| 74 |
"required" or "all". |
| 75 |
""" |
| 76 |
# test that each output validates correctly |
| 77 |
for output in outputs: |
| 78 |
assert TaskOutputValidator.validate(output)[0] is valid |
| 79 |
|
| 80 |
# test that output validation is actually being performed |
| 81 |
id_ = flow({ |
| 82 |
'scheduling': { |
| 83 |
'graph': {'R1': 'foo'} |
| 84 |
}, |
| 85 |
'runtime': { |
| 86 |
'foo': { |
| 87 |
'outputs': { |
| 88 |
output: str(random()) |
| 89 |
for output in outputs |
| 90 |
} |
| 91 |
} |
| 92 |
}, |
| 93 |
}) |
| 94 |
val = lambda: validate(id_) |
| 95 |
if valid: |
| 96 |
val() |
| 97 |
else: |
| 98 |
with pytest.raises(WorkflowConfigError): |
| 99 |
val() |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.parametrize( |
| 103 |
'messages, valid', |
| 104 |
[ |
| 105 |
pytest.param( |
| 106 |
[ |
| 107 |
'foo bar baz', |
| 108 |
'WARN:foo bar baz' |
| 109 |
], |
| 110 |
True, |
| 111 |
id='valid', |
| 112 |
), |
| 113 |
pytest.param( |
| 114 |
[ |
| 115 |
# special prefix |
| 116 |
'_cylc', |
| 117 |
# invalid colon usage |
| 118 |
'foo bar: baz' |
| 119 |
# built-in qualifiers |
| 120 |
'succeeded', |
| 121 |
'succeed-all', |
| 122 |
# alternative qualifiers |
| 123 |
'succeed', |
| 124 |
], |
| 125 |
False, |
| 126 |
id='invalid', |
| 127 |
), |
| 128 |
], |
| 129 |
) |
| 130 |
def test_messages(messages, valid, flow, validate): |
| 131 |
"""It should validate task messages. |
| 132 |
|
| 133 |
Messages i.e. the values of the [outputs] section. |
| 134 |
|
| 135 |
We don't want users adding messages that override built-in outputs (e.g. |
| 136 |
succeeded, failed). To avoid confusion it's best to prohibit outputs which |
| 137 |
override built-in qualifiers (e.g. succeed-all) too. |
| 138 |
|
| 139 |
There's a special use of the colon character which users need to conform |
| 140 |
with too. |
| 141 |
""" |
| 142 |
# test that each message validates correctly |
| 143 |
for message in messages: |
| 144 |
assert TaskMessageValidator.validate(message)[0] is valid |
| 145 |
|
| 146 |
# test that output validation is actually being performed |
| 147 |
id_ = flow({ |
| 148 |
'scheduling': { |
| 149 |
'graph': {'R1': 'foo'} |
| 150 |
}, |
| 151 |
'runtime': { |
| 152 |
'foo': { |
| 153 |
'outputs': { |
| 154 |
str(random())[2:]: message |
| 155 |
for message in messages |
| 156 |
} |
| 157 |
} |
| 158 |
}, |
| 159 |
}) |
| 160 |
val = lambda: validate(id_) |
| 161 |
if valid: |
| 162 |
val() |
| 163 |
else: |
| 164 |
with pytest.raises(WorkflowConfigError): |
| 165 |
val() |
| 166 |
|
| 167 |
|
| 168 |
@pytest.mark.parametrize( |
| 169 |
'graph, expression, message', [ |
| 170 |
pytest.param( |
| 171 |
'foo:x', |
| 172 |
'succeeded and (x or y)', |
| 173 |
r'foo:x is required in the graph.*' |
| 174 |
r' but optional in the completion expression', |
| 175 |
id='required-in-graph-optional-in-completion', |
| 176 |
), |
| 177 |
pytest.param( |
| 178 |
'foo:x?', |
| 179 |
'succeeded and x', |
| 180 |
r'foo:x is optional in the graph.*' |
| 181 |
r' but required in the completion expression', |
| 182 |
id='optional-in-graph-required-in-completion', |
| 183 |
), |
| 184 |
pytest.param( |
| 185 |
'foo:x', |
| 186 |
'succeeded', |
| 187 |
'foo:x is required in the graph.*' |
| 188 |
'but not referenced in the completion expression', |
| 189 |
id='required-in-graph-not-referenced-in-completion', |
| 190 |
), |
| 191 |
pytest.param( |
| 192 |
# tests proposal point 4: |
| 193 |
# https://cylc.github.io/cylc-admin/proposal-optional-output-extension.html#proposal |
| 194 |
'foo:expired', |
| 195 |
'succeeded', |
| 196 |
'foo:expired must be optional', |
| 197 |
id='expire-required-in-graph', |
| 198 |
), |
| 199 |
pytest.param( |
| 200 |
'foo:expired?', |
| 201 |
'succeeded', |
| 202 |
'foo:expired is permitted in the graph.*' |
| 203 |
'\nTry: completion = "succeeded or expired"', |
| 204 |
id='expire-optional-in-graph-but-not-used-in-completion' |
| 205 |
), |
| 206 |
pytest.param( |
| 207 |
# tests part of proposal point 5: |
| 208 |
# https://cylc.github.io/cylc-admin/proposal-optional-output-extension.html#proposal |
| 209 |
'foo', |
| 210 |
'finished and x', |
| 211 |
'"finished" output cannot be used in completion expressions', |
| 212 |
id='finished-output-used-in-completion-expression', |
| 213 |
), |
| 214 |
pytest.param( |
| 215 |
# https://github.com/cylc/cylc-flow/pull/6046#issuecomment-2059266086 |
| 216 |
'foo?', |
| 217 |
'x and failed', |
| 218 |
'foo:failed is optional in the graph.*' |
| 219 |
'but required in the completion expression', |
| 220 |
id='failed-implicitly-optional-in-graph-required-in-completion', |
| 221 |
), |
| 222 |
pytest.param( |
| 223 |
'foo', |
| 224 |
'(succeed and x) or failed', |
| 225 |
'Use "succeeded" not "succeed" in completion expressions', |
| 226 |
id='alt-compvar1', |
| 227 |
), |
| 228 |
pytest.param( |
| 229 |
'foo? & foo:submitted?', |
| 230 |
'submit_fail or succeeded', |
| 231 |
'Use "submit_failed" not "submit_fail" in completion expressions', |
| 232 |
id='alt-compvar2', |
| 233 |
), |
| 234 |
pytest.param( |
| 235 |
'foo? & foo:submitted?', |
| 236 |
'submit-failed or succeeded', |
| 237 |
'Use "submit_failed" rather than "submit-failed"' |
| 238 |
' in completion expressions.', |
| 239 |
id='submit-failed used in completion expression', |
| 240 |
), |
| 241 |
pytest.param( |
| 242 |
'foo:file-1', |
| 243 |
'succeeded or file-1', |
| 244 |
'Replace hyphens with underscores in task outputs when' |
| 245 |
' used in completion expressions.', |
| 246 |
id='Hyphen used in completion expression', |
| 247 |
), |
| 248 |
pytest.param( |
| 249 |
'foo:x', |
| 250 |
'not succeeded or x', |
| 251 |
'Error in .*' |
| 252 |
'\nInvalid expression', |
| 253 |
id='Non-whitelisted syntax used in completion expression', |
| 254 |
), |
| 255 |
] |
| 256 |
) |
| 257 |
def test_completion_expression_invalid( |
| 258 |
flow, |
| 259 |
validate, |
| 260 |
graph, |
| 261 |
expression, |
| 262 |
message, |
| 263 |
): |
| 264 |
"""It should ensure the completion is logically consistent with the graph. |
| 265 |
|
| 266 |
Tests proposal point 5: |
| 267 |
https://cylc.github.io/cylc-admin/proposal-optional-output-extension.html#proposal |
| 268 |
""" |
| 269 |
id_ = flow({ |
| 270 |
'scheduling': { |
| 271 |
'graph': {'R1': graph}, |
| 272 |
}, |
| 273 |
'runtime': { |
| 274 |
'foo': { |
| 275 |
'completion': expression, |
| 276 |
'outputs': { |
| 277 |
'x': 'xxx', |
| 278 |
'y': 'yyy', |
| 279 |
'file-1': 'asdf' |
| 280 |
}, |
| 281 |
}, |
| 282 |
}, |
| 283 |
}) |
| 284 |
with pytest.raises(WorkflowConfigError, match=message): |
| 285 |
validate(id_) |
| 286 |
|
| 287 |
|
| 288 |
@pytest.mark.parametrize( |
| 289 |
'graph, expression', [ |
| 290 |
('foo', 'succeeded and (x or y or z)'), |
| 291 |
('foo?', 'succeeded and (x or y or z) or failed or expired'), |
| 292 |
('foo', '(succeeded and x) or (expired and y)'), |
| 293 |
] |
| 294 |
) |
| 295 |
def test_completion_expression_valid( |
| 296 |
flow, |
| 297 |
validate, |
| 298 |
graph, |
| 299 |
expression, |
| 300 |
): |
| 301 |
id_ = flow({ |
| 302 |
'scheduling': { |
| 303 |
'graph': {'R1': graph}, |
| 304 |
}, |
| 305 |
'runtime': { |
| 306 |
'foo': { |
| 307 |
'completion': expression, |
| 308 |
'outputs': { |
| 309 |
'x': 'xxx', |
| 310 |
'y': 'yyy', |
| 311 |
'z': 'zzz', |
| 312 |
}, |
| 313 |
}, |
| 314 |
}, |
| 315 |
}) |
| 316 |
validate(id_) |
| 317 |
|
| 318 |
|
| 319 |
def test_completion_expression_cylc7_compat( |
| 320 |
flow, |
| 321 |
validate, |
| 322 |
monkeypatch |
| 323 |
): |
| 324 |
id_ = flow({ |
| 325 |
'scheduling': { |
| 326 |
'graph': {'R1': 'foo'}, |
| 327 |
}, |
| 328 |
'runtime': { |
| 329 |
'foo': { |
| 330 |
'completion': 'succeeded and x', |
| 331 |
'outputs': { |
| 332 |
'x': 'xxx', |
| 333 |
}, |
| 334 |
}, |
| 335 |
}, |
| 336 |
}) |
| 337 |
monkeypatch.setattr('cylc.flow.flags.cylc7_back_compat', True) |
| 338 |
with pytest.raises( |
| 339 |
WorkflowConfigError, |
| 340 |
match="completion cannot be used in Cylc 7 compatibility mode." |
| 341 |
): |
| 342 |
validate(id_) |
| 343 |
|
| 344 |
|
| 345 |
def test_unique_messages( |
| 346 |
flow, |
| 347 |
validate |
| 348 |
): |
| 349 |
"""Task messages must be unique in the [outputs] section. |
| 350 |
|
| 351 |
See: https://github.com/cylc/cylc-flow/issues/6056 |
| 352 |
""" |
| 353 |
id_ = flow({ |
| 354 |
'scheduling': { |
| 355 |
'graph': {'R1': 'foo'} |
| 356 |
}, |
| 357 |
'runtime': { |
| 358 |
'foo': { |
| 359 |
'outputs': { |
| 360 |
'a': 'foo', |
| 361 |
'b': 'bar', |
| 362 |
'c': 'baz', |
| 363 |
'd': 'foo', |
| 364 |
} |
| 365 |
}, |
| 366 |
} |
| 367 |
}) |
| 368 |
|
| 369 |
with pytest.raises( |
| 370 |
WorkflowConfigError, |
| 371 |
match=( |
| 372 |
r'"\[runtime\]\[foo\]\[outputs\]d = foo"' |
| 373 |
' - messages must be unique' |
| 374 |
), |
| 375 |
): |
| 376 |
validate(id_) |