Skip to content

The agent that grows with you

test(gateway): trim cgroup_cleanup salvage tests to three invariants

kshitijk4poorf0ab78aeafc8
diff --git a/tests/gateway/test_cgroup_cleanup.py b/tests/gateway/test_cgroup_cleanup.py
index 77a4858223b..28497e595f4 100644
--- a/tests/gateway/test_cgroup_cleanup.py
+++ b/tests/gateway/test_cgroup_cleanup.py
@@ -3,7 +3,6 @@
from __future__ import annotations
import os
-import signal
from pathlib import Path
import pytest
@@ -44,66 +43,6 @@ class TestReapCgroup:
class TestMain:
- def test_main_refuses_when_parent_not_systemd(self, tmp_path, monkeypatch):
- # Parent comm is a shell (any non-systemd parent): main() must refuse
- # and must never reach os.kill — the no-arg reaper shares the parent's
- # live cgroup, so reaping there SIGKILLs that process.
- comm = tmp_path / "comm"
- comm.write_text("bash\n")
- monkeypatch.setattr(cgroup_cleanup.os, "getppid", lambda: 4242)
- monkeypatch.setattr(
- cgroup_cleanup,
- "Path",
- lambda p: comm if p == "/proc/4242/comm" else Path(p),
- )
-
- def _explode(*_a, **_kw):
- pytest.fail("os.kill must not be called when the parent is not systemd")
-
- monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
- assert cgroup_cleanup.main() == 1
-
- def test_main_refuses_when_comm_unreadable(self, monkeypatch):
- # Parent pid vanished (or /proc unreadable): fail closed — refuse.
- monkeypatch.setattr(cgroup_cleanup.os, "getppid", lambda: 999999)
-
- def _missing(*_a, **_kw):
- raise OSError("no such process")
-
- monkeypatch.setattr(
- Path,
- "read_text",
- _missing,
- raising=True,
- )
-
- def _explode(*_a, **_kw):
- pytest.fail("os.kill must not be called when the parent is undeterminable")
-
- monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
- assert cgroup_cleanup.main() == 1
-
- def test_main_proceeds_when_parent_is_systemd(self, tmp_path, monkeypatch):
- # Simulates the ExecStopPost context: a real test child cannot have
- # systemd as its parent, so the manager's comm is stood in with a fake
- # /proc/<ppid>/comm. reap_cgroup is stubbed so the call never signals.
- comm = tmp_path / "comm"
- comm.write_text("systemd\n")
- monkeypatch.setattr(cgroup_cleanup.os, "getppid", lambda: 4242)
- monkeypatch.setattr(
- cgroup_cleanup,
- "Path",
- lambda p: comm if p == "/proc/4242/comm" else Path(p),
- )
- calls: list[tuple] = []
- monkeypatch.setattr(
- cgroup_cleanup,
- "reap_cgroup",
- lambda *a, **kw: calls.append((a, kw)) or 0,
- )
- assert cgroup_cleanup.main() == 0
- assert len(calls) == 1
-
def test_main_refuses_when_pid1_parent_is_not_systemd(self, tmp_path, monkeypatch):
# Regression: a container where the gateway itself is PID 1 (or init
# is tini/launchd) must NOT be authorized by "ppid == 1". PID 1 has to
@@ -127,13 +66,16 @@ class TestMain:
class TestLiveGatewayGuard:
- def test_reap_refuses_when_live_gateway_in_cgroup(self, monkeypatch):
- # Regression: a live gateway PID still in cgroup.procs (e.g. the
- # gateway is PID 1 in a plain container, or a targeted reap of a
- # still-running service) must abort the reap before any signal.
+ @pytest.mark.parametrize("verb", ["run", "restart"])
+ def test_reap_refuses_when_live_gateway_in_cgroup(self, monkeypatch, verb):
+ # Regression: a live gateway PID still in cgroup.procs (gateway is PID 1
+ # in a plain container, a targeted reap of a running service, or an
+ # in-process `gateway restart` on a host without a service manager)
+ # must abort the reap before any signal.
import gateway.status
- gateway_cmdline = "/opt/hermes/venv/bin/python -m hermes_cli.main gateway run"
+ cgroup_path = "/some.slice/some-gateway.service"
+ gateway_cmdline = f"/opt/hermes/venv/bin/python -m hermes_cli.main gateway {verb}"
monkeypatch.setattr(
cgroup_cleanup, "_read_cgroup_pids", lambda _p: [777, os.getpid()]
)
@@ -147,31 +89,7 @@ class TestLiveGatewayGuard:
pytest.fail("os.kill must not signal a cgroup holding a live gateway")
monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
- assert cgroup_cleanup.reap_cgroup("/some.slice/some-gateway.service") == 0
-
- def test_reap_refuses_when_live_restart_in_cgroup(self, monkeypatch):
- # Regression (restart form): on a host without a service manager,
- # `hermes gateway restart` runs run_gateway() in-process — the
- # restart process is the live runtime and must block a reap too.
- # The guard must therefore use the *runtime* matcher (run|restart),
- # not the strict run-only lifecycle matcher.
- import gateway.status
-
- restart_cmdline = "/opt/hermes/venv/bin/python -m hermes_cli.main gateway restart"
- monkeypatch.setattr(
- cgroup_cleanup, "_read_cgroup_pids", lambda _p: [777, os.getpid()]
- )
- monkeypatch.setattr(
- gateway.status,
- "_read_process_cmdline",
- lambda pid: restart_cmdline if pid == 777 else None,
- )
-
- def _explode(*_a, **_kw):
- pytest.fail("os.kill must not signal a cgroup holding a live restart process")
-
- monkeypatch.setattr(cgroup_cleanup.os, "kill", _explode)
- assert cgroup_cleanup.reap_cgroup("/some.slice/some-gateway.service") == 0
+ assert cgroup_cleanup.reap_cgroup(cgroup_path) == 0
def test_reap_proceeds_when_only_orphans_in_cgroup(self, monkeypatch):
# Allow-path contract: orphans with non-gateway command lines must