Files
napalm-hpe-aruba-procurve/tests/unit/test_snmp_config.py
T
Christian Manivong 03cf40de99 test(snmp): use the recorded show snmp-server
The #10 tests read a hand-written show snmp-server. They now read the
output recorded from a ProCurve switch: as the CLI prints it, and as the
REST /cli endpoint answers it, base64 with a trailing NUL byte. The
trap receiver's address is replaced. The read-only preference and the
no-community case put a line into, or take one out of, the recording.
2026-11-01 22:16:51 +01:00

99 lines
3.3 KiB
Python

"""get_snmp_config reads the switch's communities over every transport (#10).
Over the REST API there is no CLI session, so the getter returned None and a
switch managed over REST always read as "SNMP not configured". The API's
``/cli`` endpoint runs ``show snmp-server`` and answers with the output in
base64, which the SSH path's parser reads.
The fixtures are ``show snmp-server`` recorded from a ProCurve switch: as the SSH
CLI prints it, and as the REST ``/cli`` endpoint answers it (base64, ending in a
NUL byte); the trap receiver's address is replaced.
"""
from __future__ import annotations
import base64
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from napalm.base.exceptions import CommandErrorException
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()
_PUBLIC = " public Operator Restricted \n"
assert SHOW_SNMP_SERVER.count(_PUBLIC) == 1
#: The recording with a read-write community put in before the read-only one.
TWO_COMMUNITIES = SHOW_SNMP_SERVER.replace(
_PUBLIC, " private Manager Unrestricted\n" + _PUBLIC
)
#: The recording with its only community removed.
NO_COMMUNITY = SHOW_SNMP_SERVER.replace(_PUBLIC, "")
def _b64(output: str) -> str:
return base64.b64encode(output.encode()).decode()
def _cli_answer(encoded: str, status: str = "CCS_SUCCESS", http: int = 200) -> MagicMock:
resp = MagicMock(ok=200 <= http < 300, status_code=http, text="")
resp.json.return_value = {
"cmd": "show snmp-server",
"result_base64_encoded": encoded,
"status": status,
}
return resp
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_the_community_is_read_through_the_cli_endpoint():
driver = _rest_driver(_cli_answer(SHOW_SNMP_SERVER_REST))
config = driver.get_snmp_config()
assert config == {"running": True, "community": "public", "port": 161, "version": "2c"}
driver._api.post.assert_called_once_with("cli", json={"cmd": "show snmp-server"})
def test_over_rest_a_switch_without_a_community_has_no_snmp_config():
assert _rest_driver(_cli_answer(_b64(NO_COMMUNITY))).get_snmp_config() is None
@pytest.mark.parametrize(
"resp",
[
_cli_answer("", http=401),
_cli_answer(_b64("Invalid input: snmp-server"), status="CCS_FAILURE"),
],
)
def test_over_rest_a_failed_read_raises_instead_of_reading_as_no_snmp(resp):
with pytest.raises(CommandErrorException, match="show snmp-server"):
_rest_driver(resp).get_snmp_config()
def test_over_ssh_the_read_only_community_is_preferred():
driver = ProcurveDriver("10.0.0.40", "manager", "s3cret")
driver._transport = "ssh"
driver._device = MagicMock(base_prompt="swt")
driver._device.send_command.return_value = TWO_COMMUNITIES
assert driver.get_snmp_config() == {
"running": True,
"community": "public",
"port": 161,
"version": "2c",
}