From daf1b519eb52874d6c62f4626811b6b035209be7 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Mon, 2 Nov 2026 08:52:32 +0100 Subject: [PATCH] fix(ssh): raise when a node command cannot run, drop the execute call _exec_ssh_command returned "" on any failure (no SSH credential, a refused login or host key, an unreachable node, a timeout), so a getter that could not look read like one that found nothing; on API-token-only nodes get_snmp_config answered None and the SSH-backed getters empty lists on every poll. It now raises ConnectionException with the cause (SshHostKeyMismatch as it is), and refuses before connecting when there is neither a password nor a usable key. It returned stderr when stdout was empty; output is stdout now. It first posted every command to /nodes/{node}/execute, which runs a batch of API calls rather than a shell; that dead round trip is gone. The key types, the key before the password, the host-key check and the single connect path were done in f491f9f and 6bb3035. The shared transport is NAPALM/napalm-device-types#35. Closes #17 --- CHANGELOG.md | 9 +++ README.md | 19 +++-- napalm_proxmox/config_mixin.py | 6 +- napalm_proxmox/driver.py | 72 ++++++++++-------- napalm_proxmox/interfaces_mixin.py | 6 +- napalm_proxmox/routing_mixin.py | 4 +- napalm_proxmox/system_mixin.py | 3 +- napalm_proxmox/vm_provision_mixin.py | 8 +- tests/conftest.py | 38 +++++++++- tests/test_misc.py | 74 ++++++++---------- tests/test_node_exec.py | 109 +++++++++++++++++++++++++++ tests/test_ovs_and_arp.py | 10 +-- tests/test_ssh_host_key.py | 11 +-- 13 files changed, 261 insertions(+), 108 deletions(-) create mode 100644 tests/test_node_exec.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff8e4c..d0976c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -120,6 +120,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 name, the others become `_`. A checksum algorithm other than md5, sha1, sha224, sha256, sha384 or sha512 is refused with a `ValueError` before anything runs on the node (#43). +- Commands on the node (ARP, IPv6 neighbours, packages, users, NTP, SNMP config, + services, kernel facts, `cli()`, …) raise `ConnectionException` when they cannot run: + no SSH password or key (a node reached with an API token only), a refused login, an + unreachable node, a timeout; a changed host key raises `SshHostKeyMismatch` as + before. They returned empty output, so "could not look" read as "nothing there": + `get_snmp_config()` answered None on token-only nodes and the getters empty lists. + Only what a command prints on stdout is its output; stderr no longer stands in when + stdout is empty. Each command goes straight to SSH: the driver first posted it to + `/nodes/{node}/execute`, which runs API calls, not a shell, and always failed (#17). ## [0.8.0] - 2026-10-23 diff --git a/README.md b/README.md index ab36f60..683df97 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ device = driver( | `get_interfaces_ip` | IPv4/IPv6 from classic ifaces + SDN subnet gateways | | `get_interfaces_counters` | From `node/netstat` RRD data | | `get_environment` | CPU %, memory (bytes), hardware temperature sensors | -| `get_arp_table` | Reads `/proc/net/arp` via exec endpoint | +| `get_arp_table` | Reads `/proc/net/arp` over SSH | | `get_mac_address_table` | `bridge fdb show` (Linux) + `ovs-appctl fdb/show` (OVS) | | `get_vlans` | SDN VNet tags + `bridge vlan show` | | `get_network_instances` | SDN zones (VLAN→L2VPN, EVPN→L3VRF) + default instance | @@ -116,13 +116,13 @@ device = driver( | `compare_config` | Unified diff | | `commit_config` | Writes `/etc/network/interfaces` + `ifreload -a` | | `discard_config` / `rollback` | Revert candidate | -| `get_lldp_neighbors` | `lldpcli show neighbors` via exec | +| `get_lldp_neighbors` | `lldpcli show neighbors` over SSH | | `get_lldp_neighbors_detail` | Full LLDP detail | | `get_ipv6_neighbors_table` | `ip -6 neigh show` | -| `ping` | Linux `ping` via exec endpoint | -| `traceroute` | `traceroute -n` via exec endpoint | -| `cli` | Arbitrary command execution via exec endpoint | -| `get_route_to` | `ip route show` via exec endpoint | +| `ping` | Linux `ping` over SSH | +| `traceroute` | `traceroute -n` over SSH | +| `cli` | Arbitrary commands over SSH (stdout) | +| `get_route_to` | `ip route show` over SSH | **Not implemented** (raise `NotImplementedError`): `get_bgp_config`, `get_bgp_neighbors`, `get_bgp_neighbors_detail`, @@ -138,6 +138,13 @@ device = driver( | `node` | str | *auto* | Override target node name | | `token_name` | str | `None` | API token identifier (`user@realm!tokenid`) | | `token_value` | str | `None` | API token secret | +| `ssh_username` | str | *username* | Login for the node's SSH (port 22) | +| `ssh_password` | str | *password* | SSH password; the API password when not set | +| `ssh_private_key_str` | str | `None` | SSH private key (Ed25519, ECDSA or RSA), tried before the password | + +What the API does not offer (ARP, LLDP, packages, SNMP config, `cli`, …) runs in the +node's shell over SSH. Without an SSH password or key — a node reached with an API +token only — those calls raise `ConnectionException` instead of reading as empty. ## Development diff --git a/napalm_proxmox/config_mixin.py b/napalm_proxmox/config_mixin.py index fc72eba..980cba7 100644 --- a/napalm_proxmox/config_mixin.py +++ b/napalm_proxmox/config_mixin.py @@ -114,12 +114,12 @@ class ProxmoxConfigMixin: .. note:: Full programmatic apply requires the Proxmox API to accept raw - interface configs. This implementation uses ``pvesh`` via exec - which requires the node exec endpoint to be available. + interface configs. This implementation uses ``pvesh`` over the + node's SSH session. """ if not self._candidate_config: return - # Write via exec endpoint + # Write over the node's SSH session escaped = self._candidate_config.replace("'", "'\\''") self._exec_ssh_command( f"printf '%s' '{escaped}' > /etc/network/interfaces && " diff --git a/napalm_proxmox/driver.py b/napalm_proxmox/driver.py index 3647fdf..5a1c40a 100644 --- a/napalm_proxmox/driver.py +++ b/napalm_proxmox/driver.py @@ -48,7 +48,7 @@ from napalm_device_types import ( SystemdServicesMixin, SystemIdentityMixin, ) -from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch +from napalm_device_types.ssh_host_keys import HostKeyCheck try: from proxmoxer import ProxmoxAPI @@ -299,48 +299,40 @@ class ProxmoxDriver( return [] def _exec_ssh_command(self, command: str) -> str: - """Execute a shell command on the Proxmox node and return output. + """Run *command* in the node's shell over SSH and return what it printed. - Tries the Proxmox API exec endpoint first. If that fails, - falls back to a direct paramiko SSH connection. + Stdout only: stderr is not output (a command that wants it says + ``2>&1``). The exit status is not checked; a caller that needs it asks + for it (``_rc``). The node has no API endpoint that runs a shell command + (``/nodes/{node}/execute`` runs a batch of API calls), so this is the + one way, the same session ``_run_node_command`` uses (see + NAPALM/napalm-device-types#35 for the shared transport). + + :raises ConnectionException: when the command could not run: no SSH + credential, a refused login or host key (``SshHostKeyMismatch``), + an unreachable node, a timeout. """ - import base64 as _b64 - - # Try API exec endpoint - try: - encoded = _b64.b64encode(command.encode()).decode() - result = self._node_api().execute.post("command", f"echo {encoded} | base64 -d | sh") - # On PVE 8.x the exec endpoint returns a dict with 'data' key - if isinstance(result, dict): - raw = result.get("data", result.get("output", "")) - else: - raw = result - if raw: - return str(raw).strip() - except Exception as exc: - logger.debug("API exec failed, falling back to SSH: %s", exc) - - # Fallback: direct paramiko SSH try: _, stdout, stderr = self._node_ssh().exec_command(command, timeout=self.timeout) - err = stderr.read().decode().strip() - out = stdout.read().decode().strip() - return out or err - except ImportError: - logger.warning("paramiko not installed — cannot exec SSH commands") - except SshHostKeyMismatch as exc: - # Refused before any login, and already reported to the caller's store. - logger.warning("%s", exc) - except Exception as exc: - logger.debug("SSH exec command failed: %s", exc) - - return "" + out = stdout.read().decode(errors="replace") + err = stderr.read().decode(errors="replace").strip() + except ConnectionException: + raise + except _ssh_errors() as exc: + raise ConnectionException( + f"Cannot run a command on {self.hostname} over SSH: {exc}" + ) from exc + if err: + logger.debug("%s: stderr of a node command: %s", self.hostname, err[-300:]) + return out.strip() def _node_ssh(self) -> Any: # paramiko.SSHClient; paramiko is imported lazily """The SSH session to the node on port 22, opened once. Its host key is the one the caller expects for port 22, or on first use whichever the node presents; either way it is reported (netork#1167). + + :raises ConnectionException: without a password or a usable key. """ import paramiko @@ -356,6 +348,12 @@ class ProxmoxDriver( pkey = _load_private_key(self._ssh_key) if self._ssh_key else None if pkey is not None: connect_kwargs["pkey"] = pkey + if not password and pkey is None: + # An API-token-only node: nothing to log in with, so no connection. + raise ConnectionException( + f"no SSH credential for {self.hostname}: set ssh_password or " + "ssh_private_key_str" + ) client = paramiko.SSHClient() try: HostKeyCheck(self.optional_args, self.hostname, 22).connect( @@ -528,3 +526,11 @@ def _load_private_key(text: str) -> Any: # paramiko.PKey; paramiko is imported continue logger.warning("The SSH private key could not be read; logging in with the password") return None + + +def _ssh_errors() -> tuple[type[Exception], ...]: + """What a command that could not run raises: the socket's errors, timeouts + included, and paramiko's (a refused login, a closed session).""" + import paramiko + + return (OSError, paramiko.SSHException) diff --git a/napalm_proxmox/interfaces_mixin.py b/napalm_proxmox/interfaces_mixin.py index 0868e37..8e0eac7 100644 --- a/napalm_proxmox/interfaces_mixin.py +++ b/napalm_proxmox/interfaces_mixin.py @@ -160,9 +160,9 @@ class ProxmoxInterfaceMixin: def get_arp_table(self, vrf: str = "") -> list[_JsonDict]: """Return ARP table. - Proxmox does not expose ARP via the REST API directly. We attempt - to read it via the node's ``/proc/net/arp`` through the Proxmox - exec endpoint. If that is unavailable, an empty list is returned. + Proxmox does not expose ARP via the REST API, so the node's + ``/proc/net/arp`` is read over SSH; a node that cannot be reached + there raises. """ raw = self._exec_ssh_command("cat /proc/net/arp") if not raw: diff --git a/napalm_proxmox/routing_mixin.py b/napalm_proxmox/routing_mixin.py index df3ede7..e36d336 100644 --- a/napalm_proxmox/routing_mixin.py +++ b/napalm_proxmox/routing_mixin.py @@ -58,7 +58,7 @@ class ProxmoxRoutingMixin: raw = self._exec_ssh_command(f"{cmd} 2>&1 || true") if not raw: - return {"error": "Ping command not available via exec endpoint"} + return {"error": "ping printed nothing on the node"} # Detect common failure strings before parsing statistics _error_patterns = ( @@ -126,7 +126,7 @@ class ProxmoxRoutingMixin: raw = self._exec_ssh_command(" ".join(cmd_parts) + " 2>&1 || true") if not raw: - return {"error": "traceroute not available via exec endpoint"} + return {"error": "traceroute printed nothing on the node"} hops: _JsonDict = {} for line in raw.splitlines(): diff --git a/napalm_proxmox/system_mixin.py b/napalm_proxmox/system_mixin.py index 2a013bf..bf3e23f 100644 --- a/napalm_proxmox/system_mixin.py +++ b/napalm_proxmox/system_mixin.py @@ -504,7 +504,8 @@ class ProxmoxSystemMixin: def get_snmp_config(self): """Return SNMP agent config if snmpd is installed and running on the node. - Uses _exec_ssh_command (Proxmox API exec or SSH) to inspect the node. + Uses _exec_ssh_command (the node's SSH session) to inspect the node; + raises when the node cannot be reached there. Returns a SNMPConfigDict or None. """ try: diff --git a/napalm_proxmox/vm_provision_mixin.py b/napalm_proxmox/vm_provision_mixin.py index 9d1d3e8..051b082 100644 --- a/napalm_proxmox/vm_provision_mixin.py +++ b/napalm_proxmox/vm_provision_mixin.py @@ -129,10 +129,10 @@ class ProxmoxVMProvisionMixin: """ Execute a shell command on the Proxmox node via SSH, raising on failure. - Unlike ``_exec_ssh_command`` (best-effort, fixed timeout, swallows - errors), this is for critical provisioning steps — image download, - disk import — where a non-zero exit or a caller-specific timeout must - surface as a hard failure rather than an empty string. + Unlike ``_exec_ssh_command`` (fixed timeout, exit status not checked), + this is for critical provisioning steps — image download, disk import — + where a non-zero exit or a caller-specific timeout must surface as a + hard failure. """ _, stdout, stderr = self._node_ssh().exec_command(command, timeout=timeout) exit_status = stdout.channel.recv_exit_status() diff --git a/tests/conftest.py b/tests/conftest.py index b927a36..dd90d37 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -120,6 +120,36 @@ PVE_USERS = [ ] +# --------------------------------------------------------------------------- +# The node's shell +# --------------------------------------------------------------------------- + + +class _Stream: + def __init__(self, data: str) -> None: + self._data = data.encode() + + def read(self) -> bytes: + return self._data + + +class NodeShell: + """The node's SSH session for the driver fixtures. + + Every command prints ``output`` on stdout, or ``outputs[command]`` when the + command is there. A test sets either through ``driver._node_ssh()``. + """ + + def __init__(self, output: str = "") -> None: + self.output = output + self.outputs: dict[str, str] = {} + self.commands: list[str] = [] + + def exec_command(self, command: str, timeout: int | None = None): + self.commands.append(command) + return None, _Stream(self.outputs.get(command, self.output)), _Stream("") + + # --------------------------------------------------------------------------- # Driver fixture # --------------------------------------------------------------------------- @@ -137,7 +167,6 @@ def _build_mock_api( ntp=None, netstat=None, pve_users=None, - exec_return="", sensors=None, cluster_status=None, ): @@ -159,7 +188,6 @@ def _build_mock_api( node_res.dns.get.return_value = dns or DNS_INFO node_res.ntp.get.return_value = ntp or NTP_INFO node_res.netstat.get.return_value = netstat or NODE_NETSTAT - node_res.execute.post.return_value = {"data": exec_return} node_res.hardware.sensors.get.return_value = sensors or [] node_res.time.get.return_value = {} node_res.subscription.get.return_value = {} @@ -204,12 +232,13 @@ def driver(): mock_api = _build_mock_api() with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api): drv.open() + drv._node_ssh = MagicMock(return_value=NodeShell()) return drv @pytest.fixture def driver_with_exec(request): - """Return a driver whose exec endpoint returns a parametrised string.""" + """Return a driver whose node shell prints a parametrised string.""" exec_output = getattr(request, "param", "") drv = ProxmoxDriver( hostname="pve1.example.com", @@ -217,7 +246,8 @@ def driver_with_exec(request): password="secret", optional_args={"verify_ssl": False}, ) - mock_api = _build_mock_api(exec_return=exec_output) + mock_api = _build_mock_api() with patch("napalm_proxmox.driver.ProxmoxAPI", return_value=mock_api): drv.open() + drv._node_ssh = MagicMock(return_value=NodeShell(exec_output)) return drv diff --git a/tests/test_misc.py b/tests/test_misc.py index c374cac..aeec986 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -26,19 +26,19 @@ class TestGetNTPStats: ) def test_synchronized_server(self, driver): - driver._node_api().execute.post.return_value = {"data": self.NTP_OUTPUT} + driver._node_ssh().output = self.NTP_OUTPUT stats = driver.get_ntp_stats() synced = [s for s in stats if s["synchronized"]] assert len(synced) == 1 assert synced[0]["remote"] == "192.168.1.1" def test_stratum(self, driver): - driver._node_api().execute.post.return_value = {"data": self.NTP_OUTPUT} + driver._node_ssh().output = self.NTP_OUTPUT stats = driver.get_ntp_stats() assert stats[0]["stratum"] == 4 def test_empty_when_no_ntp_command(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" stats = driver.get_ntp_stats() assert stats == [] @@ -53,7 +53,7 @@ class TestGetSNMPInformation: ) def test_communities(self, driver): - driver._node_api().execute.post.return_value = {"data": self.SNMP_CONF} + driver._node_ssh().output = self.SNMP_CONF result = driver.get_snmp_information() assert "public" in result["community"] assert result["community"]["public"]["mode"] == "ro" @@ -61,30 +61,30 @@ class TestGetSNMPInformation: assert result["community"]["private"]["mode"] == "rw" def test_location(self, driver): - driver._node_api().execute.post.return_value = {"data": self.SNMP_CONF} + driver._node_ssh().output = self.SNMP_CONF result = driver.get_snmp_information() assert result["location"] == "Server Room A" def test_contact(self, driver): - driver._node_api().execute.post.return_value = {"data": self.SNMP_CONF} + driver._node_ssh().output = self.SNMP_CONF result = driver.get_snmp_information() assert result["contact"] == "admin@example.com" def test_chassis_id_is_node_name(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_snmp_information() assert result["chassis_id"] == "pve1" class TestGetUsers: def test_pve_users_returned(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_users() assert "root@pam" in result assert "napalm@pam" in result def test_user_has_required_keys(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_users() for _user, data in result.items(): assert "level" in data @@ -93,7 +93,7 @@ class TestGetUsers: def test_root_local_user_from_passwd(self, driver): passwd_output = "root:x:0:0:root:/root:/bin/bash\n" - driver._node_api().execute.post.return_value = {"data": passwd_output} + driver._node_ssh().output = passwd_output result = driver.get_users() assert "root" in result assert result["root"]["level"] == 15 @@ -136,14 +136,14 @@ class TestConfigManagement: assert diff == "" def test_get_config_running(self, driver): - driver._node_api().execute.post.return_value = {"data": self.RUNNING} + driver._node_ssh().output = self.RUNNING result = driver.get_config(retrieve="running") assert self.RUNNING in result["running"] assert result["candidate"] == "" def test_get_config_sanitized_redacts_password(self, driver): raw = "password mysecret\nauto eth0\n" - driver._node_api().execute.post.return_value = {"data": raw} + driver._node_ssh().output = raw result = driver.get_config(retrieve="running", sanitized=True) assert "mysecret" not in result["running"] assert "****" in result["running"] @@ -168,7 +168,7 @@ class TestPing: ) def test_success(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PING_SUCCESS} + driver._node_ssh().output = self.PING_SUCCESS result = driver.ping("8.8.8.8") assert "success" in result assert result["success"]["probes_sent"] == 2 @@ -177,7 +177,7 @@ class TestPing: def test_every_reply_is_a_result(self, driver): """iputils puts "from " before icmp_seq (napalm-proxmox#12).""" - driver._node_api().execute.post.return_value = {"data": self.PING_SUCCESS} + driver._node_ssh().output = self.PING_SUCCESS result = driver.ping("8.8.8.8") assert result["success"]["results"] == [ {"ip_address": "8.8.8.8", "rtt": 10.5}, @@ -193,7 +193,7 @@ class TestPing: "2 packets transmitted, 2 received, 0% packet loss, time 1001ms\n" "rtt min/avg/max/mdev = 0.045/61.5/123.0/61.4 ms\n" ) - driver._node_api().execute.post.return_value = {"data": output} + driver._node_ssh().output = output result = driver.ping("dns.google") assert result["success"]["results"] == [ {"ip_address": "8.8.4.4", "rtt": 123.0}, @@ -201,12 +201,12 @@ class TestPing: ] def test_failure(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PING_FAIL} + driver._node_ssh().output = self.PING_FAIL result = driver.ping("badhost") assert "error" in result - def test_exec_unavailable(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + def test_no_output(self, driver): + driver._node_ssh().output = "" result = driver.ping("8.8.8.8") assert "error" in result @@ -220,44 +220,34 @@ class TestTraceroute: ) def test_success_hops(self, driver): - driver._node_api().execute.post.return_value = {"data": self.TR_OUTPUT} + driver._node_ssh().output = self.TR_OUTPUT result = driver.traceroute("8.8.8.8") assert "success" in result assert 1 in result["success"] assert 3 in result["success"] def test_hop_rtt(self, driver): - driver._node_api().execute.post.return_value = {"data": self.TR_OUTPUT} + driver._node_ssh().output = self.TR_OUTPUT result = driver.traceroute("8.8.8.8") hop1 = result["success"][1]["probes"] assert hop1[1]["ip_address"] == "10.0.0.254" def test_empty_returns_error(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.traceroute("8.8.8.8") assert "error" in result class TestCLI: def test_cli_single_command(self, driver): - driver._node_api().execute.post.return_value = {"data": "hello world"} + driver._node_ssh().output = "hello world" result = driver.cli(["echo hello world"]) assert result["echo hello world"] == "hello world" def test_cli_multiple_commands(self, driver): - responses = {"uptime": "up 2 days", "hostname": "pve1"} - call_count = 0 - - def _exec(command): - nonlocal call_count - cmd = command - call_count += 1 - return {"data": responses.get(cmd, "")} - - driver._node_api().execute.post.side_effect = _exec + driver._node_ssh().outputs = {"uptime": "up 2 days", "hostname": "pve1"} result = driver.cli(["uptime", "hostname"]) - assert "uptime" in result - assert "hostname" in result + assert result == {"uptime": "up 2 days", "hostname": "pve1"} class TestGetRouteTo: @@ -267,12 +257,12 @@ class TestGetRouteTo: ) def test_returns_routes(self, driver): - driver._node_api().execute.post.return_value = {"data": self.ROUTE_OUTPUT} + driver._node_ssh().output = self.ROUTE_OUTPUT result = driver.get_route_to(destination="10.0.0.0/24") assert "10.0.0.0/24" in result def test_route_fields(self, driver): - driver._node_api().execute.post.return_value = {"data": self.ROUTE_OUTPUT} + driver._node_ssh().output = self.ROUTE_OUTPUT result = driver.get_route_to() for _prefix, routes in result.items(): for route in routes: @@ -281,7 +271,7 @@ class TestGetRouteTo: assert "outgoing_interface" in route def test_protocol_filter(self, driver): - driver._node_api().execute.post.return_value = {"data": self.ROUTE_OUTPUT} + driver._node_ssh().output = self.ROUTE_OUTPUT result = driver.get_route_to(protocol="dhcp") for _prefix, routes in result.items(): for route in routes: @@ -316,7 +306,7 @@ class TestLLDPNeighbors: assert result["eth0"][0]["hostname"] == "sw01.example.com" def test_empty_when_no_lldp(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + driver._node_ssh().output = "" result = driver.get_lldp_neighbors() assert result == {} @@ -329,18 +319,18 @@ class TestIPv6Neighbors: ) def test_neighbors_parsed(self, driver): - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() assert len(result) == 2 # FAILED entry excluded def test_mac_normalized(self, driver): - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() macs = {e["mac"] for e in result} assert "aa:bb:cc:dd:ee:ff" in macs def test_state_present(self, driver): - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() states = {e["state"] for e in result} assert "REACHABLE" in states @@ -348,7 +338,7 @@ class TestIPv6Neighbors: def test_age_is_unknown(self, driver): # `ip neigh` reports no age, as for the IPv4 table; 0.0 would read as # "confirmed just now" (NetOrk/netork#1327). - driver._node_api().execute.post.return_value = {"data": self.IPV6_NEIGH} + driver._node_ssh().output = self.IPV6_NEIGH result = driver.get_ipv6_neighbors_table() assert {e["age"] for e in result} == {-1.0} diff --git a/tests/test_node_exec.py b/tests/test_node_exec.py new file mode 100644 index 0000000..173e1b3 --- /dev/null +++ b/tests/test_node_exec.py @@ -0,0 +1,109 @@ +"""Commands on the node run over its SSH session, and a failure to run one raises (#17). + +``_exec_ssh_command`` used to post to ``/nodes/{node}/execute`` first (a batch of API +calls, not a shell), to return stderr as output, and to return ``""`` whenever the +command could not run at all: no credential, a refused login, a changed host key, a +timeout. "Nothing there" and "could not look" read the same. +""" + +from __future__ import annotations + +from unittest.mock import MagicMock + +import paramiko +import pytest +from napalm.base.exceptions import ConnectionException + +from napalm_proxmox.driver import ProxmoxDriver +from tests.test_ssh_host_key import client # noqa: F401 - the fake paramiko client + + +class _Stream: + def __init__(self, data: bytes | Exception) -> None: + self._data = data + + def read(self) -> bytes: + if isinstance(self._data, Exception): + raise self._data + return self._data + + +class _Shell: + """The node's SSH session: prints *out* on stdout and *err* on stderr.""" + + def __init__(self, out: bytes | Exception = b"", err: bytes = b"") -> None: + self.out, self.err = out, err + self.commands: list[str] = [] + + def exec_command(self, command: str, timeout: int | None = None): + self.commands.append(command) + return None, _Stream(self.out), _Stream(self.err) + + +def _driver(shell: _Shell | None = None, **kwargs) -> ProxmoxDriver: + driver = ProxmoxDriver("10.0.0.9", "root", kwargs.pop("password", "s3cret"), **kwargs) + driver._node_name = "pve1" + driver._api = MagicMock() + if shell is not None: + driver._node_ssh = MagicMock(return_value=shell) # type: ignore[method-assign] + return driver + + +def test_a_command_goes_straight_to_the_ssh_session(): + shell = _Shell(b"6.8.12-4-pve\n") + driver = _driver(shell) + + assert driver._exec_ssh_command("uname -r") == "6.8.12-4-pve" + assert shell.commands == ["uname -r"] + driver._api.nodes.return_value.execute.post.assert_not_called() + + +def test_stderr_is_not_output(): + shell = _Shell(b"", b"cat: /etc/snmp/snmpd.conf: No such file or directory\n") + + assert _driver(shell)._exec_ssh_command("cat /etc/snmp/snmpd.conf") == "" + + +def test_a_refused_login_raises(): + driver = _driver() + driver._node_ssh = MagicMock( # type: ignore[method-assign] + side_effect=paramiko.AuthenticationException("Authentication failed.") + ) + + with pytest.raises(ConnectionException, match="Authentication failed") as err: + driver._exec_ssh_command("uptime") + assert isinstance(err.value.__cause__, paramiko.AuthenticationException) + + +def test_an_unreachable_node_raises(): + driver = _driver() + driver._node_ssh = MagicMock( # type: ignore[method-assign] + side_effect=OSError(113, "No route to host") + ) + + with pytest.raises(ConnectionException, match="No route to host"): + driver._exec_ssh_command("uptime") + + +def test_a_timeout_raises(): + shell = _Shell(TimeoutError("timed out")) + + with pytest.raises(ConnectionException, match="timed out"): + _driver(shell)._exec_ssh_command("apt list --upgradable") + + +def test_without_an_ssh_credential_nothing_connects(client): # noqa: F811 + # An API-token-only node: no API password, no SSH password, no key. + driver = _driver(password="", optional_args={"token_name": "root@pam!t", "token_value": "x"}) + + with pytest.raises(ConnectionException, match="no SSH credential"): + driver._exec_ssh_command("command -v lldpd") + assert client.connected_with is None + + +def test_get_snmp_config_raises_where_it_cannot_look(client): # noqa: F811 + """netOrk reads an exception as "SSH not available" and raises no SNMP alarm.""" + driver = _driver(password="", optional_args={"token_name": "root@pam!t", "token_value": "x"}) + + with pytest.raises(ConnectionException): + driver.get_snmp_config() diff --git a/tests/test_ovs_and_arp.py b/tests/test_ovs_and_arp.py index 464f84c..091e3a7 100644 --- a/tests/test_ovs_and_arp.py +++ b/tests/test_ovs_and_arp.py @@ -103,23 +103,23 @@ class TestGetARPTable: ) def test_arp_entries_returned(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PROC_ARP} + driver._node_ssh().output = self.PROC_ARP result = driver.get_arp_table() assert len(result) == 2 # 00:00:00:00:00:00 excluded def test_mac_normalized(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PROC_ARP} + driver._node_ssh().output = self.PROC_ARP result = driver.get_arp_table() macs = {e["mac"] for e in result} assert "aa:bb:cc:dd:ee:ff" in macs def test_vrf_filter(self, driver): - driver._node_api().execute.post.return_value = {"data": self.PROC_ARP} + driver._node_ssh().output = self.PROC_ARP result = driver.get_arp_table(vrf="vmbr0") assert all(e["interface"] == "vmbr0" for e in result) - def test_empty_when_exec_unavailable(self, driver): - driver._node_api().execute.post.return_value = {"data": ""} + def test_empty_when_the_node_prints_nothing(self, driver): + driver._node_ssh().output = "" result = driver.get_arp_table() assert result == [] diff --git a/tests/test_ssh_host_key.py b/tests/test_ssh_host_key.py index e977084..e858d7d 100644 --- a/tests/test_ssh_host_key.py +++ b/tests/test_ssh_host_key.py @@ -1,7 +1,7 @@ """The node's SSH session verifies its host key, and reports the one it sees (NetOrk/netork#1167). -Both ways onto the node's shell — the best-effort ``_exec_ssh_command`` and the -provisioning steps' ``_run_node_command`` — share one session, opened on port 22. +Both ways onto the node's shell — ``_exec_ssh_command`` and the provisioning +steps' ``_run_node_command`` — share one session, opened on port 22. """ from __future__ import annotations @@ -87,7 +87,6 @@ def _driver(seen: list, known: dict | None = None) -> ProxmoxDriver: if known is not None: args[KNOWN_KEYS_ARG] = known driver = ProxmoxDriver("10.0.0.9", "root", "s3cret", optional_args=args) - driver._node_api = MagicMock(side_effect=RuntimeError("no exec endpoint")) # type: ignore[method-assign] return driver @@ -116,10 +115,12 @@ def test_a_changed_key_stops_a_provisioning_step(client): assert seen[0]["accepted"] is False -def test_a_changed_key_leaves_the_best_effort_read_empty(client): +def test_a_changed_key_stops_a_read_too(client): + """It used to read as empty output, "nothing there" (#17).""" seen: list = [] driver = _driver(seen, {"22": SshHostKey.of(OTHER_KEY).as_dict()}) - assert driver._exec_ssh_command("cat /etc/os-release") == "" + with pytest.raises(SshHostKeyMismatch): + driver._exec_ssh_command("cat /etc/os-release") assert client.commands == [] assert seen[0]["accepted"] is False