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
@@ -0,0 +1 @@
|
|||||||
|
CiBTTk1QIENvbW11bml0aWVzCgogIENvbW11bml0eSBOYW1lICAgICAgICAgICAgICAgICAgIE1JQiBWaWV3IFdyaXRlIEFjY2VzcwogIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tIC0tLS0tLS0tLS0tLQogIHB1YmxpYyAgICAgICAgICAgICAgICAgICAgICAgICAgIE9wZXJhdG9yIFJlc3RyaWN0ZWQgIAogCiBUcmFwIFJlY2VpdmVycwoKICBMaW5rLUNoYW5nZSBUcmFwcyBFbmFibGVkIG9uIFBvcnRzIFtBbGxdIDogQWxsCgogIFRyYXBzIENhdGVnb3J5ICAgICAgICAgICAgICAgICAgICAgICAgICBDdXJyZW50IFN0YXR1cyAKICBfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fICAgX19fX19fX19fX19fX19fX19fIAogIFNOTVAgQXV0aGVudGljYXRpb24gICAgICAgICAgICAgICAgICAgOiBFeHRlbmRlZCAKICBQYXNzd29yZCBjaGFuZ2UgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBMb2dpbiBmYWlsdXJlcyAgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBQb3J0LVNlY3VyaXR5ICAgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBBdXRob3JpemF0aW9uIFNlcnZlciBDb250YWN0ICAgICAgICAgIDogRW5hYmxlZCAKICBESENQLVNub29waW5nICAgICAgICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBESENQdjYtU25vb3BpbmcgT3V0IG9mIFJlc291cmNlICAgICAgIDogRW5hYmxlZCAKICBESENQdjYtU25vb3BpbmcgRXJyYW50IFJlcGxpZXMgICAgICAgIDogRW5hYmxlZCAKICBEeW5hbWljIEFSUCBQcm90ZWN0aW9uICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBEeW5hbWljIElQIExvY2tkb3duICAgICAgICAgICAgICAgICAgIDogRW5hYmxlZCAKICBEeW5hbWljIElQdjYgTG9ja2Rvd24gT3V0IG9mIFJlc291cmNlIDogRW5hYmxlZCAKICBEeW5hbWljIElQdjYgTG9ja2Rvd24gVmlvbGF0aW9ucyAgICAgIDogRW5hYmxlZCAKICBTdGFydHVwIENvbmZpZyBjaGFuZ2UgICAgICAgICAgICAgICAgIDogRGlzYWJsZWQKICBSdW5uaW5nIENvbmZpZyBDaGFuZ2UgICAgICAgICAgICAgICAgIDogRGlzYWJsZWQKICBNQUMgYWRkcmVzcyB0YWJsZSBjaGFuZ2VzICAgICAgICAgICAgIDogRGlzYWJsZWQKICBNQUMgQWRkcmVzcyBDb3VudCAgICAgICAgICAgICAgICAgICAgIDogRGlzYWJsZWQKCiAgTlRQLUNsaWVudCAgICAgICAgICAgICAgICAgICAgICAgICAgICA6IERpc2FibGVkCgogIEFkZHJlc3MgICAgICAgICAgICAgICAgQ29tbXVuaXR5ICAgICAgICAgICAgICBFdmVudHMgICBUeXBlICAgUmV0cnkgICBUaW1lb3V0CiAgLS0tLS0tLS0tLS0tLS0tLS0tLS0tLSAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tIC0tLS0tLS0tIC0tLS0tLSAtLS0tLS0tIC0tLS0tLS0KICAxOTIuMC4yLjI1NCAgICAgICAgICAgIHB1YmxpYyAgICAgICAgICAgICAgICAgTm9uZSAgICAgdHJhcCAgIDMgICAgICAgMTUgICAgIAogCgogRXhjbHVkZWQgTUlCcwoKCiBTbm1wIFJlc3BvbnNlIFBkdSBTb3VyY2UtSVAgSW5mb3JtYXRpb24KCiAgU2VsZWN0aW9uIFBvbGljeSAgIDogcmZjMTUxNyAgICAgICAgICAgICAKCiBUcmFwIFBkdSBTb3VyY2UtSVAgSW5mb3JtYXRpb24KCiAgU2VsZWN0aW9uIFBvbGljeSAgIDogcmZjMTUxNyAgICAgICAgICAgICAKCgoA
|
||||||
+49
@@ -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
|
||||||
|
|
||||||
|
|
||||||
@@ -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
|
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
|
``/cli`` endpoint runs ``show snmp-server`` and answers with the output in
|
||||||
base64, which the SSH path's parser reads.
|
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
|
from __future__ import annotations
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
from pathlib import Path
|
||||||
from unittest.mock import MagicMock
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -16,34 +21,31 @@ from napalm.base.exceptions import CommandErrorException
|
|||||||
|
|
||||||
from napalm_procurve.procurve import ProcurveDriver
|
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
|
_PUBLIC = " public Operator Restricted \n"
|
||||||
-------------------------------- -------- ------------
|
assert SHOW_SNMP_SERVER.count(_PUBLIC) == 1
|
||||||
private Manager Unrestricted
|
#: The recording with a read-write community put in before the read-only one.
|
||||||
c0mm Manager Restricted
|
TWO_COMMUNITIES = SHOW_SNMP_SERVER.replace(
|
||||||
|
_PUBLIC, " private Manager Unrestricted\n" + _PUBLIC
|
||||||
|
)
|
||||||
|
|
||||||
Trap Receivers
|
#: The recording with its only community removed.
|
||||||
|
NO_COMMUNITY = SHOW_SNMP_SERVER.replace(_PUBLIC, "")
|
||||||
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:
|
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 = MagicMock(ok=200 <= http < 300, status_code=http, text="")
|
||||||
resp.json.return_value = {
|
resp.json.return_value = {
|
||||||
"cmd": "show snmp-server",
|
"cmd": "show snmp-server",
|
||||||
"result_base64_encoded": base64.b64encode(output.encode()).decode(),
|
"result_base64_encoded": encoded,
|
||||||
"status": status,
|
"status": status,
|
||||||
}
|
}
|
||||||
return resp
|
return resp
|
||||||
@@ -57,22 +59,25 @@ def _rest_driver(resp: MagicMock) -> ProcurveDriver:
|
|||||||
return driver
|
return driver
|
||||||
|
|
||||||
|
|
||||||
def test_over_rest_the_read_only_community_is_read_through_the_cli_endpoint():
|
def test_over_rest_the_community_is_read_through_the_cli_endpoint():
|
||||||
driver = _rest_driver(_cli_answer(SHOW_SNMP_SERVER))
|
driver = _rest_driver(_cli_answer(SHOW_SNMP_SERVER_REST))
|
||||||
|
|
||||||
config = driver.get_snmp_config()
|
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"})
|
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():
|
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(
|
@pytest.mark.parametrize(
|
||||||
"resp",
|
"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):
|
def test_over_rest_a_failed_read_raises_instead_of_reading_as_no_snmp(resp):
|
||||||
with pytest.raises(CommandErrorException, match="show snmp-server"):
|
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 = ProcurveDriver("10.0.0.40", "manager", "s3cret")
|
||||||
driver._transport = "ssh"
|
driver._transport = "ssh"
|
||||||
driver._device = MagicMock(base_prompt="swt")
|
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() == {
|
assert driver.get_snmp_config() == {
|
||||||
"running": True,
|
"running": True,
|
||||||
"community": "c0mm",
|
"community": "public",
|
||||||
"port": 161,
|
"port": 161,
|
||||||
"version": "2c",
|
"version": "2c",
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user