Debian Codegraph @

tests/unit/test_task_outputs.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
17 from types import SimpleNamespace
18 from typing import (
19 Optional,
20 Set,
21 )
22
23 import pytest
24
25 from cylc.flow.task_outputs import (
26 TASK_OUTPUT_EXPIRED,
27 TASK_OUTPUT_FAILED,
28 TASK_OUTPUT_SUBMIT_FAILED,
29 TASK_OUTPUT_SUBMITTED,
30 TASK_OUTPUT_SUCCEEDED,
31 TASK_OUTPUTS,
32 TaskOutputs,
33 get_completion_expression,
34 get_trigger_completion_variable_maps,
35 )
36 from cylc.flow.util import sstrip
37
38
39 def tdef(required, optional, completion=None):
40 """Stub a task definition.
41
42 Args:
43 required: Collection of required outputs.
44 optional: Collection of optional outputs.
45 completion: User defined execution completion expression.
46
47 """
48 return SimpleNamespace(
49 rtconfig={
50 'completion': completion,
51 },
52 outputs={
53 output: (
54 output,
55 (
56 # output is required:
57 True if output in required
58 # output is optional:
59 else False if output in optional
60 # output is ambiguous (i.e. not referenced in graph):
61 else None
62 )
63 )
64 for output in set(TASK_OUTPUTS) | set(required) | set(optional)
65 },
66 )
67
68
69 def test_completion_implicit():
70 """It should generate a completion expression when none is provided.
71
72 The outputs should be considered "complete" according to the logic in
73 proposal point 5:
74 https://cylc.github.io/cylc-admin/proposal-optional-output-extension.html#proposal
75 """
76 # one required output - succeeded
77 outputs = TaskOutputs(tdef([TASK_OUTPUT_SUCCEEDED], []))
78
79 # the completion expression should only contain the one required output
80 assert outputs._completion_expression == 'succeeded'
81 # the outputs should be incomplete - it hasn't run yet
82 assert outputs.is_complete() is False
83
84 # set the submit-failed output
85 outputs.set_message_complete(TASK_OUTPUT_SUBMIT_FAILED)
86 # the outputs should be incomplete - submited-failed is a "final" output
87 assert outputs.is_complete() is False
88
89 # set the submitted and succeeded outputs
90 outputs.set_message_complete(TASK_OUTPUT_SUBMITTED)
91 outputs.set_message_complete(TASK_OUTPUT_SUCCEEDED)
92 # the outputs should be complete - it has run an succeedd
93 assert outputs.is_complete() is True
94
95 # set the expired output
96 outputs.set_message_complete(TASK_OUTPUT_EXPIRED)
97 # the outputs should still be complete - it has run and succeeded
98 assert outputs.is_complete() is True
99
100
101 def test_completion_explicit():
102 """It should use the provided completion expression.
103
104 The outputs should be considered "complete" according to the logic in
105 proposal point 5:
106 https://cylc.github.io/cylc-admin/proposal-optional-output-extension.html#proposal
107 """
108 outputs = TaskOutputs(tdef(
109 # no required outputs
110 [],
111 # four optional outputs
112 [
113 TASK_OUTPUT_SUCCEEDED,
114 TASK_OUTPUT_FAILED,
115 'x',
116 'y',
117 ],
118 # one pair must be satisfied for the outputs to be complete
119 completion='(succeeded and x) or (failed and y)',
120 ))
121
122 # the outputs should be incomplete - it hasn't run yet
123 assert outputs.is_complete() is False
124
125 # set the succeeded and failed outputs
126 outputs.set_message_complete(TASK_OUTPUT_SUCCEEDED)
127 outputs.set_message_complete(TASK_OUTPUT_FAILED)
128
129 # the task should be incomplete - it has executed but the completion
130 # expression is not satisfied
131 assert outputs.is_complete() is False
132
133 # satisfy the (failed and y) pair
134 outputs.set_message_complete('y')
135 assert outputs.is_complete() is True
136
137 # satisfy the (succeeded and x) pair
138 outputs._completed['y'] = False
139 outputs.set_message_complete('x')
140 assert outputs.is_complete() is True
141
142
143 @pytest.mark.parametrize(
144 'required, optional, expression', [
145 pytest.param(
146 {TASK_OUTPUT_SUCCEEDED},
147 [],
148 'succeeded',
149 id='0',
150 ),
151 pytest.param(
152 {TASK_OUTPUT_SUCCEEDED, 'x'},
153 [],
154 '(succeeded and x)',
155 id='1',
156 ),
157 pytest.param(
158 [],
159 {TASK_OUTPUT_SUCCEEDED},
160 'succeeded or failed',
161 id='2',
162 ),
163 pytest.param(
164 {TASK_OUTPUT_SUCCEEDED},
165 {TASK_OUTPUT_EXPIRED},
166 'succeeded or expired',
167 id='3',
168 ),
169 pytest.param(
170 [],
171 {TASK_OUTPUT_SUCCEEDED, TASK_OUTPUT_EXPIRED},
172 'succeeded or failed or expired',
173 id='4',
174 ),
175 pytest.param(
176 {TASK_OUTPUT_SUCCEEDED},
177 {TASK_OUTPUT_EXPIRED, TASK_OUTPUT_SUBMITTED},
178 'succeeded or submit_failed or expired',
179 id='5',
180 ),
181 pytest.param(
182 {TASK_OUTPUT_SUCCEEDED, TASK_OUTPUT_SUBMITTED},
183 {TASK_OUTPUT_EXPIRED},
184 '(submitted and succeeded) or expired',
185 id='6',
186 ),
187 pytest.param(
188 [],
189 {TASK_OUTPUT_SUCCEEDED, TASK_OUTPUT_SUBMIT_FAILED},
190 'succeeded or failed or submit_failed',
191 id='7',
192 ),
193 pytest.param(
194 {'x'},
195 {
196 TASK_OUTPUT_SUCCEEDED,
197 TASK_OUTPUT_SUBMIT_FAILED,
198 TASK_OUTPUT_EXPIRED,
199 },
200 '(x and succeeded) or failed or submit_failed or expired',
201 id='8',
202 ),
203 ],
204 )
205 def test_get_completion_expression_implicit(required, optional, expression):
206 """It should generate a completion expression if none is provided."""
207 assert get_completion_expression(tdef(required, optional)) == expression
208
209
210 def test_get_completion_expression_explicit():
211 """If a completion expression is used, it should be used unmodified."""
212 assert get_completion_expression(tdef(
213 {'x', 'y'},
214 {TASK_OUTPUT_SUCCEEDED, TASK_OUTPUT_FAILED, TASK_OUTPUT_EXPIRED},
215 '((failed and x) or (succeeded and y)) or expired'
216 )) == '((failed and x) or (succeeded and y)) or expired'
217
218
219 def test_format_completion_status():
220 outputs = TaskOutputs(
221 tdef(
222 {TASK_OUTPUT_SUCCEEDED, 'x', 'y'},
223 {TASK_OUTPUT_EXPIRED},
224 )
225 )
226 assert outputs.format_completion_status(
227 indent=2, gutter=2
228 ) == ' ' + sstrip(
229 '''
230 ┆ (
231 ⨯ ┆ succeeded
232 ⨯ ┆ and x
233 ⨯ ┆ and y
234 ┆ )
235 ⨯ ┆ or expired
236 '''
237 )
238 outputs.set_message_complete('succeeded')
239 outputs.set_message_complete('x')
240 assert outputs.format_completion_status(
241 indent=2, gutter=2
242 ) == ' ' + sstrip(
243 '''
244 ┆ (
245 ✓ ┆ succeeded
246 ✓ ┆ and x
247 ⨯ ┆ and y
248 ┆ )
249 ⨯ ┆ or expired
250 '''
251 )
252
253
254 @pytest.mark.parametrize(
255 'required, optional, expected_required, expected_expression', [
256 # this task has three required outputs and one optional output
257 pytest.param(
258 {TASK_OUTPUT_SUCCEEDED, 'x', 'y'},
259 {'z'},
260 {TASK_OUTPUT_SUCCEEDED, 'x', 'y'},
261 None,
262 id="3-required-1-optional",
263 ),
264 # this task does not have any required outputs (besides the implicitly
265 # required submitted/started outputs)
266 # Note: validation should prevent this at the config level
267 pytest.param(
268 {TASK_OUTPUT_SUCCEEDED, 'x', 'y'},
269 {TASK_OUTPUT_FAILED}, # task may fail
270 set(),
271 None,
272 id="no-required-outputs",
273 ),
274 # the preconditions expiry/submitted are excluded from this logic when
275 # defined as optional:
276 pytest.param(
277 {TASK_OUTPUT_SUCCEEDED, 'x', 'y'},
278 {TASK_OUTPUT_EXPIRED}, # task may expire
279 {TASK_OUTPUT_SUCCEEDED, 'x', 'y'},
280 '(succeeded and x and y) or expired',
281 id="expiry-submitted",
282 ),
283 # NOTE: a required output might not be required!
284 # If success is optional, then apparently-required outputs are made
285 # implicitly optional. See
286 # https://github.com/cylc/cylc-flow/pull/6505#issuecomment-2517781523
287 pytest.param(
288 {'x'},
289 {TASK_OUTPUT_SUCCEEDED},
290 set(),
291 '(x and succeeded) or failed',
292 id="implicit-optional",
293 ),
294 pytest.param(
295 set(),
296 {'x', TASK_OUTPUT_SUCCEEDED},
297 set(),
298 'succeeded or failed',
299 id="all-optional",
300 ),
301 ]
302 )
303 def test_iter_required_outputs(
304 required: Set[str],
305 optional: Set[str],
306 expected_required: Set[str],
307 expected_expression: Optional[str],
308 ):
309 """It should yield required outputs only."""
310 outputs = TaskOutputs(tdef(required, optional))
311 if expected_expression:
312 assert outputs._completion_expression == expected_expression
313 assert set(outputs.iter_required_messages()) == expected_required
314
315
316 def test_iter_required_outputs__disable():
317 # Get all outputs required for success path (excluding failure, what
318 # is still required):
319 outputs = TaskOutputs(
320 tdef(
321 {},
322 {'a', 'succeeded', 'b', 'y', 'failed', 'x'},
323 '(x and y and failed) or (a and b and succeeded)'
324 )
325 )
326
327 assert set(outputs.iter_required_messages()) == set()
328
329 # Disabling succeeded leaves us with failure required outputs:
330 assert set(
331 outputs.iter_required_messages(disable=TASK_OUTPUT_SUCCEEDED)
332 ) == {
333 TASK_OUTPUT_FAILED,
334 'x',
335 'y',
336 }
337
338 # Disabling failed leaves us with succeeded required outputs:
339 assert set(outputs.iter_required_messages(disable=TASK_OUTPUT_FAILED)) == {
340 TASK_OUTPUT_SUCCEEDED,
341 'a',
342 'b',
343 }
344
345 # Disabling an abitrary output leaves us with required outputs
346 # from another branch:
347 assert set(outputs.iter_required_messages(disable='a')) == {
348 TASK_OUTPUT_FAILED,
349 'x',
350 'y',
351 }
352
353
354 def test_get_trigger_completion_variable_maps():
355 """It should return a bi-map of triggers to compvars."""
356 t2c, c2t = get_trigger_completion_variable_maps(('a', 'b-b', 'c-c-c'))
357 assert t2c == {'a': 'a', 'b-b': 'b_b', 'c-c-c': 'c_c_c'}
358 assert c2t == {'a': 'a', 'b_b': 'b-b', 'c_c_c': 'c-c-c'}