From c9fc0206bc362af6682ab774b7b6e429a6afecf3 Mon Sep 17 00:00:00 2001 From: "pulse-triage[bot]" <249995291+pulse-triage[bot]@users.noreply.github.com> Date: Sun, 1 Nov 2026 05:24:15 +0000 Subject: [PATCH] Isolate stdlib diagnostic fixture reader startup Use the test suite's Python interpreter with isolated and no-site startup for synthetic performance, memory and log readers, including owned log-buffer cleanup. Avoid repeated ambient site setup and keep fixture results independent of PATH and Python environment settings. Preserve every existing test method, actual diagnostic recipe, real timeout, byte limit, reader-termination oracle and failure assertion. Add registered controls exercising the copied recipes under poisoned startup settings and checking actual child interpreter flags. Contract-Neutral: Verification fixture startup only; shipped diagnostics, runtime behaviour, safety bounds and subsystem contracts are unchanged. Change-source: pulse-maintainer --- .../tests/test_memory_troubleshooting_docs.py | 26 +++++++++++++++-- .../test_performance_troubleshooting_docs.py | 29 +++++++++++++++++-- scripts/tests/test_troubleshooting_logs.py | 28 ++++++++++++++++-- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/scripts/tests/test_memory_troubleshooting_docs.py b/scripts/tests/test_memory_troubleshooting_docs.py index d571258d6b..535226fcc1 100644 --- a/scripts/tests/test_memory_troubleshooting_docs.py +++ b/scripts/tests/test_memory_troubleshooting_docs.py @@ -44,8 +44,9 @@ def recipe() -> str: return blocks[0] -READER = '''#!/usr/bin/env python3 -import json, os, pathlib, signal, subprocess, sys, time +# These command doubles use only stdlib; keep fixture startup independent of +# PATH, Python environment and site packages without changing copied recipes. +READER = f'#!{sys.executable} -IS\n' + '''import json, os, pathlib, signal, subprocess, sys, time name = pathlib.Path(sys.argv[0]).name args = sys.argv[1:] root = pathlib.Path(os.environ['RECIPE_FIXTURE']) @@ -171,6 +172,27 @@ class MemoryTroubleshootingDocsTest(unittest.TestCase): result = subprocess.run(["bash", "-n"], input=recipe(), text=True, capture_output=True) self.assertEqual(result.returncode, 0, result.stderr) + def test_fixture_readers_use_selected_isolated_no_site_python(self): + # Exercise the actual reader body and copied recipe with hostile startup + # inputs. Flag assertions run inside the reader, not in a text scan. + checked_reader = READER.replace( + "\n", "\nimport sys\n" + f"assert sys.executable == {sys.executable!r}\n" + "assert sys.flags.isolated and sys.flags.no_site\n", 1) + with tempfile.TemporaryDirectory() as temporary: + startup = Path(temporary) + (startup / "python3").write_text("#!/bin/sh\nexit 29\n", encoding="utf-8") + (startup / "python3").chmod(0o700) + (startup / "sitecustomize.py").write_text("raise SystemExit(29)\n", encoding="utf-8") + environment = dict(PATH=f"{startup}{os.pathsep}{os.environ['PATH']}", + PYTHONPATH=str(startup), PYTHONHOME=str(startup / "missing-home")) + with patch(f"{__name__}.READER", checked_reader), \ + patch.dict(os.environ, environment): + result, calls = self.exercise() + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(len(calls), 6) + self.assertIn(COUNTERS, result.stdout) + def test_only_five_counters_and_stable_process_identity_leave_the_reader(self): result, calls = self.exercise(status="Name: private-workload\nUid: 1001 1001\n" + COUNTERS + "VmSize: 1234567 kB\nThreads: 19\n") diff --git a/scripts/tests/test_performance_troubleshooting_docs.py b/scripts/tests/test_performance_troubleshooting_docs.py index 3d0ad9a430..a9572b252f 100644 --- a/scripts/tests/test_performance_troubleshooting_docs.py +++ b/scripts/tests/test_performance_troubleshooting_docs.py @@ -16,6 +16,7 @@ import re import shutil import signal import subprocess +import sys import tempfile import threading import time @@ -47,8 +48,9 @@ def recipes() -> dict[str, str]: return {"docker" if "# Docker:" in block else "systemd": block for block in blocks} -READER = '''#!/usr/bin/env python3 -import json, os, pathlib, signal, subprocess, sys, time +# These command doubles use only stdlib; keep fixture startup independent of +# PATH, Python environment and site packages without changing copied recipes. +READER = f'#!{sys.executable} -IS\n' + '''import json, os, pathlib, signal, subprocess, sys, time name = pathlib.Path(sys.argv[0]).name args = sys.argv[1:] root = pathlib.Path(os.environ['RECIPE_FIXTURE']) @@ -279,6 +281,29 @@ class PerformanceTroubleshootingDocsTest(unittest.TestCase): self.assertEqual(result.returncode, 0, result.stderr) self.assertIn("timeout --signal=KILL 80s bash", copied) + def test_fixture_readers_use_selected_isolated_no_site_python(self): + # Exercise the actual reader body and copied recipe with hostile startup + # inputs. Flag assertions run inside the reader, not in a text scan. + checked_reader = READER.replace( + "\n", "\nimport sys\n" + f"assert sys.executable == {sys.executable!r}\n" + "assert sys.flags.isolated and sys.flags.no_site\n", 1) + with tempfile.TemporaryDirectory() as temporary: + startup = Path(temporary) + (startup / "python3").write_text("#!/bin/sh\nexit 29\n", encoding="utf-8") + (startup / "python3").chmod(0o700) + (startup / "sitecustomize.py").write_text("raise SystemExit(29)\n", encoding="utf-8") + environment = dict(PATH=f"{startup}{os.pathsep}{os.environ['PATH']}", + PYTHONPATH=str(startup), PYTHONHOME=str(startup / "missing-home")) + with mock.patch(f"{__name__}.READER", checked_reader), \ + mock.patch.dict(os.environ, environment): + for deployment in ("systemd", "docker"): + with self.subTest(deployment=deployment): + result, calls = self.exercise(deployment) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertTrue(calls) + self.assertIn("2026-10-03T00:00:00Z", result.stdout) + def test_systemd_samples_only_valid_write_counters_and_stable_identity(self): result, calls = self.exercise("systemd") self.assertEqual(result.returncode, 0, result.stderr) diff --git a/scripts/tests/test_troubleshooting_logs.py b/scripts/tests/test_troubleshooting_logs.py index 81e5f643f3..d8a3d5bf31 100644 --- a/scripts/tests/test_troubleshooting_logs.py +++ b/scripts/tests/test_troubleshooting_logs.py @@ -45,7 +45,8 @@ def recipes(heading: str = "### Correlate Logs with Requests") -> dict[str, str] for block in blocks} -READER = f'''#!{sys.executable} +# Stdlib-only doubles use the suite interpreter, not ambient startup code. +READER = f'''#!{sys.executable} -IS import json, os, pathlib, signal, sys, time pathlib.Path(os.environ['READER_ARGV']).write_text(json.dumps(sys.argv[1:])) pathlib.Path(os.environ['READER_PID']).write_text(json.dumps({{ @@ -140,7 +141,7 @@ def exercise_log_recipe(reader: str, copied: str, *, stdout: str = "", stderr: s log_directory.mkdir(mode=0o700) cleanup_file = directory / "cleanup.jsonl" real_rm = shutil.which("rm") - (directory / "rm").write_text(f'''#!{sys.executable} + (directory / "rm").write_text(f'''#!{sys.executable} -IS import json, os, pathlib, stat, sys path = pathlib.Path(sys.argv[2]) assert sys.argv[1] == '--' and len(sys.argv) == 3 @@ -272,6 +273,29 @@ class TroubleshootingLogRecipesTest(unittest.TestCase): (ROOT / "frontend-modern/public/docs/TROUBLESHOOTING.md").read_bytes()) self.assertEqual(set(recipes()), {"journalctl", "docker"}) + def test_fixture_readers_use_selected_isolated_no_site_python(self): + # Exercise the actual reader body and copied recipe with hostile startup + # inputs. Flag assertions run inside the reader, not in a text scan. + checked_reader = READER.replace( + "\n", "\nimport sys\n" + f"assert sys.executable == {sys.executable!r}\n" + "assert sys.flags.isolated and sys.flags.no_site\n", 1) + with tempfile.TemporaryDirectory() as temporary: + startup = Path(temporary) + (startup / "python3").write_text("#!/bin/sh\nexit 29\n", encoding="utf-8") + (startup / "python3").chmod(0o700) + (startup / "sitecustomize.py").write_text("raise SystemExit(29)\n", encoding="utf-8") + environment = dict(PATH=f"{startup}{os.pathsep}{os.environ['PATH']}", + PYTHONPATH=str(startup), PYTHONHOME=str(startup / "missing-home")) + with patch(f"{__name__}.READER", checked_reader), \ + patch.dict(os.environ, environment): + for reader in recipes(): + with self.subTest(reader=reader): + result = self.exercise(reader, stdout=JSON_LOG) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, JSON_LOG) + self.assertEqual(len(result.log_cleanup), 1) + def test_literal_id_matches_json_and_console_without_regex_false_matches(self): for reader in recipes(): for log in (JSON_LOG, CONSOLE_LOG):