fix(tests): stub is_alive so no test opens a socket #18

Merged
christianmanivong merged 1 commits from fix/bugs-2026-11-02c into main 2026-11-02 09:57:11 +00:00
Owner

What

The unit tests open no socket any more, and a guard keeps it that way.

  • tests/conftest.py: a session-wide autouse fixture, as in NAPALM/napalm-fritzbox#11. It replaces socket.create_connection and socket.socket.connect with functions that record the attempt and refuse it. After a final gc.collect() it fails the run if anything tried to connect. A connection attempted from __del__ is swallowed there, which is why attempts are recorded rather than only raised.
  • tests/test_zyxel.py: every driver the tests build goes through _offline(), which gives the instance an is_alive() that opens nothing. The two is_alive tests call ZyxelVMGDriver.is_alive(self.driver) with socket.create_connection patched, as before. tests/test_failed_reads.py already stubbed it (#16).

Why

NAPALM's NetworkDriver.__del__ calls is_alive(), and this driver's is_alive() (zyxel_driver.py:212) connects to the gateway over TCP. Every driver a test built connected to 192.168.1.1:80 when it was collected. Closing the drivers would not help: is_alive() connects whether or not the driver was opened or closed, so the tests stub it.

The driver itself is unchanged. is_alive() reporting the session state instead of reachability would also stop a closed driver from connecting on garbage collection, but that is a behaviour change outside this issue. netOrk never calls is_alive().

Tests

Fixtures only (no device). With only the guard added, the run failed with unit tests tried to open a socket: ["create_connection('192.168.1.1', 80)", …]. With the stubs it passes: 66 passed, repeated three times.

ruff format/check clean, mypy clean, napalm-standard check passes, napalm-standard ratchet within the baseline. No CHANGELOG entry: nothing changes for driver users (same as fritzbox#11).

Closes #17

## What The unit tests open no socket any more, and a guard keeps it that way. - `tests/conftest.py`: a session-wide autouse fixture, as in NAPALM/napalm-fritzbox#11. It replaces `socket.create_connection` and `socket.socket.connect` with functions that record the attempt and refuse it. After a final `gc.collect()` it fails the run if anything tried to connect. A connection attempted from `__del__` is swallowed there, which is why attempts are recorded rather than only raised. - `tests/test_zyxel.py`: every driver the tests build goes through `_offline()`, which gives the instance an `is_alive()` that opens nothing. The two `is_alive` tests call `ZyxelVMGDriver.is_alive(self.driver)` with `socket.create_connection` patched, as before. `tests/test_failed_reads.py` already stubbed it (#16). ## Why NAPALM's `NetworkDriver.__del__` calls `is_alive()`, and this driver's `is_alive()` (`zyxel_driver.py:212`) connects to the gateway over TCP. Every driver a test built connected to 192.168.1.1:80 when it was collected. Closing the drivers would not help: `is_alive()` connects whether or not the driver was opened or closed, so the tests stub it. The driver itself is unchanged. `is_alive()` reporting the session state instead of reachability would also stop a closed driver from connecting on garbage collection, but that is a behaviour change outside this issue. netOrk never calls `is_alive()`. ## Tests Fixtures only (no device). With only the guard added, the run failed with `unit tests tried to open a socket: ["create_connection('192.168.1.1', 80)", …]`. With the stubs it passes: 66 passed, repeated three times. ruff format/check clean, mypy clean, `napalm-standard check` passes, `napalm-standard ratchet` within the baseline. No CHANGELOG entry: nothing changes for driver users (same as fritzbox#11). Closes #17
christianmanivong added 1 commit 2026-11-02 09:41:59 +00:00
fix(tests): stub is_alive so no test opens a socket
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m9s
driver / test (3.12) (pull_request) Successful in 1m9s
driver / test (3.13) (pull_request) Successful in 1m0s
driver / lowest (pull_request) Successful in 34s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m14s
CI / driver (pull_request) Successful in 2m56s
driver / publish (pull_request) Skipped
745946387f
NAPALM's __del__ calls is_alive(), and this driver's is_alive() opens a
TCP connection to the gateway whether or not the driver was opened or
closed, so every driver a test built connected to 192.168.1.1:80 when
it was collected. The tests now build their drivers with an is_alive()
that opens nothing (the is_alive tests call the class's method), and a
session-wide guard in conftest refuses every connection and fails the
run if anything tried one.

Closes #17
christianmanivong merged commit 5f0c7c76b3 into main 2026-11-02 09:57:11 +00:00
christianmanivong deleted branch fix/bugs-2026-11-02c 2026-11-02 09:57:11 +00:00
Sign in to join this conversation.