Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/pyinfra/connectors/sshuserclient/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ def run(cmd, *args, **kwargs):
result = FakeInvokeResult()

try:
Comment thread
lotheac marked this conversation as resolved.
Resolved
cmd = [environ["SHELL"], cmd]
try:
code = check_call(cmd)
code = check_call(cmd, shell=True, executable=environ["SHELL"])
except CalledProcessError as e:
code = e.returncode
result.ok = code == 0
Expand Down
59 changes: 58 additions & 1 deletion tests/test_connectors/test_sshuserclient.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import sys
from base64 import b64decode
from pathlib import Path
from unittest import TestCase
Expand All @@ -15,7 +17,7 @@
get_host_keys,
get_ssh_config,
)
from pyinfra.connectors.sshuserclient.config import SSHConfig
from pyinfra.connectors.sshuserclient.config import FakeInvoke, SSHConfig

CERT_KEY_TYPE = "ssh-ed25519-cert-v01@openssh.com"

Expand Down Expand Up @@ -116,6 +118,17 @@
IdentityAgent none
"""

SSH_CONFIG_MATCH_EXEC_SUCCESS = """
Match exec true
User match_exec_success
"""

SSH_CONFIG_MATCH_EXEC_FAILURE = """
User default_user
Match exec false
User match_exec_failure
"""

BAD_SSH_CONFIG_DATA = """
&
"""
Expand Down Expand Up @@ -276,6 +289,42 @@ def test_load_ssh_config_inline_comments(self):
assert config.get("username") == "testuser"
assert config.get("port") == 33

@pytest.mark.skipif(
sys.platform.startswith("win"),
reason="Match exec needs a unix shell",
)
@patch(
"pyinfra.connectors.sshuserclient.client.open",
mock_open(read_data=SSH_CONFIG_MATCH_EXEC_SUCCESS),
create=True,
)
@patch.dict(os.environ, {"SHELL": "/bin/sh"})
def test_load_ssh_config_match_exec_success(self):
"""Test that successful `Match exec` block contents are applied."""
client = SSHClient()

(_, config, _, _, _, _, _) = client.parse_config("any-host")

assert config.get("username") == "match_exec_success"

@pytest.mark.skipif(
sys.platform.startswith("win"),
reason="Match exec needs a unix shell",
)
@patch(
"pyinfra.connectors.sshuserclient.client.open",
mock_open(read_data=SSH_CONFIG_MATCH_EXEC_FAILURE),
create=True,
)
@patch.dict(os.environ, {"SHELL": "/bin/sh"})
def test_load_ssh_config_match_exec_failure(self):
"""Test that unsuccessful `Match exec` block contents are not applied."""
client = SSHClient()

(_, config, _, _, _, _, _) = client.parse_config("any-host")

assert config.get("username") == "default_user"

@patch(
"pyinfra.connectors.sshuserclient.client.open",
mock_open(read_data=SSH_CONFIG_DATA),
Expand Down Expand Up @@ -829,6 +878,14 @@ def test_parse_config_keeps_key_filename_when_no_real_identityfile(
assert cfg["key_filename"] == [str(tmp_path / "does-not-exist")]


@patch("pyinfra.connectors.sshuserclient.config.check_call", return_value=0)
def test_fake_invoke_uses_shell(fake):
with patch.dict(os.environ, {"SHELL": "/bin/dash"}):
result = FakeInvoke.run("true")
assert result.ok is True
fake.assert_called_with("true", shell=True, executable="/bin/dash")


def test_paramiko_floor_supports_ssh_client():
# PKey.from_type_string and pkey.UnknownKeyType were added in paramiko 3.2 (#1290)
tomllib = pytest.importorskip("tomllib")
Expand Down
Loading