forked from datalad/datalad-extension-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Establish a documented helper to assert SSH server access within the …
…tests
- Loading branch information
Showing
3 changed files
with
57 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |