"""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()