Debian Codegraph @

tests/u/cfgspec/test_globalcfg.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 """Tests for the Cylc GlobalConfig object."""
17
18 from typing import TYPE_CHECKING, Callable
19
20 import pytest
21
22 from cylc.flow.cfgspec.globalcfg import GlobalConfig, SPEC
23 from cylc.flow.parsec.exceptions import ValidationError
24 from cylc.flow.parsec.validate import cylc_config_validate
25
26
27 if TYPE_CHECKING:
28 from pathlib import Path
29
30
31 TEST_CONF = '''
32 [platforms]
33 [[foo]]
34 hosts = of_morgoth
35 [platform groups]
36 [[BAR]]
37 platforms = mario, sonic
38 [task events]
39 # Checking that config items that aren't platforms or platform groups
40 # are not output.
41 '''
42
43
44 @pytest.fixture
45 def mock_global_config(tmp_path: 'Path', monkeypatch: pytest.MonkeyPatch):
46 """Create a mock GlobalConfig object, given the global.cylc contents as
47 a string."""
48 def _mock_global_config(cfg: str) -> GlobalConfig:
49 glblcfg = GlobalConfig(SPEC, validator=cylc_config_validate)
50 conf_path = tmp_path / GlobalConfig.CONF_BASENAME
51 conf_path.write_text(cfg)
52 monkeypatch.setenv("CYLC_CONF_PATH", str(conf_path.parent))
53 glblcfg.loadcfg(conf_path)
54 return glblcfg
55
56 return _mock_global_config
57
58
59 def test_dump_platform_names(capsys, mock_global_config):
60 """It dumps lists of platform names, nothing else."""
61 glblcfg: GlobalConfig = mock_global_config(TEST_CONF)
62 glblcfg.dump_platform_names(glblcfg)
63 stdout, _ = capsys.readouterr()
64 expected = 'localhost\nfoo\nBAR\n'
65 assert stdout == expected
66
67
68 def test_dump_platform_details(capsys, mock_global_config):
69 """It dumps lists of platform spec."""
70 glblcfg: GlobalConfig = mock_global_config(TEST_CONF)
71 glblcfg.dump_platform_details(glblcfg)
72 out, _ = capsys.readouterr()
73 expected = (
74 '[platforms]\n [[foo]]\n hosts = of_morgoth\n'
75 '[platform groups]\n [[BAR]]\n platforms = mario, sonic\n'
76 )
77 assert expected == out
78
79
80 def test_expand_commas(tmp_path: 'Path', mock_global_config: Callable):
81 """It should expand comma separated platform and install target
82 definitions."""
83 glblcfg: GlobalConfig = mock_global_config('''
84 [install]
85 [[symlink dirs]]
86 [[[foo, bar]]]
87 run = /x
88 [[[foo]]]
89 share = /y
90 [[[bar]]]
91 share = /z
92
93 [platforms]
94 [[foo]]
95 [[[meta]]]
96 x = 1
97 [["bar"]] # double quoted name
98 [[[meta]]]
99 x = 2
100 [[baz, bar, pub]] # baz before bar to test order is handled correctly
101 [[[meta]]]
102 x = 3
103 [['pub']] # single quoted name
104 [[[meta]]]
105 x = 4
106 ''')
107 glblcfg._expand_commas()
108
109 # ensure the definition order is preserved
110 assert glblcfg.get(['platforms']).keys() == [
111 'localhost',
112 'foo',
113 'bar',
114 'baz',
115 'pub',
116 ]
117
118 # ensure platform sections are correctly deep-merged
119 assert glblcfg.get(['platforms', 'foo', 'meta', 'x']) == '1'
120 assert glblcfg.get(['platforms', 'bar', 'meta', 'x']) == '3'
121 assert glblcfg.get(['platforms', 'baz', 'meta', 'x']) == '3'
122 assert glblcfg.get(['platforms', 'pub', 'meta', 'x']) == '4'
123
124 # ensure install target sections are correctly merged:
125 assert glblcfg.get(["install", "symlink dirs", "foo", "run"]) == "/x"
126 assert glblcfg.get(["install", "symlink dirs", "foo", "share"]) == "/y"
127 assert glblcfg.get(["install", "symlink dirs", "bar", "run"]) == "/x"
128 assert glblcfg.get(["install", "symlink dirs", "bar", "share"]) == "/z"
129
130
131 @pytest.mark.parametrize(
132 'src_dir, err_expected',
133 [
134 pytest.param(
135 '/theoden/rohan', False,
136 id="Abs path ok"
137 ),
138 pytest.param(
139 'theoden/rohan', True,
140 id="Rel path bad"
141 ),
142 pytest.param(
143 '~theoden/rohan', False,
144 id="Starts with usr - ok"
145 ),
146 pytest.param(
147 '$THEODEN/rohan', False,
148 id="Starts with env var - ok"
149 ),
150 pytest.param(
151 'rohan/$THEODEN', True,
152 id="Rel path with env var not at start - bad"
153 ),
154 ]
155 )
156 def test_source_dir_validation(
157 src_dir: str, err_expected: bool,
158 tmp_path: 'Path', mock_global_config: Callable
159 ):
160 glblcfg: GlobalConfig = mock_global_config(f'''
161 [install]
162 source dirs = /denethor/gondor, {src_dir}
163 ''')
164 if err_expected:
165 with pytest.raises(ValidationError) as excinfo:
166 glblcfg.load()
167 assert "must be an absolute path" in str(excinfo.value)
168 else:
169 glblcfg.load()
170
171
172 def test_platform_ssh_forward_variables(mock_global_config):
173
174 glblcfg: GlobalConfig = mock_global_config('''
175 [platforms]
176 [[foo]]
177 ssh forward environment variables = "FOO", "BAR"
178 ''')
179
180 assert glblcfg.get(
181 ['platforms', 'foo', 'ssh forward environment variables']
182 ) == ["FOO", "BAR"]
183
184
185 def test_reload(
186 mock_global_config, tmp_path: 'Path', monkeypatch: pytest.MonkeyPatch
187 ):
188 # Load a config
189 glblcfg: GlobalConfig = mock_global_config('''
190 [platforms]
191 [[foo]]
192 [[[meta]]]
193 x = 1
194 ''')
195
196 # Update the global config file and reload
197 conf_path = tmp_path / GlobalConfig.CONF_BASENAME
198 conf_path.write_text('''
199 [platforms]
200 [[foo]]
201 [[[meta]]]
202 x = 2
203 ''')
204 glblcfg.load()
205
206 # Mock the global config singleton
207 monkeypatch.setattr(GlobalConfig, "get_inst", lambda *a, **k: glblcfg)
208
209 assert glblcfg.get(['platforms', 'foo', 'meta', 'x']) == '2'
210
211 from cylc.flow.platforms import get_platform
212
213 platform = get_platform("foo")
214
215 assert platform['meta']['x'] == "2"