Debian Codegraph @

tests/integration/tui/test_app.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 from random import random
19
20 import pytest
21 import urwid
22
23 from cylc.flow import __version__
24 from cylc.flow.cycling.integer import IntegerPoint
25 from cylc.flow.id import TaskTokens
26 from cylc.flow.task_outputs import TASK_OUTPUT_SUCCEEDED
27 from cylc.flow.task_state import (
28 TASK_STATUS_EXPIRED,
29 TASK_STATUS_FAILED,
30 TASK_STATUS_RUNNING,
31 TASK_STATUS_SUBMIT_FAILED,
32 TASK_STATUS_SUBMITTED,
33 TASK_STATUS_SUCCEEDED,
34 TASK_STATUS_WAITING,
35 )
36 from cylc.flow.tui.util import MODIFIER_ATTR_MAPPING
37 from cylc.flow.workflow_files import get_contact_file_path
38 from cylc.flow.workflow_status import StopMode
39
40
41 def _add_xtrig(schd, itask, sig, label, satisfied=False):
42 """Register a new xtrigger.
43
44 This is enough to get the data store to reflect the xtrig, but don't expect
45 it to be functional!
46 """
47 (
48 schd.data_store_mgr.xtrigger_tasks
49 .setdefault(sig, set())
50 .add((itask.tokens.id, label))
51 )
52 schd.data_store_mgr.delta_xtrigger(sig, satisfied)
53
54
55 def set_task_state(schd, task_states):
56 """Force tasks into the desired states.
57
58 Task states should be of the format (cycle, task, state, is_held).
59 """
60 for cycle, task, state, modifiers in task_states:
61 itask = schd.pool.get_task(cycle, task)
62 if not itask:
63 itask = schd.pool.spawn_task(task, cycle, {1})
64 if modifiers.pop('is_retry', None):
65 _add_xtrig(
66 schd, itask, 'wall_clock(offset=0)', f'_cylc_retry_{random()}'
67 )
68 if modifiers.pop('is_wallclock', None):
69 _add_xtrig(schd, itask, 'wall_clock(offset=1)', 'my_clock')
70 if modifiers.pop('is_xtriggered', None):
71 _add_xtrig(schd, itask, 'my_custom()', 'my_custom')
72 itask.state_reset(state, **modifiers)
73 schd.data_store_mgr.delta_task_state(itask)
74 schd.data_store_mgr.increment_graph_window(
75 itask.tokens,
76 cycle,
77 )
78
79
80 async def test_tui_basics(rakiura):
81 """Test basic Tui interaction with no workflows."""
82 with rakiura(size='80,40') as rk:
83 # the app should open
84 rk.compare_screenshot('test-rakiura', 'the app should have loaded')
85
86 # "h" should bring up the onscreen help
87 rk.user_input('h')
88 rk.compare_screenshot(
89 'test-rakiura-help',
90 'the help screen should be visible'
91 )
92
93 # "q" should close the popup
94 rk.user_input('q')
95 rk.compare_screenshot(
96 'test-rakiura',
97 'the help screen should have closed',
98 )
99
100 # "enter" should bring up the context menu
101 rk.user_input('enter')
102 rk.compare_screenshot(
103 'test-rakiura-enter',
104 'the context menu should have opened',
105 )
106
107 # "enter" again should close it via the "cancel" button
108 rk.user_input('enter')
109 rk.compare_screenshot(
110 'test-rakiura',
111 'the context menu should have closed',
112 )
113
114 # "ctrl d" should exit Tui
115 with pytest.raises(urwid.ExitMainLoop):
116 rk.user_input('ctrl d')
117
118 # "q" should exit Tui
119 with pytest.raises(urwid.ExitMainLoop):
120 rk.user_input('q')
121
122
123 async def test_subscribe_unsubscribe(
124 one_conf, flow, scheduler, start, rakiura
125 ):
126 """Test a simple workflow with one task."""
127 id_ = flow(one_conf, name='one')
128 schd = scheduler(id_)
129 async with start(schd):
130 await schd.update_data_structure()
131 with rakiura(size='80,15') as rk:
132 rk.compare_screenshot(
133 'unsubscribed',
134 'the workflow should be collapsed'
135 ' (no subscription no state totals)',
136 )
137
138 # expand the workflow to subscribe to it
139 rk.user_input('down', 'right')
140 rk.wait_until_loaded()
141 rk.compare_screenshot(
142 'subscribed',
143 'the workflow should be expanded',
144 )
145
146 # collapse the workflow to unsubscribe from it
147 rk.user_input('left', 'up')
148 rk.force_update()
149 rk.compare_screenshot(
150 'unsubscribed',
151 'the workflow should be collapsed'
152 ' (no subscription no state totals)',
153 )
154
155
156 async def test_workflow_states(one_conf, flow, scheduler, start, rakiura):
157 """Test viewing multiple workflows in different states."""
158 # one => stopping
159 id_1 = flow(one_conf, name='one')
160 schd_1 = scheduler(id_1)
161 # two => paused
162 id_2 = flow(one_conf, name='two')
163 schd_2 = scheduler(id_2)
164 # tre => stopped
165 flow(one_conf, name='tre')
166
167 async with start(schd_1):
168 schd_1.stop_mode = StopMode.AUTO # make it look like we're stopping
169 await schd_1.update_data_structure()
170
171 async with start(schd_2):
172 await schd_2.update_data_structure()
173 with rakiura(size='80,15') as rk:
174 rk.compare_screenshot(
175 'unfiltered',
176 'All workflows should be visible (one, two, tree)',
177 )
178
179 # filter for active workflows (i.e. paused, running, stopping)
180 rk.user_input('p')
181 rk.compare_screenshot(
182 'filter-active',
183 'Only active workflows should be visible (one, two)'
184 )
185
186 # invert the filter so we are filtering for stopped workflows
187 rk.user_input('W', 'enter', 'q')
188 rk.compare_screenshot(
189 'filter-stopped',
190 'Only stopped workflow should be visible (tre)'
191 )
192
193 # filter in paused workflows
194 rk.user_input('W', 'down', 'enter', 'q')
195 rk.force_update()
196 rk.compare_screenshot(
197 'filter-stopped-or-paused',
198 'Only stopped or paused workflows should be visible'
199 ' (two, tre)',
200 )
201
202 # reset the state filters
203 rk.user_input('W', 'down', 'down', 'enter', 'down', 'enter')
204
205 # scroll to the id filter text box
206 rk.user_input('down', 'down', 'down', 'down')
207
208 # scroll to the end of the ID
209 rk.user_input(*['right'] * (
210 len(schd_1.tokens['workflow'].rsplit('/', 1)[0]) + 1)
211 )
212
213 # type the letter "t"
214 # (this should filter for workflows starting with "t")
215 rk.user_input('t')
216 rk.force_update() # this is required for the tests
217 rk.user_input('page up', 'q') # close the dialogue
218
219 rk.compare_screenshot(
220 'filter-starts-with-t',
221 'Only workflows starting with the letter "t" should be'
222 ' visible (two, tre)',
223 )
224
225
226 async def test_task_states(flow, scheduler, start, rakiura):
227 id_ = flow({
228 'scheduler': {
229 'allow implicit tasks': 'true',
230 },
231 'scheduling': {
232 'initial cycle point': '1',
233 'cycling mode': 'integer',
234 'runahead limit': 'P1',
235 'graph': {
236 'P1': '''
237 a & b & c
238 '''
239 },
240 },
241 'runtime': {
242 'X': {},
243 'Y': {},
244 'Y1': {'inherit': 'Y'},
245 'a': {'inherit': 'X'},
246 'b': {'inherit': 'Y'},
247 'c': {'inherit': 'Y1'},
248 },
249 }, name='test_task_states')
250 schd = scheduler(id_)
251 async with start(schd):
252 set_task_state(
253 schd,
254 [
255 (
256 IntegerPoint('1'),
257 'a',
258 TASK_STATUS_SUCCEEDED,
259 {'is_held': False}
260 ),
261 (
262 IntegerPoint('1'),
263 'b',
264 TASK_STATUS_FAILED,
265 {'is_held': False}
266 ),
267 (
268 IntegerPoint('1'),
269 'c',
270 TASK_STATUS_EXPIRED,
271 {'is_held': False}
272 ),
273 (
274 IntegerPoint('2'),
275 'a',
276 TASK_STATUS_SUBMITTED,
277 {'is_held': False}
278 ),
279 (
280 IntegerPoint('2'),
281 'b',
282 TASK_STATUS_RUNNING,
283 {'is_held': True}
284 ),
285 (
286 IntegerPoint('2'),
287 'c',
288 TASK_STATUS_SUBMIT_FAILED,
289 {'is_held': True}
290 ),
291 ]
292 )
293 await schd.update_data_structure()
294
295 with rakiura(schd.tokens.id, size='80,30') as rk:
296 rk.compare_screenshot(
297 'unfiltered',
298 'all tasks should be displayed'
299 ' (i.e. 1/*, 2/* and 3/* should be displayed)',
300 )
301
302 # filter OUT waiting tasks
303 rk.user_input('T', 'down', 'enter', 'q') # select waiting
304 rk.compare_screenshot(
305 'filter-not-waiting',
306 'waiting tasks should be filtered out'
307 ' (i.e. 1/* and 2/* should be displayed)',
308 )
309
310 # filter OUT waiting & expired tasks
311 rk.user_input('T', 'down', 'down', 'enter', 'q') # select expired
312 rk.compare_screenshot(
313 'filter-not-waiting-or-expired',
314 'waiting & expired tasks should be filtered out'
315 ' (i.e. only 1/a, 1/b and 2/* should be displayed)',
316 )
317
318 # filter FOR waiting & expired tasks
319 rk.user_input('T', 'enter', 'q') # select invert
320 rk.compare_screenshot(
321 'filter-waiting-or-expired',
322 'only waiting and expired tasks should be displayed'
323 ' (i.e. only 1/c and 3/* should be displayed)',
324 )
325
326 # filter FOR submitted tasks (using shortcuts)
327 rk.user_input('R', 's') # reset filters and apply submitted filter
328 rk.compare_screenshot(
329 'filter-submitted',
330 'only submitted tasks should be displayed'
331 ' (i.e. only 2/a should be displayed)',
332 )
333
334
335 async def test_task_modifiers(flow, scheduler, start, rakiura):
336 """It should display task modifiers and text summaries of them."""
337 id_ = flow({
338 'scheduling': {
339 'graph': {
340 'R1': '\n'.join(
341 modifier
342 for modifier, _ in MODIFIER_ATTR_MAPPING.values()
343 ) + '\nall'
344 },
345 },
346 }, name='test_task_modifiers')
347 schd = scheduler(id_)
348 async with start(schd):
349 set_task_state(
350 schd,
351 [
352 *[
353 (
354 IntegerPoint('1'),
355 modifier,
356 TASK_STATUS_WAITING,
357 # NOTE: set is_queued=False as the default because
358 # parentless tasks are autoqueued on startup
359 {**{'is_queued': False}, modifier: True},
360 )
361 for modifier, _ in MODIFIER_ATTR_MAPPING.values()
362 ],
363 (
364 IntegerPoint('1'),
365 'all',
366 TASK_STATUS_WAITING,
367 {
368 modifier: True
369 for modifier, _ in MODIFIER_ATTR_MAPPING.values()
370 },
371 )
372 ]
373 )
374 await schd.update_data_structure()
375
376 with rakiura(schd.tokens.id, size='80,30') as rk:
377 # test modifier icon rendering
378 rk.compare_screenshot(
379 'task-modifiers',
380 'all tasks should be displayed along with their modifiers'
381 )
382
383 # test modifier text summary
384 rk.user_input('down', 'down', 'down', 'enter') # select task "all"
385 rk.compare_screenshot(
386 'task-context-menu',
387 'all modifiers should be listed along with the task state'
388 ' (i.e. the text "held, runahead, queued, retry scheduled,'
389 ' wallclock, xtriggered") should be present'
390 )
391
392
393 async def test_navigation(flow, scheduler, start, rakiura):
394 """Test navigating with the arrow keys."""
395 id_ = flow({
396 'scheduling': {
397 'graph': {
398 'R1': 'A & B1 & B2',
399 }
400 },
401 'runtime': {
402 'A': {},
403 'B': {},
404 'B1': {'inherit': 'B'},
405 'B2': {'inherit': 'B'},
406 'a1': {'inherit': 'A'},
407 'a2': {'inherit': 'A'},
408 'b11': {'inherit': 'B1'},
409 'b12': {'inherit': 'B1'},
410 'b21': {'inherit': 'B2'},
411 'b22': {'inherit': 'B2'},
412 }
413 }, name='one')
414 schd = scheduler(id_)
415 async with start(schd):
416 await schd.update_data_structure()
417
418 with rakiura(size='80,30') as rk:
419 # wait for the workflow to appear (collapsed)
420 rk.wait_until_loaded('#spring')
421
422 rk.compare_screenshot(
423 'on-load',
424 'the workflow should be collapsed when Tui is loaded',
425 )
426
427 # pressing "right" should connect to the workflow
428 # and expand it once the data arrives
429 rk.user_input('down', 'right')
430 rk.wait_until_loaded(schd.tokens.id)
431 rk.compare_screenshot(
432 'workflow-expanded',
433 'the workflow should be expanded',
434 )
435
436 # pressing "left" should collapse the node
437 rk.user_input('down', 'down', 'left')
438 rk.compare_screenshot(
439 'family-A-collapsed',
440 'the family "1/A" should be collapsed',
441 )
442
443 # the "page up" and "page down" buttons should navigate to the top
444 # and bottom of the screen
445 rk.user_input('page down')
446 rk.compare_screenshot(
447 'cursor-at-bottom-of-screen',
448 'the cursor should be at the bottom of the screen',
449 )
450
451
452 async def test_auto_expansion(flow, scheduler, start, rakiura):
453 """It should automatically expand cycles and top-level families.
454
455 When a workflow is expanded, Tui should auto expand cycles and top-level
456 families. Any new cycles and top-level families should be auto-expanded
457 when added.
458 """
459 id_ = flow({
460 'scheduling': {
461 'runahead limit': 'P1',
462 'initial cycle point': '1',
463 'cycling mode': 'integer',
464 'graph': {
465 'P1': 'b[-P1] => a => b'
466 },
467 },
468 'runtime': {
469 'A': {},
470 'a': {'inherit': 'A'},
471 'b': {},
472 },
473 }, name='one')
474 schd = scheduler(id_)
475 with rakiura(size='80,20') as rk:
476 async with start(schd):
477 await schd.update_data_structure()
478 # wait for the workflow to appear (collapsed)
479 rk.wait_until_loaded('#spring')
480
481 # open the workflow
482 rk.force_update()
483 rk.user_input('down', 'right')
484 rk.wait_until_loaded(schd.tokens.id)
485
486 rk.compare_screenshot(
487 'on-load',
488 'cycle "1" and top-level family "1/A" should be expanded',
489 )
490 for task in ('a', 'b'):
491 schd.pool.set_prereqs_and_outputs(
492 items={TaskTokens('1', task)},
493 outputs=[TASK_OUTPUT_SUCCEEDED],
494 prereqs=[],
495 flow=[]
496 )
497
498 await schd._main_loop()
499 await schd.update_data_structure()
500 schd.update_data_store()
501
502 rk.compare_screenshot(
503 'later-time',
504 'cycle "2" and top-level family "2/A" should be expanded',
505 )
506
507
508 async def test_restart_reconnect(one_conf, flow, scheduler, start, rakiura):
509 """It should handle workflow shutdown and restart.
510
511 The Cylc client can raise exceptions e.g. WorkflowStopped. Any text written
512 to stdout/err will mess with Tui. The purpose of this test is to ensure Tui
513 can handle shutdown / restart without any errors occuring and any spurious
514 text appearing on the screen.
515 """
516 with rakiura(size='80,20') as rk:
517 schd = scheduler(flow(one_conf, name='one'))
518
519 # 1- start the workflow
520 async with start(schd):
521 await schd.update_data_structure()
522 # wait for the workflow to appear (collapsed)
523 rk.wait_until_loaded('#spring')
524
525 # expand the workflow (subscribes to updates from it)
526 rk.force_update()
527 rk.user_input('down', 'right')
528
529 # wait for workflow to appear (expanded)
530 rk.wait_until_loaded(schd.tokens.id)
531 rk.compare_screenshot(
532 '1-workflow-running',
533 'the workflow should appear in tui and be expanded',
534 )
535
536 # 2 - stop the worlflow
537 rk.compare_screenshot(
538 '2-workflow-stopped',
539 'the stopped workflow should be collapsed with a message saying'
540 ' workflow stopped',
541 )
542
543 # 3- restart the workflow
544 schd = scheduler(flow(one_conf, name='one'))
545 async with start(schd):
546 await schd._main_loop()
547 await schd.update_data_structure()
548 rk.wait_until_loaded(schd.tokens.id)
549 rk.compare_screenshot(
550 '3-workflow-restarted',
551 'the restarted workflow should be expanded',
552 )
553
554
555 async def test_states(flow, scheduler, start, rakiura):
556 """It should dim no-flow tasks and display state summary in context menus.
557 """
558 id_ = flow(
559 {
560 'scheduling': {
561 'graph': {
562 'R1': 'a & b & c',
563 },
564 },
565 },
566 name='one',
567 )
568 from cylc.flow.scheduler import Scheduler
569 schd: Scheduler = scheduler(id_)
570
571 async with start(schd):
572 a = schd.pool.get_task(IntegerPoint('1'), 'a')
573 b = schd.pool.get_task(IntegerPoint('1'), 'b')
574 c = schd.pool.get_task(IntegerPoint('1'), 'c')
575 assert a and b and c
576
577 # set task flow numbers
578 assert a.flow_nums == {1}
579 b.flow_nums = {1, 2}
580 c.flow_nums = {}
581
582 # set task state
583 a.state_reset(TASK_STATUS_SUCCEEDED, is_held=True)
584 b.state_reset(TASK_STATUS_WAITING, is_queued=True)
585 c.state_reset(TASK_STATUS_WAITING, is_queued=False, is_runahead=True)
586
587 # update data store
588 for task in (a, b, c):
589 schd.data_store_mgr.delta_task_state(task)
590 schd.data_store_mgr.delta_task_flow_nums(task)
591 await schd.update_data_structure()
592
593 with rakiura(schd.tokens.id, size='80,15') as rk:
594 rk.compare_screenshot(
595 'on-load',
596 'the workflow should be expanded,'
597 ' no-flow task 1/c should be dimmed'
598 )
599
600 # workflow node
601 rk.user_input('down', 'enter')
602 rk.compare_screenshot(
603 'workflow-context--paused',
604 'the workflow should show as paused in the context menu',
605 )
606
607 # cycle: 1
608 rk.user_input('q', 'down', 'enter')
609 rk.compare_screenshot(
610 'cycle-context--waiting',
611 'the cycle should show as waiting in the context menu'
612 )
613
614 # task:a
615 rk.user_input('q', 'down', 'enter')
616 rk.compare_screenshot(
617 'task-context--succeeded+held',
618 'the task should show as succeeded+held in the context menu,'
619 ' no flow numbers should be displayed',
620 )
621
622 # task:b
623 rk.user_input('q', 'down', 'enter')
624 rk.compare_screenshot(
625 'task-context--waiting+queued',
626 'the task should show as waiting+queued in the context menu,'
627 ' the flow numbers 1,2 should be displayed',
628 )
629
630 # task:c
631 rk.user_input('q', 'down', 'enter')
632 rk.compare_screenshot(
633 'task-context--waiting+runahead',
634 'the task should show as waiting+runahead in the context menu,'
635 ' the task should be marked as flows=None'
636 )
637
638
639 async def test_incompat_scheduler_version(
640 one_conf,
641 flow,
642 scheduler,
643 start,
644 rakiura
645 ):
646 """It should handle workflows it cannot subscribe to."""
647 schd = scheduler(flow(one_conf, name='one'))
648
649 async with start(schd):
650 # make it look like the scheduler is reallllyyyy old
651 contact = get_contact_file_path(schd.workflow)
652 with open(contact, 'r') as contact_file:
653 contact_lines = contact_file.read().replace(__version__, '6.11.4')
654 with open(contact, 'w') as contact_file:
655 contact_file.write(''.join(contact_lines))
656
657 with rakiura(size='80,20') as rk:
658 await schd.update_data_structure()
659 # wait for the workflow to appear (collapsed)
660 rk.wait_until_loaded('#spring')
661
662 # expand the workflow (subscribes to updates from it)
663 rk.force_update()
664 rk.user_input('down', 'right')
665
666 # it should be marked as incompatible
667 rk.compare_screenshot(
668 'on-load',
669 'the workflow should be marked as incompatible',
670 )