| 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 remote initialisation.""" |
| 17 |
|
| 18 |
from pathlib import Path |
| 19 |
from unittest.mock import patch |
| 20 |
|
| 21 |
from pytest import CaptureFixture |
| 22 |
|
| 23 |
from cylc.flow.task_remote_cmd import remote_init |
| 24 |
from cylc.flow.workflow_files import WorkflowFiles |
| 25 |
|
| 26 |
|
| 27 |
def test_existing_key_raises_error(tmp_path: Path, capsys: CaptureFixture): |
| 28 |
"""Test .service directory that contains existing incorrect key, |
| 29 |
results in REMOTE INIT FAILED""" |
| 30 |
rundir = tmp_path / 'some_rund' |
| 31 |
srvdir = rundir / WorkflowFiles.Service.DIRNAME |
| 32 |
srvdir.mkdir(parents=True) |
| 33 |
(srvdir / 'client_wrong.key').touch() |
| 34 |
|
| 35 |
assert remote_init('test_install_target', str(rundir)) |
| 36 |
assert capsys.readouterr().out == ( |
| 37 |
"REMOTE INIT FAILED\nUnexpected authentication key" |
| 38 |
" 'client_wrong.key' exists. Check global.cylc install target is" |
| 39 |
" configured correctly for this platform.\n") |
| 40 |
|
| 41 |
|
| 42 |
@patch('os.path.expandvars') |
| 43 |
def test_unexpandable_symlink_env_var_returns_failed( |
| 44 |
mocked_expandvars, capsys): |
| 45 |
"""Test unexpandable symlinks return REMOTE INIT FAILED""" |
| 46 |
mocked_expandvars.side_effect = ['some/rund/path', '$blah'] |
| 47 |
|
| 48 |
assert remote_init('test_install_target', 'some_rund', 'run=$blah') |
| 49 |
assert capsys.readouterr().out == ( |
| 50 |
"REMOTE INIT FAILED\nError occurred when symlinking." |
| 51 |
" $blah contains an invalid environment variable.\n") |
| 52 |
|
| 53 |
|
| 54 |
def test_existing_client_key_dir_raises_error( |
| 55 |
tmp_path: Path, capsys: CaptureFixture): |
| 56 |
"""Test .service directory that contains existing incorrect key, |
| 57 |
results in REMOTE INIT FAILED |
| 58 |
""" |
| 59 |
rundir = tmp_path / 'some_rund' |
| 60 |
keydir = rundir / WorkflowFiles.Service.DIRNAME / "client_public_keys" |
| 61 |
keydir.mkdir(parents=True) |
| 62 |
|
| 63 |
assert remote_init('test_install_target', rundir) |
| 64 |
assert capsys.readouterr().out == ( |
| 65 |
f"REMOTE INIT FAILED\nUnexpected key directory exists: {keydir}" |
| 66 |
" Check global.cylc install target is configured correctly for this" |
| 67 |
" platform.\n") |