Merge pull request 'fix(config): read what retrieve asks in the REST-to-SSH fallback' (#26) from fix/bugs-2026-11-02b into main
driver / standard (push) Successful in 21s
driver / lint (push) Successful in 15s
driver / test (3.12) (push) Successful in 1m7s
driver / types (push) Successful in 1m17s
driver / test (3.13) (push) Successful in 57s
driver / lowest (push) Successful in 31s
driver / build (push) Successful in 33s
driver / security (push) Successful in 1m16s
CI / driver (push) Successful in 3m9s
driver / publish (push) Skipped
driver / standard (push) Successful in 21s
driver / lint (push) Successful in 15s
driver / test (3.12) (push) Successful in 1m7s
driver / types (push) Successful in 1m17s
driver / test (3.13) (push) Successful in 57s
driver / lowest (push) Successful in 31s
driver / build (push) Successful in 33s
driver / security (push) Successful in 1m16s
CI / driver (push) Successful in 3m9s
driver / publish (push) Skipped
This commit was merged in pull request #26.
This commit is contained in:
commit
bb8b3a7182
6 files changed
+141
-29
No files matched your search
@@ -60,6 +60,10 @@ All notable changes to this driver. The format follows
|
|||||||
PoE on a switch without it.
|
PoE on a switch without it.
|
||||||
- `get_config` raises when neither the REST API nor the SSH fallback could read the
|
- `get_config` raises when neither the REST API nor the SSH fallback could read the
|
||||||
configuration. It returned an empty running configuration.
|
configuration. It returned an empty running configuration.
|
||||||
|
- `get_config` on a switch whose REST API has no configuration (2530 / YA series) reads
|
||||||
|
over SSH what `retrieve` asks for, the startup configuration included (#25). That
|
||||||
|
fallback read the running configuration only, whatever was asked, and reported the
|
||||||
|
startup configuration as empty.
|
||||||
- `cli()` over the REST API raises when the request fails. It returned
|
- `cli()` over the REST API raises when the request fails. It returned
|
||||||
`"Error: HTTP <status>"` as if that were the command's output.
|
`"Error: HTTP <status>"` as if that were the command's output.
|
||||||
|
|
||||||
|
|||||||
+23
-25
@@ -638,44 +638,42 @@ class ProcurveDriver(ConfigLifecycleMixin, SwitchDriver):
|
|||||||
result = {}
|
result = {}
|
||||||
if result.get("running"):
|
if result.get("running"):
|
||||||
return result
|
return result
|
||||||
# REST endpoint not available on this firmware (e.g. HP 2530 / YA series).
|
# No REST config on this firmware (e.g. 2530 / YA series): SSH. One session
|
||||||
# HP switches allow only one session per user — close the REST session
|
# per user, so the REST one is closed first for the switch to accept SSH.
|
||||||
# first so the switch accepts the SSH connection.
|
|
||||||
try:
|
try:
|
||||||
self._api._session.close()
|
self._api._session.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.debug("closing the REST session: ignored", exc_info=True)
|
logger.debug("closing the REST session: ignored", exc_info=True)
|
||||||
running = self._get_config_via_ssh()
|
return self._get_config_via_ssh(retrieve)
|
||||||
return {"running": running, "startup": "", "candidate": ""}
|
return self._read_config(retrieve)
|
||||||
|
|
||||||
running = ""
|
def _read_config(self, retrieve: str) -> dict[str, str]:
|
||||||
startup = ""
|
"""Running and/or startup config as *retrieve* asks; a part not asked for is ""."""
|
||||||
candidate = self._candidate_config or ""
|
commands = {
|
||||||
|
"running": "show running-config",
|
||||||
|
"startup": ["show startup-config", "show config"],
|
||||||
|
}
|
||||||
|
config = {"running": "", "startup": "", "candidate": self._candidate_config or ""}
|
||||||
|
for part, command in commands.items():
|
||||||
|
if retrieve in ("all", part):
|
||||||
|
config[part] = self._send_command(command)
|
||||||
|
return config
|
||||||
|
|
||||||
if retrieve in ("all", "running"):
|
def _get_config_via_ssh(self, retrieve: str = "running") -> dict[str, str]:
|
||||||
running = self._send_command("show running-config")
|
"""The config over a temporary SSH session on port 22 (``port`` is the REST API's)."""
|
||||||
if retrieve in ("all", "startup"):
|
device = self._device
|
||||||
startup = self._send_command(["show startup-config", "show config"])
|
|
||||||
|
|
||||||
return {"running": running, "startup": startup, "candidate": candidate}
|
|
||||||
|
|
||||||
def _get_config_via_ssh(self) -> str:
|
|
||||||
"""Retrieve running-config via netmiko on port 22.
|
|
||||||
|
|
||||||
HP ProCurve switches do not support exec_channel SSH; they require
|
|
||||||
an interactive shell (invoke_shell), which netmiko's hp_procurve
|
|
||||||
device type handles correctly. Port 22: ``port`` is the REST API's.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
conn = self._ssh_connect(port=_SSH_PORT_BESIDE_API)
|
self._device = conn = self._ssh_connect(port=_SSH_PORT_BESIDE_API)
|
||||||
try:
|
try:
|
||||||
return conn.send_command("show running-config")
|
return self._read_config(retrieve)
|
||||||
finally:
|
finally:
|
||||||
conn.disconnect()
|
conn.disconnect()
|
||||||
except ConnectionException: # a changed host key, say: as it is
|
except ConnectionException: # a changed host key, say: as it is
|
||||||
raise
|
raise
|
||||||
except Exception as exc: # a failed read raises (#22)
|
except Exception as exc: # a failed read raises (#22, #25)
|
||||||
raise ConnectionException(f"SSH config read failed for {self.hostname}: {exc}") from exc
|
raise ConnectionException(f"SSH config read failed for {self.hostname}: {exc}") from exc
|
||||||
|
finally:
|
||||||
|
self._device = device
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# NAPALM: get_environment
|
# NAPALM: get_environment
|
||||||
|
|||||||
@@ -27,7 +27,7 @@
|
|||||||
"napalm_procurve.procurve"
|
"napalm_procurve.procurve"
|
||||||
],
|
],
|
||||||
"python_file_lines": {
|
"python_file_lines": {
|
||||||
"napalm_procurve/procurve.py": 1385
|
"napalm_procurve/procurve.py": 1383
|
||||||
},
|
},
|
||||||
"untyped_public": []
|
"untyped_public": []
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
"""The REST transport's SSH fallback reads the configuration as asked (#25).
|
||||||
|
|
||||||
|
A switch whose REST API has no running config (2530 / YA series) is read over a
|
||||||
|
temporary SSH session. That path returned the running config and an empty startup
|
||||||
|
config whatever ``retrieve`` asked for.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
from napalm.base.exceptions import ConnectionException
|
||||||
|
|
||||||
|
from napalm_procurve.api_client import ProcurveApiClient
|
||||||
|
from napalm_procurve.procurve import ProcurveDriver
|
||||||
|
|
||||||
|
OUTPUTS = {
|
||||||
|
"show running-config": "hostname sw1-running",
|
||||||
|
"show startup-config": "hostname sw1-startup",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _not_found() -> requests.Response:
|
||||||
|
resp = requests.Response()
|
||||||
|
resp.status_code = 404
|
||||||
|
resp._content = b""
|
||||||
|
return resp
|
||||||
|
|
||||||
|
|
||||||
|
def _ssh(outputs: dict[str, str]) -> MagicMock:
|
||||||
|
ssh = MagicMock()
|
||||||
|
ssh.base_prompt = "sw1"
|
||||||
|
|
||||||
|
def send_command(command: str, **_: object) -> str:
|
||||||
|
answer = outputs[command]
|
||||||
|
if isinstance(answer, Exception):
|
||||||
|
raise answer
|
||||||
|
return answer
|
||||||
|
|
||||||
|
ssh.send_command.side_effect = send_command
|
||||||
|
return ssh
|
||||||
|
|
||||||
|
|
||||||
|
def _driver(ssh: MagicMock) -> ProcurveDriver:
|
||||||
|
driver = ProcurveDriver("10.0.0.40", "manager", "s3cret")
|
||||||
|
driver._transport = "api"
|
||||||
|
driver._api = ProcurveApiClient("10.0.0.40", "manager", "s3cret")
|
||||||
|
driver._api.setup("v7", "https")
|
||||||
|
driver._api._session = MagicMock()
|
||||||
|
driver._api._session.request.return_value = _not_found()
|
||||||
|
driver._ssh_connect = MagicMock(return_value=ssh)
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_fallback_reads_the_startup_config_too():
|
||||||
|
config = _driver(_ssh(OUTPUTS)).get_config(retrieve="all")
|
||||||
|
|
||||||
|
assert config == {
|
||||||
|
"running": "hostname sw1-running",
|
||||||
|
"startup": "hostname sw1-startup",
|
||||||
|
"candidate": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("retrieve", "expected"),
|
||||||
|
[
|
||||||
|
("running", {"running": "hostname sw1-running", "startup": ""}),
|
||||||
|
("startup", {"running": "", "startup": "hostname sw1-startup"}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_the_fallback_reads_only_what_was_asked(retrieve, expected):
|
||||||
|
ssh = _ssh(OUTPUTS)
|
||||||
|
|
||||||
|
config = _driver(ssh).get_config(retrieve=retrieve)
|
||||||
|
|
||||||
|
assert {k: config[k] for k in ("running", "startup")} == expected
|
||||||
|
sent = [c.args[0] for c in ssh.send_command.call_args_list]
|
||||||
|
assert sent == [f"show {retrieve}-config"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_fallback_tries_show_config_where_show_startup_config_is_unknown():
|
||||||
|
ssh = _ssh(
|
||||||
|
{
|
||||||
|
**OUTPUTS,
|
||||||
|
"show startup-config": "Invalid input: startup-config",
|
||||||
|
"show config": "hostname sw1-startup",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
assert _driver(ssh).get_config(retrieve="startup")["startup"] == "hostname sw1-startup"
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_fallback_raises_when_the_startup_config_cannot_be_read():
|
||||||
|
ssh = _ssh({**OUTPUTS, "show startup-config": OSError("socket closed")})
|
||||||
|
|
||||||
|
with pytest.raises(ConnectionException, match="socket closed"):
|
||||||
|
_driver(ssh).get_config(retrieve="all")
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_fallback_closes_its_ssh_session_and_leaves_none_behind():
|
||||||
|
ssh = _ssh(OUTPUTS)
|
||||||
|
driver = _driver(ssh)
|
||||||
|
|
||||||
|
driver.get_config()
|
||||||
|
|
||||||
|
ssh.disconnect.assert_called_once()
|
||||||
|
assert driver._device is None
|
||||||
@@ -186,7 +186,7 @@ def test_get_config_raises_when_neither_rest_nor_ssh_reads_it():
|
|||||||
|
|
||||||
def test_get_config_reads_over_ssh_when_the_firmware_has_no_config_resource():
|
def test_get_config_reads_over_ssh_when_the_firmware_has_no_config_resource():
|
||||||
driver = _rest_driver({"running-config": _response(404)})
|
driver = _rest_driver({"running-config": _response(404)})
|
||||||
ssh = MagicMock()
|
ssh = MagicMock(base_prompt="sw1")
|
||||||
ssh.send_command.return_value = "hostname sw1"
|
ssh.send_command.return_value = "hostname sw1"
|
||||||
driver._ssh_connect = MagicMock(return_value=ssh)
|
driver._ssh_connect = MagicMock(return_value=ssh)
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ HANDLER = "napalm_procurve.procurve.ConnectHandler"
|
|||||||
|
|
||||||
|
|
||||||
def _device() -> MagicMock:
|
def _device() -> MagicMock:
|
||||||
device = MagicMock()
|
device = MagicMock(base_prompt="sw1")
|
||||||
device.remote_conn_pre.get_transport.return_value.get_remote_server_key.return_value = (
|
device.remote_conn_pre.get_transport.return_value.get_remote_server_key.return_value = (
|
||||||
SERVER_KEY
|
SERVER_KEY
|
||||||
)
|
)
|
||||||
@@ -96,7 +96,7 @@ def test_the_config_fallback_checks_the_key_of_port_22():
|
|||||||
seen: list = []
|
seen: list = []
|
||||||
driver = _driver(seen, {"22": SshHostKey.of(SERVER_KEY).as_dict()}, port=443)
|
driver = _driver(seen, {"22": SshHostKey.of(SERVER_KEY).as_dict()}, port=443)
|
||||||
with patch(HANDLER, return_value=_device()) as handler:
|
with patch(HANDLER, return_value=_device()) as handler:
|
||||||
assert driver._get_config_via_ssh() == "hostname x"
|
assert driver._get_config_via_ssh()["running"] == "hostname x"
|
||||||
assert handler.call_args.kwargs["port"] == 22
|
assert handler.call_args.kwargs["port"] == 22
|
||||||
assert handler.call_args.kwargs["ssh_strict"] is True
|
assert handler.call_args.kwargs["ssh_strict"] is True
|
||||||
assert seen[0]["port"] == 22 and seen[0]["accepted"] is True
|
assert seen[0]["port"] == 22 and seen[0]["accepted"] is True
|
||||||
|
|||||||
Reference in new issue
Block a user