Files
napalm-zyxel/tests/test_interfaces.py
T
Christian Manivong ec76c1d6e8
driver / standard (pull_request) Failing after 15s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 59s
driver / test (3.13) (pull_request) Successful in 52s
driver / test (3.12) (pull_request) Successful in 56s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 27s
driver / security (pull_request) Successful in 54s
CI / driver (pull_request) Failing after 2m1s
driver / publish (pull_request) Skipped
fix(interfaces): read interfaces and addresses from the gateway's status page
get_interfaces reported a fixed set: wan, lan and two Wi-Fi radios, all
up at 1000 Mbit/s, the LAN with the WAN's MAC. get_interfaces_ip gave
both a /24, the LAN's address being the hostname the driver connected
to. getBasicInformation carries none of WanIp, LanIp or MacAddress on a
VMG4005-B50A, a VDSL bridge without Wi-Fi whose LAN is a /29, and the
gateway showed all of the invented values.

Both now read the DAL's cardpage_status, which a GET answers sealed like
a PUT: the IP interfaces in WanLanInfo (br0, atm0.1, ptm0.2) with their
status, MAC and netmask, the Ethernet ports in LanInfo (eth1) with their
speed. A speed or MTU the gateway does not report is 0, unknown. IPv6
addresses are not read yet: no gateway seen so far had IPv6 on.
2026-11-12 16:14:25 +01:00

193 lines
6.7 KiB
Python

