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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
# 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/>.
#
# Tests for the platform lookup.
from typing import (
Any,
Dict,
List,
Optional,
)
import pytest
from cylc.flow.exceptions import (
GlobalConfigError,
PlatformLookupError,
)
from cylc.flow.parsec.OrderedDict import OrderedDictWithDefaults
from cylc.flow.platforms import (
_platform_name_from_job_info,
_validate_single_host,
generic_items_match,
get_install_target_from_platform,
get_install_target_to_platforms_map,
get_platform,
get_platform_deprecated_settings,
is_platform_definition_subshell,
platform_from_name,
)
from cylc.flow.run_modes import JOBLESS_MODES
PLATFORMS = {
'desktop[0-9]{2}|laptop[0-9]{2}': {
'job runner': 'background'
},
'sugar': {
'hosts': 'localhost',
'job runner': 'slurm',
},
'hpc-no-logs': {
'hosts': ['hpc1', 'hpc2'],
'job runner': 'pbs',
'retrieve job logs': False
},
'hpc-logs': {
'hosts': ['hpc1', 'hpc2'],
'job runner': 'pbs',
'retrieve job logs': True
},
'hpc1-bg': {
'hosts': 'hpc1',
'job runner': 'background',
},
'hpc2-bg': {
'hosts': 'hpc2',
'job runner': 'background'
},
'localhost': {
'hosts': 'localhost',
'job runner': 'background'
}
}
PLATFORMS_NO_UNIQUE = {
'sugar': {
'hosts': 'localhost',
'job runner': 'slurm'
},
'pepper': {
'hosts': ['hpc1', 'hpc2'],
'job runner': 'slurm'
},
}
PLATFORMS_WITH_RE = {
'hpc.*': {'hosts': 'hpc1', 'job runner': 'background'},
'h.*': {'hosts': 'hpc3'},
r'vld\d{2,3}, anselm\d{4}': {},
'nu.*': {
'job runner': 'slurm',
'hosts': ['localhost']
},
'localhost': {
'hosts': 'localhost',
'job runner': 'background'
}
}
PLATFORMS_TREK = {
'enterprise': {
'hosts': ['kirk', 'picard'],
'install target': 'picard',
'name': 'enterprise'
},
'voyager': {
'hosts': ['janeway'],
'install target': 'janeway',
'name': 'voyager'
},
'stargazer': {
'hosts': ['picard'],
'install target': 'picard',
'name': 'stargazer'
}
}
PLATFORMS_INVALID = {
'enterprise': {
'hosts': ['kirk', 'picard'],
'install target': 'picard',
'job runner': 'background' # requires one host
},
'voyager': {
'hosts': ['janeway', 'seven-of-nine'],
'install target': 'janeway',
'job runner': 'at' # requires one host
}
}
# ----------------------------------------------------------------------------
# Tests of platform_from_name
# ----------------------------------------------------------------------------
@pytest.mark.parametrize(
"PLATFORMS, platform, expected",
[
(PLATFORMS_WITH_RE, "nutmeg", {
"job runner": "slurm",
"name": "nutmeg",
"hosts": ['localhost']
}),
(PLATFORMS_WITH_RE, "vld798", ["vld798"]),
(PLATFORMS_WITH_RE, "vld56", ["vld56"]),
(
PLATFORMS_NO_UNIQUE,
"sugar",
{
"hosts": "localhost",
"job runner": "slurm",
"name": "sugar"
},
),
(
PLATFORMS,
None,
{
"hosts": "localhost",
"name": "localhost",
"job runner": "background"
},
),
(PLATFORMS, "laptop22", {
"job runner": "background",
"name": "laptop22",
"hosts": ["laptop22"]
}),
(
PLATFORMS,
"hpc1-bg",
{
"hosts": "hpc1",
"job runner": "background",
"name": "hpc1-bg"
},
),
(PLATFORMS_WITH_RE, "hpc2", {"hosts": "hpc3", "name": "hpc2"}),
],
)
def test_basic(PLATFORMS, platform, expected):
# n.b. The name field of the platform is set in the Globalconfig object
# if the name is 'localhost', so we don't test for it here.
platform = platform_from_name(platform_name=platform, platforms=PLATFORMS)
if isinstance(expected, dict):
assert platform == expected
else:
assert platform["hosts"] == expected
def test_platform_not_there():
with pytest.raises(PlatformLookupError):
platform_from_name('moooo', PLATFORMS)
@pytest.mark.parametrize(
'platform',
[
{i: j} for i, j in PLATFORMS_INVALID.items()
]
)
def test_invalid_platforms(platform):
with pytest.raises(GlobalConfigError):
_validate_single_host(platform)
def test_similar_but_not_exact_match():
with pytest.raises(PlatformLookupError):
platform_from_name('vld1', PLATFORMS_WITH_RE)
# ----------------------------------------------------------------------------
# Tests of platform_name_from_job_info
# ----------------------------------------------------------------------------
# Basic tests that we can select sensible platforms
@pytest.mark.parametrize(
'job, remote, returns',
[
# Can we return a sensible platform for desktop 42
(
{},
{'host': 'desktop42'},
'desktop42'
),
# Basic test where the user hasn't submitted anything and the task
# returns to default, i.e. localhost.
(
{'batch system': 'background'},
{'retrieve job logs retry delays': None},
'localhost'
),
# Check that when the user asks for batch system = slurm alone
# they get system = sugar
(
{'batch system': 'slurm'},
{'host': ''},
'sugar'
),
# Check that when users asks for hpc1 and pbs they get a platform
# with hpc1 in its list of hosts
(
{'batch system': 'pbs'},
{'host': 'hpc1'},
'hpc-logs'
),
# When the user asks for hpc1 without specifying pbs user gets platform
# hpc bg1
(
{'batch system': 'background'},
{'host': 'hpc1'},
'hpc1-bg'
),
# Check that None as a value is handled correctly
(
{'batch system': None},
{'host': 'hpc1-bg'},
'hpc1-bg'
),
(
# Check that failure to set any items will return localhost
{'batch system': None},
{'host': None},
'localhost'
),
(
# Check that all generic items are matched
{'batch system': 'pbs'},
{'host': 'hpc1', 'retrieve job logs': False},
'hpc-no-logs'
),
]
)
def test_platform_name_from_job_info_basic(job, remote, returns):
assert _platform_name_from_job_info(PLATFORMS, job, remote) == returns
def test_platform_name_from_job_info_evaluated_hostname():
result = _platform_name_from_job_info(
PLATFORMS,
{'batch system': 'background'},
{'host': '$(cat tiddles)'},
evaluated_host='hpc2',
)
assert result == 'hpc2-bg'
def test_platform_name_from_job_info_ordered_dict_comparison():
"""Check that we are only comparing set items in OrderedDictWithDefaults.
"""
job = {'batch system': 'background', 'Made up key': 'Zaphod'}
remote = {'host': 'hpc1', 'Made up key': 'Arthur'}
# Set up a fake OrderedDictWith a fake unset default.
platform = OrderedDictWithDefaults()
platform.defaults_ = {k: None for k in PLATFORMS['hpc1-bg'].keys()}
platform.defaults_['Made up key'] = {}
platform.update(PLATFORMS['hpc1-bg'])
platforms = {'hpc1-bg': platform, 'dobbie': PLATFORMS['sugar']}
assert _platform_name_from_job_info(platforms, job, remote) == 'hpc1-bg'
# Cases where the error ought to be raised because no matching platform should
# be found.
@pytest.mark.parametrize(
'job, remote',
[
# Check for error when the user asks for slurm on host desktop01
(
{'batch system': 'slurm'},
{'host': 'desktop01'},
),
# ('hpc1', 'slurm', 'error'),
(
{'batch system': 'slurm'},
{'host': 'hpc1'},
),
# Localhost doesn't support pbs
(
{'batch system': 'pbs'},
{},
),
]
)
def test_reverse_PlatformLookupError(job, remote):
with pytest.raises(PlatformLookupError):
_platform_name_from_job_info(PLATFORMS, job, remote)
# An example of a global config with two Spice systems available
@pytest.mark.parametrize(
'job, remote, returns',
[
(
{'batch system': 'slurm'},
{'host': 'sugar'},
'sugar'
),
(
{'batch system': 'slurm'},
{},
'sugar'
),
(
{'batch system': 'slurm'},
{'host': 'pepper'},
'pepper'
)
]
)
def test_platform_name_from_job_info_two_spices(
job, remote, returns
):
platforms = {
'sugar': {
'hosts': ['sugar', 'localhost'],
'job runner': 'slurm',
},
'pepper': {
'job runner': 'slurm',
'hosts': 'pepper'
},
}
assert _platform_name_from_job_info(platforms, job, remote) == returns
# An example of two platforms with the same hosts and job runner settings
# but some other setting different
@pytest.mark.parametrize(
'job, remote, returns',
[
(
{
'job runner': 'background',
'job runner command template': '',
'shell': '/bin/fish'
},
{
'host': 'desktop01',
'owner': '',
'workflow definition directory': '',
'retrieve job logs': '',
'retrieve job logs max size': '',
'retrieve job logs retry delays': 'None'
},
'my-platform-with-fish'
),
]
)
def test_platform_name_from_job_info_similar_platforms(
job, remote, returns
):
platforms = {
'my-platform-with-bash': {
'hosts': 'desktop01',
'shell': '/bin/bash',
'job runner': 'background'
},
# An extra platform to check that we only pick up the first match
'my-platform-with-fish-not-this-one': {
'hosts': 'desktop01',
'shell': '/bin/fish',
'job runner': 'background'
},
'my-platform-with-fish': {
'hosts': 'desktop01',
'shell': '/bin/fish',
'job runner': 'background'
},
}
assert _platform_name_from_job_info(platforms, job, remote) == returns
# -----------------------------------------------------------------------------
# Tests for getting install target info
@pytest.mark.parametrize(
'platform, expected',
[
({'name': 'rick', 'install target': 'desktop'}, 'desktop'),
({'name': 'morty', 'install target': ''}, 'morty')
]
)
def test_get_install_target_from_platform(platform, expected):
"""Test that get_install_target_from_platform works as expected."""
assert get_install_target_from_platform(platform) == expected
@pytest.mark.parametrize(
'platform_names, expected_map',
[
(
['enterprise', 'stargazer'],
{
'picard': [
PLATFORMS_TREK['enterprise'],
PLATFORMS_TREK['stargazer']
]
},
),
(
['enterprise', 'voyager', 'enterprise'],
{
'picard': [
PLATFORMS_TREK['enterprise']
],
'janeway': [
PLATFORMS_TREK['voyager']
]
},
),
(
['enterprise', 'starkiller'], # ignores non-existent platform
{
'picard': [
PLATFORMS_TREK['enterprise']
],
},
),
]
)
def test_get_install_target_to_platforms_map(
platform_names: list[str],
expected_map: dict[str, Any],
monkeypatch: pytest.MonkeyPatch,
):
"""Test that get_install_target_to_platforms_map works as expected."""
monkeypatch.setattr('cylc.flow.platforms.platform_from_name',
lambda x: platform_from_name(x, PLATFORMS_TREK))
result = get_install_target_to_platforms_map(platform_names)
# Sort the maps:
for _map in (result, expected_map):
for install_target in _map:
_map[install_target] = sorted(
_map[install_target], key=lambda k: k['name']
)
assert result == expected_map
@pytest.mark.parametrize('mode', sorted(JOBLESS_MODES))
def test_platform_from_name__jobless_modes(mode):
result = platform_from_name(mode)
assert result['name'] == 'localhost'
@pytest.mark.parametrize('mode', sorted(JOBLESS_MODES))
def test_get_install_target_to_platforms_map__jobless_modes(mode):
result = get_install_target_to_platforms_map([mode])
assert list(result) == ['localhost']
assert len(result['localhost']) == 1
assert result['localhost'][0]['hosts'] == ['localhost']
assert result['localhost'][0]['install target'] == 'localhost'
@pytest.mark.parametrize(
'platform, job, remote, expect',
[
(
# Default, no old settings.
{'ship': 'Enterprise'}, {}, {}, True
),
(
{'captain': 'Kirk'},
{'captain': 'Picard'},
{},
False
),
(
{'captain': 'Sisko'},
{},
{'captain': 'Janeway'},
False
),
(
{'captain': 'Picard', 'ship': 'Enterprise'},
{'captain': 'Picard'},
{'ship': 'Enterprise'},
True
),
(
{'captain': 'Picard', 'ship': 'Enterprise'},
{'captain': 'Picard'},
{'ship': 'Defiant'},
False
),
(
{'captain': 'Picard', 'ship': 'Enterprise'},
{'captain': 'Picard'},
{},
True
)
]
)
def test_generic_items_match(platform, job, remote, expect):
assert generic_items_match(platform, job, remote) == expect
@pytest.mark.parametrize(
'task_conf, expected',
[
pytest.param(
{
'remote': {
'host': 'cylcdevbox',
'retrieve job logs': True
},
'job': {
'batch system': 'pbs',
'batch submit command template': 'meow'
}
},
[
'[runtime][task][job]batch submit command template = meow',
'[runtime][task][remote]retrieve job logs = True',
'[runtime][task][remote]host = cylcdevbox',
'[runtime][task][job]batch system = pbs'
],
id="All are deprecated settings"
),
pytest.param(
{
'remote': {'host': 'localhost'},
'job': {
'batch system': 'pbs',
'batch submit command template': None
}
},
['[runtime][task][job]batch system = pbs'],
id="Exclusions are excluded"
),
pytest.param(
{
'environment filter': {
'include': ['frodo', 'sam']
}
},
[],
id="No deprecated settings"
)
]
)
def test_get_platform_deprecated_settings(
task_conf: Dict[str, Any], expected: List[str]
):
output = get_platform_deprecated_settings(task_conf, task_name='task')
assert set(output) == set(expected)
@pytest.mark.parametrize(
'plat_val, expected, err_msg',
[('normal', False, None),
('$(yes)', True, None),
('`echo ${chamber}`', None, "backticks are not supported")]
)
def test_is_platform_definition_subshell(
plat_val: str, expected: Optional[bool], err_msg: Optional[str]):
if err_msg:
with pytest.raises(PlatformLookupError) as exc:
is_platform_definition_subshell(plat_val)
assert err_msg in str(exc.value)
else:
assert is_platform_definition_subshell(plat_val) is expected
def test_get_platform_from_OrderedDictWithDefaults(mock_glbl_cfg):
"""Get platform works with OrderedDictWithDefaults.
Most tests use dictionaries to check platforms functionality.
This one was added to catch an issue where the behaviour of
dict.get != OrderedDictWithDefaults.get.
See - https://github.com/cylc/cylc-flow/issues/4979
"""
mock_glbl_cfg(
'cylc.flow.platforms.glbl_cfg',
'''
[platforms]
[[skarloey]]
hosts = foo, bar
job runner = slurm
'''
)
task_conf = OrderedDictWithDefaults()
task_conf.defaults_ = OrderedDictWithDefaults([
('job', OrderedDictWithDefaults([
('batch system', 'slurm')
])),
('remote', OrderedDictWithDefaults([
('host', 'foo')
])),
])
result = get_platform(task_conf)['name']
assert result == 'skarloey'
|