Skip to content
Merged
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
7 changes: 4 additions & 3 deletions incant/provisioners/ssh_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ def _install_ssh_server(self, name: str) -> bool:
for pm in package_managers:
try:
self.incus.exec(name, ["sh", "-c", pm["check_cmd"]], capture_output=True)
for cmd in pm["install_cmds"]:
self.incus.exec(name, ["sh", "-c", cmd], capture_output=False)
return True # Installed
except IncusCommandError:
continue # Try next package manager

for cmd in pm["install_cmds"]:
self.incus.exec(name, ["sh", "-c", cmd], capture_output=False)
return True # Installed
return False # Not installed

def _get_authorized_keys_content(self, ssh_config: Union[dict, bool]) -> str:
Expand Down
52 changes: 52 additions & 0 deletions tests/test_ssh_provisioner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from unittest.mock import Mock, call
import pytest
from incant.provisioners.ssh_server import SSHServer
from incant.exceptions import IncusCommandError
from incant.reporter import Reporter


@pytest.fixture
def mock_incus_cli():
return Mock()


@pytest.fixture
def mock_reporter():
return Mock(spec=Reporter)


def test_ssh_provisioner_misleading_error(mock_incus_cli, mock_reporter):
# Setup
ssh_provisioner = SSHServer(mock_incus_cli, mock_reporter)
instance_name = "test-vm"

# Mock behavior:
# 1. check apt-get -> Success
# 2. install ssh (apt-get) -> Fail (Network error)
# 3. check dnf -> Fail (Not found)
# 4. check pacman -> Fail (Not found)

def side_effect(name, cmd, **kwargs):
cmd_str = " ".join(cmd)
if "command -v apt-get" in cmd_str:
return "" # Success
if "apt-get update" in cmd_str:
raise IncusCommandError(
"Temporary failure resolving 'deb.debian.org'",
stderr="Temporary failure resolving 'deb.debian.org'",
)
if "command -v dnf" in cmd_str:
raise IncusCommandError("dnf not found")
if "command -v pacman" in cmd_str:
raise IncusCommandError("pacman not found")
return ""

mock_incus_cli.exec.side_effect = side_effect

# Run
# This should raise IncusCommandError because apt-get was found
# but the installation failed.
with pytest.raises(IncusCommandError) as excinfo:
ssh_provisioner._install_ssh_server(instance_name)

assert "Temporary failure resolving 'deb.debian.org'" in str(excinfo.value)