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
+34
-4
@@ -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
|
||||
Reference in new issue
Block a user