Skip to content

Commit

Permalink
Establish a documented helper to assert SSH server access within the …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
mih committed Sep 13, 2023
1 parent 773715d commit 8cb2e7a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
32 changes: 4 additions & 28 deletions datalad_ria/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,14 @@
import os
from pathlib import Path
import pytest
import subprocess

from datalad_next.tests.utils import (
SkipTest,
)


def verify_ssh_access(host, port, login, seckey, path, localpath):
# we can only handle openssh
ssh_bin = os.environ.get('DATALAD_SSH_EXECUTABLE', 'ssh')

ssh_call = [
ssh_bin,
'-i', seckey,
'-p', port,
f'{login}@{host}',
]
# now try if this is a viable configuration
# verify execute and write permissions (implicitly also POSIX path handling
subprocess.run(
ssh_call + [f"bash -c 'mkdir -p {path} && touch {path}/datalad-tests-probe'"],
check=True,
)
if localpath:
# check if a given
assert (Path(localpath) / 'datalad-tests-probe').exists()
subprocess.run(
ssh_call + [f"bash -c 'rm {path}/datalad-tests-probe'"],
check=True,
)
if localpath:
assert not (Path(localpath) / 'datalad-tests-probe').exists()
from datalad_ria.tests.utils import (
assert_ssh_access,
)


@pytest.fixture(autouse=False, scope="session")
Expand All @@ -61,7 +37,7 @@ def ria_sshserver(tmp_path_factory):
path = os.environ.get('DATALAD_TESTS_RIA_SERVER_SSH_PATH', tmp_riaroot)
localpath = os.environ.get('DATALAD_TESTS_RIA_SERVER_LOCALPATH', tmp_riaroot)

verify_ssh_access(host, port, login, seckey, path, localpath)
assert_ssh_access(host, port, login, seckey, path, localpath)

info = {}
# as far as we can tell, this is good, post effective config in ENV
Expand Down
4 changes: 2 additions & 2 deletions datalad_ria/tests/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from .fixtures import verify_ssh_access
from .utils import assert_ssh_access


def test_riaserver_fixture(ria_sshserver):
# we run the same test that the fixture already ran, to verify that
# the necessary information comes out of the fixture in a usable manner
verify_ssh_access(
assert_ssh_access(
ria_sshserver['HOST'],
ria_sshserver['SSH_PORT'],
ria_sshserver['SSH_LOGIN'],
Expand Down
51 changes: 51 additions & 0 deletions datalad_ria/tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from __future__ import annotations


import os
from pathlib import Path
import subprocess


def assert_ssh_access(
host: str,
port: str,
login: str,
seckey: str,
path: str,
localpath: str | None = None,
):
"""Test for a working SSH connection and sufficient permissions to write
This helper establishes a connection to an SSH server identified by
``host`` and ``port``, using a given SSH private key file (``seckey``) for
authentication. Once logged in successfully, it tries to create a
directory and a file at POSIX ``path`` on the server. If ``localpath`` is
given, it must be a representation of that server-side path on the local
file system (e.g., a bindmount), and the helper tests whether the created
content is also reflected in this directory.
"""
# we can only handle openssh
ssh_bin = os.environ.get('DATALAD_SSH_EXECUTABLE', 'ssh')

ssh_call = [
ssh_bin,
'-i', seckey,
'-p', port,
f'{login}@{host}',
]
# now try if this is a viable configuration
# verify execute and write permissions (implicitly also POSIX path handling
subprocess.run(
ssh_call + [
f"bash -c 'mkdir -p {path} && touch {path}/datalad-tests-probe'"],
check=True,
)
if localpath:
# check if a given
assert (Path(localpath) / 'datalad-tests-probe').exists()
subprocess.run(
ssh_call + [f"bash -c 'rm {path}/datalad-tests-probe'"],
check=True,
)
if localpath:
assert not (Path(localpath) / 'datalad-tests-probe').exists()

0 comments on commit 8cb2e7a

Please sign in to comment.