diff --git a/scripts/installtests/run_unix_contracts.py b/scripts/installtests/run_unix_contracts.py index 3c6b68a40c..eae2edca8b 100644 --- a/scripts/installtests/run_unix_contracts.py +++ b/scripts/installtests/run_unix_contracts.py @@ -10,7 +10,9 @@ independent processes together, retaining complete, discovery-ordered output. Refill a free slot without waiting for its sibling. An observed failure stops admission and drains every already-started check before returning failure. Observe process exit independently of inherited output descriptors; a timed-out -test's child must not keep a proof slot waiting for pipe EOF. +test's child must not keep a proof slot waiting for pipe EOF. Compilation and +exact-binary discovery have separate preparation bounds, before any test alarm +exists; their failures retain output and cannot admit checks. """ import argparse @@ -30,6 +32,46 @@ PACKAGE = "./scripts/installtests" PREFLIGHT = ("TestInstallSHNASBootOwnersQuiesceBeforeRecoveryErasure",) MAX_CHECKS = 2 PROCESS_EXIT_GRACE_SECONDS = 5 +COMPILE_TIMEOUT_SECONDS = 10 * 60 +DISCOVERY_TIMEOUT_SECONDS = 30 + + +def prepare(command, *, phase, cwd, timeout, capture=False): + # No Go test alarm exists while the compiler or TestMain/listing runs. + # Own only this phase's process group and observe its leader independently + # of inherited output descriptors. Keep stderr out of the test inventory. + if not 0 < timeout < float("inf"): + raise ValueError("native installer preparation bound must be finite and positive") + print(f"Native Unix installer {phase}: limit {timeout}s", flush=True) + descendants = False + with tempfile.TemporaryFile(mode="w+", encoding="utf-8") as output, \ + tempfile.TemporaryFile(mode="w+", encoding="utf-8") as errors: + process = subprocess.Popen(command, cwd=cwd, stdout=output, stderr=errors, + start_new_session=True) + try: + try: + code = process.wait(timeout=timeout) + finally: + try: + os.killpg(process.pid, signal.SIGKILL) + descendants = True + except ProcessLookupError: + pass + process.wait(timeout=PROCESS_EXIT_GRACE_SECONDS) + finally: + output.seek(0) + if not capture or process.returncode != 0 or descendants: + shutil.copyfileobj(output, sys.stdout) + sys.stdout.flush() + errors.seek(0) + shutil.copyfileobj(errors, sys.stderr) + sys.stderr.flush() + if code: + raise subprocess.CalledProcessError(code, command) + if descendants: + raise RuntimeError("native installer preparation left an owned process group: " + phase) + # Only successful, fully retired discovery stdout can define coverage. + return output.read() if capture else None def process_deadline(timeout): @@ -160,11 +202,12 @@ def main(phase="all"): raise ValueError("unknown native installer phase") with tempfile.TemporaryDirectory(prefix="pulse-native-contracts-") as directory: binary = Path(directory) / "installtests.test" - subprocess.run(["go", "test", "-c", "-o", str(binary), PACKAGE], - cwd=ROOT, check=True) - inventory = subprocess.run([str(binary), "-test.list=."], cwd=ROOT / "scripts/installtests", - check=True, capture_output=True, text=True) - early, remaining = partition(inventory.stdout) + prepare(["go", "test", "-c", "-o", str(binary), PACKAGE], phase="compilation", + cwd=ROOT, timeout=COMPILE_TIMEOUT_SECONDS) + inventory = prepare([str(binary), "-test.list=."], phase="discovery", + cwd=ROOT / "scripts/installtests", timeout=DISCOVERY_TIMEOUT_SECONDS, + capture=True) + early, remaining = partition(inventory) selected = early if phase == "preflight" else remaining if phase == "remaining" else early + remaining print(f"Native Unix installer {phase}: {len(selected)} discovered checks", flush=True) if phase == "all": diff --git a/scripts/tests/test_native_installer_contract_runner.py b/scripts/tests/test_native_installer_contract_runner.py index 1a7c35cc40..5e30d830ee 100644 --- a/scripts/tests/test_native_installer_contract_runner.py +++ b/scripts/tests/test_native_installer_contract_runner.py @@ -11,7 +11,6 @@ import sys import tempfile import threading import time -from types import SimpleNamespace import unittest from unittest.mock import patch @@ -76,6 +75,123 @@ class OwnedChildObservationControls(unittest.TestCase): output_child_stopped(123) +class PreparationControls(unittest.TestCase): + def test_preparation_rejects_unbounded_limits_before_start(self): + for timeout in (0, -1, float("inf"), float("nan")): + with self.subTest(timeout=timeout), patch.object(runner.subprocess, "Popen") as start: + with self.assertRaises(ValueError): + runner.prepare(["unused"], phase="discovery", cwd="unused", timeout=timeout) + start.assert_not_called() + + def test_output_and_original_exit_survive_both_preparation_modes(self): + for capture in (False, True): + for code in (0, 23): + with self.subTest(capture=capture, code=code), tempfile.TemporaryDirectory() as root: + output, errors = io.StringIO(), io.StringIO() + result = None + command = [sys.executable, "-I", "-S", "-c", + "import sys; print('stdout Σ'); print('stderr Σ', file=sys.stderr); " + f"sys.exit({code})"] + with contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors): + if code: + with self.assertRaises(subprocess.CalledProcessError) as failure: + runner.prepare(command, phase="fixture", cwd=root, timeout=5, capture=capture) + self.assertEqual(failure.exception.returncode, code) + self.assertEqual(failure.exception.cmd, command) + else: + result = runner.prepare(command, phase="fixture", cwd=root, timeout=5, capture=capture) + self.assertEqual(result, "stdout Σ\n" if capture else None) + self.assertEqual('stdout Σ' in output.getvalue(), not capture or code != 0) + self.assertEqual(errors.getvalue(), "stderr Σ\n") + self.assertNotIn('stderr', result if code == 0 and capture else '') + + def test_stalled_compilation_or_discovery_starts_no_checks_and_retains_output(self): + for phase in ("compile", "discover"): + with self.subTest(phase=phase), tempfile.TemporaryDirectory() as directory: + root = Path(directory) + (root / "scripts/installtests").mkdir(parents=True) + discovery = (f"#!{sys.executable}\nimport sys, time\n" + "print('discovery stdout Σ', flush=True)\n" + "print('discovery stderr Σ', file=sys.stderr, flush=True)\ntime.sleep(60)\n") + go = root / "go" + go.write_text(f"#!{sys.executable}\nimport pathlib, sys, time\n" + + ("print('compile stdout Σ', flush=True)\n" + "print('compile stderr Σ', file=sys.stderr, flush=True)\ntime.sleep(60)\n" + if phase == "compile" else + f"p=pathlib.Path(sys.argv[sys.argv.index('-o')+1]); p.write_text({discovery!r}); p.chmod(0o700)\n")) + go.chmod(0o700) + output, errors = io.StringIO(), io.StringIO() + actual_start = subprocess.Popen + started = [] + def start(*args, **kwargs): + process = actual_start(*args, **kwargs) + started.append(process) + return process + before = time.monotonic() + with patch.object(runner, "ROOT", root), \ + patch.object(runner, "COMPILE_TIMEOUT_SECONDS", .5), \ + patch.object(runner, "DISCOVERY_TIMEOUT_SECONDS", .5), \ + patch.dict(os.environ, {"PATH":str(root)+os.pathsep+os.environ["PATH"]}), \ + patch.object(runner.subprocess, "Popen", side_effect=start), \ + patch.object(runner, "run_checks") as checks, \ + contextlib.redirect_stdout(output), contextlib.redirect_stderr(errors): + with self.assertRaises(subprocess.TimeoutExpired) as failure: + runner.main("all") + checks.assert_not_called() + self.assertEqual(failure.exception.timeout, .5) + self.assertLess(time.monotonic() - before, 4) + self.assertEqual(len(started), 1 if phase == "compile" else 2) + self.assertTrue(all(p.poll() is not None for p in started)) + self.assertEqual(started[-1].returncode, -signal.SIGKILL) + label = "compile" if phase == "compile" else "discovery" + self.assertIn(label + " stdout Σ", output.getvalue()) + self.assertIn(label + " stderr Σ", errors.getvalue()) + self.assertNotIn("Native Unix installer all:", output.getvalue()) + + @unittest.skipUnless(os.name == "posix", "native Unix process-group control") + def test_inherited_output_does_not_hold_preparation_or_clear_descendant_failure(self): + for code in (0, 23): + with self.subTest(code=code), tempfile.TemporaryDirectory() as root: + child = ("import pathlib, sys, time; print('owned preparation child Σ', flush=True); " + "pathlib.Path('child-ready').touch(); time.sleep(60)") + parent = ("import pathlib, subprocess, sys, time; " + f"p=subprocess.Popen([sys.executable,'-I','-S','-c',{child!r}]); " + "pathlib.Path('child-pid').write_text(str(p.pid)); " + "deadline=time.monotonic()+4\n" + "while not pathlib.Path('child-ready').exists():\n" + " assert time.monotonic() 1) self.assertEqual(kwargs["cwd"], runner.ROOT if len(calls) == 1 else runner.ROOT / "scripts/installtests") if len(calls) == failure_at: raise subprocess.CalledProcessError(23, argv) - return SimpleNamespace(stdout=self.inventory) + return self.inventory def check(binary, name, **kwargs): checks.append(name) self.assertEqual(kwargs["cwd"], runner.ROOT / "scripts/installtests") @@ -129,7 +247,7 @@ class InventoryControls(unittest.TestCase): def selected(binary, names, **kwargs): for name in names: check(binary, name, **kwargs) - with patch.object(runner.subprocess, "run", side_effect=command), patch.object(runner, "run_checks", side_effect=selected), contextlib.redirect_stdout(io.StringIO()): + with patch.object(runner, "prepare", side_effect=command), patch.object(runner, "run_checks", side_effect=selected), contextlib.redirect_stdout(io.StringIO()): if failure_at: with self.assertRaises(subprocess.CalledProcessError): runner.main(phase) @@ -161,7 +279,7 @@ class InventoryControls(unittest.TestCase): self.assertEqual(attempted, expected[:max(0, failure - 2)]) def test_unknown_phase_starts_no_go_process(self): - with patch.object(runner.subprocess, "run") as run: + with patch.object(runner, "prepare") as run: with self.assertRaises(ValueError): runner.main("skip") run.assert_not_called() @@ -171,7 +289,7 @@ class InventoryControls(unittest.TestCase): self.assertIn("invalid choice", result.stderr) def test_all_finishes_preflight_before_admitting_remaining_checks(self): - with patch.object(runner.subprocess, "run", return_value=SimpleNamespace(stdout=self.inventory)), \ + with patch.object(runner, "prepare", return_value=self.inventory), \ patch.object(runner, "run_checks") as checks, contextlib.redirect_stdout(io.StringIO()): runner.main("all") self.assertEqual(checks.call_count, 2)