Skip to content

Commit

Permalink
Implement that all transport tests can use a passed ssh-key by enviro…
Browse files Browse the repository at this point in the history
…nment variable
  • Loading branch information
agoscinski committed Sep 11, 2024
1 parent 88fa7e4 commit 400d42c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' || '' }}
Expand Down Expand Up @@ -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'


Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/setup_ssh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOT >> ~/.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}"
4 changes: 0 additions & 4 deletions tests/transports/test_all_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down
34 changes: 21 additions & 13 deletions tests/transports/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -50,6 +50,7 @@ def test_proxy_jump():
timeout=30,
load_system_host_keys=True,
key_policy='AutoAddPolicy',
key_filename=str(ssh_key),
):
pass

Expand All @@ -69,14 +70,15 @@ 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',
proxy_command='ssh -W localhost:22 localhost',
timeout=30,
load_system_host_keys=True,
key_policy='AutoAddPolicy',
key_filename=str(ssh_key),
):
pass

Expand All @@ -94,39 +96,45 @@ def test_no_host_key():
logging.disable(logging.NOTSET)


def test_gotocomputer():
def test_gotocomputer(ssh_key):
"""Test gotocomputer"""
with SshTransport(
machine='localhost',
timeout=30,
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',
timeout=30,
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)

0 comments on commit 400d42c

Please sign in to comment.