| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 4 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
"""Integration tests for Cylc Validate CLI script.""" |
| 20 |
|
| 21 |
import logging |
| 22 |
|
| 23 |
import pytest |
| 24 |
|
| 25 |
from cylc.flow.exceptions import WorkflowConfigError |
| 26 |
from cylc.flow.parsec.exceptions import IllegalItemError, Jinja2Error |
| 27 |
|
| 28 |
|
| 29 |
async def test_validate_against_source_checks_source( |
| 30 |
capsys, validate, workflow_source, install, one_conf |
| 31 |
): |
| 32 |
"""Validation fails if validating against source with broken config. |
| 33 |
""" |
| 34 |
src_dir = workflow_source(one_conf) |
| 35 |
workflow_id = await install(src_dir) |
| 36 |
|
| 37 |
# Check that the original installation validates OK: |
| 38 |
validate(workflow_id, against_source=True) |
| 39 |
|
| 40 |
# Break the source config: |
| 41 |
with open(src_dir / 'flow.cylc', 'a') as handle: |
| 42 |
handle.write('\n[runtime]\n[[foo]]\nAgrajag = bowl of petunias') |
| 43 |
|
| 44 |
# # Check that Validate now fails: |
| 45 |
with pytest.raises(IllegalItemError, match='Agrajag'): |
| 46 |
validate(workflow_id, against_source=True) |
| 47 |
|
| 48 |
|
| 49 |
async def test_validate_against_source_gets_old_tvars( |
| 50 |
workflow_source, capsys, validate, scheduler, run, install, run_dir |
| 51 |
): |
| 52 |
"""Validation will retrieve template variables from a previously played |
| 53 |
workflow. |
| 54 |
""" |
| 55 |
src_dir = workflow_source({ |
| 56 |
'#!jinja2': None, |
| 57 |
'scheduler': { |
| 58 |
'allow implicit tasks': True |
| 59 |
}, |
| 60 |
'scheduling': { |
| 61 |
'initial cycle point': '1854', |
| 62 |
'graph': { |
| 63 |
'P1Y': 'foo' |
| 64 |
}, |
| 65 |
}, |
| 66 |
'runtime': { |
| 67 |
'foo': { |
| 68 |
'script': 'cylc pause ${CYLC_WORKFLOW_ID}' |
| 69 |
} |
| 70 |
} |
| 71 |
}) |
| 72 |
|
| 73 |
wf_id = await install(src_dir) |
| 74 |
installed_dir = run_dir / wf_id |
| 75 |
|
| 76 |
# Check that the original installation validates OK: |
| 77 |
validate(installed_dir) |
| 78 |
|
| 79 |
# # Start a scheduler with tvars option: |
| 80 |
schd = scheduler( |
| 81 |
wf_id, |
| 82 |
templatevars=['FOO="foo"'] |
| 83 |
) |
| 84 |
async with run(schd): |
| 85 |
pass |
| 86 |
|
| 87 |
# Replace foo in graph with {{FOO}} and check that this still |
| 88 |
# Validates (using db value for FOO): |
| 89 |
flow_file = (installed_dir / 'flow.cylc') |
| 90 |
flow_file.write_text( |
| 91 |
flow_file.read_text().replace('P1Y = foo', 'P1Y = {{FOO}}')) |
| 92 |
validate(wf_id, against_source=True) |
| 93 |
|
| 94 |
# Check that the source will not validate alone: |
| 95 |
flow_file = (src_dir / 'flow.cylc') |
| 96 |
flow_file.write_text( |
| 97 |
flow_file.read_text().replace('P1Y = foo', 'P1Y = {{FOO}}')) |
| 98 |
with pytest.raises(Jinja2Error): |
| 99 |
validate(src_dir) |
| 100 |
|
| 101 |
|
| 102 |
def test_validate_simple_graph(flow, validate, caplog): |
| 103 |
"""Test deprecation notice for Cylc 7 simple graph (no recurrence section) |
| 104 |
""" |
| 105 |
id_ = flow({ |
| 106 |
'scheduler': {'allow implicit tasks': True}, |
| 107 |
'scheduling': {'dependencies': {'graph': 'foo'}} |
| 108 |
}) |
| 109 |
validate(id_) |
| 110 |
expect = ( |
| 111 |
'graph items were automatically upgraded' |
| 112 |
' in "workflow definition":' |
| 113 |
'\n * (8.0.0) [scheduling][dependencies]graph -> [scheduling][graph]R1' |
| 114 |
) |
| 115 |
assert expect in caplog.messages |
| 116 |
|
| 117 |
|
| 118 |
def test_pre_cylc8(flow, validate, caplog): |
| 119 |
"""Test all current non-silent workflow obsoletions and deprecations. |
| 120 |
""" |
| 121 |
id_ = flow({ |
| 122 |
'cylc': { |
| 123 |
'events': { |
| 124 |
'reset timer': 10, |
| 125 |
'reset inactivity timer': 15, |
| 126 |
} |
| 127 |
}, |
| 128 |
"scheduling": { |
| 129 |
"initial cycle point": "20150808T00", |
| 130 |
"final cycle point": "20150808T00", |
| 131 |
"graph": { |
| 132 |
"P1D": "foo => cat & dog" |
| 133 |
}, |
| 134 |
"special tasks": { |
| 135 |
"external-trigger": 'cat("meow available")' |
| 136 |
} |
| 137 |
}, |
| 138 |
'runtime': { |
| 139 |
'foo, cat, dog': { |
| 140 |
'suite state polling': {'template': ''}, |
| 141 |
'events': {'reset timer': 20} |
| 142 |
} |
| 143 |
} |
| 144 |
}, defaults=False) |
| 145 |
validate(id_) |
| 146 |
for warning in ( |
| 147 |
( |
| 148 |
' * (7.8.0) [runtime][foo, cat, dog][suite state polling]template' |
| 149 |
' - DELETED (OBSOLETE)'), |
| 150 |
' * (7.8.1) [cylc][events]reset timer - DELETED (OBSOLETE)', |
| 151 |
' * (7.8.1) [cylc][events]reset inactivity timer - DELETED (OBSOLETE)', |
| 152 |
( |
| 153 |
' * (7.8.1) [runtime][foo, cat, dog][events]reset timer' |
| 154 |
' - DELETED (OBSOLETE)'), |
| 155 |
( |
| 156 |
' * (8.0.0) [runtime][foo, cat, dog][suite state polling]' |
| 157 |
' -> [runtime][foo, cat, dog][workflow state polling]' |
| 158 |
' - value unchanged'), |
| 159 |
' * (8.0.0) [cylc] -> [scheduler] - value unchanged' |
| 160 |
): |
| 161 |
assert warning in caplog.messages |
| 162 |
|
| 163 |
|
| 164 |
def test_graph_upgrade_msg_default(flow, validate, caplog, log_filter): |
| 165 |
"""It lists Cycling definitions which need upgrading.""" |
| 166 |
id_ = flow({ |
| 167 |
'scheduler': {'allow implicit tasks': True}, |
| 168 |
'scheduling': { |
| 169 |
'initial cycle point': 1042, |
| 170 |
'dependencies': { |
| 171 |
'R1': {'graph': 'foo'}, |
| 172 |
'P1Y': {'graph': 'bar & baz'} |
| 173 |
} |
| 174 |
}, |
| 175 |
}) |
| 176 |
validate(id_) |
| 177 |
assert log_filter(contains='[scheduling][dependencies][X]graph') |
| 178 |
assert log_filter(contains='for X in:\n P1Y, R1') |
| 179 |
|
| 180 |
|
| 181 |
def test_graph_upgrade_msg_graph_equals(flow, validate, caplog, log_filter): |
| 182 |
"""It gives a more useful message in special case where graph is |
| 183 |
key rather than section: |
| 184 |
|
| 185 |
[scheduling] |
| 186 |
[[dependencies]] |
| 187 |
graph = foo => bar |
| 188 |
""" |
| 189 |
id_ = flow({ |
| 190 |
'scheduler': {'allow implicit tasks': True}, |
| 191 |
'scheduling': {'dependencies': {'graph': 'foo => bar'}}, |
| 192 |
}) |
| 193 |
validate(id_) |
| 194 |
assert log_filter( |
| 195 |
contains='[scheduling][dependencies]graph -> [scheduling][graph]R1' |
| 196 |
) |
| 197 |
|
| 198 |
|
| 199 |
def test_graph_upgrade_msg_graph_equals2(flow, validate, caplog, log_filter): |
| 200 |
"""Both an implicit R1 and explict reccurance exist: |
| 201 |
It appends a note. |
| 202 |
""" |
| 203 |
id_ = flow({ |
| 204 |
'scheduler': {'allow implicit tasks': True}, |
| 205 |
'scheduling': { |
| 206 |
'initial cycle point': '1000', |
| 207 |
'dependencies': { |
| 208 |
'graph': 'foo => bar', 'P1Y': {'graph': 'a => b'}}}, |
| 209 |
}) |
| 210 |
validate(id_) |
| 211 |
expect = ( |
| 212 |
'graph items were automatically upgraded in' |
| 213 |
' "workflow definition":' |
| 214 |
'\n * (8.0.0) [scheduling][dependencies][X]graph' |
| 215 |
' -> [scheduling][graph]X - for X in:' |
| 216 |
'\n P1Y, graph' |
| 217 |
'\n ([scheduling][dependencies]graph moves to [scheduling][graph]R1)' |
| 218 |
) |
| 219 |
assert log_filter(contains=expect) |
| 220 |
|
| 221 |
|
| 222 |
def test_undefined_parent(flow, validate): |
| 223 |
"""It should catch tasks which inherit from implicit families.""" |
| 224 |
id_ = flow({ |
| 225 |
'scheduling': {'graph': {'R1': 'foo'}}, |
| 226 |
'runtime': {'foo': {'inherit': 'FOO'}} |
| 227 |
}) |
| 228 |
with pytest.raises(WorkflowConfigError, match='undefined parent for foo'): |
| 229 |
validate(id_) |
| 230 |
|
| 231 |
|
| 232 |
def test_log_parent_demoted(flow, validate, monkeypatch, caplog, log_filter): |
| 233 |
"""It should log family "demotion" in verbose mode.""" |
| 234 |
monkeypatch.setattr( |
| 235 |
'cylc.flow.flags.verbosity', |
| 236 |
10, |
| 237 |
) |
| 238 |
caplog.set_level(logging.DEBUG) |
| 239 |
id_ = flow({ |
| 240 |
'scheduling': { |
| 241 |
'graph': { |
| 242 |
'R1': 'foo' |
| 243 |
} |
| 244 |
}, |
| 245 |
'runtime': { |
| 246 |
'foo': {'inherit': 'None, FOO'}, |
| 247 |
'FOO': {}, |
| 248 |
} |
| 249 |
}) |
| 250 |
validate(id_) |
| 251 |
assert log_filter(contains='First parent(s) demoted to secondary') |
| 252 |
assert log_filter(contains="FOO as parent of 'foo'") |