| 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 textwrap import dedent |
| 19 |
from types import SimpleNamespace |
| 20 |
from typing import TYPE_CHECKING, Set, Tuple, cast |
| 21 |
|
| 22 |
import pytest |
| 23 |
|
| 24 |
from cylc.flow import CYLC_LOG |
| 25 |
from cylc.flow.id import Tokens |
| 26 |
from cylc.flow.id_match import id_match |
| 27 |
from cylc.flow.config import WorkflowConfig |
| 28 |
|
| 29 |
if TYPE_CHECKING: |
| 30 |
from cylc.flow.id_match import TaskTokens |
| 31 |
|
| 32 |
|
| 33 |
def to_tokens(*ids): |
| 34 |
return cast('Set[TaskTokens]', {Tokens(id_, relative=True) for id_ in ids}) |
| 35 |
|
| 36 |
|
| 37 |
def to_string_ids(*ids): |
| 38 |
return {id_.relative_id for id_ in ids} |
| 39 |
|
| 40 |
|
| 41 |
def to_string_ids_with_selectors(*ids): |
| 42 |
return {id_.relative_id_with_selectors for id_ in ids} |
| 43 |
|
| 44 |
|
| 45 |
@pytest.fixture(scope='module') |
| 46 |
def mod_test_config(mod_tmp_path): |
| 47 |
path = mod_tmp_path / 'flow.cylc' |
| 48 |
with open(path, 'w+') as flow_cylc: |
| 49 |
flow_cylc.write(dedent(''' |
| 50 |
[scheduler] |
| 51 |
allow implicit tasks = True |
| 52 |
|
| 53 |
[scheduling] |
| 54 |
cycling mode = integer |
| 55 |
initial cycle point = 1 |
| 56 |
[[graph]] |
| 57 |
P1 = a => b => c => d |
| 58 |
P3 = z |
| 59 |
''')) |
| 60 |
|
| 61 |
return WorkflowConfig('test', str(path), SimpleNamespace()) |
| 62 |
|
| 63 |
|
| 64 |
@pytest.fixture(scope='module') |
| 65 |
def mod_test_family_config(mod_tmp_path): |
| 66 |
"""It should match family IDs.""" |
| 67 |
path = mod_tmp_path / 'flow.cylc' |
| 68 |
with open(path, 'w+') as flow_cylc: |
| 69 |
flow_cylc.write(dedent(''' |
| 70 |
[scheduling] |
| 71 |
cycling mode = integer |
| 72 |
initial cycle point = 1 |
| 73 |
[[graph]] |
| 74 |
P1 = a & b1 & b2 |
| 75 |
P2 = z1 & z2 |
| 76 |
3/P3 = z3 |
| 77 |
[runtime] |
| 78 |
[[a]] |
| 79 |
inherit = A |
| 80 |
[[b1, b2]] |
| 81 |
inherit = B |
| 82 |
[[A, B]] |
| 83 |
[[Z]] |
| 84 |
[[z1, z2, z3]] |
| 85 |
inherit = Z |
| 86 |
''')) |
| 87 |
return WorkflowConfig('test', str(path), SimpleNamespace()) |
| 88 |
|
| 89 |
|
| 90 |
def _id_match( |
| 91 |
config: 'WorkflowConfig', |
| 92 |
pool: 'Set[TaskTokens]', |
| 93 |
ids: 'Set[TaskTokens]', |
| 94 |
only_match_pool: bool = False, |
| 95 |
) -> 'Tuple[Set[str], Set[str]]': |
| 96 |
"""Convenience function for testing, converts strings to tokens.""" |
| 97 |
matched, unmatched = id_match( |
| 98 |
config, |
| 99 |
pool, |
| 100 |
to_tokens(*ids), |
| 101 |
only_match_pool=only_match_pool, |
| 102 |
) |
| 103 |
return ( |
| 104 |
to_string_ids(*matched), |
| 105 |
to_string_ids_with_selectors(*unmatched), |
| 106 |
) |
| 107 |
|
| 108 |
|
| 109 |
@pytest.mark.parametrize( |
| 110 |
'ids,matched,unmatched', |
| 111 |
[ |
| 112 |
( |
| 113 |
{'1'}, |
| 114 |
{'1/a', '1/b', '1/c'}, |
| 115 |
set(), |
| 116 |
), |
| 117 |
( |
| 118 |
{'2'}, |
| 119 |
set(), |
| 120 |
{'2'} |
| 121 |
), |
| 122 |
( |
| 123 |
{'*'}, |
| 124 |
{'1/a', '1/b', '1/c'}, |
| 125 |
set(), |
| 126 |
), |
| 127 |
( |
| 128 |
{'1/*'}, |
| 129 |
{'1/a', '1/b', '1/c'}, |
| 130 |
set(), |
| 131 |
), |
| 132 |
( |
| 133 |
{'2/*'}, |
| 134 |
set(), |
| 135 |
{'2/*'} |
| 136 |
), |
| 137 |
( |
| 138 |
{'*/*'}, |
| 139 |
{'1/a', '1/b', '1/c'}, |
| 140 |
set(), |
| 141 |
), |
| 142 |
( |
| 143 |
{'*/a'}, |
| 144 |
{'1/a'}, |
| 145 |
set(), |
| 146 |
), |
| 147 |
( |
| 148 |
{'*/z'}, |
| 149 |
set(), |
| 150 |
{'*/z'} |
| 151 |
), |
| 152 |
( |
| 153 |
{'*/*:x'}, |
| 154 |
{'1/a', '1/b', '1/c'}, |
| 155 |
set(), |
| 156 |
), |
| 157 |
( |
| 158 |
{'*/*:y'}, |
| 159 |
set(), |
| 160 |
{'*/*:y'}, |
| 161 |
), |
| 162 |
] |
| 163 |
) |
| 164 |
def test_match_task(mod_test_config, ids, matched, unmatched): |
| 165 |
"""It should match task IDs.""" |
| 166 |
pool = to_tokens('1/a:x', '1/b:x', '1/c:x') |
| 167 |
_matched, _unmatched = _id_match( |
| 168 |
mod_test_config, |
| 169 |
pool, |
| 170 |
ids, |
| 171 |
only_match_pool=True, |
| 172 |
) |
| 173 |
assert _matched == matched |
| 174 |
assert _unmatched == unmatched |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.parametrize( |
| 178 |
'ids,matched,unmatched', |
| 179 |
[ |
| 180 |
( |
| 181 |
{'1/a'}, |
| 182 |
{'1/a'}, |
| 183 |
set(), |
| 184 |
), |
| 185 |
( |
| 186 |
{'1/*'}, |
| 187 |
{'1/a', '1/b'}, |
| 188 |
set(), |
| 189 |
), |
| 190 |
( |
| 191 |
{'1/*:x'}, |
| 192 |
{'1/a', '1/b'}, |
| 193 |
set(), |
| 194 |
), |
| 195 |
( |
| 196 |
{'1/*:y'}, |
| 197 |
set(), |
| 198 |
{'1/*:y'}, |
| 199 |
), |
| 200 |
( |
| 201 |
{'*/*:x'}, |
| 202 |
{'1/a', '1/b', '2/a'}, |
| 203 |
set(), |
| 204 |
), |
| 205 |
( |
| 206 |
{'1/z'}, |
| 207 |
set(), |
| 208 |
{'1/z'}, |
| 209 |
), |
| 210 |
( |
| 211 |
{'1'}, |
| 212 |
{'1/a', '1/b'}, |
| 213 |
set(), |
| 214 |
), |
| 215 |
( |
| 216 |
{'3'}, |
| 217 |
set(), |
| 218 |
{'3'}, |
| 219 |
), |
| 220 |
] |
| 221 |
) |
| 222 |
def test_match_cycle(mod_test_config, ids, matched, unmatched): |
| 223 |
"""It should match cycle IDs.""" |
| 224 |
pool = to_tokens('1/a:x', '1/b:x', '2/a:x') |
| 225 |
assert _id_match( |
| 226 |
mod_test_config, |
| 227 |
pool, |
| 228 |
ids, |
| 229 |
only_match_pool=True, |
| 230 |
) == (matched, unmatched) |
| 231 |
|
| 232 |
|
| 233 |
@pytest.mark.parametrize( |
| 234 |
'ids,matched,unmatched', |
| 235 |
[ |
| 236 |
( |
| 237 |
{'1/root'}, |
| 238 |
{'1/a', '1/b', '1/c', '1/d', '1/z'}, |
| 239 |
set(), |
| 240 |
), |
| 241 |
( |
| 242 |
{'2/root'}, |
| 243 |
{'2/a', '2/b', '2/c', '2/d'}, # 1/z isn't in cycle 2 |
| 244 |
set(), |
| 245 |
), |
| 246 |
( |
| 247 |
{'*/root'}, |
| 248 |
# should match all active cycles (i.e. cycles 1 and 2) |
| 249 |
{'1/a', '1/b', '1/c', '1/d', '1/z', '2/a', '2/b', '2/c', '2/d'}, |
| 250 |
set(), |
| 251 |
), |
| 252 |
( |
| 253 |
{'1/[ad]'}, |
| 254 |
{'1/a', '1/d'}, |
| 255 |
set(), |
| 256 |
), |
| 257 |
( |
| 258 |
{'1/[!ad]'}, |
| 259 |
{'1/b', '1/c', '1/z'}, |
| 260 |
set(), |
| 261 |
), |
| 262 |
] |
| 263 |
) |
| 264 |
def test_match_inactive(mod_test_config, ids, matched, unmatched): |
| 265 |
"""It should match non-pool tasks""" |
| 266 |
assert _id_match( |
| 267 |
mod_test_config, |
| 268 |
to_tokens('1/a:running', '2/a:waiting'), # active cycles are 1 and 2 |
| 269 |
ids, |
| 270 |
) == (matched, unmatched) |
| 271 |
|
| 272 |
|
| 273 |
@pytest.mark.parametrize( |
| 274 |
'ids,matched,unmatched', |
| 275 |
[ |
| 276 |
({'1/A'}, {'1/a'}, set()), |
| 277 |
({'1/B'}, {'1/b1', '1/b2'}, set()), |
| 278 |
({'1/C'}, set(), {'1/C'}), |
| 279 |
({'1/root'}, {'1/a', '1/b1', '1/b2', '1/z1', '1/z2'}, set()), |
| 280 |
] |
| 281 |
) |
| 282 |
def test_match_family(mod_test_family_config, ids, matched, unmatched): |
| 283 |
"""It should match family IDs.""" |
| 284 |
pool = to_tokens('1/a:x', '1/b1:x', '1/b2:x') |
| 285 |
assert _id_match(mod_test_family_config, pool, ids) == (matched, unmatched) |
| 286 |
|
| 287 |
|
| 288 |
@pytest.mark.parametrize( |
| 289 |
'ids,matched,unmatched,warnings', |
| 290 |
[ |
| 291 |
# These IDs all reference 2/z<x>, but, z<x> are not valid for cycle 2: |
| 292 |
|
| 293 |
# explicit reference -> warning |
| 294 |
({'2/z1'}, set(), {'2/z1'}, ('2/z1',)), |
| 295 |
({'2/Z'}, set(), {'2/Z'}, ('2/z1', '2/z2', '2/z3')), |
| 296 |
|
| 297 |
# implicit reference -> no warning |
| 298 |
({'*/Z'}, {'1/z1', '1/z2'}, set(), set()), |
| 299 |
({'2/*'}, {'2/a', '2/b1', '2/b2'}, set(), set()), |
| 300 |
({'2/root'}, {'2/a', '2/b1', '2/b2'}, set(), set()), |
| 301 |
({'*/[XYZ]'}, {'1/z1', '1/z2'}, set(), set()), |
| 302 |
] |
| 303 |
) |
| 304 |
def test_match_warnings( |
| 305 |
mod_test_family_config, |
| 306 |
ids, |
| 307 |
matched, |
| 308 |
unmatched, |
| 309 |
warnings, |
| 310 |
caplog, |
| 311 |
): |
| 312 |
"""It should warn about invalid cycle points, only when relevant. |
| 313 |
|
| 314 |
See https://github.com/cylc/cylc-flow/issues/7184 |
| 315 |
""" |
| 316 |
pool = to_tokens('1/a:x', '1/b1:x', '1/b2:x', '2/a:x') |
| 317 |
caplog.clear() |
| 318 |
caplog.set_level(logging.WARNING, CYLC_LOG) |
| 319 |
assert _id_match(mod_test_family_config, pool, ids) == (matched, unmatched) |
| 320 |
assert caplog.messages == [ |
| 321 |
f'Invalid cycle point for task:' |
| 322 |
f' {Tokens(id_, relative=True)["task"]}' |
| 323 |
f', {Tokens(id_, relative=True)["cycle"]}' |
| 324 |
for id_ in warnings |
| 325 |
] |