fix(facts): read serial number and uptime from the status page #28
No files matched your search
@@ -13,6 +13,10 @@ All notable changes to this driver. The format follows
|
||||
3.12 venv no longer has.
|
||||
|
||||
### Fixed
|
||||
- `get_facts()` reports the gateway's serial number and uptime (#27). It read them
|
||||
from `getBasicInformation`, which a VMG4005-B50A does not send them in, and reported
|
||||
`""` and `0`. They now come from `DeviceInfo` in the DAL's `cardpage_status`, the
|
||||
answer the interfaces are read from; `get_facts()` reads that answer once for both.
|
||||
- `get_facts()` reports the gateway's host name and domain: `hostname` is the name on
|
||||
the gateway's System page (`GET /cgi-bin/System?oid=snmp`), `fqdn` that name with its
|
||||
domain (`VMG4005-B50A.home`). Both were read from `getBasicInformation`, which a
|
||||
|
||||
@@ -76,6 +76,24 @@ def _ethernet_port(port: dict[str, Any]) -> dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _interfaces(page: dict[str, Any]) -> dict[str, dict[str, Any]]:
|
||||
"""The IP interfaces (``WanLanInfo``) and Ethernet ports (``LanInfo``) of a
|
||||
``cardpage_status`` answer, keyed by the gateway's own interface name."""
|
||||
ifaces = {
|
||||
entry["X_ZYXEL_IfName"]: _ip_interface(entry)
|
||||
for entry in page.get("WanLanInfo") or []
|
||||
if entry.get("X_ZYXEL_IfName")
|
||||
}
|
||||
ifaces.update(
|
||||
{
|
||||
port["Name"]: _ethernet_port(port)
|
||||
for port in page.get("LanInfo") or []
|
||||
if port.get("Name")
|
||||
}
|
||||
)
|
||||
return ifaces
|
||||
|
||||
|
||||
def _ipv4_addresses(entry: dict[str, Any]) -> dict[str, dict[str, int]]:
|
||||
"""The enabled IPv4 addresses of one ``WanLanInfo`` entry, with their prefix length."""
|
||||
return {
|
||||
@@ -354,21 +372,23 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
||||
def get_facts(self) -> dict[str, Any]:
|
||||
"""Return device facts such as model, serial, OS version, and uptime.
|
||||
|
||||
The host name and domain are the ones on the gateway's System page;
|
||||
``getBasicInformation`` does not carry them.
|
||||
``getBasicInformation`` carries model and firmware only. The host name and
|
||||
domain are the ones on the gateway's System page; serial number and uptime
|
||||
are the status page's ``DeviceInfo``, read with the interfaces (#27).
|
||||
"""
|
||||
info = self._device_info or {}
|
||||
iface_list = list(self.get_interfaces().keys())
|
||||
page = self._status_page()
|
||||
device = page.get("DeviceInfo") or {}
|
||||
hostname, domain = self._system_name()
|
||||
return {
|
||||
"vendor": self.VENDOR,
|
||||
"model": info.get("ModelName", ""),
|
||||
"serial_number": info.get("SerialNumber", ""),
|
||||
"serial_number": str(device.get("SerialNumber") or ""),
|
||||
"os_version": info.get("SoftwareVersion", ""),
|
||||
"hostname": hostname,
|
||||
"fqdn": f"{hostname}.{domain}" if hostname and domain else hostname,
|
||||
"uptime": int(info.get("SystemUpTime", 0) or 0),
|
||||
"interface_list": iface_list,
|
||||
"uptime": int(device.get("UpTime") or 0),
|
||||
"interface_list": list(_interfaces(page)),
|
||||
}
|
||||
|
||||
def _system_name(self) -> tuple[str, str]:
|
||||
@@ -419,20 +439,7 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
||||
Keyed by the gateway's own interface name (``br0``, ``ptm0.2``, ``eth1``). A
|
||||
speed or MTU the gateway does not report is 0: unknown.
|
||||
"""
|
||||
page = self._status_page()
|
||||
ifaces = {
|
||||
entry["X_ZYXEL_IfName"]: _ip_interface(entry)
|
||||
for entry in page.get("WanLanInfo") or []
|
||||
if entry.get("X_ZYXEL_IfName")
|
||||
}
|
||||
ifaces.update(
|
||||
{
|
||||
port["Name"]: _ethernet_port(port)
|
||||
for port in page.get("LanInfo") or []
|
||||
if port.get("Name")
|
||||
}
|
||||
)
|
||||
return ifaces
|
||||
return _interfaces(self._status_page())
|
||||
|
||||
def get_interfaces_ip(self) -> dict[str, dict[str, Any]]:
|
||||
"""The IPv4 addresses of the gateway's IP interfaces, with their netmask.
|
||||
|
||||
+88
-19
@@ -6,8 +6,13 @@ to: netOrk kept the device named ``192.168.1.1``. The gateway's own Maintenance
|
||||
System page reads the name with ``GET /cgi-bin/System?oid=snmp``, sealed like a DAL
|
||||
answer; the driver now reads it there too.
|
||||
|
||||
Serial number and uptime are not in ``getBasicInformation`` either; they come from
|
||||
``DeviceInfo`` in the DAL's ``cardpage_status``, the answer the interfaces are read
|
||||
from (#27).
|
||||
|
||||
``BASIC_INFO`` and ``SYSTEM`` are those two answers from that gateway (the SNMP
|
||||
community strings the System answer can carry are left out).
|
||||
community strings the System answer can carry are left out), ``STATUS`` its
|
||||
``cardpage_status`` cut down to ``DeviceInfo`` and one interface.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -35,7 +40,24 @@ SYSTEM: list[dict[str, Any]] = [
|
||||
{"HostName": "VMG4005-B50A", "DomainName": "home", "Location": "Taiwan", "Contact": ""}
|
||||
]
|
||||
|
||||
SEALED = {"content": "SEALED", "iv": "IV"}
|
||||
STATUS: dict[str, Any] = {
|
||||
"result": "ZCFG_SUCCESS",
|
||||
"Object": [
|
||||
{
|
||||
"DeviceInfo": {
|
||||
"ModelName": "VMG4005-B50A",
|
||||
"SerialNumber": "S250Y02049498",
|
||||
"SoftwareVersion": "V5.17(ABQA.3.1)C0",
|
||||
"UpTime": 13544274,
|
||||
},
|
||||
"WanLanInfo": [{"X_ZYXEL_IfName": "br0", "Name": "LAN", "Status": "Up"}],
|
||||
"LanInfo": [],
|
||||
}
|
||||
],
|
||||
}
|
||||
|
||||
SYSTEM_PATH = "cgi-bin/System?oid=snmp"
|
||||
STATUS_PATH = "cgi-bin/DAL?oid=cardpage_status"
|
||||
|
||||
|
||||
def _response(status: int, text: str = "") -> requests.Response:
|
||||
@@ -46,61 +68,108 @@ def _response(status: int, text: str = "") -> requests.Response:
|
||||
return resp
|
||||
|
||||
|
||||
def _driver(system: Any) -> ZyxelVMGDriver:
|
||||
"""A logged-in driver whose gateway answers the System page with *system*, sealed
|
||||
the way the gateway sends it, or with *system* itself when it is a response."""
|
||||
def _driver(system: Any = SYSTEM, status: Any = STATUS) -> ZyxelVMGDriver:
|
||||
"""A logged-in driver whose gateway answers the System page with *system* and
|
||||
``cardpage_status`` with *status*, each sealed the way the gateway sends it, or
|
||||
with the answer itself when it is a response."""
|
||||
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]
|
||||
reply = system if isinstance(system, requests.Response) else _response(200, json.dumps(SEALED))
|
||||
answers = {SYSTEM_PATH: system, STATUS_PATH: status}
|
||||
|
||||
def get(url: str, params: dict[str, Any] | None = None, **_: Any) -> requests.Response:
|
||||
if url.endswith("cgi-bin/System?oid=snmp"):
|
||||
return reply
|
||||
for path, answer in answers.items():
|
||||
if url.endswith(path):
|
||||
if isinstance(answer, requests.Response):
|
||||
return answer
|
||||
return _response(200, json.dumps({"content": path, "iv": "IV"}))
|
||||
return _response(404)
|
||||
|
||||
def decrypt(data: dict[str, Any]) -> Any:
|
||||
if data.get("content") not in answers:
|
||||
pytest.fail(f"decrypted {data!r}")
|
||||
return answers[data["content"]]
|
||||
|
||||
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: (
|
||||
system if data == SEALED else pytest.fail(f"decrypted {data!r}")
|
||||
)
|
||||
driver._crypto.decrypt_response.side_effect = decrypt
|
||||
driver._device_info = BASIC_INFO
|
||||
driver.get_interfaces = lambda: {"br0": {}} # type: ignore[method-assign] # test_interfaces.py
|
||||
return driver
|
||||
|
||||
|
||||
def _status_reads(driver: ZyxelVMGDriver) -> int:
|
||||
return sum(
|
||||
call.args[0].endswith(STATUS_PATH)
|
||||
for call in driver._session.get.call_args_list # type: ignore[union-attr]
|
||||
)
|
||||
|
||||
|
||||
def test_the_host_name_is_the_one_on_the_system_page():
|
||||
assert _driver(SYSTEM).get_facts()["hostname"] == "VMG4005-B50A"
|
||||
assert _driver().get_facts()["hostname"] == "VMG4005-B50A"
|
||||
|
||||
|
||||
def test_the_fqdn_joins_the_host_name_and_the_domain():
|
||||
assert _driver(SYSTEM).get_facts()["fqdn"] == "VMG4005-B50A.home"
|
||||
assert _driver().get_facts()["fqdn"] == "VMG4005-B50A.home"
|
||||
|
||||
|
||||
def test_without_a_domain_the_fqdn_is_the_host_name():
|
||||
facts = _driver([{**SYSTEM[0], "DomainName": ""}]).get_facts()
|
||||
facts = _driver(system=[{**SYSTEM[0], "DomainName": ""}]).get_facts()
|
||||
assert facts["fqdn"] == "VMG4005-B50A"
|
||||
|
||||
|
||||
def test_a_gateway_without_a_name_reports_none_not_its_address():
|
||||
facts = _driver([{**SYSTEM[0], "HostName": "", "DomainName": ""}]).get_facts()
|
||||
facts = _driver(system=[{**SYSTEM[0], "HostName": "", "DomainName": ""}]).get_facts()
|
||||
assert (facts["hostname"], facts["fqdn"]) == ("", "")
|
||||
|
||||
|
||||
def test_the_page_is_also_read_unsealed():
|
||||
facts = _driver(_response(200, json.dumps(SYSTEM))).get_facts()
|
||||
facts = _driver(system=_response(200, json.dumps(SYSTEM))).get_facts()
|
||||
assert facts["hostname"] == "VMG4005-B50A"
|
||||
|
||||
|
||||
def test_get_facts_raises_when_the_system_page_cannot_be_read():
|
||||
with pytest.raises(RuntimeError, match="System"):
|
||||
_driver(_response(500)).get_facts()
|
||||
_driver(system=_response(500)).get_facts()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("answer", [[], {"result": "ZCFG_SUCCESS"}, ["VMG4005-B50A"]])
|
||||
def test_get_facts_raises_on_a_system_page_it_cannot_read(answer: Any):
|
||||
with pytest.raises(RuntimeError, match="System"):
|
||||
_driver(_response(200, json.dumps(answer))).get_facts()
|
||||
_driver(system=_response(200, json.dumps(answer))).get_facts()
|
||||
|
||||
|
||||
# --- serial number and uptime (#27) ---------------------------------------------------
|
||||
|
||||
|
||||
def test_the_serial_number_is_the_one_on_the_status_page():
|
||||
assert _driver().get_facts()["serial_number"] == "S250Y02049498"
|
||||
|
||||
|
||||
def test_the_uptime_is_the_one_on_the_status_page():
|
||||
assert _driver().get_facts()["uptime"] == 13544274
|
||||
|
||||
|
||||
def test_model_and_firmware_still_come_from_the_basic_information():
|
||||
facts = _driver().get_facts()
|
||||
assert (facts["model"], facts["os_version"]) == ("VMG4005-B50A", "V5.17(ABQA.3.1)C0")
|
||||
|
||||
|
||||
def test_a_status_page_without_device_info_reports_them_unknown():
|
||||
status = {"result": "ZCFG_SUCCESS", "Object": [{"WanLanInfo": [], "LanInfo": []}]}
|
||||
facts = _driver(status=status).get_facts()
|
||||
assert (facts["serial_number"], facts["uptime"]) == ("", 0)
|
||||
|
||||
|
||||
def test_get_facts_reads_the_status_page_once():
|
||||
driver = _driver()
|
||||
facts = driver.get_facts()
|
||||
assert facts["interface_list"] == ["br0"]
|
||||
assert _status_reads(driver) == 1
|
||||
|
||||
|
||||
def test_get_facts_raises_when_the_status_page_cannot_be_read():
|
||||
with pytest.raises(RuntimeError, match="cardpage_status"):
|
||||
_driver(status=_response(500)).get_facts()
|
||||
@@ -141,7 +141,5 @@ def test_a_gateway_that_lists_no_hosts_has_none():
|
||||
|
||||
|
||||
def test_get_facts_raises_when_the_interfaces_cannot_be_read():
|
||||
driver = _driver({})
|
||||
driver.get_interfaces = MagicMock(side_effect=RuntimeError("no interfaces"))
|
||||
with pytest.raises(RuntimeError, match="no interfaces"):
|
||||
driver.get_facts()
|
||||
with pytest.raises(RuntimeError, match="cardpage_status"):
|
||||
_driver({}).get_facts()
|
||||
+4
-2
@@ -10,7 +10,6 @@ from napalm_zyxel import ZyxelVMGDriver
|
||||
MOCK_BASIC_INFO = {
|
||||
"ModelName": "VMG4005-B50A",
|
||||
"SoftwareVersion": "V5.13(ABKS.1)C0",
|
||||
"SerialNumber": "S220L12345678",
|
||||
"WanIp": "85.2.34.56",
|
||||
"LanIp": "192.168.1.1",
|
||||
"MacAddress": "00:1A:2B:3C:4D:5E",
|
||||
@@ -88,6 +87,9 @@ class TestZyxelVMGDriver:
|
||||
return MockResponse({"RSAPublicKey": MOCK_RSA_KEY})
|
||||
if "cgi-bin/System?oid=snmp" in url: # tests/test_facts.py
|
||||
return MockResponse([{"HostName": "ZyxelVMG", "DomainName": "home"}])
|
||||
if "cgi-bin/DAL?oid=cardpage_status" in url: # tests/test_facts.py
|
||||
device = {"SerialNumber": "S220L12345678", "UpTime": 1234567}
|
||||
return MockResponse({"result": "ZCFG_SUCCESS", "Object": [{"DeviceInfo": device}]})
|
||||
if "UserLogout" in url:
|
||||
return MockResponse({})
|
||||
return MockResponse({})
|
||||
@@ -177,7 +179,6 @@ class TestZyxelVMGDriver:
|
||||
|
||||
def test_get_facts(self):
|
||||
self._setup_mocked_http()
|
||||
self.driver.get_interfaces = lambda: {} # type: ignore[method-assign] # tests/test_interfaces.py
|
||||
facts = self.driver.get_facts()
|
||||
assert facts["vendor"] == "Zyxel"
|
||||
assert facts["model"] == "VMG4005-B50A"
|
||||
@@ -185,6 +186,7 @@ class TestZyxelVMGDriver:
|
||||
assert facts["os_version"] == "V5.13(ABKS.1)C0"
|
||||
assert facts["hostname"] == "ZyxelVMG"
|
||||
assert facts["fqdn"] == "ZyxelVMG.home"
|
||||
assert facts["uptime"] == 1234567
|
||||
|
||||
def test_get_wan_status(self):
|
||||
self._setup_mocked_http()
|
||||
|
||||
Reference in new issue
Block a user