Files
napalm-zyxel/tests/test_facts.py
T
Christian Manivong 61a6e4f7ba
driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 1m8s
driver / test (3.13) (pull_request) Successful in 1m5s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 1m15s
CI / driver (pull_request) Successful in 2m37s
driver / publish (pull_request) Skipped
fix(facts): read the host name from the gateway's System page
get_facts read HostName from getBasicInformation, which a VMG4005-B50A
does not send, and fell back to the address the driver connects to:
netOrk kept the gateway named 192.168.1.1. The gateway's own System page
reads host name and domain with GET /cgi-bin/System?oid=snmp, sealed like
a DAL answer; get_facts now reads them there. fqdn is the host name with
its domain. A gateway with no name set reports "", and get_facts raises
when the System page cannot be read.
2026-11-12 16:42:47 +01:00

107 lines
3.8 KiB
Python

"""The gateway's host name comes from its System page.
``get_facts`` read ``HostName`` from ``getBasicInformation``, which a VMG4005-B50A
(V5.17(ABQA.3.1)C0) does not send, and fell back to the address the driver connects
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.
``BASIC_INFO`` and ``SYSTEM`` are those two answers from that gateway (the SNMP
community strings the System answer can carry are left out).
"""
from __future__ import annotations
import json
from typing import Any
from unittest.mock import MagicMock
import pytest
import requests
from napalm_zyxel import ZyxelVMGDriver
BASIC_INFO: dict[str, Any] = {
"result": "ZCFG_SUCCESS",
"ModelName": "VMG4005-B50A",
"SoftwareVersion": "V5.17(ABQA.3.1)C0",
"CurrentLanguage": "de",
"AvailableLanguages": "en,cz,it,nl,pt,ru,tr,fr,de,es",
"RememberPassword": 0,
"FWUpgradeProgressST": 0,
}
SYSTEM: list[dict[str, Any]] = [
{"HostName": "VMG4005-B50A", "DomainName": "home", "Location": "Taiwan", "Contact": ""}
]
SEALED = {"content": "SEALED", "iv": "IV"}
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(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."""
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))
def get(url: str, params: dict[str, Any] | None = None, **_: Any) -> requests.Response:
if url.endswith("cgi-bin/System?oid=snmp"):
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: (
system if data == SEALED else pytest.fail(f"decrypted {data!r}")
)
driver._device_info = BASIC_INFO
driver.get_interfaces = lambda: {"br0": {}} # type: ignore[method-assign] # test_interfaces.py
return driver
def test_the_host_name_is_the_one_on_the_system_page():
assert _driver(SYSTEM).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"
def test_without_a_domain_the_fqdn_is_the_host_name():
facts = _driver([{**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()
assert (facts["hostname"], facts["fqdn"]) == ("", "")
def test_the_page_is_also_read_unsealed():
facts = _driver(_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()
@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()