fix(dal): raise on a failed request instead of reading it as empty #16
No files matched your search
@@ -13,6 +13,15 @@ All notable changes to this driver. The format follows
|
|||||||
3.12 venv no longer has.
|
3.12 venv no longer has.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
- A failed request to the gateway raises instead of reading as empty data (#15). A
|
||||||
|
timeout, a refused session or an HTTP error made `get_hosts`, `get_arp_table`,
|
||||||
|
`get_port_forwards`, `get_nat_translations`, `get_wireless_clients`, `get_ssids`,
|
||||||
|
`get_radio_status` and `get_diagnostics` report a gateway with nothing configured.
|
||||||
|
They raise `ConnectionException` when the gateway does not answer or refuses the
|
||||||
|
session (HTTP 401/403) and `RuntimeError` naming the request otherwise. A getter
|
||||||
|
whose DAL request fails still reads the HTML page, and raises when that fails too.
|
||||||
|
`get_facts` no longer reports an empty interface list when the interfaces cannot be
|
||||||
|
read, and `reboot_host` / `restart_wifi` raise the same way when the request fails.
|
||||||
- `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types'
|
- `get_arp_table()` reports an unknown age as `-1.0` (napalm-device-types'
|
||||||
`ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": the host
|
`ARP_AGE_UNKNOWN`) instead of `0.0`, which means "confirmed just now": the host
|
||||||
list says a host is active, not when the gateway last saw it (NetOrk/netork#1327).
|
list says a host is active, not when the gateway last saw it (NetOrk/netork#1327).
|
||||||
|
|||||||
@@ -8,10 +8,12 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
from typing import Any
|
from collections.abc import Callable
|
||||||
|
from typing import Any, TypeVar
|
||||||
from urllib.parse import urljoin
|
from urllib.parse import urljoin
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@@ -33,6 +35,13 @@ from napalm_device_types.models import (
|
|||||||
WirelessClientDict,
|
WirelessClientDict,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
#: Answers that mean the session is not (or no longer) accepted.
|
||||||
|
_SESSION_REFUSED = (401, 403)
|
||||||
|
|
||||||
|
_T = TypeVar("_T")
|
||||||
|
|
||||||
_PING_UNSUPPORTED = "Not supported on Zyxel VMG: the DAL does not report ping replies"
|
_PING_UNSUPPORTED = "Not supported on Zyxel VMG: the DAL does not report ping replies"
|
||||||
|
|
||||||
|
|
||||||
@@ -238,7 +247,8 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
except (requests.RequestException, json.JSONDecodeError, TypeError):
|
except (requests.RequestException, json.JSONDecodeError, TypeError):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _dal_query(self, oid: str, data: dict | None = None) -> dict | None:
|
def _dal_query(self, oid: str, data: dict | None = None) -> dict:
|
||||||
|
"""The gateway's decrypted answer to DAL *oid*; a failed request raises (#15)."""
|
||||||
if self._session is None or self._sessionkey is None or self._crypto is None:
|
if self._session is None or self._sessionkey is None or self._crypto is None:
|
||||||
raise ConnectionClosedException("Not connected")
|
raise ConnectionClosedException("Not connected")
|
||||||
if data is None:
|
if data is None:
|
||||||
@@ -253,10 +263,11 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
)
|
)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
return self._crypto.decrypt_response(resp.json())
|
return self._crypto.decrypt_response(resp.json())
|
||||||
except Exception:
|
except (requests.RequestException, ValueError, TypeError, KeyError) as exc:
|
||||||
return None
|
raise self._request_failed(f"DAL {oid}", exc) from exc
|
||||||
|
|
||||||
def _fetch_page(self, path: str) -> str | None:
|
def _fetch_page(self, path: str) -> str:
|
||||||
|
"""The text of *path*; a failed request raises (#15)."""
|
||||||
if self._session is None or self._sessionkey is None:
|
if self._session is None or self._sessionkey is None:
|
||||||
raise ConnectionClosedException("Not connected")
|
raise ConnectionClosedException("Not connected")
|
||||||
try:
|
try:
|
||||||
@@ -267,16 +278,34 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
)
|
)
|
||||||
resp.raise_for_status()
|
resp.raise_for_status()
|
||||||
return resp.text
|
return resp.text
|
||||||
except requests.RequestException:
|
except requests.RequestException as exc:
|
||||||
return None
|
raise self._request_failed(f"GET {path}", exc) from exc
|
||||||
|
|
||||||
|
def _request_failed(self, what: str, exc: Exception) -> Exception:
|
||||||
|
"""``ConnectionException`` when the gateway did not answer or refused the session,
|
||||||
|
``RuntimeError`` naming *what* for any other failure."""
|
||||||
|
detail = f"{what} on {self._base_url} failed: {exc}"
|
||||||
|
if isinstance(exc, ValueError | TypeError | KeyError): # an answer, but not one we read
|
||||||
|
return RuntimeError(detail)
|
||||||
|
status = exc.response.status_code if getattr(exc, "response", None) is not None else None
|
||||||
|
if status is None or status in _SESSION_REFUSED:
|
||||||
|
return ConnectionException(detail)
|
||||||
|
return RuntimeError(detail)
|
||||||
|
|
||||||
|
def _read_dal_or_page(
|
||||||
|
self, oid: str, page: str, parse_dal: Callable[[Any], _T], parse_page: Callable[[str], _T]
|
||||||
|
) -> _T:
|
||||||
|
"""The DAL's answer to *oid*, else the HTML *page*; raises when neither can be read."""
|
||||||
|
try:
|
||||||
|
return parse_dal(json.loads(self._fetch_page(f"cgi-bin/DAL?oid={oid}")))
|
||||||
|
except (ConnectionException, RuntimeError, ValueError, TypeError) as exc:
|
||||||
|
logger.debug("DAL %s not read (%s), reading %s", oid, exc, page)
|
||||||
|
return parse_page(self._fetch_page(page))
|
||||||
|
|
||||||
def get_facts(self) -> dict[str, Any]:
|
def get_facts(self) -> dict[str, Any]:
|
||||||
"""Return device facts such as model, serial, OS version, and uptime."""
|
"""Return device facts such as model, serial, OS version, and uptime."""
|
||||||
info = self._device_info or {}
|
info = self._device_info or {}
|
||||||
try:
|
iface_list = list(self.get_interfaces().keys())
|
||||||
iface_list = list(self.get_interfaces().keys())
|
|
||||||
except Exception:
|
|
||||||
iface_list = []
|
|
||||||
return {
|
return {
|
||||||
"vendor": self.VENDOR,
|
"vendor": self.VENDOR,
|
||||||
"model": info.get("ModelName", ""),
|
"model": info.get("ModelName", ""),
|
||||||
@@ -520,17 +549,12 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
|
|
||||||
def get_port_forwards(self) -> list[PortForwardDict]:
|
def get_port_forwards(self) -> list[PortForwardDict]:
|
||||||
"""Return port forwarding rules from DAL or HTML fallback."""
|
"""Return port forwarding rules from DAL or HTML fallback."""
|
||||||
html = self._fetch_page("cgi-bin/DAL?oid=NAT_PORTMAP")
|
return self._read_dal_or_page(
|
||||||
if html is not None:
|
"NAT_PORTMAP",
|
||||||
try:
|
"Status.htm",
|
||||||
data = json.loads(html)
|
self._parse_port_forwards_dal,
|
||||||
return self._parse_port_forwards_dal(data)
|
self._parse_port_forwards_html,
|
||||||
except (json.JSONDecodeError, TypeError):
|
)
|
||||||
pass
|
|
||||||
html = self._fetch_page("Status.htm")
|
|
||||||
if html is not None:
|
|
||||||
return self._parse_port_forwards_html(html)
|
|
||||||
return []
|
|
||||||
|
|
||||||
def _parse_port_forwards_dal(self, data: dict) -> list[PortForwardDict]:
|
def _parse_port_forwards_dal(self, data: dict) -> list[PortForwardDict]:
|
||||||
forwards: list[PortForwardDict] = []
|
forwards: list[PortForwardDict] = []
|
||||||
@@ -577,17 +601,9 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
|
|
||||||
def get_hosts(self) -> list[HostDict]:
|
def get_hosts(self) -> list[HostDict]:
|
||||||
"""Return DHCP host entries from DAL or HTML fallback."""
|
"""Return DHCP host entries from DAL or HTML fallback."""
|
||||||
html = self._fetch_page("cgi-bin/DAL?oid=LAN_HOSTS")
|
return self._read_dal_or_page(
|
||||||
if html is not None:
|
"LAN_HOSTS", "dhcp.htm", self._parse_hosts_dal, self._parse_hosts_html
|
||||||
try:
|
)
|
||||||
data = json.loads(html)
|
|
||||||
return self._parse_hosts_dal(data)
|
|
||||||
except (json.JSONDecodeError, TypeError):
|
|
||||||
pass
|
|
||||||
html = self._fetch_page("dhcp.htm")
|
|
||||||
if html is not None:
|
|
||||||
return self._parse_hosts_html(html)
|
|
||||||
return []
|
|
||||||
|
|
||||||
def _parse_hosts_dal(self, data: dict) -> list[HostDict]:
|
def _parse_hosts_dal(self, data: dict) -> list[HostDict]:
|
||||||
hosts: list[HostDict] = []
|
hosts: list[HostDict] = []
|
||||||
@@ -659,17 +675,12 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
|
|
||||||
def get_wireless_clients(self) -> list[WirelessClientDict]:
|
def get_wireless_clients(self) -> list[WirelessClientDict]:
|
||||||
"""Return connected wireless clients from DAL or HTML fallback."""
|
"""Return connected wireless clients from DAL or HTML fallback."""
|
||||||
html = self._fetch_page("cgi-bin/DAL?oid=WIFI_CLIENTS")
|
return self._read_dal_or_page(
|
||||||
if html is not None:
|
"WIFI_CLIENTS",
|
||||||
try:
|
"wireless.htm",
|
||||||
data = json.loads(html)
|
self._parse_wireless_clients_dal,
|
||||||
return self._parse_wireless_clients_dal(data)
|
self._parse_wireless_clients_html,
|
||||||
except (json.JSONDecodeError, TypeError):
|
)
|
||||||
pass
|
|
||||||
html = self._fetch_page("wireless.htm")
|
|
||||||
if html is not None:
|
|
||||||
return self._parse_wireless_clients_html(html)
|
|
||||||
return []
|
|
||||||
|
|
||||||
def _parse_wireless_clients_dal(self, data: dict) -> list[WirelessClientDict]:
|
def _parse_wireless_clients_dal(self, data: dict) -> list[WirelessClientDict]:
|
||||||
clients: list[WirelessClientDict] = []
|
clients: list[WirelessClientDict] = []
|
||||||
@@ -725,17 +736,9 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
|
|
||||||
def get_ssids(self) -> dict[str, SSIDDict]:
|
def get_ssids(self) -> dict[str, SSIDDict]:
|
||||||
"""Return WiFi SSID configurations from DAL or HTML fallback."""
|
"""Return WiFi SSID configurations from DAL or HTML fallback."""
|
||||||
html = self._fetch_page("cgi-bin/DAL?oid=WIFI_SSIDS")
|
return self._read_dal_or_page(
|
||||||
if html is not None:
|
"WIFI_SSIDS", "wireless.htm", self._parse_ssids_dal, self._parse_ssids_html
|
||||||
try:
|
)
|
||||||
data = json.loads(html)
|
|
||||||
return self._parse_ssids_dal(data)
|
|
||||||
except (json.JSONDecodeError, TypeError):
|
|
||||||
pass
|
|
||||||
html = self._fetch_page("wireless.htm")
|
|
||||||
if html is not None:
|
|
||||||
return self._parse_ssids_html(html)
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def _parse_ssids_dal(self, data: dict) -> dict[str, SSIDDict]:
|
def _parse_ssids_dal(self, data: dict) -> dict[str, SSIDDict]:
|
||||||
ssids: dict[str, SSIDDict] = {}
|
ssids: dict[str, SSIDDict] = {}
|
||||||
@@ -778,54 +781,55 @@ class ZyxelVMGDriver(ResidentialGatewayDriver):
|
|||||||
|
|
||||||
def get_radio_status(self) -> dict[str, RadioStatusDict]:
|
def get_radio_status(self) -> dict[str, RadioStatusDict]:
|
||||||
"""Return WiFi radio status from DAL or HTML fallback."""
|
"""Return WiFi radio status from DAL or HTML fallback."""
|
||||||
|
return self._read_dal_or_page(
|
||||||
|
"WIFI_RADIO",
|
||||||
|
"wireless.htm",
|
||||||
|
self._parse_radio_status_dal,
|
||||||
|
self._parse_radio_status_html,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _parse_radio_status_dal(self, data: dict) -> dict[str, RadioStatusDict]:
|
||||||
radios: dict[str, RadioStatusDict] = {}
|
radios: dict[str, RadioStatusDict] = {}
|
||||||
html = self._fetch_page("cgi-bin/DAL?oid=WIFI_RADIO")
|
entries = data.get("radios", data.get("entries", []))
|
||||||
if html is not None:
|
if isinstance(entries, dict):
|
||||||
try:
|
entries = [entries]
|
||||||
data = json.loads(html)
|
for entry in entries if isinstance(entries, list) else []:
|
||||||
entries = data.get("radios", data.get("entries", []))
|
name = entry.get("Name", entry.get("name", ""))
|
||||||
if isinstance(entries, dict):
|
channel = int(entry.get("Channel", entry.get("channel", 0)) or 0)
|
||||||
entries = [entries]
|
radios[name] = {
|
||||||
for entry in entries if isinstance(entries, list) else []:
|
"enabled": bool(entry.get("Enabled", entry.get("enabled", True))),
|
||||||
name = entry.get("Name", entry.get("name", ""))
|
"band": "2.4GHz" if channel <= 14 else "5GHz",
|
||||||
channel = int(entry.get("Channel", entry.get("channel", 0)) or 0)
|
"channel": channel,
|
||||||
radios[name] = {
|
"channel_width": int(entry.get("ChannelWidth", entry.get("channel_width", 0)) or 0),
|
||||||
"enabled": bool(entry.get("Enabled", entry.get("enabled", True))),
|
"tx_power": int(entry.get("TxPower", entry.get("tx_power", 0)) or 0),
|
||||||
"band": "2.4GHz" if channel <= 14 else "5GHz",
|
"frequency": float(entry.get("Frequency", entry.get("frequency", 0)) or 0),
|
||||||
"channel": channel,
|
}
|
||||||
"channel_width": int(
|
return radios
|
||||||
entry.get("ChannelWidth", entry.get("channel_width", 0)) or 0
|
|
||||||
),
|
def _parse_radio_status_html(self, html: str) -> dict[str, RadioStatusDict]:
|
||||||
"tx_power": int(entry.get("TxPower", entry.get("tx_power", 0)) or 0),
|
radios: dict[str, RadioStatusDict] = {}
|
||||||
"frequency": float(entry.get("Frequency", entry.get("frequency", 0)) or 0),
|
for band, pat in [("2.4GHz", r"2\.4[Gg]"), ("5GHz", r"5[Gg]")]:
|
||||||
}
|
if re.search(pat, html):
|
||||||
return radios
|
radios[band] = {
|
||||||
except (json.JSONDecodeError, TypeError, ValueError):
|
"enabled": True,
|
||||||
pass
|
"band": band,
|
||||||
html = self._fetch_page("wireless.htm")
|
"channel": 0,
|
||||||
if html is not None:
|
"channel_width": 0,
|
||||||
for band, pat in [("2.4GHz", r"2\.4[Gg]"), ("5GHz", r"5[Gg]")]:
|
"tx_power": 0,
|
||||||
if re.search(pat, html):
|
"frequency": 0.0,
|
||||||
radios[band] = {
|
}
|
||||||
"enabled": True,
|
|
||||||
"band": band,
|
|
||||||
"channel": 0,
|
|
||||||
"channel_width": 0,
|
|
||||||
"tx_power": 0,
|
|
||||||
"frequency": 0.0,
|
|
||||||
}
|
|
||||||
return radios
|
return radios
|
||||||
|
|
||||||
def reboot_host(self) -> None:
|
def reboot_host(self) -> None:
|
||||||
"""Restart the gateway (``HostRebootMixin``); raises when it refuses."""
|
"""Restart the gateway (``HostRebootMixin``); raises when it refuses."""
|
||||||
result = self._dal_query("REBOOT", {"Command": "reboot"})
|
result = self._dal_query("REBOOT", {"Command": "reboot"})
|
||||||
if result is None or result.get("result") != "ZCFG_SUCCESS":
|
if result.get("result") != "ZCFG_SUCCESS":
|
||||||
raise RuntimeError(f"the gateway refused to reboot: {result}")
|
raise RuntimeError(f"the gateway refused to reboot: {result}")
|
||||||
|
|
||||||
def restart_wifi(self, band: str = "both") -> None:
|
def restart_wifi(self, band: str = "both") -> None:
|
||||||
"""Restart the radios of *band* (``2.4G``, ``5G`` or ``both``); raises when refused."""
|
"""Restart the radios of *band* (``2.4G``, ``5G`` or ``both``); raises when refused."""
|
||||||
result = self._dal_query("WIFI_RESTART", {"Band": band})
|
result = self._dal_query("WIFI_RESTART", {"Band": band})
|
||||||
if result is None or result.get("result") != "ZCFG_SUCCESS":
|
if result.get("result") != "ZCFG_SUCCESS":
|
||||||
raise RuntimeError(f"the gateway refused to restart Wi-Fi ({band}): {result}")
|
raise RuntimeError(f"the gateway refused to restart Wi-Fi ({band}): {result}")
|
||||||
|
|
||||||
def get_diagnostics(self) -> dict:
|
def get_diagnostics(self) -> dict:
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
{
|
{
|
||||||
"coverage_floor": 68.0,
|
"coverage_floor": 68.0,
|
||||||
"function_complexity": {
|
"function_complexity": {
|
||||||
"napalm_zyxel/zyxel_driver.py::ZyxelVMGDriver._parse_wireless_clients_dal": 12,
|
"napalm_zyxel/zyxel_driver.py::ZyxelVMGDriver._parse_wireless_clients_dal": 12
|
||||||
"napalm_zyxel/zyxel_driver.py::ZyxelVMGDriver.get_radio_status": 14
|
|
||||||
},
|
},
|
||||||
"function_lines": {},
|
"function_lines": {},
|
||||||
"mypy_relaxed": [
|
"mypy_relaxed": [
|
||||||
|
|||||||
@@ -0,0 +1,151 @@
|
|||||||
|
"""A failed request to the gateway raises; it is not read as "nothing configured" (#15).
|
||||||
|
|
||||||
|
``_dal_query`` and ``_fetch_page`` returned ``None`` for any failure, and the getters
|
||||||
|
turned that into empty data: a timeout or an expired session read as a gateway with
|
||||||
|
no hosts, port forwards, SSIDs or Wi-Fi clients. Empty data is now the answer of a
|
||||||
|
gateway that answered with none; a getter whose DAL request fails still reads the
|
||||||
|
HTML page, and raises when that fails too.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
from typing import Any
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
import requests
|
||||||
|
from napalm.base.exceptions import ConnectionException
|
||||||
|
|
||||||
|
from napalm_zyxel import ZyxelVMGDriver
|
||||||
|
|
||||||
|
HOSTS_DAL = {"hosts": [{"MACAddress": "AA:BB:CC:DD:EE:01", "IPAddress": "192.168.1.33"}]}
|
||||||
|
HOSTS_PAGE = "192.168.1.34 aa:bb:cc:dd:ee:02 tablet"
|
||||||
|
|
||||||
|
|
||||||
|
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(answers: dict[str, requests.Response | Exception]) -> ZyxelVMGDriver:
|
||||||
|
"""A logged-in driver whose session answers each path as told; 404 otherwise."""
|
||||||
|
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]
|
||||||
|
session = MagicMock(spec=requests.Session)
|
||||||
|
|
||||||
|
def answer(url: str, params: dict[str, Any] | None = None, **_: Any) -> requests.Response:
|
||||||
|
path = url.removeprefix("http://192.168.1.1:80/")
|
||||||
|
oid = (params or {}).get("oid")
|
||||||
|
reply = answers.get(f"{path}?oid={oid}" if oid else path, _response(404))
|
||||||
|
if isinstance(reply, Exception):
|
||||||
|
raise reply
|
||||||
|
return reply
|
||||||
|
|
||||||
|
session.get.side_effect = answer
|
||||||
|
session.put.side_effect = answer
|
||||||
|
driver._session = session
|
||||||
|
driver._sessionkey = "1"
|
||||||
|
driver._crypto = MagicMock()
|
||||||
|
driver._crypto.decrypt_response.side_effect = lambda data: data
|
||||||
|
driver._device_info = {"ModelName": "VMG4005-B50A"}
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
|
# --- _dal_query --------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def test_dal_query_returns_the_gateways_answer():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=DIAGNOSTICS": _response(200, '{"result": "ZCFG_SUCCESS"}')})
|
||||||
|
assert driver._dal_query("DIAGNOSTICS") == {"result": "ZCFG_SUCCESS"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_dal_query_raises_when_the_gateway_does_not_answer():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=DIAGNOSTICS": requests.ConnectTimeout("timed out")})
|
||||||
|
with pytest.raises(ConnectionException, match="timed out"):
|
||||||
|
driver._dal_query("DIAGNOSTICS")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("status", [401, 403])
|
||||||
|
def test_dal_query_raises_connection_exception_for_a_refused_session(status):
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=DIAGNOSTICS": _response(status)})
|
||||||
|
with pytest.raises(ConnectionException, match=str(status)):
|
||||||
|
driver._dal_query("DIAGNOSTICS")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("reply", [_response(500), _response(200, "<html>busy</html>")])
|
||||||
|
def test_dal_query_names_the_endpoint_of_any_other_failure(reply):
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=DIAGNOSTICS": reply})
|
||||||
|
with pytest.raises(RuntimeError, match="DIAGNOSTICS"):
|
||||||
|
driver._dal_query("DIAGNOSTICS")
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_diagnostics_raises_when_the_dal_request_fails():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=DIAGNOSTICS": _response(500)})
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
driver.get_diagnostics()
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_reboot_that_fails_to_reach_the_gateway_raises():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=REBOOT": requests.ConnectionError("refused")})
|
||||||
|
with pytest.raises(ConnectionException):
|
||||||
|
driver.reboot_host()
|
||||||
|
|
||||||
|
|
||||||
|
# --- the getters ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("getter", "oid", "page"),
|
||||||
|
[
|
||||||
|
("get_hosts", "LAN_HOSTS", "dhcp.htm"),
|
||||||
|
("get_port_forwards", "NAT_PORTMAP", "Status.htm"),
|
||||||
|
("get_wireless_clients", "WIFI_CLIENTS", "wireless.htm"),
|
||||||
|
("get_ssids", "WIFI_SSIDS", "wireless.htm"),
|
||||||
|
("get_radio_status", "WIFI_RADIO", "wireless.htm"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_a_getter_raises_when_neither_the_dal_nor_the_page_can_be_read(getter, oid, page):
|
||||||
|
driver = _driver(
|
||||||
|
{f"cgi-bin/DAL?oid={oid}": requests.ReadTimeout("read timed out"), page: _response(500)}
|
||||||
|
)
|
||||||
|
with pytest.raises((ConnectionException, RuntimeError)):
|
||||||
|
getattr(driver, getter)()
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_arp_table_raises_when_the_hosts_cannot_be_read():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=LAN_HOSTS": _response(500), "dhcp.htm": _response(500)})
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
driver.get_arp_table()
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_failed_dal_request_falls_back_to_the_page():
|
||||||
|
driver = _driver(
|
||||||
|
{"cgi-bin/DAL?oid=LAN_HOSTS": _response(500), "dhcp.htm": _response(200, HOSTS_PAGE)}
|
||||||
|
)
|
||||||
|
assert [h["ip"] for h in driver.get_hosts()] == ["192.168.1.34"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_the_dal_answer_is_used_when_there_is_one():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=LAN_HOSTS": _response(200, json.dumps(HOSTS_DAL))})
|
||||||
|
assert [h["ip"] for h in driver.get_hosts()] == ["192.168.1.33"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_gateway_that_lists_no_hosts_has_none():
|
||||||
|
driver = _driver({"cgi-bin/DAL?oid=LAN_HOSTS": _response(200, '{"hosts": []}')})
|
||||||
|
assert driver.get_hosts() == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_facts_lists_the_interfaces():
|
||||||
|
assert _driver({}).get_facts()["interface_list"] == ["wan", "lan", "wlan_2_4ghz", "wlan_5ghz"]
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
Reference in new issue
Block a user