mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-29 17:21:13 +01:00
Let the verified installer retain its own output and status for help, cancellation, removal and actual updates instead of adding a false restart claim. Cover exact signed-helper output and exits across saved channel choices and bounded acquisition failures. Measure service polling only after a separately bounded initialization handshake, keeping every existing service limit, cleanup and state check plus a deliberately delayed initialization regression. Contract-Neutral: Restores existing truthful installer completion and timeout-proof scope; service budgets, installation authority, signature, channel, saved-state and rendered UI contracts are unchanged. Change-source: pulse-maintainer
203 lines
10 KiB
Python
203 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Real timeout/clock controls for the server installer's service boundary.
|
|
|
|
Only a private ordinary-user manager and its child run. No host service,
|
|
application binary, database, privilege change or native recovery is exercised.
|
|
"""
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import select
|
|
import signal
|
|
import subprocess
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
INSTALLER = Path(os.environ.get("PULSE_INSTALLER_UNDER_TEST", ROOT / "install.sh")).resolve()
|
|
|
|
MANAGER = r'''#!/usr/bin/env python3
|
|
import json, os, signal, subprocess, sys, time
|
|
from pathlib import Path
|
|
f = Path(os.environ["FIXTURE"])
|
|
args = sys.argv[1:]
|
|
mode = os.environ["MANAGER_MODE"]
|
|
with (f / "calls").open("a") as stream:
|
|
stream.write(json.dumps(args) + "\n")
|
|
stubborn = (mode == "stubborn" or
|
|
mode == "post-stop-stubborn" and args[0] == "show" and (f / "stopped").exists())
|
|
if stubborn:
|
|
signal.signal(signal.SIGTERM, signal.SIG_IGN)
|
|
child = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(60)"])
|
|
(f / "processes").write_text(json.dumps({"group": os.getpgrp(), "pids": [os.getpid(), child.pid]}))
|
|
child.wait()
|
|
sys.exit(97)
|
|
if mode.startswith("slow-"):
|
|
time.sleep(1.4)
|
|
if args[0] == "is-active":
|
|
sys.exit(0 if mode in ("active", "slow-active") else 3)
|
|
if args[0] == "show":
|
|
print("inactive" if (f / "stopped").exists() else "active")
|
|
elif args[0] == "stop":
|
|
(f / "stopped").touch()
|
|
else:
|
|
sys.exit(98)
|
|
'''
|
|
|
|
|
|
class ServiceDeadlines(unittest.TestCase):
|
|
def exercise(self, body, mode="active", limit=8, startup_delay=0):
|
|
with tempfile.TemporaryDirectory(prefix="pulse-service-deadline-") as directory:
|
|
f = Path(directory)
|
|
(f / "tools").mkdir()
|
|
manager = f / "tools/systemctl"
|
|
manager.write_text(MANAGER)
|
|
manager.chmod(0o700)
|
|
originals = {f / "saved-config": b"private synthetic state\n",
|
|
f / "old-executable": b"synthetic old binary\n"}
|
|
for path, data in originals.items():
|
|
path.write_bytes(data)
|
|
path.chmod(0o600)
|
|
env = {k: v for k, v in os.environ.items() if not k.startswith("PULSE_")}
|
|
env.update(INSTALLER=str(INSTALLER), FIXTURE=str(f), MANAGER_MODE=mode,
|
|
PATH=str(f / "tools") + os.pathsep + env["PATH"],
|
|
PULSE_INSTALL_DIR=str(f / "install"), PULSE_CONFIG_DIR=str(f / "config"))
|
|
script = 'set -euo pipefail\n'
|
|
if startup_delay:
|
|
script += f'command sleep {startup_delay}\n'
|
|
script += ('source "$INSTALLER"\n'
|
|
'printf R >&"$FIXTURE_READY_FD"\n'
|
|
'exec {FIXTURE_READY_FD}>&-\n'
|
|
'IFS= read -r fixture_start\n' + body)
|
|
# Production starts its budget inside the polling function, not
|
|
# while Bash loads the installer. Bound startup separately, then
|
|
# release the ready child through stdin only after starting the
|
|
# existing measurement. This is a pipe handshake, not a sleep or
|
|
# a wider service deadline, and keeps all fixture output intact.
|
|
ready_read, ready_write = os.pipe()
|
|
env["FIXTURE_READY_FD"] = str(ready_write)
|
|
startup_started = time.monotonic()
|
|
with os.fdopen(ready_read, "rb", buffering=0) as ready:
|
|
try:
|
|
process = subprocess.Popen(["bash", "-c", script], cwd=f, env=env,
|
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE, text=True,
|
|
start_new_session=True, pass_fds=(ready_write,))
|
|
finally:
|
|
os.close(ready_write)
|
|
startup_ready = bool(select.select([ready], [], [], limit)[0] and ready.read(1) == b"R")
|
|
startup_elapsed = time.monotonic() - startup_started
|
|
start = time.monotonic()
|
|
exceeded = not startup_ready
|
|
stdout = stderr = ""
|
|
if startup_ready:
|
|
try:
|
|
stdout, stderr = process.communicate(input="\n", timeout=limit)
|
|
except subprocess.TimeoutExpired:
|
|
exceeded = True
|
|
elapsed = time.monotonic() - start
|
|
owned = json.loads((f / "processes").read_text()) if (f / "processes").exists() else None
|
|
# Both stubborn processes inherit these output pipes and never
|
|
# close them. EOF before our backstop proves timeout ended that
|
|
# fixture tree, without depending on a host /proc mount or claiming
|
|
# that an orphaned zombie has already been reaped.
|
|
# Always clean up this recorded, test-created group on a failure.
|
|
if owned and exceeded:
|
|
self.assertGreater(owned["group"], 1)
|
|
self.assertNotEqual(owned["group"], os.getpgrp())
|
|
try:
|
|
os.killpg(owned["group"], signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
if exceeded:
|
|
try:
|
|
os.killpg(process.pid, signal.SIGKILL)
|
|
except ProcessLookupError:
|
|
pass
|
|
stdout, stderr = process.communicate(timeout=3)
|
|
self.assertEqual({p: p.read_bytes() for p in originals}, originals)
|
|
self.assertTrue(all(p.stat().st_mode & 0o777 == 0o600 for p in originals))
|
|
self.assertTrue(startup_ready, dict(startup_elapsed=startup_elapsed,
|
|
stdout=stdout, stderr=stderr))
|
|
return dict(code=process.returncode, stdout=stdout, stderr=stderr,
|
|
exceeded=exceeded, elapsed=elapsed, startup_elapsed=startup_elapsed, owned=owned,
|
|
calls=[json.loads(line) for line in (f / "calls").read_text().splitlines()]
|
|
if (f / "calls").exists() else [])
|
|
|
|
def assert_refused(self, result):
|
|
self.assertFalse(result["exceeded"], result)
|
|
self.assertEqual(result["code"], 1, result)
|
|
self.assertNotIn("UNEXPECTED_SUCCESS", result["stdout"])
|
|
|
|
def test_startup_budget_includes_slow_manager_reads(self):
|
|
result = self.exercise('wait_for_service_active fixture 2\n', "slow-inactive", limit=6)
|
|
self.assert_refused(result)
|
|
# Bash's elapsed clock has one-second resolution. This allows that
|
|
# granularity, not two extra five-second probes per requested second.
|
|
self.assertLess(result["elapsed"], 3.6, result)
|
|
self.assertLessEqual(len(result["calls"]), 2, result)
|
|
|
|
def test_active_response_after_budget_cannot_pass(self):
|
|
result = self.exercise('wait_for_service_active fixture 1\nprintf UNEXPECTED_SUCCESS\n',
|
|
"slow-active", limit=4)
|
|
self.assert_refused(result)
|
|
self.assertLess(result["elapsed"], 2.5, result)
|
|
|
|
def test_installer_initialization_is_outside_the_poll_budget(self):
|
|
# The old oracle counts this deliberate fixture startup delay as a
|
|
# service timeout, even though the real one-second poll refuses it.
|
|
result = self.exercise('wait_for_service_active fixture 1\nprintf UNEXPECTED_SUCCESS\n',
|
|
"slow-active", limit=6, startup_delay=3)
|
|
self.assert_refused(result)
|
|
self.assertGreaterEqual(result["startup_elapsed"], 3, result)
|
|
self.assertLess(result["elapsed"], 2.5, result)
|
|
self.assertEqual(result["calls"], [["is-active", "--quiet", "fixture"]])
|
|
|
|
def test_startup_kills_term_ignoring_manager_and_child(self):
|
|
result = self.exercise('wait_for_service_active fixture 1\n', "stubborn", limit=4)
|
|
self.assert_refused(result)
|
|
self.assertLess(result["elapsed"], 3.5, result)
|
|
self.assertEqual(len(result["owned"]["pids"]), 2)
|
|
self.assertEqual(result["calls"], [["is-active", "--quiet", "fixture"]])
|
|
|
|
def test_stop_command_kills_term_ignoring_manager_and_child(self):
|
|
result = self.exercise('safe_systemctl stop fixture\n', "stubborn")
|
|
self.assert_refused(result)
|
|
self.assertLess(result["elapsed"], 7.5, result)
|
|
self.assertEqual(len(result["owned"]["pids"]), 2)
|
|
self.assertEqual(result["calls"], [["stop", "fixture"]])
|
|
|
|
def test_uncertain_initial_state_never_stops_or_restarts_service(self):
|
|
result = self.exercise('stop_pulse_for_replacement fixture\nprintf UNEXPECTED_SUCCESS\n', "stubborn")
|
|
self.assert_refused(result)
|
|
self.assertIn("refusing binary replacement", result["stderr"])
|
|
self.assertEqual(result["calls"], [["show", "fixture", "--property=ActiveState", "--value"]])
|
|
|
|
def test_uncertain_post_stop_state_never_restarts_or_admits_replacement(self):
|
|
result = self.exercise('stop_pulse_for_replacement fixture\nprintf UNEXPECTED_SUCCESS\n',
|
|
"post-stop-stubborn")
|
|
self.assert_refused(result)
|
|
self.assertIn("Could not confirm Pulse stopped", result["stderr"])
|
|
self.assertEqual(result["calls"], [["show", "fixture", "--property=ActiveState", "--value"],
|
|
["stop", "fixture"],
|
|
["show", "fixture", "--property=ActiveState", "--value"]])
|
|
|
|
def test_fast_active_and_confirmed_stop_still_succeed(self):
|
|
for body in ('wait_for_service_active fixture 2\n', 'stop_pulse_for_replacement fixture\n'):
|
|
with self.subTest(body=body):
|
|
result = self.exercise(body)
|
|
self.assertFalse(result["exceeded"], result)
|
|
self.assertEqual(result["code"], 0, result)
|
|
|
|
def test_invalid_budget_does_not_start_a_manager(self):
|
|
for budget in ("0", "-1", "1x", "1+1"):
|
|
with self.subTest(budget=budget):
|
|
result = self.exercise(f'wait_for_service_active fixture "{budget}"\n')
|
|
self.assert_refused(result)
|
|
self.assertEqual(result["calls"], [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|