Files
hermes-agent/tests/gateway/test_systemd_notify.py
Ben Barclay ecefb1e2a2 test(gateway): make 7 macOS-failing tests host-honest (#107035)
scripts/run_tests.sh tests/gateway/ on a clean macOS checkout fails 7 tests
that pass on the Linux CI lane. The macOS CI lane runs only -m macos_only,
so these had never executed on a Mac. Each failure was diagnosed
individually, not assumed to share a cause:

* test_systemd_notify::test_notify_supports_systemd_abstract_socket
  binds a NUL-prefixed abstract AF_UNIX name, a Linux kernel feature.
  -> @pytest.mark.linux_only (the abstract socket IS the subject).

* test_shutdown_forensics::test_spawns_subprocess_and_writes_output
  spawn_async_diagnostic() wraps a Linux-only script (ps --sort,
  /proc/loadavg, dmesg, pstree) in GNU `timeout`, which macOS lacks.
  Popen raises, the producer returns None by design (fail-soft).
  -> @pytest.mark.linux_only, replacing a bare skipif(win32).

* test_scale_to_zero::test_suspend_self_{posts_suspend,non_2xx}
  bound the fake flaps socket under pytest tmp_path, which exceeds
  sun_path (104 B on macOS/BSD, 108 on Linux). Production connects to the
  fixed /.fly/api so the code is not length-exposed; only the fake needed
  a short home. -> short_sock_dir fixture (gettempdir -> /tmp fallback,
  skip if neither fits). Verified with a 130-char TMPDIR on Linux.

* test_buzz_adapter::test_live_media_redacts_long_path_before_bounding
  built 160-char path components; a single component is capped at 255 B
  on both hosts and the full path hit PATH_MAX (1024) on macOS. The
  invariant (path redacted BEFORE the 900-char bound) needs the path to
  exceed 900 chars, not 1000+. -> grow 80-char components until the path
  passes _MAX_CLI_MESSAGE_CHARS, assert < 1024. Mutation-checked: moving
  the redaction after the bound fails the test.

* test_wecom_callback::{test_build_event_extracts_text_message,
  test_poll_loop_dispatches_handle_message}
  NOT environmental: `_build_event` parses with defusedxml, which ships
  only via the `wecom` extra (or transitively via `youtube` -> [all] in
  CI). A dev venv without it gets ET=None. -> dependency skipif on
  DEFUSEDXML_AVAILABLE (not an OS marker; the 4 other tests in the file
  still run).

Verified: scripts/run_tests.sh on macOS: 226 passed, 4 skipped (2
linux_only, 2 defusedxml). python:3.11-slim container with [dev,wecom]:
-m "linux_only and not integration" selects and passes both marked tests;
all 5 files 229 passed.
2026-09-10 12:08:51 +10:00

80 lines
2.2 KiB
Python

"""Tests for the optional systemd event-loop watchdog protocol."""
from __future__ import annotations
import asyncio
import socket
import pytest
@pytest.mark.linux_only # abstract (NUL-prefixed) AF_UNIX names are a Linux kernel feature
def test_notify_supports_systemd_abstract_socket(monkeypatch):
name = "\0hermes-test-notify"
receiver = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
receiver.bind(name)
receiver.settimeout(1.0)
monkeypatch.setenv("NOTIFY_SOCKET", "@hermes-test-notify")
try:
from gateway.systemd_notify import notify
assert notify("WATCHDOG=1") is True
assert receiver.recv(4096) == b"WATCHDOG=1"
finally:
receiver.close()
def test_notify_uses_nonblocking_datagram_send(monkeypatch):
calls: list[object] = []
class _Sender:
def __enter__(self):
return self
def __exit__(self, *_args):
return None
def setblocking(self, value):
calls.append(("setblocking", value))
def connect(self, address):
calls.append(("connect", address))
def send(self, payload):
calls.append(("send", payload))
import gateway.systemd_notify as notify_mod
monkeypatch.setenv("NOTIFY_SOCKET", "/tmp/hermes-test-notify")
monkeypatch.setattr(notify_mod.socket, "socket", lambda *_args: _Sender())
assert notify_mod.notify("READY=1") is True
assert calls[0] == ("setblocking", False)
@pytest.mark.asyncio
async def test_watchdog_sends_ready_heartbeat_and_stopping(monkeypatch):
calls: list[str] = []
monkeypatch.setenv("NOTIFY_SOCKET", "/tmp/hermes-test-notify")
monkeypatch.setenv("WATCHDOG_USEC", "20000")
import gateway.systemd_notify as notify_mod
monkeypatch.setattr(
notify_mod, "notify", lambda message: calls.append(message) or True
)
watchdog = notify_mod.SystemdWatchdog(lag_tolerance_seconds=1.0)
assert watchdog.start() is True
assert watchdog.ready("Gateway running") is True
await asyncio.sleep(0.04)
await watchdog.stop()
assert any(message.startswith("READY=1") for message in calls)
assert "WATCHDOG=1" in calls
assert calls[-1] == "STOPPING=1"
assert watchdog.unhealthy is False