fix(tests): stub is_alive so no test opens a socket #18
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:
|
||||
def __init__(self, json_data, status_code=200, text=""):
|
||||
self._json = json_data
|
||||
@@ -61,11 +72,7 @@ class MockResponse:
|
||||
|
||||
class TestZyxelVMGDriver:
|
||||
def setup_method(self):
|
||||
self.driver = ZyxelVMGDriver(
|
||||
"192.168.1.1",
|
||||
"admin",
|
||||
"password",
|
||||
)
|
||||
self.driver = _offline(ZyxelVMGDriver("192.168.1.1", "admin", "password"))
|
||||
|
||||
def _setup_mocked_http(self, monkeypatch_crypto=True):
|
||||
session = MagicMock(spec=requests.Session)
|
||||
@@ -158,12 +165,12 @@ class TestZyxelVMGDriver:
|
||||
def test_is_alive(self):
|
||||
with patch("socket.create_connection") as mock_conn:
|
||||
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):
|
||||
with patch("socket.create_connection") as mock_conn:
|
||||
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):
|
||||
self._setup_mocked_http()
|
||||
@@ -439,7 +446,7 @@ class TestEveryRequestHasATimeout:
|
||||
"""requests ignores a ``timeout`` attribute on a Session; it must go with each call."""
|
||||
|
||||
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.get.return_value = MockResponse(MOCK_BASIC_INFO)
|
||||
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."""
|
||||
|
||||
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.put.return_value = MockResponse({})
|
||||
driver._sessionkey = "1"
|
||||
|
||||
Reference in new issue
Block a user