Files
napalm-hpe-aruba-procurve/tests/unit/test_snmp_config.py
T
Christian Manivong b26584c7da
driver / standard (pull_request) Successful in 15s
driver / lint (pull_request) Successful in 12s
driver / types (pull_request) Successful in 57s
driver / test (3.12) (pull_request) Successful in 56s
driver / test (3.13) (pull_request) Successful in 47s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 25s
driver / security (pull_request) Successful in 1m6s
CI / driver (pull_request) Successful in 2m29s
driver / publish (pull_request) Skipped
fix(snmp): read the communities over the REST API too
get_snmp_config sent "show snmp-server" through the CLI session, which
does not exist on the REST transport, and returned None, so every switch
managed over REST read as "SNMP not configured". Over REST the command
now runs through the API's /cli endpoint and the output goes through the
parser the SSH path uses. A failed REST read raises.

Closes #10
2026-11-01 21:51:06 +01:00

94 lines
2.8 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.
"""
from __future__ import annotations
import base64
from unittest.mock import MagicMock
import pytest
from napalm.base.exceptions import CommandErrorException
from napalm_procurve.procurve import ProcurveDriver
SHOW_SNMP_SERVER = """\
Community Names
Community Name MIB View Write Access
-------------------------------- -------- ------------
private Manager Unrestricted
c0mm Manager Restricted
Trap Receivers
Link-Change Traps Enabled on Ports [All] : All
"""
NO_COMMUNITY = """\
Community Names
Community Name MIB View Write Access
-------------------------------- -------- ------------
"""
def _cli_answer(output: 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": base64.b64encode(output.encode()).decode(),
"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_read_only_community_is_read_through_the_cli_endpoint():
driver = _rest_driver(_cli_answer(SHOW_SNMP_SERVER))
config = driver.get_snmp_config()
assert config == {"running": True, "community": "c0mm", "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(NO_COMMUNITY)).get_snmp_config() is None
@pytest.mark.parametrize(
"resp",
[_cli_answer("", http=401), _cli_answer("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 = SHOW_SNMP_SERVER
assert driver.get_snmp_config() == {
"running": True,
"community": "c0mm",
"port": 161,
"version": "2c",
}