From 0904e50c24d82073a3fd91eb1b469d99e703aedc Mon Sep 17 00:00:00 2001 From: Mel Hall <37735232+datamel@users.noreply.github.com> Date: Tue, 17 Nov 2020 14:08:19 +0000 Subject: [PATCH 1/3] Fix remote init failures --- cylc/flow/task_remote_cmd.py | 2 ++ tests/unit/test_task_remote_cmd.py | 51 ++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 tests/unit/test_task_remote_cmd.py diff --git a/cylc/flow/task_remote_cmd.py b/cylc/flow/task_remote_cmd.py index 5368f2f6c9e..06118fd5549 100644 --- a/cylc/flow/task_remote_cmd.py +++ b/cylc/flow/task_remote_cmd.py @@ -105,6 +105,7 @@ def remote_init(install_target, rund, *dirs_to_symlink, indirect_comm=None): src = os.path.expandvars(val) if '$' in src: print(REMOTE_INIT_FAILED) + return make_symlink(src, dst) srvd = os.path.join(rund, SuiteFiles.Service.DIRNAME) os.makedirs(srvd, exist_ok=True) @@ -119,6 +120,7 @@ def remote_init(install_target, rund, *dirs_to_symlink, indirect_comm=None): if pattern.match(filepath) and f"{install_target}" not in filepath: # client key for a different install target exists print(REMOTE_INIT_FAILED) + return try: remove_keys_on_client(srvd, install_target) create_client_keys(srvd, install_target) diff --git a/tests/unit/test_task_remote_cmd.py b/tests/unit/test_task_remote_cmd.py new file mode 100644 index 00000000000..6f655731720 --- /dev/null +++ b/tests/unit/test_task_remote_cmd.py @@ -0,0 +1,51 @@ +# THIS FILE IS PART OF THE CYLC SUITE 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 . +"""Tests for remote initialisation.""" + +from io import StringIO +from mock import patch + +from cylc.flow.task_remote_cmd import remote_init + + +@patch('sys.stdout', new_callable=StringIO) +@patch('os.makedirs') +@patch('os.listdir') +@patch('os.path.join') +@patch('os.path.expandvars') +def test_existing_key_raises_error( + mocked_expandvars, mocked_pathjoin, mocked_listdir, + mocked_makedirs, mocked_stdout): + """Test .service directory that contains existing incorrect key, + results in REMOTE INIT FAILED + """ + mocked_expandvars.return_value = "some/expanded/path" + mocked_pathjoin.return_value = "joined.path" + mocked_listdir.return_value = ['client_wrong.key'] + + remote_init('test_install_target', 'some_rund') + assert mocked_stdout.getvalue() == "REMOTE INIT FAILED\n" + + +@patch('sys.stdout', new_callable=StringIO) +@patch('os.path.expandvars') +def test_unexpandable_symlink_env_var_returns_failed( + mocked_expandvars, mocked_stdout): + """Test unexpandable symlinks return REMOTE INIT FAILED""" + mocked_expandvars.side_effect = ['some/rund/path', '$blah'] + + remote_init('test_install_target', 'some_rund', 'run=$blah') + assert mocked_stdout.getvalue() == "REMOTE INIT FAILED\n" From 47b17e362d6ce2f32e8292047f48bda63227949d Mon Sep 17 00:00:00 2001 From: Mel Hall <37735232+datamel@users.noreply.github.com> Date: Tue, 17 Nov 2020 14:37:36 +0000 Subject: [PATCH 2/3] fix import --- tests/unit/test_task_remote_cmd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_task_remote_cmd.py b/tests/unit/test_task_remote_cmd.py index 6f655731720..05f7bf6b30c 100644 --- a/tests/unit/test_task_remote_cmd.py +++ b/tests/unit/test_task_remote_cmd.py @@ -16,7 +16,7 @@ """Tests for remote initialisation.""" from io import StringIO -from mock import patch +from unittest.mock import patch from cylc.flow.task_remote_cmd import remote_init From 79ba14c5bd4432a7d57122824979599c366812f0 Mon Sep 17 00:00:00 2001 From: Tim Pillinger <26465611+wxtim@users.noreply.github.com> Date: Wed, 18 Nov 2020 11:34:59 +0000 Subject: [PATCH 3/3] suggestion: use builtin fixture to monitor sys.stdout --- tests/unit/test_task_remote_cmd.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tests/unit/test_task_remote_cmd.py b/tests/unit/test_task_remote_cmd.py index 05f7bf6b30c..3573c9f8851 100644 --- a/tests/unit/test_task_remote_cmd.py +++ b/tests/unit/test_task_remote_cmd.py @@ -21,14 +21,13 @@ from cylc.flow.task_remote_cmd import remote_init -@patch('sys.stdout', new_callable=StringIO) @patch('os.makedirs') @patch('os.listdir') @patch('os.path.join') @patch('os.path.expandvars') def test_existing_key_raises_error( mocked_expandvars, mocked_pathjoin, mocked_listdir, - mocked_makedirs, mocked_stdout): + mocked_makedirs, capsys): """Test .service directory that contains existing incorrect key, results in REMOTE INIT FAILED """ @@ -37,15 +36,14 @@ def test_existing_key_raises_error( mocked_listdir.return_value = ['client_wrong.key'] remote_init('test_install_target', 'some_rund') - assert mocked_stdout.getvalue() == "REMOTE INIT FAILED\n" + assert capsys.readouterr().out == "REMOTE INIT FAILED\n" -@patch('sys.stdout', new_callable=StringIO) @patch('os.path.expandvars') def test_unexpandable_symlink_env_var_returns_failed( - mocked_expandvars, mocked_stdout): + mocked_expandvars, capsys): """Test unexpandable symlinks return REMOTE INIT FAILED""" mocked_expandvars.side_effect = ['some/rund/path', '$blah'] remote_init('test_install_target', 'some_rund', 'run=$blah') - assert mocked_stdout.getvalue() == "REMOTE INIT FAILED\n" + assert capsys.readouterr().out == "REMOTE INIT FAILED\n"