"""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(): driver = _driver(CARDPAGE) driver._system_name = lambda: ("VMG4005-B50A", "home") # type: ignore[method-assign] # test_facts.py names = driver.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()