| 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 json |
| 18 |
import pytest |
| 19 |
import re |
| 20 |
from types import SimpleNamespace |
| 21 |
|
| 22 |
from colorama import init as colour_init |
| 23 |
|
| 24 |
from cylc.flow.id import Tokens |
| 25 |
from cylc.flow.scripts.show import ( |
| 26 |
ShowOptions, |
| 27 |
show, |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
RE_STATE = re.compile('state:.*') |
| 32 |
|
| 33 |
|
| 34 |
@pytest.fixture(scope='module') |
| 35 |
def mod_my_conf(): |
| 36 |
"""A workflow configuration with some workflow metadata.""" |
| 37 |
return { |
| 38 |
'meta': { |
| 39 |
'title': 'Workflow Title', |
| 40 |
'description': """ |
| 41 |
My |
| 42 |
multiline |
| 43 |
description. |
| 44 |
""", |
| 45 |
'URL': 'http://ismycomputerturnedon.com/', |
| 46 |
'answer': '42', |
| 47 |
}, |
| 48 |
'scheduling': { |
| 49 |
'graph': { |
| 50 |
'R1': 'foo' |
| 51 |
} |
| 52 |
}, |
| 53 |
'runtime': { |
| 54 |
'foo': { |
| 55 |
'meta': { |
| 56 |
'title': 'Task Title', |
| 57 |
'description': ''' |
| 58 |
Task |
| 59 |
multiline |
| 60 |
description |
| 61 |
''', |
| 62 |
'URL': ( |
| 63 |
'http://hasthelargehadroncollider' |
| 64 |
'destroyedtheworldyet.com/' |
| 65 |
), |
| 66 |
'question': 'mutually exclusive', |
| 67 |
}, |
| 68 |
}, |
| 69 |
}, |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture(scope='module') |
| 74 |
async def mod_my_schd(mod_flow, mod_scheduler, mod_start, mod_my_conf): |
| 75 |
"""A "started" workflow.""" |
| 76 |
id_ = mod_flow(mod_my_conf) |
| 77 |
schd = mod_scheduler(id_) |
| 78 |
async with mod_start(schd): |
| 79 |
yield schd |
| 80 |
|
| 81 |
|
| 82 |
async def test_workflow_meta_query(mod_my_schd, capsys): |
| 83 |
"""It should fetch workflow metadata.""" |
| 84 |
colour_init(strip=True, autoreset=True) |
| 85 |
opts = SimpleNamespace( |
| 86 |
comms_timeout=5, |
| 87 |
json=False, |
| 88 |
list_prereqs=False, |
| 89 |
task_defs=None, |
| 90 |
) |
| 91 |
|
| 92 |
# plain output |
| 93 |
ret = await show(mod_my_schd.workflow, [], opts) |
| 94 |
assert ret == 0 |
| 95 |
out, err = capsys.readouterr() |
| 96 |
assert out.splitlines() == [ |
| 97 |
'title: Workflow Title', |
| 98 |
'description: My', |
| 99 |
'multiline', |
| 100 |
'description.', |
| 101 |
'answer: 42', |
| 102 |
'URL: http://ismycomputerturnedon.com/', |
| 103 |
] |
| 104 |
|
| 105 |
# json output |
| 106 |
opts.json = True |
| 107 |
ret = await show(mod_my_schd.workflow, [], opts) |
| 108 |
assert ret == 0 |
| 109 |
out, err = capsys.readouterr() |
| 110 |
assert json.loads(out) == { |
| 111 |
'title': 'Workflow Title', |
| 112 |
'description': 'My\nmultiline\ndescription.', |
| 113 |
'answer': '42', |
| 114 |
'URL': 'http://ismycomputerturnedon.com/', |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
async def test_task_meta_query(mod_my_schd, capsys): |
| 119 |
"""It should fetch task metadata.""" |
| 120 |
colour_init(strip=True, autoreset=True) |
| 121 |
opts = SimpleNamespace( |
| 122 |
comms_timeout=5, |
| 123 |
json=False, |
| 124 |
list_prereqs=False, |
| 125 |
task_defs=['foo'], |
| 126 |
) |
| 127 |
|
| 128 |
# plain output |
| 129 |
ret = await show( |
| 130 |
mod_my_schd.workflow, |
| 131 |
None, |
| 132 |
opts, |
| 133 |
) |
| 134 |
assert ret == 0 |
| 135 |
out, err = capsys.readouterr() |
| 136 |
|
| 137 |
assert out.splitlines() == [ |
| 138 |
'title: Task Title', |
| 139 |
'question: mutually exclusive', |
| 140 |
'description: Task', |
| 141 |
'multiline', |
| 142 |
'description', |
| 143 |
'URL: http://hasthelargehadroncolliderdestroyedtheworldyet.com/', |
| 144 |
] |
| 145 |
|
| 146 |
# json output |
| 147 |
opts.json = True |
| 148 |
ret = await show(mod_my_schd.workflow, [], opts) |
| 149 |
assert ret == 0 |
| 150 |
out, err = capsys.readouterr() |
| 151 |
assert json.loads(out) == { |
| 152 |
'foo': { |
| 153 |
'title': 'Task Title', |
| 154 |
'question': 'mutually exclusive', |
| 155 |
'description': 'Task\nmultiline\ndescription', |
| 156 |
'URL': 'http://hasthelargehadroncolliderdestroyedtheworldyet.com/', |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
async def test_task_instance_query( |
| 162 |
flow, scheduler, start, capsys |
| 163 |
): |
| 164 |
"""It should fetch task instance data, sorted by task name.""" |
| 165 |
|
| 166 |
colour_init(strip=True, autoreset=True) |
| 167 |
opts = SimpleNamespace( |
| 168 |
comms_timeout=5, |
| 169 |
json=False, |
| 170 |
task_defs=None, |
| 171 |
list_prereqs=False, |
| 172 |
) |
| 173 |
schd = scheduler( |
| 174 |
flow( |
| 175 |
{ |
| 176 |
'scheduling': { |
| 177 |
'graph': {'R1': 'zed & dog & cat & ant'}, |
| 178 |
}, |
| 179 |
}, |
| 180 |
), |
| 181 |
paused_start=False, |
| 182 |
) |
| 183 |
async with start(schd): |
| 184 |
await schd.update_data_structure() |
| 185 |
ret = await show( |
| 186 |
schd.workflow, |
| 187 |
[Tokens('//1/*')], |
| 188 |
opts, |
| 189 |
) |
| 190 |
assert ret == 0 |
| 191 |
|
| 192 |
out, _ = capsys.readouterr() |
| 193 |
assert [ |
| 194 |
line for line in out.splitlines() |
| 195 |
if line.startswith("Task ID") |
| 196 |
] == [ # results should be sorted |
| 197 |
'Task ID: 1/ant', |
| 198 |
'Task ID: 1/cat', |
| 199 |
'Task ID: 1/dog', |
| 200 |
'Task ID: 1/zed', |
| 201 |
] |
| 202 |
|
| 203 |
|
| 204 |
@pytest.mark.parametrize( |
| 205 |
'workflow_run_mode, run_mode_info', |
| 206 |
( |
| 207 |
('live', 'Skip'), |
| 208 |
('dummy', 'Dummy'), |
| 209 |
('simulation', 'Simulation'), |
| 210 |
) |
| 211 |
) |
| 212 |
@pytest.mark.parametrize( |
| 213 |
'attributes_bool, flow_nums, expected_state, expected_flows', |
| 214 |
[ |
| 215 |
pytest.param( |
| 216 |
False, [1], 'state: waiting (run mode={})', None, |
| 217 |
), |
| 218 |
pytest.param( |
| 219 |
True, |
| 220 |
[1, 2], |
| 221 |
'state: waiting (held,queued,runahead,run mode={})', |
| 222 |
'flows: [1,2]', |
| 223 |
) |
| 224 |
] |
| 225 |
) |
| 226 |
async def test_task_instance_state_flows( |
| 227 |
flow, scheduler, start, capsys, |
| 228 |
workflow_run_mode, run_mode_info, |
| 229 |
attributes_bool, flow_nums, expected_state, expected_flows |
| 230 |
): |
| 231 |
"""It should print task instance state, attributes, and flows.""" |
| 232 |
|
| 233 |
colour_init(strip=True, autoreset=True) |
| 234 |
opts = SimpleNamespace( |
| 235 |
comms_timeout=5, |
| 236 |
json=False, |
| 237 |
task_defs=None, |
| 238 |
list_prereqs=False, |
| 239 |
) |
| 240 |
schd = scheduler( |
| 241 |
flow( |
| 242 |
{ |
| 243 |
'scheduling': { |
| 244 |
'graph': {'R1': 'a'}, |
| 245 |
}, |
| 246 |
'runtime': { |
| 247 |
'a': {'run mode': 'skip'} |
| 248 |
} |
| 249 |
}, |
| 250 |
), |
| 251 |
paused_start=True, |
| 252 |
run_mode=workflow_run_mode, |
| 253 |
) |
| 254 |
async with start(schd): |
| 255 |
|
| 256 |
[itask] = schd.pool.get_tasks() |
| 257 |
itask.state_reset( |
| 258 |
is_held=attributes_bool, |
| 259 |
is_queued=attributes_bool, |
| 260 |
is_runahead=attributes_bool |
| 261 |
) |
| 262 |
itask.flow_nums = set(flow_nums) |
| 263 |
|
| 264 |
schd.pool.data_store_mgr.delta_task_held( |
| 265 |
itask.tdef.name, itask.point, itask.state.is_held) |
| 266 |
schd.pool.data_store_mgr.delta_task_state(itask) |
| 267 |
schd.pool.data_store_mgr.delta_task_flow_nums(itask) |
| 268 |
await schd.update_data_structure() |
| 269 |
|
| 270 |
ret = await show( |
| 271 |
schd.workflow, |
| 272 |
[Tokens('//1/*')], |
| 273 |
opts, |
| 274 |
) |
| 275 |
assert ret == 0 |
| 276 |
|
| 277 |
out, _ = capsys.readouterr() |
| 278 |
assert [ |
| 279 |
line for line in out.splitlines() |
| 280 |
if line.startswith("state:") |
| 281 |
] == [ |
| 282 |
expected_state.format(run_mode_info), |
| 283 |
] |
| 284 |
|
| 285 |
if expected_flows is not None: |
| 286 |
assert [ |
| 287 |
line for line in out.splitlines() |
| 288 |
if line.startswith("flows:") |
| 289 |
] == [ |
| 290 |
expected_flows, |
| 291 |
] |
| 292 |
|
| 293 |
|
| 294 |
async def test_task_run_mode_changes(flow, scheduler, start, capsys): |
| 295 |
"""Broadcasting a change of run mode changes run mode shown by cylc show. |
| 296 |
""" |
| 297 |
opts = ShowOptions() |
| 298 |
schd = scheduler( |
| 299 |
flow({'scheduling': {'graph': {'R1': 'a'}}}), |
| 300 |
run_mode='live' |
| 301 |
) |
| 302 |
|
| 303 |
async with start(schd): |
| 304 |
# Control: No mode set, the Run Mode setting is not shown: |
| 305 |
await schd.update_data_structure() |
| 306 |
ret = await show( |
| 307 |
schd.workflow, |
| 308 |
[Tokens('//1/a')], |
| 309 |
opts, |
| 310 |
) |
| 311 |
assert ret == 0 |
| 312 |
out, _ = capsys.readouterr() |
| 313 |
state, = RE_STATE.findall(out) |
| 314 |
assert 'waiting' in state |
| 315 |
|
| 316 |
# Broadcast change task to skip mode: |
| 317 |
schd.broadcast_mgr.put_broadcast(['1'], ['a'], [{'run mode': 'skip'}]) |
| 318 |
await schd.update_data_structure() |
| 319 |
|
| 320 |
# show now shows skip mode: |
| 321 |
ret = await show( |
| 322 |
schd.workflow, |
| 323 |
[Tokens('//1/a')], |
| 324 |
opts, |
| 325 |
) |
| 326 |
assert ret == 0 |
| 327 |
|
| 328 |
out, _ = capsys.readouterr() |
| 329 |
state, = RE_STATE.findall(out) |
| 330 |
assert 'run mode=Skip' in state |