| 1 | """Exercise environment policy at the mount/perf/external-timer boundaries."""
|
| 2 | import json
|
| 3 | import os
|
| 4 | from pathlib import Path
|
| 5 | import subprocess
|
| 6 | import sys
|
| 7 | import tempfile
|
| 8 | import unittest
|
| 9 | from unittest.mock import patch
|
| 10 |
|
| 11 | sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
|
| 12 | import mount_probe
|
| 13 | import crosscheck_hyperfine
|
| 14 | from oakbench import perf_loop
|
| 15 | from oakbench.environment import ENV_ISOLATION_VERSION
|
| 16 |
|
| 17 |
|
| 18 | class MeasurementEnvironmentPathsTest(unittest.TestCase):
|
| 19 | def test_real_mount_subprocess_sanitizes_without_claiming_isolated_home(self):
|
| 20 | with tempfile.TemporaryDirectory() as tmp:
|
| 21 | poison = {"HOME": tmp, "GIT_DIR": "/wrong/repo", "GIT_CONFIG_COUNT": "1",
|
| 22 | "OAK_NO_UPDATE_CHECK": "0", "OAK_BENCH_ENV_ISOLATION_VERSION": ENV_ISOLATION_VERSION}
|
| 23 | with patch.dict(os.environ, poison):
|
| 24 | result = mount_probe.run_process([sys.executable, "-c",
|
| 25 | "import os,json;print(json.dumps({k:os.environ.get(k) for k in "
|
| 26 | "['HOME','GIT_DIR','GIT_CONFIG_COUNT','OAK_NO_UPDATE_CHECK','OAK_BENCH_ENV_ISOLATION_VERSION']}))"],
|
| 27 | Path(tmp), 10, 0)
|
| 28 | meta = mount_probe.metadata("test", Path(tmp) / "spec", {}, None)
|
| 29 | observed = json.loads(result["stdout"])
|
| 30 | self.assertEqual(result["returncode"], 0)
|
| 31 | self.assertEqual(observed["HOME"], tmp)
|
| 32 | self.assertIsNone(observed["GIT_DIR"])
|
| 33 | self.assertIsNone(observed["GIT_CONFIG_COUNT"])
|
| 34 | self.assertEqual(observed["OAK_NO_UPDATE_CHECK"], "1")
|
| 35 | self.assertNotEqual(meta["env_isolation_version"], ENV_ISOLATION_VERSION)
|
| 36 | self.assertEqual(meta["env_isolation_version"], observed["OAK_BENCH_ENV_ISOLATION_VERSION"])
|
| 37 |
|
| 38 | def test_perf_loop_applies_policy_after_explicit_environment_merge(self):
|
| 39 | observed = []
|
| 40 | def run(command, **kwargs):
|
| 41 | observed.append(kwargs["env"])
|
| 42 | return subprocess.CompletedProcess(command, 0, stderr="")
|
| 43 | with patch("oakbench.perf_loop.subprocess.run", side_effect=run):
|
| 44 | samples = perf_loop.run_interleaved_commands(["oak", "status"], ["oak", "status"],
|
| 45 | cwd=Path.cwd(), pairs=20, env={"GIT_DIR": "/wrong/repo", "OAK_NO_UPDATE_CHECK": "0"})
|
| 46 | self.assertEqual(len(samples), 40)
|
| 47 | for env in observed:
|
| 48 | self.assertFalse("GIT_DIR" in env, "GIT_DIR leaked into measurement environment")
|
| 49 | self.assertEqual(env["OAK_NO_UPDATE_CHECK"], "1")
|
| 50 | self.assertNotEqual(env["OAK_BENCH_ENV_ISOLATION_VERSION"], ENV_ISOLATION_VERSION)
|
| 51 |
|
| 52 | def test_external_timer_direct_api_enforces_policy(self):
|
| 53 | def runner(command, **kwargs):
|
| 54 | env = kwargs["env"]
|
| 55 | self.assertFalse("GIT_DIR" in env, "GIT_DIR leaked into measurement environment")
|
| 56 | self.assertEqual(env["OAK_NO_UPDATE_CHECK"], "1")
|
| 57 | self.assertNotEqual(env["OAK_BENCH_ENV_ISOLATION_VERSION"], ENV_ISOLATION_VERSION)
|
| 58 | Path(command[command.index("--export-json") + 1]).write_text(json.dumps({"results": [{"median": .001}]}))
|
| 59 | return subprocess.CompletedProcess(command, 0, stdout="", stderr="")
|
| 60 | with patch("crosscheck_hyperfine.hyperfine_path", return_value="hyperfine"):
|
| 61 | crosscheck_hyperfine.run_hyperfine(["oak", "status"], cwd=Path.cwd(), runs=1, warmup=0,
|
| 62 | env={"GIT_DIR": "/wrong/repo", "OAK_NO_UPDATE_CHECK": "0"}, runner=runner)
|