tcp_check swallowed every exception and icmp_check returned on a non-zero exit without a reason, so every down ICMP/TCP monitor stored error_msg NULL and the Monitors page and MCP showed it down with no why. Each failure now names its cause: "No reply within 2 s", "Connection refused on port N", "No answer on port N within 5 s", "Can't resolve <name>: ...", the OS error, or ping's own first stderr line. An ICMP check that fell back to TCP says so, and a ping that outlives its timeout is killed rather than left running. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.5 KiB
Python
66 lines
2.5 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):
|
|
async def boom(*a, **kw):
|
|
raise exc
|
|
monkeypatch.setattr(ping.asyncio, "open_connection", 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""):
|
|
self.returncode, self._stderr = returncode, stderr
|
|
|
|
async def communicate(self):
|
|
return b"", 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_without_ping_the_tcp_stand_in_says_so(monkeypatch):
|
|
async def no_ping(*a, **kw):
|
|
raise FileNotFoundError("ping")
|
|
|
|
async def refused(*a, **kw):
|
|
raise ConnectionRefusedError(111, "Connection refused")
|
|
monkeypatch.setattr(ping.asyncio, "create_subprocess_exec", no_ping)
|
|
monkeypatch.setattr(ping.asyncio, "open_connection", refused)
|
|
got = asyncio.run(ping.icmp_check("nas.lan"))
|
|
assert got["error_msg"] == "ping unavailable, TCP check instead: Connection refused on port 80"
|