Add a fake-only end-to-end stack, Firefox journey, and bounded process cleanup for normal, failed, and interrupted runs. Refs #12 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
127 lines
6.0 KiB
Python
127 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline lifecycle regression: kill the owner/launcher, not their process group."""
|
|
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
import shlex
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
import json
|
|
|
|
sys.dont_write_bytecode = True
|
|
|
|
SUPERVISOR = Path(__file__).with_name("e2e-supervisor.py")
|
|
spec = importlib.util.spec_from_file_location("e2e_supervisor", SUPERVISOR)
|
|
supervision = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(supervision)
|
|
|
|
|
|
@unittest.skipUnless(sys.platform == "linux", "Linux /proc supervision")
|
|
class Lifecycle(unittest.TestCase):
|
|
def run_case(self, victim, normal_exit=False):
|
|
with tempfile.TemporaryDirectory(prefix="e2e-supervisor-test-", dir=os.environ.get("TMPDIR")) as temporary:
|
|
root = Path(temporary)
|
|
project = root / "project"
|
|
scripts = project / "backend/scripts"
|
|
scripts.mkdir(parents=True)
|
|
sleeper = "import signal,time;signal.signal(signal.SIGTERM,signal.SIG_IGN);time.sleep(60)"
|
|
code = (
|
|
"import subprocess,sys,signal,time;"
|
|
f"subprocess.Popen([sys.executable,'-E','-c',{sleeper!r}],start_new_session=True);"
|
|
"signal.signal(signal.SIGTERM,signal.SIG_IGN);"
|
|
+ ("time.sleep(.2)" if normal_exit else "time.sleep(60)")
|
|
)
|
|
(scripts / "test-e2e-worker.sh").write_text(
|
|
"#!/bin/sh\nexec " + shlex.quote(sys.executable) + " -E -c " + shlex.quote(code) + "\n"
|
|
)
|
|
owner_code = (
|
|
"import subprocess,sys,time;"
|
|
f"p=subprocess.Popen([sys.executable,'-E',{str(SUPERVISOR)!r},{str(project)!r}]);"
|
|
"p.wait();time.sleep(60)"
|
|
)
|
|
with (root / "output.log").open("w") as output:
|
|
owner = subprocess.Popen([sys.executable, "-E", "-c", owner_code], env=dict(os.environ, TMPDIR=str(root)), stdout=output, stderr=output)
|
|
identities = {}
|
|
try:
|
|
deadline = time.monotonic() + 5
|
|
info = None
|
|
while time.monotonic() < deadline:
|
|
files = list(root.glob("tradarr-e2e.*/supervisor.json"))
|
|
if files:
|
|
try:
|
|
info = json.loads(files[0].read_text())
|
|
identities = supervision.descendants(info["watcher"])
|
|
if len(identities) >= 2:
|
|
workspace = files[0].parent
|
|
break
|
|
except (OSError, ValueError):
|
|
pass
|
|
time.sleep(.02)
|
|
else:
|
|
self.fail("supervised descendants did not start")
|
|
if not normal_exit:
|
|
if victim == "owner":
|
|
os.kill(owner.pid, signal.SIGKILL)
|
|
else:
|
|
launcher = supervision.process(info["watcher"])[0]
|
|
os.kill(launcher, signal.SIGKILL)
|
|
deadline = time.monotonic() + 7
|
|
while time.monotonic() < deadline:
|
|
alive = [pid for pid, identity in identities.items() if supervision.same_process(pid, identity)]
|
|
if not alive and not workspace.exists():
|
|
break
|
|
time.sleep(.02)
|
|
self.assertFalse(alive, f"surviving descendants: {alive}")
|
|
self.assertFalse(workspace.exists(), "workspace leaked")
|
|
if victim == "launcher":
|
|
self.assertIsNone(owner.poll(), "owner should remain alive for broken-pipe case")
|
|
finally:
|
|
# Tests never kill by a name/shared group, even on failure.
|
|
for pid, identity in identities.items():
|
|
if supervision.same_process(pid, identity):
|
|
os.kill(pid, signal.SIGKILL)
|
|
if owner.poll() is None:
|
|
owner.kill()
|
|
owner.wait(timeout=5)
|
|
|
|
def test_empty_proc_snapshot_is_not_cleanup_confirmation(self):
|
|
# /proc traversal is not atomic during exit/reparenting. Even an empty
|
|
# ancestry snapshot must not override the kernel's live-child state.
|
|
code = "import signal,time;signal.signal(signal.SIGTERM,signal.SIG_IGN);print('ready',flush=True);time.sleep(60)"
|
|
child = subprocess.Popen([sys.executable, "-E", "-c", code], stdout=subprocess.PIPE, text=True)
|
|
original = supervision.descendants
|
|
try:
|
|
self.assertEqual(child.stdout.readline().strip(), "ready")
|
|
supervision.descendants = lambda _root: {}
|
|
supervision.stop_tree(child)
|
|
self.assertIsNotNone(child.poll(), "empty /proc snapshot left a live child")
|
|
finally:
|
|
supervision.descendants = original
|
|
if child.poll() is None:
|
|
child.kill()
|
|
child.wait()
|
|
child.stdout.close()
|
|
|
|
def test_gnu_timeout_duration_parsing(self):
|
|
self.assertEqual(supervision.timeout_duration(["timeout", "--foreground", "--preserve-status", "--signal=INT", "--kill-after=8s", "8s", "make", "test-e2e"]), 8)
|
|
self.assertEqual(supervision.timeout_duration(["timeout", "-k", "8s", "-s", "INT", "1.5m", "make"]), 90)
|
|
self.assertIsNone(supervision.timeout_duration(["timeout", "0", "make"]))
|
|
self.assertIsNone(supervision.timeout_duration(["timeout", "--unknown", "8s", "make"]))
|
|
|
|
def test_sigkill_owner_only(self):
|
|
self.run_case("owner")
|
|
|
|
def test_sigkill_launcher_broken_liveness_pipe(self):
|
|
self.run_case("launcher")
|
|
|
|
def test_normal_worker_exit_reaps_detached_grandchildren(self):
|
|
self.run_case("normal", normal_exit=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|