File: test_show.py

package info (click to toggle)
cylc-flow 8.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,368 kB
  • sloc: python: 87,751; sh: 17,109; sql: 233; xml: 171; javascript: 78; lisp: 55; makefile: 11
file content (330 lines) | stat: -rw-r--r-- 8,903 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json
import pytest
import re
from types import SimpleNamespace

from colorama import init as colour_init

from cylc.flow.id import Tokens
from cylc.flow.scripts.show import (
    ShowOptions,
    show,
)


RE_STATE = re.compile('state:.*')


@pytest.fixture(scope='module')
def mod_my_conf():
    """A workflow configuration with some workflow metadata."""
    return {
        'meta': {
            'title': 'Workflow Title',
            'description': """
                My
                multiline
                description.
            """,
            'URL': 'http://ismycomputerturnedon.com/',
            'answer': '42',
        },
        'scheduling': {
            'graph': {
                'R1': 'foo'
            }
        },
        'runtime': {
            'foo': {
                'meta': {
                    'title': 'Task Title',
                    'description': '''
                        Task
                        multiline
                        description
                    ''',
                    'URL': (
                        'http://hasthelargehadroncollider'
                        'destroyedtheworldyet.com/'
                    ),
                    'question': 'mutually exclusive',
                },
            },
        },
    }


@pytest.fixture(scope='module')
async def mod_my_schd(mod_flow, mod_scheduler, mod_start, mod_my_conf):
    """A "started" workflow."""
    id_ = mod_flow(mod_my_conf)
    schd = mod_scheduler(id_)
    async with mod_start(schd):
        yield schd


async def test_workflow_meta_query(mod_my_schd, capsys):
    """It should fetch workflow metadata."""
    colour_init(strip=True, autoreset=True)
    opts = SimpleNamespace(
        comms_timeout=5,
        json=False,
        list_prereqs=False,
        task_defs=None,
    )

    # plain output
    ret = await show(mod_my_schd.workflow, [], opts)
    assert ret == 0
    out, err = capsys.readouterr()
    assert out.splitlines() == [
        'title: Workflow Title',
        'description: My',
        'multiline',
        'description.',
        'answer: 42',
        'URL: http://ismycomputerturnedon.com/',
    ]

    # json output
    opts.json = True
    ret = await show(mod_my_schd.workflow, [], opts)
    assert ret == 0
    out, err = capsys.readouterr()
    assert json.loads(out) == {
        'title': 'Workflow Title',
        'description': 'My\nmultiline\ndescription.',
        'answer': '42',
        'URL': 'http://ismycomputerturnedon.com/',
    }


async def test_task_meta_query(mod_my_schd, capsys):
    """It should fetch task metadata."""
    colour_init(strip=True, autoreset=True)
    opts = SimpleNamespace(
        comms_timeout=5,
        json=False,
        list_prereqs=False,
        task_defs=['foo'],
    )

    # plain output
    ret = await show(
        mod_my_schd.workflow,
        None,
        opts,
    )
    assert ret == 0
    out, err = capsys.readouterr()

    assert out.splitlines() == [
        'title: Task Title',
        'question: mutually exclusive',
        'description: Task',
        'multiline',
        'description',
        'URL: http://hasthelargehadroncolliderdestroyedtheworldyet.com/',
    ]

    # json output
    opts.json = True
    ret = await show(mod_my_schd.workflow, [], opts)
    assert ret == 0
    out, err = capsys.readouterr()
    assert json.loads(out) == {
        'foo': {
            'title': 'Task Title',
            'question': 'mutually exclusive',
            'description': 'Task\nmultiline\ndescription',
            'URL': 'http://hasthelargehadroncolliderdestroyedtheworldyet.com/',
        }
    }


