Debian Codegraph @

tests/unit/scripts/test_config.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import asyncio
18 import os
19 from pathlib import Path
20 from textwrap import dedent
21 from typing import Any, Optional, List
22
23 import pytest
24
25 from cylc.flow.cfgspec.globalcfg import GlobalConfig
26 from cylc.flow.option_parsers import Options
27 from cylc.flow.scripts.config import (
28 _main,
29 get_config_file_hierarchy,
30 get_option_parser,
31 )
32 from cylc.flow.workflow_files import WorkflowFiles
33
34
35 Fixture = Any
36 HOME: str = str(Path('~').expanduser())
37
38
39 @pytest.fixture
40 def conf_env(monkeypatch):
41 """Clear any env vars that affect which conf files get loaded.
42
43 Return a convenience function for setting environment variables.
44 """
45 # wipe any cached config
46 monkeypatch.setattr(
47 GlobalConfig,
48 '_DEFAULT',
49 None,
50 )
51
52 for envvar in ('CYLC_SITE_CONF_PATH', 'CYLC_CONF_PATH'):
53 if envvar in os.environ:
54 monkeypatch.delenv(envvar)
55
56 def _set_env(key, value):
57 if value:
58 monkeypatch.setenv(key, value)
59
60 return _set_env
61
62
63 @pytest.fixture
64 def dummy_version_hierarchy(monkeypatch):
65 """Set the config version hierarchy."""
66 monkeypatch.setattr(
67 'cylc.flow.cfgspec.globalcfg.GlobalConfig.VERSION_HIERARCHY',
68 ['', '1', '1.0']
69 )
70
71
72 @pytest.fixture
73 def capload(monkeypatch):
74 """Capture configuration load events.
75
76 This prevents actual file loading.
77
78 If the file name contains the string "invalid" it will not appear in the
79 results as if it diddn't exist on the filesystem.
80 """
81 files = []
82
83 def _capload(glblcfg, fname, _):
84 if 'invalid' not in fname:
85 # if the file is called invalid skip it
86 # this is to replicate the behaviour of skipping files that
87 # don't exist
88 files.append(fname.replace(HOME, '~'))
89
90 monkeypatch.setattr(
91 GlobalConfig,
92 '_load',
93 _capload
94 )
95 return files
96
97
98 def test_get_config_file_hierarchy_global(
99 monkeypatch: Fixture,
100 conf_env: Fixture,
101 capload: Fixture,
102 dummy_version_hierarchy: Fixture
103 ):
104 """Test get_config_file_hierarchy() for the global hierarchy only."""
105 assert [
106 path.replace(HOME, '~')
107 for path in get_config_file_hierarchy()
108 ] == [
109 '/etc/cylc/flow/global.cylc',
110 '/etc/cylc/flow/1/global.cylc',
111 '/etc/cylc/flow/1.0/global.cylc',
112 '~/.cylc/flow/global.cylc',
113 '~/.cylc/flow/1/global.cylc',
114 '~/.cylc/flow/1.0/global.cylc'
115 ]
116
117
118 @pytest.mark.parametrize(
119 'conf_path,site_conf_path,files',
120 [
121 pytest.param(
122 None,
123 None,
124 [
125 '/etc/cylc/flow/global.cylc',
126 '/etc/cylc/flow/1/global.cylc',
127 '/etc/cylc/flow/1.0/global.cylc',
128 '~/.cylc/flow/global.cylc',
129 '~/.cylc/flow/1/global.cylc',
130 '~/.cylc/flow/1.0/global.cylc',
131 ],
132 id='(default)'
133 ),
134
135 pytest.param(
136 None,
137 '<path>',
138 [
139 '<path>/flow/global.cylc',
140 '<path>/flow/1/global.cylc',
141 '<path>/flow/1.0/global.cylc',
142 '~/.cylc/flow/global.cylc',
143 '~/.cylc/flow/1/global.cylc',
144 '~/.cylc/flow/1.0/global.cylc',
145 ],
146 id='CYLC_SITE_CONF_PATH=valid'
147 ),
148
149 pytest.param(
150 None,
151 'invalid',
152 [
153 '~/.cylc/flow/global.cylc',
154 '~/.cylc/flow/1/global.cylc',
155 '~/.cylc/flow/1.0/global.cylc',
156 ],
157 id='CYLC_SITE_CONF_PATH=invalid'
158 ),
159
160 pytest.param(
161 '<path>',
162 None,
163 ['<path>/global.cylc'],
164 id='CYLC_CONF_PATH=valid'
165 ),
166
167 pytest.param(
168 'invalid',
169 None,
170 [],
171 id='CYLC_CONF_PATH=invalid'
172 ),
173
174 pytest.param(
175 '<path1>',
176 '<path2>',
177 ['<path1>/global.cylc'], # should ignore CYLC_SITE_CONF_PATH
178 id='CYLC_CONF_PATH=valid, CYLC_SITE_CONF_PATH=valid'
179 ),
180
181 pytest.param(
182 'invalid',
183 '<path>',
184 [],
185 id='CYLC_CONF_PATH=invalid, CYLC_SITE_CONF_PATH=valid'
186 ),
187 ]
188 )
189 def test_cylc_site_conf_path_env_var(
190 monkeypatch: Fixture,
191 conf_env: Fixture,
192 capload: Fixture,
193 dummy_version_hierarchy: Fixture,
194 conf_path: Optional[str],
195 site_conf_path: Optional[str],
196 files: List[str],
197 ):
198 """Test that the right files are loaded according to env vars."""
199 # set the relevant environment variables
200 conf_env('CYLC_CONF_PATH', conf_path)
201 conf_env('CYLC_SITE_CONF_PATH', site_conf_path)
202
203 # load the global config
204 GlobalConfig.get_inst()
205
206 assert capload == files
207
208
209 def test_cylc_config_xtriggers(tmp_run_dir, capsys: pytest.CaptureFixture):
210 """Test `cylc config` outputs any xtriggers properly"""
211 run_dir: Path = tmp_run_dir('constellation')
212 flow_file = run_dir / WorkflowFiles.FLOW_FILE
213 flow_file.write_text(dedent("""
214 [scheduler]
215 allow implicit tasks = True
216 [scheduling]
217 initial cycle point = 2020-05-05
218 [[xtriggers]]
219 clock_1 = wall_clock(offset=PT1H):PT4S
220 rotund = xrandom(90, 2)
221 [[graph]]
222 R1 = @rotund => foo
223 """))
224 option_parser = get_option_parser()
225
226 asyncio.run(
227 _main(option_parser, Options(option_parser)(), 'constellation')
228 )
229 assert capsys.readouterr().out == dedent("""\
230 [scheduler]
231 allow implicit tasks = True
232 [scheduling]
233 initial cycle point = 2020-05-05
234 [[xtriggers]]
235 clock_1 = wall_clock(offset=PT1H):4.0
236 rotund = xrandom(90, 2):10.0
237 [[graph]]
238 R1 = @rotund => foo
239 [runtime]
240 [[root]]
241 [[foo]]
242 completion = succeeded
243 """)