diff --git a/CHANGELOG.md b/CHANGELOG.md index 12f48ad..eae98f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ All notable changes to this driver. The format follows 3.12 venv no longer has. ### Fixed +- `is_alive()` returns `False` without touching the network when the driver has no + open session (never opened, or closed), and with an open session reports whether the + gateway still answers on its port (#19). It connected to the gateway in every case, + so each garbage-collected driver (NAPALM's `__del__` asks `is_alive()`) connected + once more and could stall for up to five seconds. - 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`, diff --git a/README.md b/README.md index 8551658..0ddb1d1 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ with driver("192.0.2.1", "admin", "secret", optional_args={"use_tls": True}) as | `get_port_forwards`, `get_nat_translations` | DAL `NAT_PORTMAP`, else the status page | | `get_wireless_clients`, `get_ssids`, `get_radio_status` | DAL, else the wireless page | | `get_diagnostics` | DAL `DIAGNOSTICS` | -| `is_alive` | TCP connect to the web port | +| `is_alive` | the session is open and the web port answers; without a session `False`, no connection | | `reboot_host` | DAL `REBOOT` | | `restart_wifi` | DAL `WIFI_RESTART` | diff --git a/napalm_zyxel/zyxel_driver.py b/napalm_zyxel/zyxel_driver.py index 94efb27..005aceb 100644 --- a/napalm_zyxel/zyxel_driver.py +++ b/napalm_zyxel/zyxel_driver.py @@ -219,7 +219,13 @@ class ZyxelVMGDriver(ResidentialGatewayDriver): self._device_info = None def is_alive(self) -> dict[str, bool]: - """Check if the device is reachable via TCP connection.""" + """Whether this driver's session is open and the gateway still answers on its port. + + Without an open session (never opened, or closed) it is not alive, and no + connection is made: NAPALM's ``__del__`` asks this of every collected driver (#19). + """ + if self._session is None or self._sessionkey is None: + return {"is_alive": False} try: sock = socket.create_connection((self.hostname, self.port), timeout=5) sock.close() diff --git a/tests/test_is_alive.py b/tests/test_is_alive.py new file mode 100644 index 0000000..c0e7289 --- /dev/null +++ b/tests/test_is_alive.py @@ -0,0 +1,56 @@ +"""is_alive() reports the driver's own session; without one it asks nobody (#19). + +It opened a TCP connection to the gateway whether or not the driver was opened, and +after close() too. NAPALM's ``__del__`` calls it, so every collected driver connected +to the gateway once more and could block for up to five seconds. +""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +import pytest +import requests + +from napalm_zyxel import ZyxelVMGDriver + + +def _open_driver() -> ZyxelVMGDriver: + driver = ZyxelVMGDriver("192.168.1.1", "admin", "password") + driver._session = MagicMock(spec=requests.Session) + driver._sessionkey = "1" + driver._crypto = MagicMock() + return driver + + +def _closed(driver: ZyxelVMGDriver) -> ZyxelVMGDriver: + driver.close() + return driver + + +@pytest.mark.parametrize( + "driver", + [ + pytest.param(lambda: ZyxelVMGDriver("192.168.1.1", "admin", "password"), id="never opened"), + pytest.param(lambda: _closed(_open_driver()), id="closed"), + ], +) +def test_without_a_session_it_is_not_alive_and_opens_no_socket(driver): + with patch("socket.create_connection") as connect: + assert driver().is_alive() == {"is_alive": False} + connect.assert_not_called() + + +def test_with_an_open_session_it_reports_the_gateway_answering(): + driver = _open_driver() + with patch("socket.create_connection") as connect: + assert driver.is_alive() == {"is_alive": True} + connect.assert_called_once_with(("192.168.1.1", 80), timeout=5) + driver._session = None # nothing left for __del__ to check + + +def test_with_an_open_session_it_reports_a_gateway_that_stopped_answering(): + driver = _open_driver() + with patch("socket.create_connection", side_effect=OSError("refused")): + assert driver.is_alive() == {"is_alive": False} + driver._session = None diff --git a/tests/test_zyxel.py b/tests/test_zyxel.py index 5b8c854..df6f1f3 100644 --- a/tests/test_zyxel.py +++ b/tests/test_zyxel.py @@ -44,9 +44,9 @@ MOCK_LOGIN_RESPONSE = { def _offline(driver: ZyxelVMGDriver) -> ZyxelVMGDriver: """*driver*, with an is_alive() that opens no socket (#17). - NAPALM's ``__del__`` asks ``is_alive()``, and this driver's ``is_alive()`` connects - to the gateway whether or not the driver was opened or closed. The tests of - ``is_alive()`` itself call the class's method. + NAPALM's ``__del__`` asks ``is_alive()``, which connects to the gateway while the + driver has a session; many tests give it a mock one and never close it. The tests + of ``is_alive()`` itself call the class's method (and ``tests/test_is_alive.py``). """ driver.is_alive = lambda: {"is_alive": False} # type: ignore[method-assign] return driver @@ -163,11 +163,13 @@ class TestZyxelVMGDriver: assert self.driver._sessionkey is None def test_is_alive(self): + self._setup_mocked_http() with patch("socket.create_connection") as mock_conn: mock_conn.return_value.__enter__.return_value = MagicMock() assert ZyxelVMGDriver.is_alive(self.driver)["is_alive"] is True def test_is_alive_fail(self): + self._setup_mocked_http() with patch("socket.create_connection") as mock_conn: mock_conn.side_effect = OSError("refused") assert ZyxelVMGDriver.is_alive(self.driver)["is_alive"] is False