async def test_task_instance_query(
    flow, scheduler, start, capsys
):
    """It should fetch task instance data, sorted by task name."""

    colour_init(strip=True, autoreset=True)
    opts = SimpleNamespace(
        comms_timeout=5,
        json=False,
        task_defs=None,
        list_prereqs=False,
    )
    schd = scheduler(
        flow(
            {
                'scheduling': {
                    'graph': {'R1': 'zed & dog & cat & ant'},
                },
            },
        ),
        paused_start=False,
    )
    async with start(schd):
        await schd.update_data_structure()
        ret = await show(
            schd.workflow,
            [Tokens('//1/*')],
            opts,
        )
        assert ret == 0

    out, _ = capsys.readouterr()
    assert [
        line for line in out.splitlines()
        if line.startswith("Task ID")
    ] == [  # results should be sorted
        'Task ID: 1/ant',
        'Task ID: 1/cat',
        'Task ID: 1/dog',
        'Task ID: 1/zed',
    ]


@pytest.mark.parametrize(
    'workflow_run_mode, run_mode_info',
    (
        ('live', 'Skip'),
        ('dummy', 'Dummy'),
        ('simulation', 'Simulation'),
    )
)
@pytest.mark.parametrize(
    'attributes_bool, flow_nums, expected_state, expected_flows',
    [
        pytest.param(
            False, [1], 'state: waiting (run mode={})', None,
        ),
        pytest.param(
            True,
            [1, 2],
            'state: waiting (held,queued,runahead,run mode={})',
            'flows: [1,2]',
        )
    ]
)
async def test_task_instance_state_flows(
    flow, scheduler, start, capsys,
    workflow_run_mode, run_mode_info,
    attributes_bool, flow_nums, expected_state, expected_flows
):
    """It should print task instance state, attributes, and flows."""

    colour_init(strip=True, autoreset=True)
    opts = SimpleNamespace(
        comms_timeout=5,
        json=False,
        task_defs=None,
        list_prereqs=False,
    )
    schd = scheduler(
        flow(
            {
                'scheduling': {
                    'graph': {'R1': 'a'},
                },
                'runtime': {
                    'a': {'run mode': 'skip'}
                }
            },
        ),
        paused_start=True,
        run_mode=workflow_run_mode,
    )
    async with start(schd):

        [itask] = schd.pool.get_tasks()
        itask.state_reset(
            is_held=attributes_bool,
            is_queued=attributes_bool,
            is_runahead=attributes_bool
        )
        itask.flow_nums = set(flow_nums)

        schd.pool.data_store_mgr.delta_task_held(
            itask.tdef.name, itask.point, itask.state.is_held)
        schd.pool.data_store_mgr.delta_task_state(itask)
        schd.pool.data_store_mgr.delta_task_flow_nums(itask)
        await schd.update_data_structure()

        ret = await show(
            schd.workflow,
            [Tokens('//1/*')],
            opts,
        )
        assert ret == 0

    out, _ = capsys.readouterr()
    assert [
        line for line in out.splitlines()
        if line.startswith("state:")
    ] == [
        expected_state.format(run_mode_info),
    ]

    if expected_flows is not None:
        assert [
            line for line in out.splitlines()
            if line.startswith("flows:")
        ] == [
            expected_flows,
        ]


async def test_task_run_mode_changes(flow, scheduler, start, capsys):
    """Broadcasting a change of run mode changes run mode shown by cylc show.
    """
    opts = ShowOptions()
    schd = scheduler(
        flow({'scheduling': {'graph': {'R1': 'a'}}}),
        run_mode='live'
    )

    async with start(schd):
        # Control: No mode set, the Run Mode setting is not shown:
        await schd.update_data_structure()
        ret = await show(
            schd.workflow,
            [Tokens('//1/a')],
            opts,
        )
        assert ret == 0
        out, _ = capsys.readouterr()
        state, = RE_STATE.findall(out)
        assert 'waiting' in state

        # Broadcast change task to skip mode:
        schd.broadcast_mgr.put_broadcast(['1'], ['a'], [{'run mode': 'skip'}])
        await schd.update_data_structure()

        # show now shows skip mode:
        ret = await show(
            schd.workflow,
            [Tokens('//1/a')],
            opts,
        )
        assert ret == 0

        out, _ = capsys.readouterr()
        state, = RE_STATE.findall(out)
        assert 'run mode=Skip' in state