Merge pull request 'fix(tests): stub is_alive so no test opens a socket' (#18) from fix/bugs-2026-11-02c into main
driver / standard (push) Successful in 18s
driver / lint (push) Failing after 7s
driver / types (push) Successful in 1m30s
driver / test (3.13) (push) Successful in 1m16s
driver / test (3.12) (push) Successful in 1m31s
driver / lowest (push) Successful in 34s
driver / build (push) Successful in 44s
driver / security (push) Successful in 1m23s
CI / driver (push) Failing after 3m15s
driver / publish (push) Skipped
driver / standard (push) Successful in 18s
driver / lint (push) Failing after 7s
driver / types (push) Successful in 1m30s
driver / test (3.13) (push) Successful in 1m16s
driver / test (3.12) (push) Successful in 1m31s
driver / lowest (push) Successful in 34s
driver / build (push) Successful in 44s
driver / security (push) Successful in 1m23s
CI / driver (push) Failing after 3m15s
driver / publish (push) Skipped
This commit was merged in pull request #18.
This commit is contained in:
commit
5f0c7c76b3
2 files changed
+53
-9
No files matched your search
@@ -0,0 +1,37 @@
|
|||||||
|
"""Shared test setup: unit tests never open a socket (#17)."""
|
||||||
|
|
||||||
|
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()``, and this driver's ``is_alive()`` connects to the gateway) 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}"
|
||||||
+16
-9
@@ -41,6 +41,17 @@ 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.
|
||||||
|
"""
|
||||||
|
driver.is_alive = lambda: {"is_alive": False} # type: ignore[method-assign]
|
||||||
|
return driver
|
||||||
|
|
||||||
|
|
||||||
class MockResponse:
|
class MockResponse:
|
||||||
def __init__(self, json_data, status_code=200, text=""):
|
def __init__(self, json_data, status_code=200, text=""):
|
||||||
self._json = json_data
|
self._json = json_data
|
||||||
@@ -61,11 +72,7 @@ class MockResponse:
|
|||||||
|
|
||||||
class TestZyxelVMGDriver:
|
class TestZyxelVMGDriver:
|
||||||
def setup_method(self):
|
def setup_method(self):
|
||||||
self.driver = ZyxelVMGDriver(
|
self.driver = _offline(ZyxelVMGDriver("192.168.1.1", "admin", "password"))
|
||||||
"192.168.1.1",
|
|
||||||
"admin",
|
|
||||||
"password",
|
|
||||||
)
|
|
||||||
|
|
||||||
def _setup_mocked_http(self, monkeypatch_crypto=True):
|
def _setup_mocked_http(self, monkeypatch_crypto=True):
|
||||||
session = MagicMock(spec=requests.Session)
|
session = MagicMock(spec=requests.Session)
|
||||||
@@ -158,12 +165,12 @@ class TestZyxelVMGDriver:
|
|||||||
def test_is_alive(self):
|
def test_is_alive(self):
|
||||||
with patch("socket.create_connection") as mock_conn:
|
with patch("socket.create_connection") as mock_conn:
|
||||||
mock_conn.return_value.__enter__.return_value = MagicMock()
|
mock_conn.return_value.__enter__.return_value = MagicMock()
|
||||||
assert self.driver.is_alive()["is_alive"] is True
|
assert ZyxelVMGDriver.is_alive(self.driver)["is_alive"] is True
|
||||||
|
|
||||||
def test_is_alive_fail(self):
|
def test_is_alive_fail(self):
|
||||||
with patch("socket.create_connection") as mock_conn:
|
with patch("socket.create_connection") as mock_conn:
|
||||||
mock_conn.side_effect = OSError("refused")
|
mock_conn.side_effect = OSError("refused")
|
||||||
assert self.driver.is_alive()["is_alive"] is False
|
assert ZyxelVMGDriver.is_alive(self.driver)["is_alive"] is False
|
||||||
|
|
||||||
def test_get_facts(self):
|
def test_get_facts(self):
|
||||||
self._setup_mocked_http()
|
self._setup_mocked_http()
|
||||||
@@ -439,7 +446,7 @@ class TestEveryRequestHasATimeout:
|
|||||||
"""requests ignores a ``timeout`` attribute on a Session; it must go with each call."""
|
"""requests ignores a ``timeout`` attribute on a Session; it must go with each call."""
|
||||||
|
|
||||||
def _driver_with_session(self):
|
def _driver_with_session(self):
|
||||||
driver = ZyxelVMGDriver("192.168.1.1", "admin", "password", timeout=7)
|
driver = _offline(ZyxelVMGDriver("192.168.1.1", "admin", "password", timeout=7))
|
||||||
session = MagicMock(spec=requests.Session)
|
session = MagicMock(spec=requests.Session)
|
||||||
session.get.return_value = MockResponse(MOCK_BASIC_INFO)
|
session.get.return_value = MockResponse(MOCK_BASIC_INFO)
|
||||||
session.post.return_value = MockResponse(MOCK_LOGIN_RESPONSE)
|
session.post.return_value = MockResponse(MOCK_LOGIN_RESPONSE)
|
||||||
@@ -483,7 +490,7 @@ class TestPingReportsNoTargetAlive:
|
|||||||
"""The DAL PINGTEST call only acknowledges the request; it carries no reply count."""
|
"""The DAL PINGTEST call only acknowledges the request; it carries no reply count."""
|
||||||
|
|
||||||
def _connected_driver(self):
|
def _connected_driver(self):
|
||||||
driver = ZyxelVMGDriver("192.168.1.1", "admin", "password")
|
driver = _offline(ZyxelVMGDriver("192.168.1.1", "admin", "password"))
|
||||||
driver._session = MagicMock(spec=requests.Session)
|
driver._session = MagicMock(spec=requests.Session)
|
||||||
driver._session.put.return_value = MockResponse({})
|
driver._session.put.return_value = MockResponse({})
|
||||||
driver._sessionkey = "1"
|
driver._sessionkey = "1"
|
||||||
|
|||||||
Reference in new issue
Block a user