Debian Codegraph @

tests/u/scripts/test_clean.py

raw
1 #!/usr/bin/env python3
2
3 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
4 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from typing import Callable, List, Type, Union
20
21 import pytest
22
23 from cylc.flow.exceptions import InputError
24 from cylc.flow.scripts.clean import (
25 CleanOptions, _main, parse_timeout, scan, run
26 )
27
28
29 async def test_scan(tmp_run_dir):
30 """It should scan the filesystem to expand partial IDs."""
31 # regular workflows pass straight through
32 tmp_run_dir('foo')
33 workflows, multi_mode = await scan(['foo'], False)
34 assert workflows == ['foo']
35 assert multi_mode is False
36
37 # hierarchies, however, get expanded
38 tmp_run_dir('bar/run1')
39 workflows, multi_mode = await scan(['bar'], False)
40 assert workflows == ['bar/run1']
41 assert multi_mode is True # because an expansion has happened
42
43 tmp_run_dir('bar/run2')
44 workflows, multi_mode = await scan(['bar'], False)
45 assert workflows == ['bar/run1', 'bar/run2']
46 assert multi_mode is True
47
48
49 @pytest.fixture
50 def mute(monkeypatch: pytest.MonkeyPatch) -> List[str]:
51 """Stop cylc clean from doing anything and log all init_clean calls."""
52 items = []
53
54 def _clean(id_, *_):
55 items.append(id_)
56
57 monkeypatch.setattr('cylc.flow.scripts.clean.init_clean', _clean)
58 monkeypatch.setattr('cylc.flow.scripts.clean.prompt', lambda x: None)
59
60 return items
61
62
63 async def test_multi(tmp_run_dir: Callable, mute: List[str]):
64 """It supports cleaning multiple workflows."""
65 # cli opts
66 opts = CleanOptions()
67
68 # create three dummy workflows
69 tmp_run_dir('bar/pub/beer')
70 tmp_run_dir('baz/run1')
71 tmp_run_dir('foo')
72
73 # an explicit workflow ID goes straight through
74 mute.clear()
75 await run('foo', opts=opts)
76 assert mute == ['foo']
77
78 # a partial hierarchical ID gets expanded to all workflows contained
79 # in the hierarchy (note runs are a special case of hierarchical ID)
80 mute.clear()
81 await run('bar', opts=opts)
82 assert mute == ['bar/pub/beer']
83
84 # test a mixture of explicit and partial IDs
85 mute.clear()
86 await run('bar', 'baz', 'foo', opts=opts)
87 assert mute == ['bar/pub/beer', 'baz/run1', 'foo']
88
89 # test a glob
90 mute.clear()
91 await run('*', opts=opts)
92 assert mute == ['bar/pub/beer', 'baz/run1', 'foo']
93
94
95 @pytest.mark.parametrize(
96 'timeout, expected',
97 [('100', '100'),
98 ('PT1M2S', '62'),
99 ('', ''),
100 ('oopsie', InputError),
101 (' ', InputError)]
102 )
103 def test_parse_timeout(
104 timeout: str,
105 expected: Union[str, Type[InputError]]
106 ):
107 """It should accept ISO 8601 format or number of seconds."""
108 opts = CleanOptions(remote_timeout=timeout)
109
110 if expected is InputError:
111 with pytest.raises(expected):
112 parse_timeout(opts)
113 else:
114 parse_timeout(opts)
115 assert opts.remote_timeout == expected
116
117
118 @pytest.mark.parametrize(
119 'opts, expected_msg',
120 [
121 ({'local_only': True, 'remote_only': True}, "mutually exclusive"),
122 ({'remote_timeout': 'oops'}, "Invalid timeout"),
123 ]
124 )
125 def test_bad_user_input(opts: dict, expected_msg: str, mute):
126 """It should raise an InputError for bad user input."""
127 with pytest.raises(InputError) as exc_info:
128 _main(CleanOptions(**opts), 'blah')
129 assert expected_msg in str(exc_info.value)