From 0888eeb5221976e1cc07aef41ee78e94bce46dec Mon Sep 17 00:00:00 2001 From: morebrackets <19673572+morebrackets@users.noreply.github.com> Date: Wed, 2 Dec 2026 17:24:29 -0800 Subject: [PATCH] Address Greptile findings on #314 - tools/_lsf_oracle_elf.py (4777595945): match '/analysis/lsf-580/' first and use rfind() in _strip_checkout_root() so checkouts nested under an 'analysis' parent directory (e.g. /srv/analysis/nvdriver/analysis/lsf-580/...) normalize identically across checkouts. - tests/offline/test_lsf_oracles.py (4777595953): test nested-analysis path normalization, retain_bytes matching/drift/missing/write paths, verify retain_json never overwrites on match or drift, and add a mocked main(['--check']) test verifying --check reaches both binary and report checks. --- tests/offline/test_lsf_oracles.py | 72 +++++++++++++++++++++++++++++-- tools/_lsf_oracle_elf.py | 4 +- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/tests/offline/test_lsf_oracles.py b/tests/offline/test_lsf_oracles.py index 0ff7b4d2..c3c2c952 100644 --- a/tests/offline/test_lsf_oracles.py +++ b/tests/offline/test_lsf_oracles.py @@ -5,6 +5,7 @@ import sys import tempfile import unittest from pathlib import Path +from unittest import mock ROOT = Path(__file__).resolve().parents[2] SCRIPTS = [ @@ -17,8 +18,9 @@ SCRIPTS = [ def _load(name: str, rel: str): spec = importlib.util.spec_from_file_location(name, ROOT / rel) + if spec is None or spec.loader is None: + raise RuntimeError(f"cannot load module spec for {rel}") mod = importlib.util.module_from_spec(spec) - assert spec.loader is not None spec.loader.exec_module(mod) return mod @@ -51,13 +53,14 @@ class TestLsfOracles(unittest.TestCase): "records": [ { "engine": "FECS", - "image_file": "/home/geodd/nvidia-driver-rs/analysis/lsf-580/fecs-original-image.bin", - "layout_file": "/home/geodd/nvidia-driver-rs/analysis/lsf-580/fecs-original-layout.bin", + "image_file": "/srv/analysis/nvdriver/analysis/lsf-580/fecs-original-image.bin", + "layout_file": "/srv/analysis/nvdriver/analysis/lsf-580/fecs-original-layout.bin", "image_sha256": "abc", } ] } - p.write_text(json.dumps(existing) + "\n") + orig_text = json.dumps(existing) + "\n" + p.write_text(orig_text) relocated = { "records": [ { @@ -69,6 +72,7 @@ class TestLsfOracles(unittest.TestCase): ] } helper.retain_json(p, relocated, check=True) + self.assertEqual(p.read_text(), orig_text) drifted = { "records": [ { @@ -81,6 +85,23 @@ class TestLsfOracles(unittest.TestCase): } with self.assertRaises(SystemExit): helper.retain_json(p, drifted, check=True) + self.assertEqual(p.read_text(), orig_text) + missing_json = Path(td) / "missing.json" + with self.assertRaises(SystemExit): + helper.retain_json(missing_json, relocated, check=True) + self.assertFalse(missing_json.exists()) + + # retain_bytes: matching, drifted, missing, and write modes. + bpath = Path(td) / "fecs.bin" + with self.assertRaises(SystemExit): + helper.retain_bytes(bpath, b"\x01\x02", check=True) + self.assertFalse(bpath.exists()) + helper.retain_bytes(bpath, b"\x01\x02", check=False) + self.assertEqual(bpath.read_bytes(), b"\x01\x02") + helper.retain_bytes(bpath, b"\x01\x02", check=True) + with self.assertRaises(SystemExit): + helper.retain_bytes(bpath, b"\x03\x04", check=True) + self.assertEqual(bpath.read_bytes(), b"\x01\x02") # Changing the repository-relative directory must be rejected even with matching basename. moved_dir = { "records": [ @@ -101,6 +122,49 @@ class TestLsfOracles(unittest.TestCase): with self.assertRaises(SystemExit): helper.retain_json(p2, {"physical_gpu_access": 0}, check=True) + def test_whole_image_main_forwards_check_flag_to_binary_and_report_checks(self): + mod = _load("gpu_lsf_whole_image_check", "tools/gpu_lsf_whole_image_oracle.py") + helper = _load("_lsf_oracle_elf_check", "tools/_lsf_oracle_elf.py") + with tempfile.TemporaryDirectory() as td: + fake_root = Path(td) + (fake_root / "analysis/gr-580").mkdir(parents=True) + (fake_root / "analysis/gr-580/gv100-netlist.bin").write_bytes(b"netlist") + (fake_root / "analysis/lsf-580").mkdir(parents=True) + (fake_root / "reports").mkdir(parents=True) + seen_checks: list[bool] = [] + + class FakeHarness: + def __init__(self, _loaded): + pass + + def run_engine_image(self, engine, _s, _i, _b, _c, _d, _n, out_dir, check): + seen_checks.append(check) + img = out_dir / f"{engine.lower()}-original-image.bin" + helper.retain_bytes(img, b"img", check) + return {"engine": engine, "image_file": str(img)} + + with ( + mock.patch.object(mod, "ROOT", fake_root), + mock.patch.object(mod, "discover_rm_object", return_value=fake_root / "rm.o"), + mock.patch.object(mod, "load_relocated_rm_object", return_value={"digest": "deadbeef"}), + mock.patch.object(mod, "LsfUnicornHarness", FakeHarness), + ): + # Missing binary under --check must fail without creating files. + with self.assertRaises(SystemExit): + mod.main(["--check"]) + self.assertEqual(seen_checks, [True]) + self.assertEqual(list((fake_root / "analysis/lsf-580").iterdir()), []) + # Generate once, then verify --check passes and does not overwrite. + seen_checks.clear() + self.assertEqual(mod.main([]), 0) + self.assertTrue(all(c is False for c in seen_checks)) + report_path = fake_root / "reports/gpu-lsf-whole-image-oracle.json" + orig_report = report_path.read_text() + seen_checks.clear() + self.assertEqual(mod.main(["--check"]), 0) + self.assertTrue(all(c is True for c in seen_checks)) + self.assertEqual(report_path.read_text(), orig_report) + def test_stdlib_relocator_works_live_against_580_rm_object(self): helper = _load("_lsf_oracle_elf_live", "tools/_lsf_oracle_elf.py") rm_obj = helper.discover_rm_object() diff --git a/tools/_lsf_oracle_elf.py b/tools/_lsf_oracle_elf.py index 58f6077b..51e45fcd 100644 --- a/tools/_lsf_oracle_elf.py +++ b/tools/_lsf_oracle_elf.py @@ -81,8 +81,8 @@ def _strip_checkout_root(path_str: str) -> str: root_prefix = str(ROOT).replace("\\", "/").rstrip("/") + "/" if norm.startswith(root_prefix): return norm[len(root_prefix) :] - for anchor in ("/analysis/", "/reports/", "/src/", "/tests/"): - idx = norm.find(anchor) + for anchor in ("/analysis/lsf-580/", "/analysis/", "/reports/", "/src/", "/tests/"): + idx = norm.rfind(anchor) if idx >= 0: return norm[idx + 1 :] return norm