driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 14s
driver / types (pull_request) Successful in 1m14s
driver / test (3.13) (pull_request) Successful in 1m3s
driver / test (3.12) (pull_request) Successful in 1m11s
driver / lowest (pull_request) Successful in 28s
driver / build (pull_request) Successful in 38s
driver / security (pull_request) Successful in 1m22s
CI / driver (pull_request) Successful in 2m57s
driver / publish (pull_request) Skipped
The driver fixtures and test_open_sets_fc_on_success left the driver open. When it was collected, NAPALM's NetworkDriver.__del__ called is_alive(), which resolved fritz.box and connected to port 49443 once per test; the error was swallowed, so it only showed as slow or flaky runs. The fixtures now close the driver after the test. A session-wide guard in conftest refuses every connection and fails the run if any test, including a __del__, tried one. Closes #11
37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
"""Shared test setup: unit tests never open a socket."""
|
|
|
|
import gc
|
|
import socket
|
|
from collections.abc import Iterator
|
|
from typing import Any
|
|
|
|
import pytest
|
|
|
|
_attempts: list[str] = []
|
|
|
|
|
|
def _refuse_create_connection(address: Any, *args: Any, **kwargs: Any) -> socket.socket:
|
|
_attempts.append(f"create_connection{address!r}")
|
|
raise OSError("unit tests never open a socket")
|
|
|
|
|
|
def _refuse_connect(self: socket.socket, address: Any) -> None:
|
|
_attempts.append(f"connect{address!r}")
|
|
raise OSError("unit tests never open a socket")
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
def _no_network() -> Iterator[None]:
|
|
"""Refuse every connection and fail the run if anything tried one.
|
|
|
|
A connection attempted from ``__del__`` (NAPALM's ``NetworkDriver.__del__`` calls
|
|
``is_alive()`` on a driver that was never closed) is swallowed there, so the
|
|
attempts are recorded and checked once every driver has been collected.
|
|
"""
|
|
with pytest.MonkeyPatch.context() as mp:
|
|
mp.setattr(socket, "create_connection", _refuse_create_connection)
|
|
mp.setattr(socket.socket, "connect", _refuse_connect)
|
|
yield
|
|
gc.collect()
|
|
assert not _attempts, f"unit tests tried to open a socket: {_attempts}"
|