fix(ssh): raise when a node command cannot run, drop the execute call
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 40s
driver / security (pull_request) Successful in 1m15s
CI / driver (pull_request) Successful in 2m53s
driver / publish (pull_request) Skipped
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 35s
driver / build (pull_request) Successful in 40s
driver / security (pull_request) Successful in 1m15s
CI / driver (pull_request) Successful in 2m53s
driver / publish (pull_request) Skipped
_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
This commit is contained in:
1 parent
bfba804ec2
commit
daf1b519eb
13 files changed
+261
-108
No files matched your search
@@ -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()
|
||||
Reference in new issue
Block a user