Skip to content

Commit

Permalink
New ria_sshserver fixture
Browse files Browse the repository at this point in the history
This should be easier to use. The old implementation is now available in
`ria_sshserver_setup` and should be considered internal.

The new fixture returns the base RIA URL, and a local path to that same
root directory (if one exists).

It also sets `DATALAD_SSH_IDENTITYFILE`, which is needed by datalad-core's
`SSHManager`. The setup may still need to use the `datalad_cfg` fixture,
and possibly a dedicated reload to be effective.

Closes #60
  • Loading branch information
mih committed Sep 13, 2023
1 parent 8cb2e7a commit a2eaf76
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
19 changes: 18 additions & 1 deletion datalad_ria/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
from pathlib import Path
import pytest

from datalad_ria.utils import (
build_ria_url,
)

from datalad_next.tests.utils import (
SkipTest,
)
Expand All @@ -16,7 +20,7 @@


@pytest.fixture(autouse=False, scope="session")
def ria_sshserver(tmp_path_factory):
def ria_sshserver_setup(tmp_path_factory):
if not os.environ.get('DATALAD_TESTS_SSH'):
raise SkipTest(
"set DATALAD_TESTS_SSH=1 to enable")
Expand Down Expand Up @@ -55,3 +59,16 @@ def ria_sshserver(tmp_path_factory):
info[e] = v

yield info


@pytest.fixture(autouse=False, scope="function")
def ria_sshserver(ria_sshserver_setup, monkeypatch):
ria_baseurl = build_ria_url(
protocol='ssh',
host=ria_sshserver_setup['HOST'],
user=ria_sshserver_setup['SSH_LOGIN'],
path=ria_sshserver_setup['SSH_PATH'],
)
with monkeypatch.context() as m:
m.setenv("DATALAD_SSH_IDENTITYFILE", ria_sshserver_setup['SSH_SECKEY'])
yield ria_baseurl, ria_sshserver_setup['LOCALPATH']
26 changes: 18 additions & 8 deletions datalad_ria/tests/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
from .utils import assert_ssh_access
from datalad_ria.tests.utils import assert_ssh_access

# we import this one, rather than putting it into conftest.py, because
# it is considered internal, in contrast to `ria_sshserver`
from datalad_ria.tests.fixtures import ria_sshserver_setup

def test_riaserver_fixture(ria_sshserver):

def test_riaserver_setup_fixture(ria_sshserver_setup):
# 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
assert_ssh_access(
ria_sshserver['HOST'],
ria_sshserver['SSH_PORT'],
ria_sshserver['SSH_LOGIN'],
ria_sshserver['SSH_SECKEY'],
ria_sshserver['SSH_PATH'],
ria_sshserver['LOCALPATH'],
ria_sshserver_setup['HOST'],
ria_sshserver_setup['SSH_PORT'],
ria_sshserver_setup['SSH_LOGIN'],
ria_sshserver_setup['SSH_SECKEY'],
ria_sshserver_setup['SSH_PATH'],
ria_sshserver_setup['LOCALPATH'],
)


def test_riaserver_fixture(ria_sshserver):
# base url and local path
assert len(ria_sshserver) == 2
assert ria_sshserver[0].startswith('ria+ssh://')
31 changes: 31 additions & 0 deletions datalad_ria/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from __future__ import annotations


def build_ria_url(
protocol: str,
host: str,
port: str | None = None,
path: str | None = None,
user: str | None = None,
passwd: str | None = None,

):
if passwd and (user is None):
raise ValueError('password without user name given')

url = (
'ria+{protocol}://{user}{passdlm}{passwd}{userdlm}'
'{host}{portdlm}{port}{pathdlm}{path}'
).format(
protocol=protocol,
user=user or '',
passdlm=':' if passwd else '',
passwd=passwd or '',
userdlm='@' if user else '',
host=host or '',
portdlm=':' if port else '',
port=port or '',
pathdlm='/' if path and not path.startswith('/') else '',
path=path or '',
)
return url

0 comments on commit a2eaf76

Please sign in to comment.