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
|
# 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 asyncio
import os
from pathlib import Path
from textwrap import dedent
from typing import Any, Optional, List
import pytest
from cylc.flow.cfgspec.globalcfg import GlobalConfig
from cylc.flow.option_parsers import Options
from cylc.flow.scripts.config import (
_main,
get_config_file_hierarchy,
get_option_parser,
)
from cylc.flow.workflow_files import WorkflowFiles
Fixture = Any
HOME: str = str(Path('~').expanduser())
@pytest.fixture
def conf_env(monkeypatch):
"""Clear any env vars that affect which conf files get loaded.
Return a convenience function for setting environment variables.
"""
# wipe any cached config
monkeypatch.setattr(
GlobalConfig,
'_DEFAULT',
None,
)
for envvar in ('CYLC_SITE_CONF_PATH', 'CYLC_CONF_PATH'):
if envvar in os.environ:
monkeypatch.delenv(envvar)
def _set_env(key, value):
if value:
monkeypatch.setenv(key, value)
return _set_env
@pytest.fixture
def dummy_version_hierarchy(monkeypatch):
"""Set the config version hierarchy."""
monkeypatch.setattr(
'cylc.flow.cfgspec.globalcfg.GlobalConfig.VERSION_HIERARCHY',
['', '1', '1.0']
)
@pytest.fixture
def capload(monkeypatch):
"""Capture configuration load events.
This prevents actual file loading.
If the file name contains the string "invalid" it will not appear in the
results as if it diddn't exist on the filesystem.
"""
files = []
def _capload(glblcfg, fname, _):
if 'invalid' not in fname:
# if the file is called invalid skip it
# this is to replicate the behaviour of skipping files that
# don't exist
files.append(fname.replace(HOME, '~'))
monkeypatch.setattr(
GlobalConfig,
'_load',
_capload
)
return files
def test_get_config_file_hierarchy_global(
monkeypatch: Fixture,
conf_env: Fixture,
capload: Fixture,
dummy_version_hierarchy: Fixture
):
"""Test get_config_file_hierarchy() for the global hierarchy only."""
assert [
path.replace(HOME, '~')
for path in get_config_file_hierarchy()
] == [
'/etc/cylc/flow/global.cylc',
'/etc/cylc/flow/1/global.cylc',
'/etc/cylc/flow/1.0/global.cylc',
'~/.cylc/flow/global.cylc',
'~/.cylc/flow/1/global.cylc',
'~/.cylc/flow/1.0/global.cylc'
]
@pytest.mark.parametrize(
'conf_path,site_conf_path,files',
[
pytest.param(
None,
None,
[
'/etc/cylc/flow/global.cylc',
'/etc/cylc/flow/1/global.cylc',
'/etc/cylc/flow/1.0/global.cylc',
'~/.cylc/flow/global.cylc',
'~/.cylc/flow/1/global.cylc',
'~/.cylc/flow/1.0/global.cylc',
],
id='(default)'
),
pytest.param(
None,
'<path>',
[
'<path>/flow/global.cylc',
'<path>/flow/1/global.cylc',
'<path>/flow/1.0/global.cylc',
'~/.cylc/flow/global.cylc',
'~/.cylc/flow/1/global.cylc',
'~/.cylc/flow/1.0/global.cylc',
],
id='CYLC_SITE_CONF_PATH=valid'
),
pytest.param(
None,
'invalid',
[
'~/.cylc/flow/global.cylc',
'~/.cylc/flow/1/global.cylc',
'~/.cylc/flow/1.0/global.cylc',
],
id='CYLC_SITE_CONF_PATH=invalid'
),
pytest.param(
'<path>',
None,
['<path>/global.cylc'],
id='CYLC_CONF_PATH=valid'
),
pytest.param(
'invalid',
None,
[],
id='CYLC_CONF_PATH=invalid'
),
pytest.param(
'<path1>',
'<path2>',
['<path1>/global.cylc'], # should ignore CYLC_SITE_CONF_PATH
id='CYLC_CONF_PATH=valid, CYLC_SITE_CONF_PATH=valid'
),
pytest.param(
'invalid',
'<path>',
[],
id='CYLC_CONF_PATH=invalid, CYLC_SITE_CONF_PATH=valid'
),
]
)
def test_cylc_site_conf_path_env_var(
monkeypatch: Fixture,
conf_env: Fixture,
capload: Fixture,
dummy_version_hierarchy: Fixture,
conf_path: Optional[str],
site_conf_path: Optional[str],
files: List[str],
):
"""Test that the right files are loaded according to env vars."""
# set the relevant environment variables
conf_env('CYLC_CONF_PATH', conf_path)
conf_env('CYLC_SITE_CONF_PATH', site_conf_path)
# load the global config
GlobalConfig.get_inst()
assert capload == files
def test_cylc_config_xtriggers(tmp_run_dir, capsys: pytest.CaptureFixture):
"""Test `cylc config` outputs any xtriggers properly"""
run_dir: Path = tmp_run_dir('constellation')
flow_file = run_dir / WorkflowFiles.FLOW_FILE
flow_file.write_text(dedent("""
[scheduler]
allow implicit tasks = True
[scheduling]
initial cycle point = 2020-05-05
[[xtriggers]]
clock_1 = wall_clock(offset=PT1H):PT4S
rotund = xrandom(90, 2)
[[graph]]
R1 = @rotund => foo
"""))
option_parser = get_option_parser()
asyncio.run(
_main(option_parser, Options(option_parser)(), 'constellation')
)
assert capsys.readouterr().out == dedent("""\
[scheduler]
allow implicit tasks = True
[scheduling]
initial cycle point = 2020-05-05
[[xtriggers]]
clock_1 = wall_clock(offset=PT1H):4.0
rotund = xrandom(90, 2):10.0
[[graph]]
R1 = @rotund => foo
[runtime]
[[root]]
[[foo]]
completion = succeeded
""")
|