Merge pull request 'fix(tests): close fixture drivers so no test opens a socket (#11)' (#12) from fix/bugs-2026-11-02 into main
driver / standard (push) Successful in 20s
driver / lint (push) Successful in 16s
driver / types (push) Successful in 1m3s
driver / test (3.12) (push) Successful in 1m5s
driver / test (3.13) (push) Successful in 1m0s
driver / lowest (push) Successful in 32s
driver / build (push) Successful in 36s
driver / security (push) Successful in 1m29s
CI / driver (push) Successful in 2m57s
driver / publish (push) Skipped
driver / standard (push) Successful in 20s
driver / lint (push) Successful in 16s
driver / types (push) Successful in 1m3s
driver / test (3.12) (push) Successful in 1m5s
driver / test (3.13) (push) Successful in 1m0s
driver / lowest (push) Successful in 32s
driver / build (push) Successful in 36s
driver / security (push) Successful in 1m29s
CI / driver (push) Successful in 2m57s
driver / publish (push) Skipped
This commit was merged in pull request #12.
This commit is contained in:
commit
8b419c54e6
2 files changed
+41
No files matched your search
@@ -0,0 +1,36 @@
|
|||||||
|
"""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}"
|
||||||
@@ -268,6 +268,8 @@ def driver():
|
|||||||
drv = FritzBoxDriver(hostname="fritz.box", username="user", password="pass")
|
drv = FritzBoxDriver(hostname="fritz.box", username="user", password="pass")
|
||||||
drv.fc = mock_fc
|
drv.fc = mock_fc
|
||||||
yield drv
|
yield drv
|
||||||
|
# An open driver makes NAPALM's __del__ probe fritz.box over the network (#11).
|
||||||
|
drv.close()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
@@ -288,6 +290,8 @@ def pppoe_driver():
|
|||||||
drv = FritzBoxDriver(hostname="fritz.box", username="user", password="pass")
|
drv = FritzBoxDriver(hostname="fritz.box", username="user", password="pass")
|
||||||
drv.fc = mock_fc
|
drv.fc = mock_fc
|
||||||
yield drv
|
yield drv
|
||||||
|
# An open driver makes NAPALM's __del__ probe fritz.box over the network (#11).
|
||||||
|
drv.close()
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -309,6 +313,7 @@ class TestOpenClose:
|
|||||||
with patch("napalm_fritzbox.fritzbox.FritzConnection", return_value=mock_fc):
|
with patch("napalm_fritzbox.fritzbox.FritzConnection", return_value=mock_fc):
|
||||||
drv.open()
|
drv.open()
|
||||||
assert drv.fc is mock_fc
|
assert drv.fc is mock_fc
|
||||||
|
drv.close()
|
||||||
|
|
||||||
def test_close_clears_fc(self, driver):
|
def test_close_clears_fc(self, driver):
|
||||||
driver.close()
|
driver.close()
|
||||||
|
|||||||
Reference in new issue
Block a user