From 03cf40de99503bb44eb65233d1c667c6a7cbb7c0 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 22:16:51 +0100 Subject: [PATCH] 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. --- .../procurve-rest-cli-show-snmp-server.b64 | 1 + tests/fixtures/procurve-show-snmp-server.txt | 49 +++++++++++++++ tests/unit/test_snmp_config.py | 59 ++++++++++--------- 3 files changed, 82 insertions(+), 27 deletions(-) create mode 100644 tests/fixtures/procurve-rest-cli-show-snmp-server.b64 create mode 100644 tests/fixtures/procurve-show-snmp-server.txt diff --git a/tests/fixtures/procurve-rest-cli-show-snmp-server.b64 b/tests/fixtures/procurve-rest-cli-show-snmp-server.b64 new file mode 100644 index 0000000..cd4ea69 --- /dev/null +++ b/tests/fixtures/procurve-rest-cli-show-snmp-server.b64 @@ -0,0 +1 @@ +CiBTTk1QIENvbW11bml0aWVzCgogIENvbW11bml0eSBOYW1lICAgICAgICAgICAgICAgICAgIE1JQiBWaWV3IFdyaXRlIEFjY2VzcwogIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tIC0tLS0tLS0tLS0tLQogIHB1YmxpYyAgICAgICAgICAgICAgICAgICAgICAgICAgIE9wZXJhdG9yIFJlc3RyaWN0ZWQgIAogCiBUcmFwIFJlY2VpdmVycwoKICBMaW5rLUNoYW5nZSBUcmFwcyBFbmFibGVkIG9uIFBvcnRzIFtBbGxdIDogQWxsCgogIFRyYXBzIENhdGVnb3J5ICAgICAgICAgICAgICAgICAgICAgICAgICBDdXJyZW50IFN0YXR1cyAKICBfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fICAgX19fX19fX19fX19fX19fX19fIAogIFNOTVAgQXV0aGVudGljYXRpb24gICAgICAgICAgICAgICAgICAgOiBFeHRlbmRlZCAKICBQYXNzd29yZCBjaGFuZ2UgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBMb2dpbiBmYWlsdXJlcyAgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBQb3J0LVNlY3VyaXR5ICAgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBBdXRob3JpemF0aW9uIFNlcnZlciBDb250YWN0ICAgICAgICAgIDogRW5hYmxlZCAKICBESENQLVNub29waW5nICAgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBESENQdjYtU25vb3BpbmcgT3V0IG9mIFJlc291cmNlICAgICAgIDogRW5hYmxlZCAKICBESENQdjYtU25vb3BpbmcgRXJyYW50IFJlcGxpZXMgICAgICAgIDogRW5hYmxlZCAKICBEeW5hbWljIEFSUCBQcm90ZWN0aW9uICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBEeW5hbWljIElQIExvY2tkb3duICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBEeW5hbWljIElQdjYgTG9ja2Rvd24gT3V0IG9mIFJlc291cmNlIDogRW5hYmxlZCAKICBEeW5hbWljIElQdjYgTG9ja2Rvd24gVmlvbGF0aW9ucyAgICAgIDogRW5hYmxlZCAKICBTdGFydHVwIENvbmZpZyBjaGFuZ2UgICAgICAgICAgICAgICAgIDogRGlzYWJsZWQKICBSdW5uaW5nIENvbmZpZyBDaGFuZ2UgICAgICAgICAgICAgICAgIDogRGlzYWJsZWQKICBNQUMgYWRkcmVzcyB0YWJsZSBjaGFuZ2VzICAgICAgICAgICAgIDogRGlzYWJsZWQKICBNQUMgQWRkcmVzcyBDb3VudCAgICAgICAgICAgICAgICAgICAgIDogRGlzYWJsZWQKCiAgTlRQLUNsaWVudCAgICAgICAgICAgICAgICAgICAgICAgICAgICA6IERpc2FibGVkCgogIEFkZHJlc3MgICAgICAgICAgICAgICAgQ29tbXVuaXR5ICAgICAgICAgICAgICBFdmVudHMgICBUeXBlICAgUmV0cnkgICBUaW1lb3V0CiAgLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tIC0tLS0tLSAtLS0tLS0tIC0tLS0tLS0KICAxOTIuMC4yLjI1NCAgICAgICAgICAgIHB1YmxpYyAgICAgICAgICAgICAgICAgTm9uZSAgICAgdHJhcCAgIDMgICAgICAgMTUgICAgIAogCgogRXhjbHVkZWQgTUlCcwoKCiBTbm1wIFJlc3BvbnNlIFBkdSBTb3VyY2UtSVAgSW5mb3JtYXRpb24KCiAgU2VsZWN0aW9uIFBvbGljeSAgIDogcmZjMTUxNyAgICAgICAgICAgICAKCiBUcmFwIFBkdSBTb3VyY2UtSVAgSW5mb3JtYXRpb24KCiAgU2VsZWN0aW9uIFBvbGljeSAgIDogcmZjMTUxNyAgICAgICAgICAgICAKCgoA diff --git a/tests/fixtures/procurve-show-snmp-server.txt b/tests/fixtures/procurve-show-snmp-server.txt new file mode 100644 index 0000000..2ad3d64 --- /dev/null +++ b/tests/fixtures/procurve-show-snmp-server.txt @@ -0,0 +1,49 @@ + + SNMP Communities + + Community Name MIB View Write Access + -------------------------------- -------- ------------ + public Operator Restricted + + Trap Receivers + + Link-Change Traps Enabled on Ports [All] : All + + Traps Category Current Status + _____________________________________ __________________ + SNMP Authentication : Extended + Password change : Enabled + Login failures : Enabled + Port-Security : Enabled + Authorization Server Contact : Enabled + DHCP-Snooping : Enabled + DHCPv6-Snooping Out of Resource : Enabled + DHCPv6-Snooping Errant Replies : Enabled + Dynamic ARP Protection : Enabled + Dynamic IP Lockdown : Enabled + Dynamic IPv6 Lockdown Out of Resource : Enabled + Dynamic IPv6 Lockdown Violations : Enabled + Startup Config change : Disabled + Running Config Change : Disabled + MAC address table changes : Disabled + MAC Address Count : Disabled + + NTP-Client : Disabled + + Address Community Events Type Retry Timeout + ---------------------- ---------------------- -------- ------ ------- ------- + 192.0.2.254 public None trap 3 15 + + + Excluded MIBs + + + Snmp Response Pdu Source-IP Information + + Selection Policy : rfc1517 + + Trap Pdu Source-IP Information + + Selection Policy : rfc1517 + + diff --git a/tests/unit/test_snmp_config.py b/tests/unit/test_snmp_config.py index 5d49e33..d22be13 100644 --- a/tests/unit/test_snmp_config.py +++ b/tests/unit/test_snmp_config.py @@ -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", }