Files
napalm-zyxel/tests/test_is_alive.py
T
Christian Manivong e5ce4e32b1
driver / standard (pull_request) Successful in 19s
driver / lint (pull_request) Successful in 19s
driver / types (pull_request) Successful in 1m5s
driver / test (3.12) (pull_request) Successful in 1m3s
driver / lowest (pull_request) Successful in 34s
driver / test (3.13) (pull_request) Successful in 1m9s
driver / build (pull_request) Successful in 31s
driver / security (pull_request) Successful in 1m12s
CI / driver (pull_request) Successful in 2m53s
driver / publish (pull_request) Skipped
fix(session): report is_alive from the driver's own session
is_alive() opened a TCP connection to the gateway whether or not the
driver was opened, and after close() too. NAPALM's __del__ asks it of
every collected driver, so each one connected once more and could stall
for up to five seconds. Without an open session it now answers False
and connects nowhere; with one, it reports whether the gateway still
answers on its port.

Closes #19
2026-11-02 10:58:14 +01:00

57 lines
1.8 KiB
Python

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