Files
napalm-tplink-jetstream/tests/unit/test_snmp_information.py
T
Christian Manivong 9df131db78
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / types (pull_request) Successful in 1m12s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 2m55s
driver / publish (pull_request) Skipped
fix(snmp): read get_snmp_information's community from the Name column
The switch's community table starts with an Index column. The getter
took the first word of each row as the community, so "public" was
reported as "1", and read the access from the second word, the name,
so every community came out read-only. It now uses the header-based
community table parser: the name from Name, the access from Type.

Closes #14
2026-11-02 08:40:54 +01:00

77 lines
3.0 KiB
Python

"""get_snmp_information reads the community from the Name column, its access from Type (#14).
It took the first word of every row, which is the ``Index`` column on the switch, and
read the access from the second, which is the name: the community came out as ``"1"``
and every one as read-only.
The fixture is ``show snmp-server community`` recorded from a JetStream switch; the
read-write rows below are that recording with a row added in the same layout.
"""
from pathlib import Path
from unittest.mock import MagicMock
import pytest
from napalm_tplink_jetstream.parsers import parse_snmp_community_access
from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver
FIXTURES = Path(__file__).parent.parent / "fixtures"
SHOW_SNMP_COMMUNITY = (FIXTURES / "jetstream-show-snmp-server-community.txt").read_text()
WITH_READ_WRITE = SHOW_SNMP_COMMUNITY + "2 private read-write viewDefault\n"
#: A read-only community whose name says "write": only the Type column decides.
NAMED_WRITE = SHOW_SNMP_COMMUNITY + "2 writers read-only viewDefault\n"
SYSTEM_INFO = """
System Description - JetStream 24-Port Gigabit L2 Managed Switch
Contact Information - noc
System Location - rack 2
Mac Address - 00-11-22-33-44-55
"""
def _driver(community_output: str) -> TPLinkJetstreamDriver:
driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw")
outputs = {"show system-info": SYSTEM_INFO, "show snmp community": community_output}
driver._send_command = MagicMock(side_effect=lambda command, **_: outputs[command])
return driver
def test_the_recorded_community_is_reported_by_name():
info = _driver(SHOW_SNMP_COMMUNITY).get_snmp_information()
assert info["community"] == {"public": {"acl": "", "mode": "ro"}}
assert (info["contact"], info["location"]) == ("noc", "rack 2")
def test_the_access_is_read_from_the_type_column():
info = _driver(WITH_READ_WRITE).get_snmp_information()
assert info["community"] == {
"public": {"acl": "", "mode": "ro"},
"private": {"acl": "", "mode": "rw"},
}
def test_a_name_that_says_write_does_not_make_it_read_write():
assert _driver(NAMED_WRITE).get_snmp_information()["community"]["writers"]["mode"] == "ro"
@pytest.mark.parametrize(
("output", "expected"),
[
(SHOW_SNMP_COMMUNITY, {"public": "ro"}),
(WITH_READ_WRITE, {"public": "ro", "private": "rw"}),
# The header and dashes alone: no community.
("".join(SHOW_SNMP_COMMUNITY.splitlines(keepends=True)[:2]), {}),
# No column headed Name: its rows name no community.
(SHOW_SNMP_COMMUNITY.replace("Name ", "Comm ", 1), {}),
# No column headed Type: the access is not read from another one.
(NAMED_WRITE.replace("Type ", "Kind ", 1), {"public": "ro", "writers": "ro"}),
("something the parser does not know\n", {}),
],
)
def test_parse_snmp_community_access(output, expected):
assert parse_snmp_community_access(output) == expected