Debian Codegraph @

tests/u/run_modes/test_skip_units.py

raw
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 """Unit tests for utilities supporting skip modes
17 """
18 import logging
19 import pytest
20 from pytest import param, raises
21 from types import SimpleNamespace
22
23 from cylc.flow.exceptions import WorkflowConfigError
24 from cylc.flow.run_modes.skip import (
25 check_task_skip_config,
26 process_outputs,
27 skip_mode_validate,
28 )
29
30
31 @pytest.mark.parametrize(
32 'conf',
33 (
34 param({}, id='no-skip-config'),
35 param({'skip': {'outputs': []}}, id='no-skip-outputs'),
36 param({'skip': {'outputs': ['foo1', 'failed']}}, id='ok-skip-outputs'),
37 )
38 )
39 def test_good_check_task_skip_config(conf):
40 """It returns none if the problems this function checks are not present.
41 """
42 tdef = SimpleNamespace(rtconfig=conf)
43 tdef.name = 'foo'
44 assert check_task_skip_config(tdef) is None
45
46
47 def test_raises_check_task_skip_config():
48 """It raises an error if succeeded and failed are set.
49 """
50 tdef = SimpleNamespace(
51 rtconfig={'skip': {'outputs': ['foo1', 'failed', 'succeeded']}}
52 )
53 tdef.name = 'foo'
54 with raises(WorkflowConfigError, match='succeeded AND failed'):
55 check_task_skip_config(tdef)
56
57
58 @pytest.mark.parametrize(
59 'outputs, required, expect',
60 (
61 param([], [], ['succeeded'], id='implicit-succeded'),
62 param(
63 ['succeeded'], ['succeeded'], ['succeeded'],
64 id='explicit-succeded'
65 ),
66 param(['submitted'], [], ['succeeded'], id='only-1-submit'),
67 param(
68 ['foo', 'bar', 'baz', 'qux'],
69 ['bar', 'qux'],
70 ['bar', 'qux', 'succeeded'],
71 id='required-only'
72 ),
73 param(
74 ['foo', 'baz'],
75 ['bar', 'qux'],
76 ['succeeded'],
77 id='no-required'
78 ),
79 param(
80 ['failed'],
81 [],
82 ['failed'],
83 id='explicit-failed'
84 ),
85 )
86 )
87 def test_process_outputs(outputs, required, expect):
88 """Check that skip outputs:
89
90 1. Doesn't send submitted twice.
91 2. Sends every required output.
92 3. If failed is set send failed
93 4. If failed in not set send succeeded.
94 """
95 # Create a mocked up task-proxy:
96 rtconf = {'skip': {'outputs': outputs}}
97 itask = SimpleNamespace(
98 tdef=SimpleNamespace(rtconfig=rtconf),
99 state=SimpleNamespace(
100 outputs=SimpleNamespace(
101 iter_required_messages=lambda *a, **k: iter(required),
102 _message_to_trigger={v: v for v in required},
103 )
104 ),
105 )
106
107 assert process_outputs(itask, rtconf) == {'submitted', 'started'}.union(
108 expect
109 )
110
111
112 def test_skip_mode_validate(caplog, log_filter):
113 """It logs a message if we've set a task config to nonlive mode.
114
115 (And not otherwise)
116
117 Point 3 from the skip mode proposal
118 https://github.com/cylc/cylc-admin/blob/master/docs/proposal-skip-mode.md
119
120 | If the run mode is set to simulation or skip in the workflow
121 | configuration, then cylc validate and cylc lint should produce
122 | warning (similar to development features in other languages / systems).
123
124 Edit:
125 Warning demoted to info to prevent the orange warning triangles
126 popping up in the GUI every time a workflow with configured skip tasks
127 is started. See: https://github.com/cylc/cylc-flow/pull/6854
128 """
129 caplog.set_level(logging.INFO)
130 taskdefs = {
131 f'{run_mode}_task': SimpleNamespace(
132 rtconfig={'run mode': run_mode},
133 name=f'{run_mode}_task'
134 )
135 for run_mode
136 in ['live', 'skip']
137 }
138
139 skip_mode_validate(taskdefs)
140
141 assert len(caplog.records) == 1
142 assert log_filter(
143 level=logging.INFO,
144 exact_match=(
145 "The following tasks are set to run in skip mode:\n"
146 " * skip_task"
147 ),
148 log=caplog
149 )