From 400d42c352d36593a8b10687741cf35d78fc122c Mon Sep 17 00:00:00 2001 From: Alexander Goscinski Date: Wed, 11 Sep 2024 05:54:59 +0200 Subject: [PATCH] Implement that all transport tests can use a passed ssh-key by environment variable --- .github/workflows/ci-code.yml | 4 ++-- .github/workflows/setup_ssh.sh | 10 ++++---- tests/transports/test_all_plugins.py | 4 ---- tests/transports/test_ssh.py | 34 +++++++++++++++++----------- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci-code.yml b/.github/workflows/ci-code.yml index 3e0ab40656..ca1bd22489 100644 --- a/.github/workflows/ci-code.yml +++ b/.github/workflows/ci-code.yml @@ -102,7 +102,7 @@ jobs: env: AIIDA_TEST_PROFILE: test_aiida AIIDA_WARN_v3: 1 - AIIDA_PYTEST_SSH_KEY: ${{ github.workspace }}/.ssh/id_rsa_aiida_pytest + AIIDA_PYTEST_SSH_KEY: $HOME/.ssh/id_rsa_aiida_pytest # Python 3.12 has a performance regression when running with code coverage # so run code coverage only for python 3.9. run: pytest -v tests -m 'not nightly' ${{ matrix.python-version == '3.9' && '--cov aiida' || '' }} @@ -140,7 +140,7 @@ jobs: - name: Run test suite env: AIIDA_WARN_v3: 0 - AIIDA_PYTEST_SSH_KEY: ${{ github.workspace }}/.ssh/id_rsa_aiida_pytest + AIIDA_PYTEST_SSH_KEY: $HOME/.ssh/id_rsa_aiida_pytest run: pytest -m 'presto' diff --git a/.github/workflows/setup_ssh.sh b/.github/workflows/setup_ssh.sh index 599ecf26f6..e955424823 100755 --- a/.github/workflows/setup_ssh.sh +++ b/.github/workflows/setup_ssh.sh @@ -3,14 +3,16 @@ # because localhost is used as remote address to run the tests locally. set -ev -mkdir -p ${PWD}/.ssh mkdir -p ${HOME}/.ssh ssh-keygen -q -t rsa -b 4096 -N "" -f "${PWD}/.ssh/id_rsa_aiida_pytest" ssh-keygen -y -f "${PWD}/.ssh/id_rsa_aiida_pytest" >> "${HOME}/.ssh/authorized_keys" -# for the ssh_auto the tests still require the default key -ssh-keygen -q -t rsa -b 4096 -N "" -f "${HOME}/.ssh/id_rsa" -ssh-keygen -y -f "${HOME}/.ssh/id_rsa" >> "${HOME}/.ssh/authorized_keys" ssh-keyscan -H localhost >> "${HOME}/.ssh/known_hosts" +# to test core.ssh_auto transport plugin we need to append this to the config +cat <> ~/.ssh/id_rsa_aiida_pytest +Host localhost + IdentityFile ~/code/aiida-core/.ssh/id_rsa_aiida_pytest +EOT + # The permissions on the GitHub runner are 777 which will cause SSH to refuse the keys and cause authentication to fail chmod 755 "${HOME}" diff --git a/tests/transports/test_all_plugins.py b/tests/transports/test_all_plugins.py index ffee6e722d..da950ee8ac 100644 --- a/tests/transports/test_all_plugins.py +++ b/tests/transports/test_all_plugins.py @@ -48,10 +48,6 @@ def custom_transport(request, tmp_path, monkeypatch, ssh_key) -> Transport: } elif request.param == 'core.ssh_auto': kwargs = {'machine': 'localhost'} - filepath_config = tmp_path / 'config' - monkeypatch.setattr(plugin, 'FILEPATH_CONFIG', filepath_config) - if not filepath_config.exists(): - filepath_config.write_text('Host localhost') else: kwargs = {} diff --git a/tests/transports/test_ssh.py b/tests/transports/test_ssh.py index 27698dfa54..120184a0f3 100644 --- a/tests/transports/test_ssh.py +++ b/tests/transports/test_ssh.py @@ -30,16 +30,16 @@ def test_closed_connection_sftp(): transport.listdir() -def test_auto_add_policy(): +def test_auto_add_policy(ssh_key): """Test the auto add policy.""" - with SshTransport(machine='localhost', timeout=30, load_system_host_keys=True, key_policy='AutoAddPolicy'): + with SshTransport(machine='localhost', timeout=30, load_system_host_keys=True, key_policy='AutoAddPolicy', key_filename=str(ssh_key)): pass -def test_proxy_jump(): +def test_proxy_jump(ssh_key): """Test the connection with a proxy jump or several""" with SshTransport( - machine='localhost', proxy_jump='localhost', timeout=30, load_system_host_keys=True, key_policy='AutoAddPolicy' + machine='localhost', proxy_jump='localhost', timeout=30, load_system_host_keys=True, key_policy='AutoAddPolicy', key_filename=str(ssh_key) ): pass @@ -50,6 +50,7 @@ def test_proxy_jump(): timeout=30, load_system_host_keys=True, key_policy='AutoAddPolicy', + key_filename=str(ssh_key), ): pass @@ -69,7 +70,7 @@ def test_proxy_jump_invalid(): pass -def test_proxy_command(): +def test_proxy_command(ssh_key): """Test the connection with a proxy command""" with SshTransport( machine='localhost', @@ -77,6 +78,7 @@ def test_proxy_command(): timeout=30, load_system_host_keys=True, key_policy='AutoAddPolicy', + key_filename=str(ssh_key), ): pass @@ -94,7 +96,7 @@ def test_no_host_key(): logging.disable(logging.NOTSET) -def test_gotocomputer(): +def test_gotocomputer(ssh_key): """Test gotocomputer""" with SshTransport( machine='localhost', @@ -102,18 +104,21 @@ def test_gotocomputer(): use_login_shell=False, key_policy='AutoAddPolicy', proxy_command='ssh -W localhost:22 localhost', + key_filename=str(ssh_key), ) as transport: cmd_str = transport.gotocomputer_command('/remote_dir/') - expected_str = ( - """ssh -t localhost -o ProxyCommand='ssh -W localhost:22 localhost' "if [ -d '/remote_dir/' ] ;""" + expected_startwith = "ssh -t localhost -i " + expected_endwith = ( + """ -o ProxyCommand='ssh -W localhost:22 localhost' "if [ -d '/remote_dir/' ] ;""" """ then cd '/remote_dir/' ; bash ; else echo ' ** The directory' ; """ """echo ' ** /remote_dir/' ; echo ' ** seems to have been deleted, I logout...' ; fi" """ ) - assert cmd_str == expected_str + assert cmd_str.startswith(expected_startwith) + assert cmd_str.endswith(expected_endwith) -def test_gotocomputer_proxyjump(): +def test_gotocomputer_proxyjump(ssh_key): """Test gotocomputer""" with SshTransport( machine='localhost', @@ -121,12 +126,15 @@ def test_gotocomputer_proxyjump(): use_login_shell=False, key_policy='AutoAddPolicy', proxy_jump='localhost', + key_filename=str(ssh_key), ) as transport: cmd_str = transport.gotocomputer_command('/remote_dir/') - expected_str = ( - """ssh -t localhost -o ProxyJump='localhost' "if [ -d '/remote_dir/' ] ;""" + expected_startwith = "ssh -t localhost -i " + expected_endwith = ( + """-o ProxyJump='localhost' "if [ -d '/remote_dir/' ] ;""" """ then cd '/remote_dir/' ; bash ; else echo ' ** The directory' ; """ """echo ' ** /remote_dir/' ; echo ' ** seems to have been deleted, I logout...' ; fi" """ ) - assert cmd_str == expected_str + assert cmd_str.startswith(expected_startwith) + assert cmd_str.endswith(expected_endwith)