| 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 typing import Set, Tuple, cast, TYPE_CHECKING |
| 18 |
|
| 19 |
from cylc.flow import commands |
| 20 |
from cylc.flow.id import Tokens |
| 21 |
from cylc.flow.scheduler import Scheduler |
| 22 |
|
| 23 |
if TYPE_CHECKING: |
| 24 |
from cylc.flow.id import TaskTokens |
| 25 |
|
| 26 |
|
| 27 |
async def test_id_match(flow, scheduler, start, caplog): |
| 28 |
id_ = flow({ |
| 29 |
'scheduling': { |
| 30 |
'cycling mode': 'integer', |
| 31 |
'initial cycle point': '1', |
| 32 |
'final cycle point': '3', |
| 33 |
'graph': { |
| 34 |
'P2': ''' |
| 35 |
a1 => b1 => c1 |
| 36 |
a2 => b2 => c2 |
| 37 |
|
| 38 |
b1[-P1] => b1 |
| 39 |
b2[-P1] => b2 |
| 40 |
''', |
| 41 |
}, |
| 42 |
}, |
| 43 |
'runtime': { |
| 44 |
'a1, a2': {'inherit': 'A'}, |
| 45 |
'A': {}, |
| 46 |
'b1, b2': {'inherit': 'B'}, |
| 47 |
'B': {}, |
| 48 |
'c1, c2': {}, |
| 49 |
}, |
| 50 |
}) |
| 51 |
schd: Scheduler = scheduler(id_) |
| 52 |
|
| 53 |
def match(*ids: str) -> Tuple[Set[str], Set[str]]: |
| 54 |
matched, unmatched = schd.pool.id_match( |
| 55 |
{cast('TaskTokens', Tokens(id_, relative=True)) for id_ in ids}, |
| 56 |
) |
| 57 |
return {id_.relative_id for id_ in matched}, { |
| 58 |
id_.relative_id_with_selectors for id_ in unmatched |
| 59 |
} |
| 60 |
|
| 61 |
async with start(schd): |
| 62 |
await commands.run_cmd( |
| 63 |
commands.set_prereqs_and_outputs( |
| 64 |
schd, ['1/a2'], ['1'], ['succeeded'], None |
| 65 |
) |
| 66 |
) |
| 67 |
await commands.run_cmd( |
| 68 |
commands.set_prereqs_and_outputs( |
| 69 |
schd, ['1/b2'], ['1'], ['failed'], None |
| 70 |
) |
| 71 |
) |
| 72 |
|
| 73 |
# task pool state: |
| 74 |
# * cycle 1 |
| 75 |
# * n=0 a1 waiting |
| 76 |
# * n=1 b1 waiting |
| 77 |
# * n=2 c1 waiting |
| 78 |
# * n=1 a2 succeeded |
| 79 |
# * n=0 b2 failed |
| 80 |
# * n=1 c2 waiting |
| 81 |
# * cycle 2 |
| 82 |
# * n=0 a1 waiting |
| 83 |
# * n=1 b1 waiting |
| 84 |
# * n=2 c1 waiting |
| 85 |
# * n=0 a2 waiting |
| 86 |
# * n=1 b2 waiting |
| 87 |
# * n=2 c2 waiting |
| 88 |
|
| 89 |
# check the n=0 window matches expecations before proceeding |
| 90 |
assert { |
| 91 |
itask.tokens.relative_id for itask in schd.pool.get_tasks() |
| 92 |
} == {'1/a1', '1/b2', '3/a1', '3/a2'} |
| 93 |
|
| 94 |
# test active task selection: waiting selector |
| 95 |
assert ( |
| 96 |
match('*:waiting') |
| 97 |
== match('*/root:waiting') |
| 98 |
== match('*/*:waiting') |
| 99 |
== match('*/A:waiting') |
| 100 |
== match('*/a*:waiting') |
| 101 |
== match('1/a1:waiting', '3/a1:waiting', '3/a2:waiting') |
| 102 |
== match('^/a1:waiting', '3/a1:waiting', '$/a2:waiting') |
| 103 |
== ({'1/a1', '3/a1', '3/a2'}, set()) |
| 104 |
) |
| 105 |
|
| 106 |
# test active task selection: failed selector |
| 107 |
assert ( |
| 108 |
match('*:failed') |
| 109 |
== match('*/root:failed') |
| 110 |
== match('*/*:failed') |
| 111 |
== match('*/B:failed') |
| 112 |
== match('*/b*:failed') |
| 113 |
== match('1/b2:failed') |
| 114 |
== match('^/b2:failed') |
| 115 |
== ({'1/b2'}, set()) |
| 116 |
) |
| 117 |
|
| 118 |
# test active task selection: failed selector |
| 119 |
assert ( |
| 120 |
match('1/b1:failed', '1/b2:failed') |
| 121 |
== match('1/B:failed', '1/b1:failed') |
| 122 |
== match('*:failed', '1/B:failed', '1/b1:failed') |
| 123 |
== ({'1/b2'}, {'1/b1:failed'}) |
| 124 |
) |
| 125 |
|
| 126 |
# test active task selection: submit-failed selector |
| 127 |
assert match('*:submit-failed') == (set(), {'*:submit-failed'}) |
| 128 |
|
| 129 |
# test globs, cycle and family matching |
| 130 |
assert ( |
| 131 |
match('*') |
| 132 |
== match('*/*') |
| 133 |
== match('*/root') |
| 134 |
== match('*/A', '*/B', '*/c1', '*/c2') |
| 135 |
== match('*/a*', '*/b*', '*/c*') |
| 136 |
== ( |
| 137 |
{ |
| 138 |
'1/a1', |
| 139 |
'1/a2', |
| 140 |
'1/b1', |
| 141 |
'1/b2', |
| 142 |
'1/c1', |
| 143 |
'1/c2', |
| 144 |
'3/a1', |
| 145 |
'3/a2', |
| 146 |
'3/b1', |
| 147 |
'3/b2', |
| 148 |
'3/c1', |
| 149 |
'3/c2', |
| 150 |
}, |
| 151 |
set(), |
| 152 |
) |
| 153 |
) |
| 154 |
|
| 155 |
# test globs, cycle and family matching |
| 156 |
assert ( |
| 157 |
match('1/A') |
| 158 |
== match('1/a*') |
| 159 |
== match('^/a*') |
| 160 |
== match('1/a1', '1/a2') |
| 161 |
== ({'1/a1', '1/a2'}, set()) |
| 162 |
) |
| 163 |
assert match('1/X') == (set(), {'1/X'}) |
| 164 |
assert match('5:waiting') == (set(), {'5:waiting'}) |
| 165 |
|
| 166 |
# test invalid IDs |
| 167 |
assert match('not-a-cycle/*', '*/not_a_task', '*:not-a-state') == ( |
| 168 |
set(), |
| 169 |
{'not-a-cycle/*', '*/not_a_task', '*:not-a-state'}, |
| 170 |
) |
| 171 |
# test invalid cycle point in combination with a selector |
| 172 |
assert match('x/y:succeeded') == (set(), {'x/y:succeeded'}) |
| 173 |
|
| 174 |
# ensure that off-sequence IDs are filtered out |
| 175 |
# NOTE: cycle 2 is not on sequence for task a1 |
| 176 |
assert match('1/a1', '2/a1', '3/a1') == ({'1/a1', '3/a1'}, {'2/a1'}) |
| 177 |
|
| 178 |
# ensure warnings are raised for off-sequence tasks if the user |
| 179 |
# explcitly specified them |
| 180 |
# NOTE: 2/a1 should result in a warning (because the user asked for |
| 181 |
# this exact combination), however, "2/a*" should not (because there |
| 182 |
# may be a combination of tasks which are or are not valid at the given |
| 183 |
# cycle(s) which match the task name pattern) |
| 184 |
caplog.clear() |
| 185 |
assert match('2/a1', '2/a*') == (set(), {'2/a1', '2/a*'}) |
| 186 |
assert caplog.messages == ['Invalid cycle point for task: a1, 2'] |
| 187 |
|
| 188 |
|
| 189 |
async def test_match_removed_task(flow, scheduler, run, complete): |
| 190 |
"""It should match and operate on tasks no longer in the config.""" |
| 191 |
config = { |
| 192 |
'scheduling': { |
| 193 |
'graph': {'R1': 'foo & bar'}, |
| 194 |
}, |
| 195 |
} |
| 196 |
id_ = flow(config) |
| 197 |
schd = scheduler(id_, paused_start=False) |
| 198 |
|
| 199 |
def list_tasks(): |
| 200 |
return { |
| 201 |
itask.tokens.relative_id for itask in schd.pool.get_tasks() |
| 202 |
} |
| 203 |
|
| 204 |
async with run(schd): |
| 205 |
# workflow starts with "foo" and "bar" in the pool |
| 206 |
assert list_tasks() == {'1/foo', '1/bar'} |
| 207 |
|
| 208 |
# remove "bar" from the config and reload |
| 209 |
config['scheduling']['graph']['R1'] = 'x => foo' |
| 210 |
flow(config, name=id_) |
| 211 |
await commands.run_cmd(commands.reload_workflow(schd)) |
| 212 |
|
| 213 |
# both "foo" and "bar" are still in the pool ("bar" is orphaned) |
| 214 |
assert list_tasks() == {'1/foo', '1/bar'} |
| 215 |
|
| 216 |
# remove all tasks from the workflow |
| 217 |
await commands.run_cmd(commands.remove_tasks(schd, {'*'}, ['1'])) |
| 218 |
|
| 219 |
# this should match the orphaned task "bar" |
| 220 |
assert list_tasks() == set() |