Debian Codegraph @

tests/u/network/test_multi.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 functools import partial
18
19 import pytest
20
21 from cylc.flow.exceptions import CylcError, WorkflowStopped
22 from cylc.flow.network.multi import _report, _process_response
23 from cylc.flow.terminal import DIM
24
25
26 def response(success, msg, operation='set'):
27 return {
28 operation: {
29 'result': [{'id': '~user/workflow', 'response': [success, msg]}]
30 }
31 }
32
33
34 def test_report_valid(monkeypatch):
35 """It should report command outcome."""
36 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
37
38 # fail case
39 assert _report(response(False, 'MyError')) == (
40 None,
41 '<red>MyError</red>',
42 False,
43 )
44
45 # success case
46 assert _report(response(True, '12345')) == (
47 '<green>Command queued</green>',
48 None,
49 True,
50 )
51
52 # success case (debug mode)
53 monkeypatch.setattr('cylc.flow.flags.verbosity', 1)
54 assert _report(response(True, '12345')) == (
55 f'<green>Command queued</green> <{DIM}>id=12345</{DIM}>',
56 None,
57 True,
58 )
59
60
61 def test_report_invalid(monkeypatch):
62 """It should report invalid responses.
63
64 Tests that the code behaves as well as can be expected when confronted with
65 responses which should not be possible.
66 """
67 # test "None" response
68 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
69 assert _report({'set': None}) == (
70 None,
71 '<red>Error processing command:'
72 "\n TypeError: 'NoneType' object is not subscriptable</red>",
73 False,
74 )
75
76 # test "None" response in debug mode
77 monkeypatch.setattr('cylc.flow.flags.verbosity', 2)
78 assert _report({'set': None}) == (
79 None,
80 '<red>Error processing command:'
81 "\n TypeError: 'NoneType' object is not subscriptable</red>"
82 # the response should be output in debug mode
83 "\n <fg 248>response={'set': None}</fg 248>",
84 False,
85 )
86
87 # test multiple mutations in one operation (not supported)
88 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
89 assert _report(
90 {
91 **response(True, '12345'),
92 **response(True, '23456', 'trigger'),
93 }
94 ) == (
95 None,
96 '<red>Error processing command:'
97 '\n NotImplementedError:'
98 ' Cannot process multiple mutations in one operation.</red>',
99 False,
100 )
101
102 # test zero mutations in the operation
103 assert _report(
104 {}
105 ) == (
106 None,
107 '<red>Error processing command:'
108 '\n Exception: {}</red>',
109 False,
110 )
111
112
113 def test_process_response(monkeypatch):
114 """It should handle exceptions and return processed results."""
115 def report(exception_class, _response):
116 raise exception_class('xxx')
117
118 class Foo(Exception):
119 pass
120
121 # WorkflowStopped -> fail case
122 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
123 assert _process_response(partial(report, WorkflowStopped), {}) == (
124 None,
125 '<yellow>WorkflowStopped: xxx is not running</yellow>',
126 False,
127 )
128
129 # WorkflowStopped -> success case for this command
130 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
131 assert _process_response(
132 partial(report, WorkflowStopped),
133 {},
134 # this overrides the default interpretation of "WorkflowStopped" as a
135 # fail case
136 success_exceptions=(WorkflowStopped,),
137 ) == (
138 '<green>WorkflowStopped: xxx is not running</green>',
139 None,
140 True, # success outcome
141 )
142
143 # CylcError -> expected error, log it
144 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
145 assert _process_response(partial(report, CylcError), {}) == (
146 None,
147 '<red>CylcError: xxx</red>',
148 False,
149 )
150
151 # CylcError -> expected error, raise it (debug mode)
152 monkeypatch.setattr('cylc.flow.flags.verbosity', 2)
153 with pytest.raises(CylcError):
154 _process_response(partial(report, CylcError), {})
155
156 # Exception -> unexpected error, raise it
157 monkeypatch.setattr('cylc.flow.flags.verbosity', 0)
158 with pytest.raises(Foo):
159 _process_response(partial(report, Foo), {})