"""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()