| 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 |
# Tests for the platform lookup module's get_platform method. |
| 18 |
|
| 19 |
from typing import Callable, Dict, Optional |
| 20 |
import pytest |
| 21 |
from cylc.flow.platforms import ( |
| 22 |
get_localhost_install_target, |
| 23 |
get_platform |
| 24 |
) |
| 25 |
from cylc.flow.exceptions import PlatformLookupError |
| 26 |
|
| 27 |
|
| 28 |
def test_get_platform_no_args(): |
| 29 |
# If no task conf is given, we get localhost args. |
| 30 |
assert get_platform()['hosts'] == ['localhost'] |
| 31 |
|
| 32 |
|
| 33 |
@pytest.mark.parametrize( |
| 34 |
'platform_re', |
| 35 |
[ |
| 36 |
None, |
| 37 |
'localhost', |
| 38 |
'localhost, otherplatform', |
| 39 |
'otherplatform, localhost', |
| 40 |
'localhost, xylophone\\d{1,5}' |
| 41 |
] |
| 42 |
) |
| 43 |
def test_get_localhost_platform(mock_glbl_cfg, platform_re): |
| 44 |
# Check that an arbitrary string name returns a sensible platform |
| 45 |
mock_glbl_cfg( |
| 46 |
'cylc.flow.platforms.glbl_cfg', |
| 47 |
f''' |
| 48 |
[platforms] |
| 49 |
[[localhost]] |
| 50 |
hosts = localhost |
| 51 |
ssh command = ssh -oConnectTimeout=42 |
| 52 |
[[{platform_re}]] |
| 53 |
hosts = localhost |
| 54 |
ssh command = ssh -oConnectTimeout=24 |
| 55 |
''' |
| 56 |
) |
| 57 |
platform = get_platform('localhost') |
| 58 |
if platform_re: |
| 59 |
assert platform['ssh command'] == 'ssh -oConnectTimeout=24' |
| 60 |
else: |
| 61 |
assert platform['ssh command'] == 'ssh -oConnectTimeout=42' |
| 62 |
|
| 63 |
|
| 64 |
@pytest.mark.parametrize( |
| 65 |
'platform_re', |
| 66 |
[ |
| 67 |
'saffron', |
| 68 |
'sumac|saffron', |
| 69 |
'sumac, saffron', |
| 70 |
'sumac|asafoetida, saffron', |
| 71 |
] |
| 72 |
) |
| 73 |
def test_get_platform_from_platform_name_str(mock_glbl_cfg, platform_re): |
| 74 |
# Check that an arbitrary string name returns a sensible platform |
| 75 |
mock_glbl_cfg( |
| 76 |
'cylc.flow.platforms.glbl_cfg', |
| 77 |
f''' |
| 78 |
[platforms] |
| 79 |
[[{platform_re}]] |
| 80 |
hosts = saff01 |
| 81 |
job runner = slurm |
| 82 |
''' |
| 83 |
) |
| 84 |
platform = get_platform('saffron') |
| 85 |
assert platform['hosts'] == ['saff01'] |
| 86 |
assert platform['job runner'] == 'slurm' |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize( |
| 90 |
'task_conf, err_expected', |
| 91 |
[ |
| 92 |
( |
| 93 |
{ |
| 94 |
'platform': 'localhost', |
| 95 |
'remote': { |
| 96 |
'host': 'localhost' |
| 97 |
} |
| 98 |
}, |
| 99 |
True |
| 100 |
), |
| 101 |
( |
| 102 |
{ |
| 103 |
'platform': 'gondor', |
| 104 |
'remote': { |
| 105 |
'retrieve job logs': False |
| 106 |
} |
| 107 |
}, |
| 108 |
True |
| 109 |
), |
| 110 |
( |
| 111 |
{ |
| 112 |
'platform': 'gondor', |
| 113 |
'remote': { |
| 114 |
'host': None |
| 115 |
} |
| 116 |
}, |
| 117 |
False |
| 118 |
), |
| 119 |
] |
| 120 |
) |
| 121 |
def test_get_platform_cylc7_8_syntax_mix_fails( |
| 122 |
task_conf: dict, err_expected: bool, mock_glbl_cfg: Callable |
| 123 |
): |
| 124 |
"""If a task with a mix of Cylc7 and 8 syntax is passed to get_platform |
| 125 |
this should return an error. |
| 126 |
""" |
| 127 |
mock_glbl_cfg( |
| 128 |
'cylc.flow.platforms.glbl_cfg', |
| 129 |
''' |
| 130 |
[platforms] |
| 131 |
[[gondor]] |
| 132 |
hosts = denethor |
| 133 |
''' |
| 134 |
) |
| 135 |
if err_expected: |
| 136 |
with pytest.raises( |
| 137 |
PlatformLookupError, |
| 138 |
match=( |
| 139 |
r"Task .* has the following deprecated '\[runtime\]' " |
| 140 |
r"setting\(s\) which cannot be used with 'platform.*" |
| 141 |
) |
| 142 |
): |
| 143 |
get_platform(task_conf) |
| 144 |
else: |
| 145 |
get_platform(task_conf) |
| 146 |
|
| 147 |
|
| 148 |
def test_get_platform_from_config_with_platform_name(mock_glbl_cfg): |
| 149 |
# A platform name is present, and no clashing cylc7 configs are: |
| 150 |
mock_glbl_cfg( |
| 151 |
'cylc.flow.platforms.glbl_cfg', |
| 152 |
''' |
| 153 |
[platforms] |
| 154 |
[[mace]] |
| 155 |
hosts = mace001, mace002 |
| 156 |
job runner = slurm |
| 157 |
''' |
| 158 |
) |
| 159 |
task_conf = {'platform': 'mace'} |
| 160 |
platform = get_platform(task_conf) |
| 161 |
assert platform['hosts'] == ['mace001', 'mace002'] |
| 162 |
assert platform['job runner'] == 'slurm' |
| 163 |
|
| 164 |
|
| 165 |
@pytest.mark.parametrize( |
| 166 |
'task_conf, expected_platform_name', |
| 167 |
[ |
| 168 |
( |
| 169 |
{ |
| 170 |
'remote': {'host': 'cumin'}, |
| 171 |
'job': {'batch system': 'slurm'} |
| 172 |
}, |
| 173 |
'ras_el_hanout' |
| 174 |
), |
| 175 |
( |
| 176 |
{'remote': {'host': 'cumin'}}, |
| 177 |
'spice_bg' |
| 178 |
), |
| 179 |
( |
| 180 |
{'job': {'batch system': 'batchyMcBatchFace'}}, |
| 181 |
'local_job_runner' |
| 182 |
), |
| 183 |
( |
| 184 |
{'script': 'true'}, |
| 185 |
'localhost' |
| 186 |
), |
| 187 |
( |
| 188 |
{ |
| 189 |
'remote': {'host': 'localhost'}, |
| 190 |
'job': { |
| 191 |
'batch system': None, |
| 192 |
'batch submit command template': None, |
| 193 |
'execution polling intervals': None |
| 194 |
} |
| 195 |
}, |
| 196 |
'localhost' |
| 197 |
), |
| 198 |
( |
| 199 |
{ |
| 200 |
'remote': {'host': 'cylcdevbox'}, |
| 201 |
'job': { |
| 202 |
'batch system': None, |
| 203 |
'batch submit command template': None, |
| 204 |
'execution polling intervals': None |
| 205 |
} |
| 206 |
}, |
| 207 |
'cylcdevbox' |
| 208 |
) |
| 209 |
] |
| 210 |
) |
| 211 |
def test_get_platform_using_platform_name_from_job_info( |
| 212 |
mock_glbl_cfg, task_conf, expected_platform_name |
| 213 |
): |
| 214 |
"""Calculate platform from Cylc 7 config: n.b. If this fails we don't |
| 215 |
warn because this might lead to many thousands of warnings |
| 216 |
|
| 217 |
This should not contain a comprehensive set of use-cases - these should |
| 218 |
be coverend by the unit tests for `platform_from_host_items` |
| 219 |
""" |
| 220 |
mock_glbl_cfg( |
| 221 |
'cylc.flow.platforms.glbl_cfg', |
| 222 |
''' |
| 223 |
[platforms] |
| 224 |
[[ras_el_hanout]] |
| 225 |
hosts = rose, chilli, cumin, paprika |
| 226 |
job runner = slurm |
| 227 |
[[spice_bg]] |
| 228 |
hosts = rose, chilli, cumin, paprika |
| 229 |
[[local_job_runner]] |
| 230 |
hosts = localhost |
| 231 |
job runner = batchyMcBatchFace |
| 232 |
[[cylcdevbox]] |
| 233 |
hosts = cylcdevbox |
| 234 |
''' |
| 235 |
) |
| 236 |
assert get_platform(task_conf)['name'] == expected_platform_name |
| 237 |
|
| 238 |
|
| 239 |
def test_get_platform_groups_basic(mock_glbl_cfg): |
| 240 |
"""get platform from group works. |
| 241 |
|
| 242 |
Additionally, ensure that we stop after selecting the first |
| 243 |
appropriate platform. |
| 244 |
""" |
| 245 |
mock_glbl_cfg( |
| 246 |
'cylc.flow.platforms.glbl_cfg', |
| 247 |
''' |
| 248 |
[platforms] |
| 249 |
[[aleph, bet, alpha, beta]] |
| 250 |
|
| 251 |
[platform groups] |
| 252 |
[[hebrew_letters]] |
| 253 |
platforms = alpha, beta |
| 254 |
[[[selection]]] |
| 255 |
method = definition order |
| 256 |
[[aleph]] |
| 257 |
# Group with same name as platform to try and |
| 258 |
# trip up the platform selection logic after it |
| 259 |
# has processed [[.*_letters]] below |
| 260 |
platforms = alpha |
| 261 |
[[.*_letters]] |
| 262 |
platforms = aleph, bet |
| 263 |
[[[selection]]] |
| 264 |
method = definition order |
| 265 |
''' |
| 266 |
) |
| 267 |
output = get_platform('hebrew_letters') |
| 268 |
assert output['name'] == 'aleph' |
| 269 |
|
| 270 |
|
| 271 |
@pytest.mark.parametrize( |
| 272 |
'task_conf, expected_err_msg', |
| 273 |
[ |
| 274 |
({'platform': '$(host)'}, None), |
| 275 |
({'platform': '$(host)-suffix'}, None), |
| 276 |
({'platform': '`echo ${chamber}`'}, "backticks are not supported") |
| 277 |
] |
| 278 |
) |
| 279 |
def test_get_platform_subshell( |
| 280 |
task_conf: Dict[str, str], expected_err_msg: Optional[str]): |
| 281 |
"""Test get_platform() for subshell platform definition.""" |
| 282 |
if expected_err_msg: |
| 283 |
with pytest.raises(PlatformLookupError) as err: |
| 284 |
get_platform(task_conf) |
| 285 |
assert expected_err_msg in str(err.value) |
| 286 |
else: |
| 287 |
assert get_platform(task_conf) is None |
| 288 |
|
| 289 |
|
| 290 |
def test_get_localhost_install_target(): |
| 291 |
assert get_localhost_install_target() == 'localhost' |
| 292 |
|
| 293 |
|
| 294 |
def test_localhost_different_install_target(mock_glbl_cfg): |
| 295 |
mock_glbl_cfg( |
| 296 |
'cylc.flow.platforms.glbl_cfg', |
| 297 |
''' |
| 298 |
[platforms] |
| 299 |
[[localhost]] |
| 300 |
install target = file_system_1 |
| 301 |
''' |
| 302 |
) |
| 303 |
|
| 304 |
assert get_localhost_install_target() == 'file_system_1' |