Files
napalm-hpe-aruba-procurve/tests/unit/test_cli.py
T
Christian Manivong 1d5399e51b
driver / standard (pull_request) Successful in 14s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 58s
driver / test (3.12) (pull_request) Successful in 1m0s
driver / test (3.13) (pull_request) Successful in 49s
driver / lowest (pull_request) Successful in 24s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 56s
CI / driver (pull_request) Successful in 2m25s
driver / publish (pull_request) Skipped
fix(cli): decode the REST CLI answer
Over the REST API, cli() returned the /cli endpoint's
result_base64_encoded field as it came: base64 instead of the command's
output. It now decodes it and drops the NUL byte the switch ends it with,
in the same parser the SNMP read over REST uses.

New finding, confirmed by the show snmp-server answer recorded from a
ProCurve switch; no issue filed.
2026-11-01 22:17:59 +01:00

45 lines
1.4 KiB
Python

"""cli() over REST returns the command's output, not the API's base64 of it.
The REST ``/cli`` endpoint answers ``{"result_base64_encoded": ...}``: the output in
base64, ending in a NUL byte. cli() returned that string as it came.
"""
from __future__ import annotations
from pathlib import Path
from unittest.mock import MagicMock
from napalm_procurve.procurve import ProcurveDriver
FIXTURES = Path(__file__).parent.parent / "fixtures"
SHOW_SNMP_SERVER = (FIXTURES / "procurve-show-snmp-server.txt").read_text(encoding="utf-8")
SHOW_SNMP_SERVER_REST = (FIXTURES / "procurve-rest-cli-show-snmp-server.b64").read_text().strip()
def _rest_driver(resp: MagicMock) -> ProcurveDriver:
driver = ProcurveDriver("10.0.0.40", "manager", "s3cret")
driver._transport = "api"
driver._api = MagicMock()
driver._api.post.return_value = resp
return driver
def test_over_rest_cli_returns_the_decoded_output():
resp = MagicMock(ok=True, status_code=200)
resp.json.return_value = {
"cmd": "show snmp-server",
"result_base64_encoded": SHOW_SNMP_SERVER_REST,
"status": "CCS_SUCCESS",
}
out = _rest_driver(resp).cli(["show snmp-server"])
assert out == {"show snmp-server": SHOW_SNMP_SERVER}
def test_over_rest_cli_names_a_failed_request():
resp = MagicMock(ok=False, status_code=401)
assert _rest_driver(resp).cli(["show version"]) == {"show version": "Error: HTTP 401"}