Files
napalm-netgear/tests/unit/test_reads_not_empty.py
T
Christian Manivong 7e3c7d6285
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m10s
driver / test (3.12) (pull_request) Successful in 1m17s
driver / test (3.13) (pull_request) Successful in 1m13s
driver / lowest (pull_request) Successful in 29s
driver / build (pull_request) Successful in 32s
driver / security (pull_request) Successful in 1m16s
CI / driver (pull_request) Successful in 3m7s
driver / publish (pull_request) Skipped
fix(plus): stop reporting unread or failed reads as empty
Netgear Plus answered reads its web interface cannot do (MAC table,
LLDP, config, users, SNMP, NTP) with empty values without asking the
switch; those now fall through to NAPALM's NotImplementedError, and the
switch-role getters that only returned {} (spanning tree, port channels,
802.1X, MAC ACL) are gone, as device-types reads capability from the
method being there. get_vlans_detail raises when vlan.cgi cannot be
read, get_vlans keys VLANs by int, and Smart get_snmp_config raises when
show snmp fails instead of reading as "no SNMP".

Closes #21
2026-11-02 09:22:22 +01:00

118 lines
4.3 KiB
Python

"""A read that cannot happen or that failed is not reported as "nothing there" (#21).
- Netgear Plus answered reads its web interface cannot do (MAC table, LLDP, the
configuration, users, SNMP, NTP, spanning tree, port channels, 802.1X, MAC ACLs)
with empty values, without asking the switch. NAPALM's getters now raise NAPALM's
own ``NotImplementedError``; the switch-role getters, which napalm-device-types only
declares, are not there at all -- as for the VLAN writes in #9.
- Plus ``get_vlans_detail`` read a failed fetch of ``vlan.cgi`` as no VLANs, and
``get_vlans`` keyed VLANs by strings where NAPALM uses ``int``.
- Smart ``get_snmp_config`` read a failed ``show snmp`` as "no SNMP".
"""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from napalm.base import NetworkDriver
from napalm.base.exceptions import ConnectionException
from napalm_netgear.netgear_plus import NetgearPlusDriver
from napalm_netgear.netgear_smart import NetgearSmartDriver
#: The parts of ``vlan.cgi`` the driver reads, in the layout its parser documents:
#: VLAN 1 untagged on both ports, VLAN 24 tagged on port 2.
VLAN_PAGE = (
'<span vid-4 class="x"><i class="y"></i>1</span><span vnm-4 class="z">Default</span>'
'<span vid-4 class="x"><i class="y"></i>24</span><span vnm-4 class="z">Guest</span>'
'<span class="p pvid-str">1*,</span>'
'<span class="p pvid-str">1*, 24,</span>'
)
NAPALM_GETTERS = [
"get_mac_address_table",
"get_lldp_neighbors",
"get_lldp_neighbors_detail",
"get_config",
"get_users",
"get_snmp_information",
"get_ntp_servers",
"get_ntp_peers",
"get_ntp_stats",
]
SWITCH_ROLE_GETTERS = ["get_spanning_tree", "get_port_channels", "get_dot1x_ports", "get_mac_acl"]
def _plus(page: MagicMock | Exception | None = None) -> NetgearPlusDriver:
driver = NetgearPlusDriver("10.0.0.20", "", "pw")
driver._connector = MagicMock(ports=2)
if isinstance(page, Exception):
driver._connector.fetch_page.side_effect = page
else:
driver._connector.fetch_page.return_value = page
return driver
def _page(status: int, text: str) -> MagicMock:
return MagicMock(status_code=status, text=text)
# --- reads Plus cannot do ---------------------------------------------------------
@pytest.mark.parametrize("getter", NAPALM_GETTERS)
def test_plus_leaves_a_read_it_cannot_do_to_napalm(getter):
"""NAPALM's own stub: it raises, and an application can see it is not overridden."""
assert getattr(NetgearPlusDriver, getter) is getattr(NetworkDriver, getter)
with pytest.raises(NotImplementedError):
getattr(_plus(), getter)()
@pytest.mark.parametrize("getter", SWITCH_ROLE_GETTERS)
def test_plus_has_no_switch_role_read_it_cannot_do(getter):
assert not callable(getattr(NetgearPlusDriver, getter, None))
def test_plus_still_reports_no_arp_table():
"""An L2-only switch has no ARP table: empty is the true answer."""
assert _plus().get_arp_table() == []
# --- Plus VLANs ---------------------------------------------------------------------
def test_plus_reads_the_vlans_from_the_page():
assert _plus(_page(200, VLAN_PAGE)).get_vlans_detail() == {
"1": {"name": "Default", "tagged": [], "untagged": ["port1", "port2"]},
"24": {"name": "Guest", "tagged": ["port2"], "untagged": []},
}
def test_plus_get_vlans_keys_are_ints():
vlans = _plus(_page(200, VLAN_PAGE)).get_vlans()
assert sorted(vlans) == [1, 24]
assert vlans[24] == {"name": "Guest", "interfaces": ["port2"]}
def test_plus_vlans_raise_when_the_switch_does_not_answer():
with pytest.raises(ConnectionException, match="timed out"):
_plus(TimeoutError("timed out")).get_vlans_detail()
@pytest.mark.parametrize("page", [None, _page(500, "error"), _page(200, "")])
def test_plus_vlans_raise_on_an_answer_that_is_no_vlan_page(page):
with pytest.raises(RuntimeError, match="vlan.cgi"):
_plus(page).get_vlans_detail()
# --- Smart SNMP ---------------------------------------------------------------------
def test_smart_get_snmp_config_raises_when_show_snmp_fails():
driver = NetgearSmartDriver("10.0.0.21", "admin", "pw")
driver._send_command = MagicMock(side_effect=OSError("channel closed"))
with pytest.raises(OSError, match="channel closed"):
driver.get_snmp_config()