mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
- tools/probe_original_control_copies.py (4777567593): use a nested finally so os.close(fd) always runs while propagating any OSError from freeing the root handle instead of silently hiding cleanup failures. - tests/offline/test_original_probe_scripts.py (4777567602): add mocked two-descriptor second-open failure tests for all four two-fd probe scripts plus a python3 -O test exercising root alloc failure and root free cleanup.
144 lines
6.0 KiB
Python
144 lines
6.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline tests for the six probe_original_*.py control-node probe scripts."""
|
|
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
import struct
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
SCRIPTS = [
|
|
"tools/probe_original_clients.py",
|
|
"tools/probe_original_control_copies.py",
|
|
"tools/probe_original_null_control.py",
|
|
"tools/probe_original_software_controls.py",
|
|
"tools/probe_original_software_controls_followup.py",
|
|
"tools/probe_original_software_controls_replay.py",
|
|
]
|
|
|
|
|
|
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)
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
class OriginalProbeScriptsTest(unittest.TestCase):
|
|
def test_all_probe_scripts_import_cleanly_without_opening_devices(self):
|
|
with mock.patch("os.open", side_effect=AssertionError("must not open devices on import")):
|
|
for idx, rel in enumerate(SCRIPTS):
|
|
mod = _load(f"probe_mod_{idx}", rel)
|
|
self.assertTrue(callable(mod.main), rel)
|
|
|
|
def test_module_version_fallback_returns_unloaded_when_missing(self):
|
|
clients = _load("probe_clients_ver", "tools/probe_original_clients.py")
|
|
self.assertEqual(clients._read_module_version("nonexistent_nv_module_xyz"), "unloaded")
|
|
|
|
sw = _load("probe_sw_ver", "tools/probe_original_software_controls.py")
|
|
with mock.patch("pathlib.Path.read_text", side_effect=FileNotFoundError):
|
|
self.assertEqual(sw._read_module_version("/dev/nvidia-rsctl"), "unloaded")
|
|
|
|
def test_replay_rejects_unrecognized_device_node_and_finds_modinfo(self):
|
|
replay = _load("probe_replay", "tools/probe_original_software_controls_replay.py")
|
|
with mock.patch.dict(os.environ, {"NV_RS_NODE": "/dev/bogus"}):
|
|
with self.assertRaisesRegex(SystemExit, "Explicit original or isolated Rust node required"):
|
|
replay.main()
|
|
with mock.patch("shutil.which", return_value=None):
|
|
found = replay._find_modinfo()
|
|
self.assertTrue(found.endswith("modinfo"))
|
|
|
|
def test_two_descriptor_probes_close_first_fd_when_second_open_fails(self):
|
|
two_fd_probes = [
|
|
("tools/probe_original_clients.py", ["/dev/nvidiactl"]),
|
|
("tools/probe_original_software_controls.py", None),
|
|
("tools/probe_original_software_controls_followup.py", None),
|
|
("tools/probe_original_software_controls_replay.py", None),
|
|
]
|
|
for idx, (rel, argv) in enumerate(two_fd_probes):
|
|
with self.subTest(script=rel):
|
|
mod = _load(f"two_fd_probe_{idx}", rel)
|
|
closed = []
|
|
opens = []
|
|
|
|
def fake_open(path, flags):
|
|
opens.append(path)
|
|
if len(opens) == 1:
|
|
return 77
|
|
raise OSError("second open failed")
|
|
|
|
with (
|
|
mock.patch.dict(os.environ, {"NV_RS_NODE": "/dev/nvidia-rsctl"}),
|
|
mock.patch.object(Path, "read_text", return_value="580.178.04\n"),
|
|
mock.patch.object(Path, "read_bytes", return_value=b"fake-ko"),
|
|
mock.patch.object(mod.os, "open", side_effect=fake_open),
|
|
mock.patch.object(mod.os, "close", side_effect=lambda fd: closed.append(fd)),
|
|
):
|
|
with self.assertRaisesRegex(OSError, "second open failed"):
|
|
if argv is not None:
|
|
mod.main(argv)
|
|
else:
|
|
mod.main()
|
|
self.assertEqual(closed, [77], f"{rel} leaked first descriptor")
|
|
|
|
def test_control_copies_enforces_alloc_and_cleanup_errors_under_python_O(self):
|
|
script = f"""
|
|
import importlib.util, struct
|
|
from unittest import mock
|
|
spec = importlib.util.spec_from_file_location("pcc", {str(ROOT / "tools/probe_original_control_copies.py")!r})
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
|
|
# 1. Non-zero root alloc status must raise RuntimeError even under -O and still close fd.
|
|
closed = []
|
|
def alloc_fail_ioctl(fd, req, buf, mutate=True):
|
|
struct.pack_into("<I", buf, 40, 0x56)
|
|
return 0
|
|
with mock.patch.object(mod.os, "open", return_value=88), \\
|
|
mock.patch.object(mod.os, "close", side_effect=closed.append), \\
|
|
mock.patch.object(mod.fcntl, "ioctl", side_effect=alloc_fail_ioctl):
|
|
try:
|
|
mod.main()
|
|
except RuntimeError as e:
|
|
if "root allocation failed" not in str(e):
|
|
raise SystemExit(f"unexpected error: {{e}}")
|
|
else:
|
|
raise SystemExit("expected RuntimeError on failed root alloc under -O")
|
|
if closed != [88]:
|
|
raise SystemExit(f"expected fd 88 closed, got {{closed}}")
|
|
|
|
# 2. OSError during root free in finally must propagate AND still close fd.
|
|
closed2 = []
|
|
def free_fail_ioctl(fd, req, buf, mutate=True):
|
|
if req == 0xc030462b:
|
|
struct.pack_into("<I", buf, 8, 0xc1d00001)
|
|
struct.pack_into("<I", buf, 40, 0)
|
|
return 0
|
|
if req == 0xc0104629:
|
|
raise OSError("free ioctl failed")
|
|
return 0
|
|
with mock.patch.object(mod.os, "open", return_value=99), \\
|
|
mock.patch.object(mod.os, "close", side_effect=closed2.append), \\
|
|
mock.patch.object(mod.fcntl, "ioctl", side_effect=free_fail_ioctl):
|
|
try:
|
|
mod.main()
|
|
except OSError as e:
|
|
if "free ioctl failed" not in str(e):
|
|
raise SystemExit(f"unexpected cleanup error: {{e}}")
|
|
else:
|
|
raise SystemExit("expected OSError from root free cleanup")
|
|
if closed2 != [99]:
|
|
raise SystemExit(f"expected fd 99 closed, got {{closed2}}")
|
|
"""
|
|
proc = subprocess.run([sys.executable, "-O", "-c", script], capture_output=True, text=True)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|