Files
FabledSteward/tests/core/test_ping_checks.py
T
bvandeusenandClaude Opus 5.5 4639ac6e52
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 1m28s
CI / integration (push) Successful in 1m39s
CI / publish (push) Successful in 1m14s
fix(monitors): a ping or TCP check times the network, not Steward (#6105)
response_ms was the whole `ping -c 1` process, or an open_connection awaited
on the event loop: about 15 ms on a LAN whose round trip is under 1, and every
host read slow together whenever Steward was busy. A ping now reports the
round trip ping itself printed (the elapsed time only if it printed none);
a TCP connect is timed in a thread, the name resolved first and not counted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 15:38:17 -04:00

98 lines
3.8 KiB
Python

"""A failed ping or TCP check says why (#5430): no reply, refused, no answer
in time, a name that doesn't resolve, or ping's own complaint; an ICMP check
that fell back to TCP says so."""
import asyncio
import socket
import pytest
from steward.monitors import ping
@pytest.mark.parametrize("exc,want", [
(asyncio.TimeoutError(), "No answer on port 22 within 5 s"),
(ConnectionRefusedError(111, "Connection refused"), "Connection refused on port 22"),
(socket.gaierror(-2, "Name or service not known"), "Can't resolve nas.lan: Name or service not known"),
(OSError(113, "No route to host"), "Can't connect to port 22: No route to host"),
(RuntimeError("odd"), "odd"),
])
def test_a_tcp_failure_says_why(monkeypatch, exc, want):
def boom(*a, **kw):
raise exc
monkeypatch.setattr(ping, "_connect_ms", boom)
got = asyncio.run(ping.tcp_check("nas.lan", 22))
assert got == {"is_up": False, "response_ms": None, "error_msg": want}
class _Proc:
def __init__(self, returncode, stderr=b"", stdout=b""):
self.returncode, self._stderr, self._stdout = returncode, stderr, stdout
async def communicate(self):
return self._stdout, self._stderr
@pytest.mark.parametrize("code,stderr,want", [
(1, b"", "No reply within 2 s"),
(2, b"ping: nas.lan: Name or service not known\n", "ping: nas.lan: Name or service not known"),
(2, b"", "ping failed (exit 2)"),
])
def test_a_ping_failure_says_why(monkeypatch, code, stderr, want):
async def spawn(*a, **kw):
return _Proc(code, stderr)
monkeypatch.setattr(ping.asyncio, "create_subprocess_exec", spawn)
got = asyncio.run(ping.icmp_check("nas.lan"))
assert got == {"is_up": False, "response_ms": None, "error_msg": want}
def test_a_reply_is_up_with_no_error(monkeypatch):
async def spawn(*a, **kw):
return _Proc(0)
monkeypatch.setattr(ping.asyncio, "create_subprocess_exec", spawn)
got = asyncio.run(ping.icmp_check("nas.lan"))
assert got["is_up"] is True and got["error_msg"] is None
def test_a_reply_s_time_is_the_round_trip_ping_reports_not_the_process(monkeypatch):
out = (b"PING nas.lan (192.168.0.7) 56(84) bytes of data.\n"
b"64 bytes from 192.168.0.7: icmp_seq=1 ttl=64 time=0.412 ms\n")
async def spawn(*a, **kw):
await asyncio.sleep(0.05) # a slow process start counts for nothing
return _Proc(0, stdout=out)
monkeypatch.setattr(ping.asyncio, "create_subprocess_exec", spawn)
assert asyncio.run(ping.icmp_check("nas.lan"))["response_ms"] == 0.41
@pytest.mark.parametrize("out,want", [
(b"64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=12.3 ms", 12.3),
(b"64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time<1 ms", 1.0),
(b"64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.05ms", 0.05),
(b"", None),
])
def test_ping_s_round_trip_is_read_from_its_line(out, want):
assert ping.ping_rtt(out) == want
def test_a_tcp_connect_is_timed_apart_from_the_event_loop(monkeypatch):
import threading
def connect(address, port):
assert threading.current_thread() is not threading.main_thread()
return 0.8
monkeypatch.setattr(ping, "_connect_ms", connect)
assert asyncio.run(ping.tcp_check("nas.lan", 22)) == {"is_up": True, "response_ms": 0.8, "error_msg": None}
def test_without_ping_the_tcp_stand_in_says_so(monkeypatch):
async def no_ping(*a, **kw):
raise FileNotFoundError("ping")
def refused(*a, **kw):
raise ConnectionRefusedError(111, "Connection refused")
monkeypatch.setattr(ping.asyncio, "create_subprocess_exec", no_ping)
monkeypatch.setattr(ping, "_connect_ms", refused)
got = asyncio.run(ping.icmp_check("nas.lan"))
assert got["error_msg"] == "ping unavailable, TCP check instead: Connection refused on port 80"