Debian Codegraph @

tests/i/tui/test_mutations.py

raw
1 #!/usr/bin/env python3
2 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
3 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import asyncio
19
20 import pytest
21
22 from cylc.flow.exceptions import ClientError
23
24
25 async def process_command(schd, tries=10, interval=0.1):
26 """Wait for command(s) to be queued and run.
27
28 Waits for at least one command to be queued and for all queued commands to
29 be run.
30 """
31 # wait for the command to be queued
32 for _try in range(tries):
33 await asyncio.sleep(interval)
34 if not schd.command_queue.empty():
35 break
36 else:
37 raise Exception(f'No command was queued after {tries * interval}s')
38
39 # run the command
40 await schd.process_command_queue()
41
42 # push out updates
43 await schd.update_data_structure()
44
45 # make sure it ran
46 assert schd.command_queue.empty(), 'command queue has not emptied'
47
48
49 async def test_online_mutation(
50 one_conf,
51 flow,
52 scheduler,
53 start,
54 rakiura,
55 monkeypatch,
56 log_filter,
57 ):
58 """Test a simple workflow with one task."""
59 id_ = flow(one_conf, name='one')
60 schd = scheduler(id_)
61 with rakiura(size='80,15') as rk:
62 async with start(schd):
63 await schd.update_data_structure()
64 assert schd.command_queue.empty()
65
66 # open the workflow
67 rk.force_update()
68 rk.user_input('down', 'right')
69 rk.wait_until_loaded(schd.tokens.id)
70
71 # focus on a task
72 rk.user_input('down', 'right', 'down', 'right')
73 rk.compare_screenshot(
74 # take a screenshot to ensure we have focused on the task
75 # successfully
76 'task-selected',
77 'the cursor should be on the task 1/foo',
78 )
79
80 # focus on the hold mutation for a task
81 rk.user_input('enter', 'down')
82 rk.compare_screenshot(
83 # take a screenshot to ensure we have focused on the mutation
84 # successfully
85 'hold-mutation-selected',
86 'the cursor should be on the "hold" mutation',
87 )
88
89 # run the hold mutation
90 rk.user_input('enter')
91
92 # the mutation should be in the scheduler's command_queue
93 await asyncio.sleep(0)
94 assert log_filter(contains="hold(tasks=['1/one'])")
95
96 # close the dialogue and re-run the hold mutation
97 rk.user_input('q', 'q', 'enter')
98 rk.compare_screenshot(
99 'command-failed-workflow-stopped',
100 'an error should be visible explaining that the operation'
101 ' cannot be performed on a stopped workflow',
102 # NOTE: don't update so Tui still thinks the workflow is running
103 force_update=False,
104 )
105
106 # force mutations to raise ClientError
107 def _get_client(*args, **kwargs):
108 raise ClientError('mock error')
109 monkeypatch.setattr(
110 'cylc.flow.tui.data.get_client',
111 _get_client,
112 )
113
114 # close the dialogue and re-run the hold mutation
115 rk.user_input('q', 'q', 'enter')
116 rk.compare_screenshot(
117 'command-failed-client-error',
118 'an error should be visible explaining that the operation'
119 ' failed due to a client error',
120 # NOTE: don't update so Tui still thinks the workflow is running
121 force_update=False,
122 )
123
124
125 @pytest.fixture
126 def standardise_cli_cmds(monkeypatch):
127 """This remove the variable bit of the workflow ID from CLI commands.
128
129 The workflow ID changes from run to run. In order to make screenshots
130 stable, this
131 """
132 from cylc.flow.tui.data import extract_context
133
134 def _extract_context(selection):
135 context = extract_context(selection)
136 if 'workflow' in context:
137 context['workflow'] = [
138 workflow.rsplit('/', 1)[-1]
139 for workflow in context.get('workflow', [])
140 ]
141 return context
142 monkeypatch.setattr(
143 'cylc.flow.tui.data.extract_context',
144 _extract_context,
145 )
146
147
148 @pytest.fixture
149 def capture_commands(monkeypatch):
150 ret = []
151 returncode = [0]
152
153 class _Popen:
154 def __init__(self, *args, **kwargs):
155 ret.append(args)
156
157 def communicate(self):
158 return 'mock-stdout', 'mock-stderr'
159
160 @property
161 def returncode(self):
162 return returncode[0]
163
164 monkeypatch.setattr(
165 'cylc.flow.tui.data.Popen',
166 _Popen,
167 )
168
169 return ret, returncode
170
171
172 async def test_offline_mutation(
173 one_conf,
174 flow,
175 rakiura,
176 capture_commands,
177 standardise_cli_cmds,
178 ):
179 flow(one_conf, name='one')
180 commands, returncode = capture_commands
181
182 with rakiura(size='80,15') as rk:
183 # run the stop-all mutation
184 rk.wait_until_loaded('root')
185 rk.user_input('enter', 'down')
186 rk.compare_screenshot(
187 # take a screenshot to ensure we have focused on the task
188 # successfully
189 'stop-all-mutation-selected',
190 'the stop-all mutation should be selected',
191 )
192 rk.user_input('enter')
193
194 # the command "cylc stop '*'" should have been run
195 assert commands == [(['cylc', 'stop', '*'],)]
196 commands.clear()
197
198 # run the clean command on the workflow
199 rk.user_input('down', 'enter', 'down')
200 rk.compare_screenshot(
201 # take a screenshot to ensure we have focused on the mutation
202 # successfully
203 'clean-mutation-selected',
204 'the clean mutation should be selected',
205 )
206 rk.user_input('enter')
207
208 # the command "cylc clean <id>" should have been run
209 assert commands == [(['cylc', 'clean', '--yes', 'one'],)]
210 commands.clear()
211
212 # make commands fail
213 returncode[:] = [1]
214 rk.user_input('enter', 'down')
215 rk.compare_screenshot(
216 # take a screenshot to ensure we have focused on the mutation
217 # successfully
218 'clean-mutation-selected',
219 'the clean mutation should be selected',
220 )
221 rk.user_input('enter')
222
223 assert commands == [(['cylc', 'clean', '--yes', 'one'],)]
224
225 rk.compare_screenshot(
226 # take a screenshot to ensure we have focused on the mutation
227 # successfully
228 'clean-command-error',
229 'there should be a box displaying the error containing the stderr'
230 ' returned by the command',
231 )
232
233
234 async def test_set_mutation(
235 flow,
236 scheduler,
237 start,
238 rakiura,
239 ):
240 id_ = flow({
241 'scheduling': {
242 'graph': {
243 'R1': 'a => z'
244 },
245 },
246 }, name='one')
247 schd = scheduler(id_)
248 async with start(schd):
249 await schd.update_data_structure()
250 with rakiura(schd.tokens.id, size='80,15') as rk:
251 # open the context menu on 1/a
252 rk.user_input('down', 'down', 'down', 'enter')
253 rk.force_update()
254
255 # select the "set" mutation
256 rk.user_input(*(('down',) * 7)) # 7th command down
257
258 rk.compare_screenshot(
259 # take a screenshot to ensure we have focused on the mutation
260 # successfully
261 'set-command-selected',
262 'The command menu should be open for the task 1/a with the'
263 ' set command selected.'
264 )
265
266 # issue the "set" mutation
267 rk.user_input('enter')
268
269 # wait for the command to be received and run it
270 await process_command(schd)
271 await schd._main_loop()
272
273 # close the error dialogue
274 # NOTE: This hides an asyncio error that does not occur outside of
275 # the tests
276 rk.user_input('q', 'q', 'q')
277
278 rk.compare_screenshot(
279 # take a screenshot to ensure we have focused on the mutation
280 # successfully
281 'task-state-updated',
282 '1/a should now show as succeeded,'
283 ' there should be no associated job.'
284 )