"""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