"""Interfaces and their addresses come from the gateway's own status page.
``get_interfaces`` used to report a fixed set: ``wan``, ``lan`` and two Wi-Fi radios,
all up, at 1000 Mbit/s, with the WAN's MAC on the LAN. ``get_interfaces_ip`` gave
both a /24. A VMG4005-B50A, a VDSL bridge without Wi-Fi whose LAN is a /29, showed
all of that. Both now read the DAL's ``cardpage_status``: the IP interfaces in
``WanLanInfo`` and the Ethernet ports in ``LanInfo``. IPv6 addresses are not read
yet: the gateway this was taken from has IPv6 off and so shows none to learn from.
``CARDPAGE`` is that answer from a VMG4005-B50A (V5.17(ABQA.3.1)C0), cut down to the
fields the driver reads.
"""
from __future__ import annotations
import json
from typing import Any
from unittest.mock import MagicMock
import pytest
import requests
from napalm_zyxel import ZyxelVMGDriver
CARDPAGE: dict[str, Any] = {
"result": "ZCFG_SUCCESS",
"Object": [
{
"WanLanInfo": [
{
"Enable": True,
"X_ZYXEL_Type": "LAN",
"MACAddress": "6c:4f:89:6a:bf:50",
"Status": "Up",
"X_ZYXEL_SrvName": "Default",
"Name": "LAN",
"X_ZYXEL_IfName": "br0",
"IPv4Address": [
{
"Enable": True,
"IPAddress": "192.168.1.1",
"SubnetMask": "255.255.255.248",
"Status": "Enabled",
}
],
"IPv6Address": [],
},
{
# No "Status" at all: the gateway leaves it out for this bridge.
"Enable": True,
"X_ZYXEL_Type": "WAN",
"MACAddress": "",
"X_ZYXEL_SrvName": "Bridge_ATM",
"Name": "WAN",
"X_ZYXEL_IfName": "atm0.1",
"IPv4Address": [],
"IPv6Address": [],
},
{
"Enable": True,
"X_ZYXEL_Type": "WAN",
"MACAddress": "",
"Status": "LowerLayerDown",
"X_ZYXEL_SrvName": "Bridge_PTM",
"Name": "WAN",
"X_ZYXEL_IfName": "ptm0.2",
"IPv4Address": [],
"IPv6Address": [],
},
],
"LanInfo": [
{
"Enable": True,
"Status": "Up",
"Name": "eth1",
"MACAddress": "6c:4f:89:6a:bf:53",
"MaxBitRate": 1000,
"X_ZYXEL_LanPort": "LAN1",
}
],
}
],
}
def _response(status: int, text: str = "") -> requests.Response:
resp = requests.Response()
resp.status_code = status
resp._content = text.encode()
resp.url = "http://192.168.1.1/"
return resp
def _driver(answer: dict[str, Any] | requests.Response) -> ZyxelVMGDriver:
"""A logged-in driver whose gateway answers ``cardpage_status`` with *answer*,
encrypted the way the gateway sends it."""
driver = ZyxelVMGDriver("192.168.1.1", "admin", "password")
# NAPALM's __del__ asks is_alive(), which opens a TCP connection: not in a test.
driver.is_alive = lambda: {"is_alive": False} # type: ignore[method-assign]
sealed = {"content": "SEALED", "iv": "IV"}
reply = answer if isinstance(answer, requests.Response) else _response(200, json.dumps(sealed))
def get(url: str, params: dict[str, Any] | None = None, **_: Any) -> requests.Response:
if url.endswith("cgi-bin/DAL?oid=cardpage_status"):
return reply
return _response(404)
session = MagicMock(spec=requests.Session)
session.get.side_effect = get
driver._session = session
driver._sessionkey = "1"
driver._crypto = MagicMock()
driver._crypto.decrypt_response.side_effect = lambda data: (
answer if data == sealed else pytest.fail(f"decrypted {data!r}")
)
driver._device_info = {"ModelName": "VMG4005-B50A"}
return driver
# --- get_interfaces -------------------------------------------------------------------
def test_reports_the_gateways_interfaces_and_nothing_else():
assert set(_driver(CARDPAGE).get_interfaces()) == {"br0", "atm0.1", "ptm0.2", "eth1"}
def test_an_ip_interface_is_up_only_when_the_gateway_says_so():
ifaces = _driver(CARDPAGE).get_interfaces()
assert ifaces["br0"]["is_up"] is True
assert ifaces["ptm0.2"]["is_up"] is False
assert ifaces["atm0.1"]["is_up"] is False # no status reported
def test_an_ip_interface_carries_its_own_mac_and_name():
br0 = _driver(CARDPAGE).get_interfaces()["br0"]
assert br0["mac_address"] == "6c:4f:89:6a:bf:50"
assert br0["description"] == "LAN"
assert br0["is_enabled"] is True
def test_a_wan_bridge_is_named_after_its_service():
assert _driver(CARDPAGE).get_interfaces()["ptm0.2"]["description"] == "WAN (Bridge_PTM)"
def test_an_ethernet_port_carries_its_speed_mac_and_label():
eth1 = _driver(CARDPAGE).get_interfaces()["eth1"]
assert eth1["speed"] == 1000.0
assert eth1["mac_address"] == "6c:4f:89:6a:bf:53"
assert eth1["description"] == "LAN1"
assert eth1["is_up"] is True
def test_a_speed_or_mtu_the_gateway_does_not_report_is_left_unknown():
br0 = _driver(CARDPAGE).get_interfaces()["br0"]
assert br0["speed"] == 0.0
assert br0["mtu"] == 0
def test_a_gateway_without_wifi_has_no_wifi_interfaces():
assert not [n for n in _driver(CARDPAGE).get_interfaces() if "wlan" in n]
def test_get_facts_lists_the_gateways_interfaces():
names = _driver(CARDPAGE).get_facts()["interface_list"]
assert sorted(names) == ["atm0.1", "br0", "eth1", "ptm0.2"]
# --- get_interfaces_ip ----------------------------------------------------------------
def test_the_lan_address_keeps_the_gateways_netmask():
ips = _driver(CARDPAGE).get_interfaces_ip()
assert ips == {"br0": {"ipv4": {"192.168.1.1": {"prefix_length": 29}}, "ipv6": {}}}
def test_a_disabled_address_is_not_reported():
page = json.loads(json.dumps(CARDPAGE))
page["Object"][0]["WanLanInfo"][0]["IPv4Address"][0]["Enable"] = False
assert _driver(page).get_interfaces_ip() == {}
# --- a status page that cannot be read ------------------------------------------------
def test_a_refused_status_page_raises():
with pytest.raises(RuntimeError, match="cardpage_status"):
_driver({"result": "ZCFG_NO_SUCH_OBJECT", "Object": []}).get_interfaces()
def test_a_failed_request_raises():
with pytest.raises(RuntimeError, match="500"):
_driver(_response(500)).get_interfaces()