From 34f00f9f3aea699dfd5d85cfaf36d0e811ef8b57 Mon Sep 17 00:00:00 2001 From: Christian Manivong Date: Sun, 1 Nov 2026 21:52:55 +0100 Subject: [PATCH] fix(snmp): report no community instead of inventing "public" get_snmp_config started from community "public" and returned it with running=True when no row of the community table parsed. Without a listed community it now reports the agent as not running, as it already did for "no community entry exists". The issue's other two points were already fixed on main: the management VLAN is matched exactly since c2f0f7c (_management_vlan, with the caller's listen address), and the floor on napalm-device-types is 4.21.1 with no requirements.txt since 4e171bf. Closes #3 --- CHANGELOG.md | 2 + napalm_tplink_jetstream/tplink_jetstream.py | 21 +++++------ tests/quality_baseline.json | 6 +-- tests/unit/test_snmp_config.py | 42 +++++++++++++++++++++ 4 files changed, 55 insertions(+), 16 deletions(-) create mode 100644 tests/unit/test_snmp_config.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c7873f4..d1145c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ All notable changes to this driver. The format follows `disabled_algorithms` in `optional_args` still takes precedence. - `ping()` lists only replies in `results`. A "Request timed out" line was listed too, so a ping sweep reported hosts that never answered as alive. +- `get_snmp_config()` no longer reports the community `"public"` when the switch lists + none: without a listed community it reports the agent as not running. ### Changed - Python 3.12 is the floor (`requires-python = ">=3.12"`): netOrk runs every driver diff --git a/napalm_tplink_jetstream/tplink_jetstream.py b/napalm_tplink_jetstream/tplink_jetstream.py index b056ef1..b1a6bab 100644 --- a/napalm_tplink_jetstream/tplink_jetstream.py +++ b/napalm_tplink_jetstream/tplink_jetstream.py @@ -33,7 +33,7 @@ from napalm.base.exceptions import ( ReplaceConfigException, ) from napalm_device_types import ConfigLifecycleMixin, FingerprintRule, SnmpAgentMixin, SwitchDriver -from napalm_device_types.models import InterfaceConfigDict, VlanConfigDict +from napalm_device_types.models import InterfaceConfigDict, SNMPConfigDict, VlanConfigDict from napalm_device_types.ssh_host_keys import HostKeyCheck, SshHostKeyMismatch from netmiko import ConnectHandler from netmiko.exceptions import NetmikoAuthenticationException, NetmikoTimeoutException @@ -1927,23 +1927,19 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): # ── SNMP / Health ────────────────────────────────────────────────────────── - def get_snmp_config(self): + def get_snmp_config(self) -> SNMPConfigDict: """Return SNMP agent config if SNMP is enabled on the switch. TP-Link JetStream switches have SNMP built-in — no daemon to install. - Reads community from 'show snmp community'. + Reads community from 'show snmp community'; without a listed one the agent + does not run (#3). """ - try: - from napalm_device_types.models import SNMPConfigDict - except ImportError: - return None - + no_agent = SNMPConfigDict(running=False, community="", port=161, version="2c") out = self._send_command("show snmp community") if not out or "disabled" in out.lower() or "no community entry exists" in out.lower(): - return SNMPConfigDict(running=False, community="", port=161, version="2c") + return no_agent - # Parse first read-only community - community = "public" + community = None in_table = False for line in out.splitlines(): line_s = line.strip() @@ -1956,7 +1952,8 @@ class TPLinkJetstreamDriver(ConfigLifecycleMixin, SnmpAgentMixin, SwitchDriver): if len(parts) >= 2: community = parts[0] break - + if community is None: + return no_agent return SNMPConfigDict(running=True, community=community, port=161, version="2c") def get_device_warnings(self) -> list: diff --git a/tests/quality_baseline.json b/tests/quality_baseline.json index 9f261a3..7094dfe 100644 --- a/tests/quality_baseline.json +++ b/tests/quality_baseline.json @@ -27,9 +27,7 @@ "napalm_tplink_jetstream.tplink_jetstream" ], "python_file_lines": { - "napalm_tplink_jetstream/tplink_jetstream.py": 2027 + "napalm_tplink_jetstream/tplink_jetstream.py": 2024 }, - "untyped_public": [ - "napalm_tplink_jetstream/tplink_jetstream.py::TPLinkJetstreamDriver.get_snmp_config" - ] + "untyped_public": [] } diff --git a/tests/unit/test_snmp_config.py b/tests/unit/test_snmp_config.py new file mode 100644 index 0000000..40ad090 --- /dev/null +++ b/tests/unit/test_snmp_config.py @@ -0,0 +1,42 @@ +"""get_snmp_config reports the community the switch lists, never one it made up (#3). + +It started from "public" and kept it when no table row parsed, so a switch +whose community table could not be read reported a running agent with the +community "public". +""" + +from unittest.mock import MagicMock + +import pytest + +from napalm_tplink_jetstream.tplink_jetstream import TPLinkJetstreamDriver + + +def _driver(output: str) -> TPLinkJetstreamDriver: + driver = TPLinkJetstreamDriver("10.0.0.30", "admin", "pw") + driver._send_command = MagicMock(return_value=output) + return driver + + +def test_the_listed_community_is_reported(): + out = "Index Community-Name Access\n----- -------------- ------\nc0mm read-only x\n" + + config = _driver(out).get_snmp_config() + + assert config == {"running": True, "community": "c0mm", "port": 161, "version": "2c"} + + +@pytest.mark.parametrize( + "output", + [ + "Index Community-Name Access\n----- -------------- ------\n", + "Index Community-Name Access\n", + "something the parser does not know\n", + ], +) +def test_without_a_listed_community_none_is_invented(output): + config = _driver(output).get_snmp_config() + + assert config is not None + assert config["community"] != "public" + assert config == {"running": False, "community": "", "port": 161, "version": "2c"}