Files
nvdriver/tests/offline/test_gpu_graphics_binary_oracles.py
morebrackets e8dfdfc652 Replace elidable asserts in shared check_gpu_graphics_load_binary.py harness with require()
Replace all 31 assert statements in check_gpu_graphics_load_binary.py
(including side-effecting calls in memory_descriptor(), construct(), and main())
with _gpu_graphics_oracle_check.require() so python3 -O runs every required
setup call and invariant check across all five graphics binary oracles.
Add check_gpu_graphics_load_binary.py to the AST no-assert offline test.
2026-12-02 16:22:30 -08:00

103 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Offline tests for the 5 GPU graphics binary oracle scripts and --check helpers."""
from __future__ import annotations
import ast
import json
import pathlib
import sys
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tools"))
import _gpu_graphics_oracle_check as helper # noqa: E402
SCRIPTS = (
"tools/check_gpu_graphics_fecs_start_binary.py",
"tools/check_gpu_graphics_init_binary.py",
"tools/check_gpu_graphics_level_binary.py",
"tools/check_gpu_graphics_unload_binary.py",
"tools/check_gpu_graphics_zbc_binary.py",
)
class GpuGraphicsBinaryOraclesTests(unittest.TestCase):
def test_no_elidable_asserts_and_check_flag_wired(self) -> None:
for rel in ("tools/check_gpu_graphics_load_binary.py", *SCRIPTS):
src = (ROOT / rel).read_text()
tree = ast.parse(src, filename=rel)
asserts = [node.lineno for node in ast.walk(tree) if isinstance(node, ast.Assert)]
self.assertEqual(asserts, [], f"{rel} still contains assert at lines {asserts}")
for rel in SCRIPTS:
src = (ROOT / rel).read_text()
tree = ast.parse(src, filename=rel)
self.assertIn("--check", src, f"{rel} missing --check flag")
self.assertIn("write_or_check_text", src, f"{rel} missing write_or_check_text")
self.assertIn("write_or_check_json", src, f"{rel} missing write_or_check_json")
self.assertIn("finally:\n sys.argv = _saved_argv", src, f"{rel} must restore sys.argv")
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name == "main":
stores = [
n.lineno
for n in ast.walk(node)
if isinstance(n, ast.Name) and n.id == "check" and isinstance(n.ctx, ast.Store)
]
self.assertEqual(stores, [], f"{rel}: main() must not shadow 'check' at {stores}")
def test_require_enforced_under_optimize(self) -> None:
helper.require(True, "ok")
with self.assertRaises(AssertionError) as ctx:
helper.require(False, ("mismatch", 1, 2))
self.assertIn("mismatch", str(ctx.exception))
def test_write_or_check_text_readonly_and_drift(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
target = pathlib.Path(tmp) / "nested" / "oracle.txt"
with self.assertRaises(SystemExit):
helper.write_or_check_text(target, "row1\n", check=True)
self.assertFalse(target.parent.exists(), "--check must not create parent dirs")
helper.write_or_check_text(target, "row1\n", check=False)
self.assertEqual(target.read_text(), "row1\n")
helper.write_or_check_text(target, "row1\n", check=True)
with self.assertRaises(SystemExit):
helper.write_or_check_text(target, "row2\n", check=True)
self.assertEqual(target.read_text(), "row1\n", "--check must not overwrite file")
def test_write_or_check_json_type_preserving_and_original_basename(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
target = pathlib.Path(tmp) / "reports" / "out.json"
with self.assertRaises(SystemExit):
helper.write_or_check_json(target, {"passed": True}, check=True)
self.assertFalse(target.parent.exists())
helper.write_or_check_json(
target,
{"original": "/home/a/nv-kernel.o_binary", "flag": False, "count": 0},
check=False,
)
# Same basename for `original` matches when normalize_original_basename=True.
helper.write_or_check_json(
target,
{"original": "/usr/src/nvidia-580.178.04/nvidia/nv-kernel.o_binary", "flag": False, "count": 0},
check=True,
normalize_original_basename=True,
)
# Type drift (0 vs False) must be detected.
with self.assertRaises(SystemExit):
helper.write_or_check_json(
target,
{"original": "/usr/src/nvidia-580.178.04/nvidia/nv-kernel.o_binary", "flag": 0, "count": 0},
check=True,
normalize_original_basename=True,
)
# File remains untouched on drift.
self.assertIs(json.loads(target.read_text())["flag"], False)
if __name__ == "__main__":
unittest.main()