Debian Codegraph @

tests/i/tui/test_updater.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 from copy import deepcopy
20 from pathlib import Path
21 from queue import Queue
22 import re
23 from time import time
24
25 import pytest
26
27 from cylc.flow.cycling.integer import IntegerPoint
28 from cylc.flow.id import Tokens
29 from cylc.flow.tui.updater import (
30 Updater,
31 get_default_filters,
32 )
33 from cylc.flow.workflow_status import WorkflowStatus
34
35
36 @pytest.fixture
37 def updater(monkeypatch, test_dir):
38 """Return an updater ready for testing."""
39 # patch the update intervals so that everything runs for every update
40 monkeypatch.setattr(
41 'cylc.flow.tui.updater.Updater.BASE_UPDATE_INTERVAL',
42 0,
43 )
44 monkeypatch.setattr(
45 'cylc.flow.tui.updater.Updater.BASE_SCAN_INTERVAL',
46 0,
47 )
48
49 # create the updater
50 updater = Updater()
51
52 # swap multiprocessing.Queue for queue.Queue
53 # (this means queued operations are instant making tests more stable)
54 updater.update_queue = Queue()
55 updater._command_queue = Queue()
56
57 # set up the filters
58 # (these filter for the workflows created in this test only)
59 filters = get_default_filters()
60 id_base = str(test_dir.relative_to(Path("~/cylc-run").expanduser()))
61 filters['workflows']['id'] = f'^{re.escape(id_base)}/.*'
62 updater._update_filters(filters)
63
64 return updater
65
66
67 def get_child_tokens(root_node, types, relative=False):
68 """Return all ID of the specified types contained within the provided tree.
69
70 Args:
71 root_node:
72 The Tui tree you want to look for IDs in.
73 types:
74 The Tui types (e.g. 'workflow' or 'task') you want to extract.
75 relative:
76 If True, the relative IDs will be returned.
77
78 """
79 ret = set()
80 stack = [root_node]
81 while stack:
82 node = stack.pop()
83 stack.extend(node['children'])
84 if node['type_'] in types:
85
86 tokens = Tokens(node['id_'])
87 if relative:
88 ret.add(tokens.relative_id)
89 else:
90 ret.add(tokens.id)
91 return ret
92
93
94 async def test_subscribe(one_conf, flow, scheduler, run, updater):
95 """It should subscribe and unsubscribe from workflows."""
96 id_ = flow(one_conf)
97 schd = scheduler(id_)
98
99 async with run(schd):
100 # run the updater and the test
101 async with asyncio.timeout(10):
102 # wait for the first update
103 root_node = await updater._update()
104
105 # there should be a root root_node
106 assert root_node['id_'] == 'root'
107 # a single root_node representing the workflow
108 assert root_node['children'][0]['id_'] == schd.tokens.id
109 # and a "spring" root_node used to active the subscription
110 # mechanism
111 assert root_node['children'][0]['children'][0]['id_'] == '#spring'
112
113 # subscribe to the workflow
114 updater.subscribe(schd.tokens.id)
115 root_node = await updater._update()
116
117 # check the workflow contains one cycle with one task in it
118 workflow_node = root_node['children'][0]
119 assert len(workflow_node['children']) == 1
120 cycle_node = workflow_node['children'][0]
121 assert Tokens(cycle_node['id_']).relative_id == '1' # cycle ID
122 assert len(cycle_node['children']) == 1
123 task_node = cycle_node['children'][0]
124 assert Tokens(task_node['id_']).relative_id == '1/one' # task ID
125
126 # unsubscribe from the workflow
127 updater.unsubscribe(schd.tokens.id)
128 root_node = await updater._update()
129
130 # the workflow should be replaced by a "spring" node again
131 assert root_node['children'][0]['children'][0]['id_'] == '#spring'
132
133
134 async def test_filters(one_conf, flow, scheduler, run, updater):
135 """It should filter workflow and task states.
136
137 Note:
138 The workflow ID filter is not explicitly tested here, but it is
139 indirectly tested, otherwise other workflows would show up in the
140 updater results.
141
142 """
143 one = scheduler(flow({
144 'scheduler': {
145 'allow implicit tasks': 'True',
146 },
147 'scheduling': {
148 'graph': {
149 'R1': 'a & b & c',
150 }
151 },
152 'runtime': {
153 # TODO: remove this runtime section in
154 # https://github.com/cylc/cylc-flow/pull/5721
155 'root': {
156 'simulation': {
157 'default run length': 'PT1M',
158 },
159 },
160 },
161 }, name='one'), paused_start=True)
162 two = scheduler(flow(one_conf, name='two'))
163 tre = scheduler(flow(one_conf, name='tre'))
164
165 # start workflow "one"
166 async with run(one):
167 # mark "1/a" as running and "1/b" as succeeded
168 one_a = one.pool.get_task(IntegerPoint('1'), 'a')
169 one_a.summary['started_time'] = time()
170 one_a.state_reset('running')
171 one.data_store_mgr.delta_task_state(one_a)
172 one.pool.get_task(IntegerPoint('1'), 'b').state_reset('succeeded')
173
174 # start workflow "two"
175 async with run(two):
176 # run the updater and the test
177 filters = deepcopy(updater.filters)
178
179 root_node = await updater._update()
180 assert {child['id_'] for child in root_node['children']} == {
181 one.tokens.id,
182 two.tokens.id,
183 tre.tokens.id,
184 }
185
186 # filter out paused workflows
187 filters = deepcopy(filters)
188 filters['workflows'][WorkflowStatus.STOPPED.value] = True
189 filters['workflows'][WorkflowStatus.PAUSED.value] = False
190 updater.update_filters(filters)
191
192 # "one" and "two" should now be filtered out
193 root_node = await updater._update()
194 assert {child['id_'] for child in root_node['children']} == {
195 tre.tokens.id,
196 }
197
198 # filter out stopped workflows
199 filters = deepcopy(filters)
200 filters['workflows'][WorkflowStatus.STOPPED.value] = False
201 filters['workflows'][WorkflowStatus.PAUSED.value] = True
202 updater.update_filters(filters)
203
204 # "tre" should now be filtered out
205 root_node = await updater._update()
206 assert {child['id_'] for child in root_node['children']} == {
207 one.tokens.id,
208 two.tokens.id,
209 }
210
211 # subscribe to "one"
212 updater._subscribe(one.tokens.id)
213 root_node = await updater._update()
214 assert get_child_tokens(
215 root_node, types={'task'}, relative=True
216 ) == {
217 '1/a',
218 '1/b',
219 '1/c',
220 }
221
222 # filter out running tasks
223 # TODO: see https://github.com/cylc/cylc-flow/issues/5716
224 # filters = deepcopy(filters)
225 # filters['tasks'][TASK_STATUS_RUNNING] = False
226 # updater.update_filters(filters)
227
228 # root_node = await updater._update()
229 # assert get_child_tokens(
230 # root_node,
231 # types={'task'},
232 # relative=True
233 # ) == {
234 # '1/b',
235 # '1/c',
236 # }