feat(mini): send p4/p6 presence flags and connectivity metric from hbc_mini.py
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uc9V1v7iXruubpoLiSE24M
This commit is contained in:
@@ -304,3 +304,78 @@ def test_local_address_on_this_host_never_raises():
|
||||
except mini._ProbeUnavailable:
|
||||
continue
|
||||
assert r is None or isinstance(r, str)
|
||||
|
||||
|
||||
# ---- heartbeat message and plugin wiring ---------------------------------------------
|
||||
|
||||
class _Conn:
|
||||
ackcount = 3
|
||||
rtts = [0.0, 12.5]
|
||||
|
||||
|
||||
class _FlagsProbe:
|
||||
def __init__(self, flags):
|
||||
self._flags = flags
|
||||
|
||||
def flags(self):
|
||||
return self._flags
|
||||
|
||||
|
||||
def test_heartbeat_without_probe_is_unchanged():
|
||||
assert mini._build_heartbeat_msg(_Conn(), 60) == {"acks": 3, "rtt": 12.5, "interval": 60}
|
||||
|
||||
|
||||
def test_heartbeat_carries_presence_flags():
|
||||
msg = mini._build_heartbeat_msg(_Conn(), 60, _FlagsProbe({"p4": True, "p6": False}))
|
||||
assert msg == {"acks": 3, "rtt": 12.5, "interval": 60, "p4": True, "p6": False}
|
||||
|
||||
|
||||
def test_heartbeat_omits_unknown_flags():
|
||||
msg = mini._build_heartbeat_msg(_Conn(), 60, _FlagsProbe({}))
|
||||
assert "p4" not in msg and "p6" not in msg
|
||||
|
||||
|
||||
def test_flags_survive_the_wire_encoding():
|
||||
msg = mini._build_heartbeat_msg(_Conn(), 60, _FlagsProbe({"p4": True, "p6": False}))
|
||||
out = mini._stodict(mini._dicttos("HTB", dict(msg, name="h", id=1)))
|
||||
assert out["p4"] == 1 and out["p6"] == 0
|
||||
|
||||
|
||||
class _FakeProbe:
|
||||
def as_metric(self):
|
||||
return {"ipv4": {"present": True, "addr": "192.0.2.5", "gateway": "192.0.2.1", "rtt": 9.0},
|
||||
"ipv6": {"present": False, "addr": None, "gateway": None, "rtt": None}}
|
||||
|
||||
|
||||
def test_plugin_reports_connectivity_alongside_interfaces(monkeypatch):
|
||||
monkeypatch.setattr(mini, "_probe", _FakeProbe())
|
||||
monkeypatch.setattr(mini.platform, "system", lambda: "Linux")
|
||||
plugin = mini.NetworkMonitorPlugin({})
|
||||
assert asyncio.run(plugin.initialize()) is True
|
||||
data = asyncio.run(plugin._collect_metrics())
|
||||
assert data["connectivity"]["ipv4"]["present"] is True
|
||||
assert data["connectivity"]["ipv6"]["present"] is False
|
||||
|
||||
|
||||
def test_plugin_probe_only_mode_on_non_linux(monkeypatch):
|
||||
monkeypatch.setattr(mini, "_probe", _FakeProbe())
|
||||
monkeypatch.setattr(mini.platform, "system", lambda: "FreeBSD")
|
||||
plugin = mini.NetworkMonitorPlugin({})
|
||||
assert asyncio.run(plugin.initialize()) is True
|
||||
assert list(asyncio.run(plugin._collect_metrics())) == ["connectivity"]
|
||||
|
||||
|
||||
def test_plugin_still_skips_on_non_linux_without_probe(monkeypatch):
|
||||
monkeypatch.setattr(mini, "_probe", None)
|
||||
monkeypatch.setattr(mini.platform, "system", lambda: "FreeBSD")
|
||||
plugin = mini.NetworkMonitorPlugin({})
|
||||
assert asyncio.run(plugin.initialize()) is False
|
||||
assert plugin.skip_reason
|
||||
|
||||
|
||||
def test_plugin_omits_connectivity_without_probe(monkeypatch):
|
||||
monkeypatch.setattr(mini, "_probe", None)
|
||||
monkeypatch.setattr(mini.platform, "system", lambda: "Linux")
|
||||
plugin = mini.NetworkMonitorPlugin({})
|
||||
assert asyncio.run(plugin.initialize()) is True
|
||||
assert "connectivity" not in asyncio.run(plugin._collect_metrics())
|
||||
|
||||
Reference in New Issue
Block a user