driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 25s
driver / types (pull_request) Successful in 1m15s
driver / test (3.12) (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m9s
CI / driver (pull_request) Successful in 2m54s
driver / publish (pull_request) Skipped
A switch whose REST API has no configuration is read over a temporary SSH session. That path read the running config only, whatever retrieve asked for, and returned an empty startup config. It now reads through the same code as the SSH transport: running and/or startup as asked, "show config" where "show startup-config" is unknown, and a part that could not be read raises. Closes #25
111 lines
3.2 KiB
Python
111 lines
3.2 KiB
Python
"""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
|