1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Test the cylc scan Python API (which is equivalent to the CLI)."""
import json
from pathlib import Path
from shutil import (
copytree,
rmtree
)
import pytest
from cylc.flow.scripts.scan import (
main,
ScanOptions
)
from cylc.flow.workflow_files import (
ContactFileFields,
WorkflowFiles,
dump_contact_file,
load_contact_file
)
@pytest.fixture(scope='module')
async def flows(mod_flow, mod_scheduler, mod_run, mod_one_conf):
"""Three workflows in different states.
One stopped, one paused and one that thinks its running.
TODO:
Start one of the workflows with tasks in funny states
in order to test the state totals functionality properly.
Requires: https://github.com/cylc/cylc-flow/pull/3668
"""
# a simple workflow we will leave stopped
mod_flow(mod_one_conf, name='-stopped-')
# a simply hierarchically registered workflow we will leave stopped
mod_flow(mod_one_conf, name='a/b/c')
# a simple workflow we will leave paused
reg1 = mod_flow(mod_one_conf, name='-paused-')
schd1 = mod_scheduler(reg1, paused_start=True)
# a workflow with some metadata we will make look like it's running
reg2 = mod_flow(
{
'meta': {
'title': 'Foo',
'description': '''
Here we find a
multi
line
description
'''
},
'scheduler': {
'allow implicit tasks': True
},
'scheduling': {
'graph': {
'R1': 'foo'
}
},
'runtime': {
'foo': {
'simulation': {'default run length': 'PT10S'}
}
}
},
name='-running-'
)
schd2 = mod_scheduler(reg2, paused_start=False)
# run cylc run
async with mod_run(schd1):
async with mod_run(schd2):
yield
async def test_state_filter(flows, mod_test_dir):
"""It should filter flows by state."""
# one stopped flow
opts = ScanOptions(states='stopped', sort=True)
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 2
assert '-stopped-' in lines[0]
assert 'a/b/c' in lines[1]
# one paused flow
opts = ScanOptions(states='paused')
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 1
assert '-paused-' in lines[0]
# one running flow
opts = ScanOptions(states='running')
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 1
assert '-running-' in lines[0]
# two active flows
opts = ScanOptions(states='paused,running')
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 2
# three registered flows
opts = ScanOptions(states='paused,running,stopped')
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 4
async def test_name_filter(flows, mod_test_dir):
"""It should filter flows by name regex."""
# one stopped flow
opts = ScanOptions(states='all', name=['.*paused.*'])
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 1
assert '-paused-' in lines[0]
async def test_name_sort(flows, mod_test_dir):
"""It should sort flows by name."""
# one stopped flow
opts = ScanOptions(states='all', sort=True)
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
assert len(lines) == 4
assert '-paused-' in lines[0]
assert '-running-' in lines[1]
assert '-stopped-' in lines[2]
assert 'a/b/c' in lines[3]
async def test_format_json(flows, mod_test_dir):
"""It should dump results in json format."""
# one stopped flow
opts = ScanOptions(states='all', format='json')
lines = []
await main(opts, write=lines.append, scan_dir=mod_test_dir)
data = json.loads(lines[0])
assert len(data) == 4
assert data[0]['name']
async def test_format_tree(flows, run_dir, ses_test_dir, mod_test_dir):
"""It should dump results in an ascii tree format."""
# one stopped flow
opts = ScanOptions(states='running', format='tree')
workflows = []
await main(opts, write=workflows.append, scan_dir=mod_test_dir)
assert len(workflows) == 1
lines = workflows[0].splitlines()
# this flow is hierarchically registered in the run dir already
# it should be registered as <session test dir>/<module test dir>/<name>
assert ses_test_dir.name in lines[0]
assert mod_test_dir.name in lines[1]
assert '-running-' in lines[2]
async def test_format_rich(flows, mod_test_dir):
"""It should print results in a long human-friendly format."""
# one stopped flow (--colour-blind)
opts = ScanOptions(states='running', format='rich', colour_blind=True)
workflows = []
await main(opts, write=workflows.append, scan_dir=mod_test_dir)
assert len(workflows) == 1
lines = workflows[0].splitlines()
# test that the multi-line description was output correctly
# with trailing lines indented correctly
desc_lines = [
'Here we find a',
'multi',
'line',
'description'
]
prev_ind = -1
prev_offset = -1
for expected in desc_lines:
for ind, line in enumerate(lines):
if expected in line:
offset = line.index(expected)
if prev_ind < 1:
prev_ind = ind
prev_offset = offset
else:
if ind != prev_ind + 1:
raise Exception(
f'Lines found in wrong order: {line}')
if offset != prev_offset:
raise Exception('Line incorrectly indented: {line}')
break
else:
raise Exception(f'Missing line: {line}')
# test that the state totals show one task running (colour_blind mode)
for line in lines:
if 'running:1' in line:
break
else:
raise Exception('missing state totals line (colour_blind)')
# one stopped flow (--colour=always)
opts = ScanOptions(states='running', format='rich')
workflows = []
await main(
opts, write=workflows.append, scan_dir=mod_test_dir, color='always'
)
assert len(workflows) == 1
lines = workflows[0].splitlines()
# test that the state totals show one task running (colour mode)
for line in lines:
if '1 ■' in line:
break
else:
raise Exception('missing state totals line (colourful)')
async def test_scan_cleans_stuck_contact_files(
start,
scheduler,
flow,
one_conf,
run_dir,
test_dir,
):
"""Ensure scan tidies up contact files from crashed flows."""
# create a flow
id_ = flow(one_conf, name='-crashed-')
schd = scheduler(id_)
srv_dir = Path(run_dir, id_, WorkflowFiles.Service.DIRNAME)
tmp_dir = test_dir / 'srv'
cont = srv_dir / WorkflowFiles.Service.CONTACT
# run the flow, copy the contact, stop the flow, copy back the contact
async with start(schd):
copytree(srv_dir, tmp_dir)
rmtree(srv_dir)
copytree(tmp_dir, srv_dir)
rmtree(tmp_dir)
# the old contact file check uses the CLI command that the flow was run
# with to check whether the flow is running. Because this is an
# integration test the process is the pytest process and it is still
# running so we need to change the command so that Cylc sees the flow as
# having crashed
contact_info = load_contact_file(id_)
contact_info[ContactFileFields.COMMAND] += 'xyz'
dump_contact_file(id_, contact_info)
# make sure this flow shows for a regular filesystem-only scan
opts = ScanOptions(states='running,paused', format='name')
flows = []
await main(opts, write=flows.append, scan_dir=test_dir)
assert len(flows) == 1
assert '-crashed-' in flows[0]
# the contact file should still be there
assert cont.exists()
# make sure this flow shows for a regular filesystem-only scan
opts = ScanOptions(states='running,paused', format='name', ping=True)
flows = []
await main(opts, write=flows.append, scan_dir=test_dir)
assert len(flows) == 0
# the contact file should have been removed by the scan
assert not cont.exists()
async def test_scan_fail_well_when_client_unreachable(
start,
scheduler,
flow,
one_conf,
run_dir,
test_dir,
caplog,
):
"""It handles WorkflowRuntimeClient.async_request raising a WorkflowStopped
elegently.
"""
# create a flow
id_ = flow(one_conf, name='-crashed-')
schd = scheduler(id_)
srv_dir = Path(run_dir, id_, WorkflowFiles.Service.DIRNAME)
tmp_dir = test_dir / 'srv'
# run the flow, copy the contact, stop the flow, copy back the contact
async with start(schd):
copytree(srv_dir, tmp_dir)
rmtree(srv_dir)
copytree(tmp_dir, srv_dir)
rmtree(tmp_dir)
# the old contact file check uses the CLI command that the flow was run
# with to check whether the flow is running. Because this is an
# integration test the process is the pytest process and it is still
# running so we need to change the command so that Cylc sees the flow as
# having crashed
contact_info = load_contact_file(id_)
contact_info[ContactFileFields.COMMAND] += 'xyz'
dump_contact_file(id_, contact_info)
# Run Cylc Scan
opts = ScanOptions(states='all', format='rich', ping=True)
flows = []
await main(opts, write=flows.append, scan_dir=test_dir)
# Check that the records contain a message but not an error
rec = caplog.records[-1]
assert not rec.exc_text
assert 'Workflow not running' in rec.msg
|