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.
This commit is contained in:
1 parent
b26584c7da
commit
03cf40de99
3 files changed
+82
-27
No files matched your search
@@ -4,11 +4,16 @@ 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
|
||||
@@ -16,34 +21,31 @@ from napalm.base.exceptions import CommandErrorException
|
||||
|
||||
from napalm_procurve.procurve import ProcurveDriver
|
||||
|
||||
SHOW_SNMP_SERVER = """\
|
||||
FIXTURES = Path(__file__).parent.parent / "fixtures"
|
||||
|
||||
Community Names
|
||||
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()
|
||||
|
||||
Community Name MIB View Write Access
|
||||
-------------------------------- -------- ------------
|
||||
private Manager Unrestricted
|
||||
c0mm Manager Restricted
|
||||
_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
|
||||
)
|
||||
|
||||
Trap Receivers
|
||||
|
||||
Link-Change Traps Enabled on Ports [All] : All
|
||||
"""
|
||||
|
||||
NO_COMMUNITY = """\
|
||||
|
||||
Community Names
|
||||
|
||||
Community Name MIB View Write Access
|
||||
-------------------------------- -------- ------------
|
||||
"""
|
||||
#: The recording with its only community removed.
|
||||
NO_COMMUNITY = SHOW_SNMP_SERVER.replace(_PUBLIC, "")
|
||||
|
||||
|
||||
def _cli_answer(output: str, status: str = "CCS_SUCCESS", http: int = 200) -> MagicMock:
|
||||
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": base64.b64encode(output.encode()).decode(),
|
||||
"result_base64_encoded": encoded,
|
||||
"status": status,
|
||||
}
|
||||
return resp
|
||||
@@ -57,22 +59,25 @@ def _rest_driver(resp: MagicMock) -> ProcurveDriver:
|
||||
return driver
|
||||
|
||||
|
||||
def test_over_rest_the_read_only_community_is_read_through_the_cli_endpoint():
|
||||
driver = _rest_driver(_cli_answer(SHOW_SNMP_SERVER))
|
||||
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": "c0mm", "port": 161, "version": "2c"}
|
||||
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(NO_COMMUNITY)).get_snmp_config() is None
|
||||
assert _rest_driver(_cli_answer(_b64(NO_COMMUNITY))).get_snmp_config() is None
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"resp",
|
||||
[_cli_answer("", http=401), _cli_answer("Invalid input: snmp-server", status="CCS_FAILURE")],
|
||||
[
|
||||
_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"):
|
||||
@@ -83,11 +88,11 @@ 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
|
||||
driver._device.send_command.return_value = TWO_COMMUNITIES
|
||||
|
||||
assert driver.get_snmp_config() == {
|
||||
"running": True,
|
||||
"community": "c0mm",
|
||||
"community": "public",
|
||||
"port": 161,
|
||||
"version": "2c",
|
||||
}
|
||||
Reference in new issue
Block a user