mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
137 lines
5.8 KiB
Python
137 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline tests for dev_mode.sh, dev_health.sh, dev_reload.sh, and extract_gv100_firmware.py."""
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import os
|
|
from pathlib import Path
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
REPO = Path(__file__).resolve().parents[2]
|
|
STOCK_OBJ = Path("/usr/src/nvidia-580.178.04/nvidia/nv-kernel.o_binary")
|
|
|
|
|
|
class TestDevLoopScripts(unittest.TestCase):
|
|
def test_shell_syntax_valid(self):
|
|
for rel in ("tools/dev_mode.sh", "tools/dev_health.sh", "tools/dev_reload.sh"):
|
|
proc = subprocess.run(["sh", "-n", str(REPO / rel)], capture_output=True, text=True)
|
|
self.assertEqual(proc.returncode, 0, f"{rel} syntax error: {proc.stderr}")
|
|
|
|
def test_dev_mode_and_health_reject_bad_args(self):
|
|
for cmd in (
|
|
["sh", str(REPO / "tools/dev_mode.sh"), "bogus"],
|
|
["sh", str(REPO / "tools/dev_mode.sh"), "status", "extra"],
|
|
["sh", str(REPO / "tools/dev_health.sh"), "bogus"],
|
|
["sh", str(REPO / "tools/dev_health.sh"), "pre", "extra"],
|
|
):
|
|
proc = subprocess.run(cmd, capture_output=True, text=True)
|
|
self.assertEqual(proc.returncode, 2, f"expected exit 2 for {cmd}, got {proc.returncode}")
|
|
|
|
def test_dev_reload_rejects_malformed_and_missing_bdf(self):
|
|
for bad_bdf in ("", "0000:01:00.0/../../x", "ZZZZ:01:00.0", "0000:ff:1f.7"):
|
|
env = os.environ.copy()
|
|
env["NV_RS_DEV_RELOAD_BDF"] = bad_bdf
|
|
proc = subprocess.run(
|
|
["sh", str(REPO / "tools/dev_reload.sh")],
|
|
env=env,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(proc.returncode, 2, f"expected exit 2 for BDF={bad_bdf!r}")
|
|
|
|
|
|
class TestExtractGv100Firmware(unittest.TestCase):
|
|
def test_atomic_write_sets_readable_mode_and_preserves_existing_metadata(self):
|
|
spec = importlib.util.spec_from_file_location(
|
|
"extract_gv100_firmware", REPO / "tools/extract_gv100_firmware.py"
|
|
)
|
|
assert spec is not None and spec.loader is not None
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = Path(td) / "manifest.json"
|
|
mod._atomic_write_bytes(p, b"first\n")
|
|
self.assertEqual(p.read_bytes(), b"first\n")
|
|
self.assertEqual(stat.S_IMODE(p.stat().st_mode), 0o644)
|
|
|
|
p.chmod(0o640)
|
|
st_before = p.stat()
|
|
with mock.patch.object(mod.os, "chown", wraps=mod.os.chown) as chown_mock:
|
|
mod._atomic_write_bytes(p, b"second\n")
|
|
self.assertEqual(p.read_bytes(), b"second\n")
|
|
self.assertEqual(stat.S_IMODE(p.stat().st_mode), 0o640)
|
|
chown_mock.assert_called_once()
|
|
self.assertEqual(chown_mock.call_args.args[1:], (st_before.st_uid, st_before.st_gid))
|
|
|
|
def test_missing_rm_object_gives_clean_error(self):
|
|
proc = subprocess.run(
|
|
[sys.executable, str(REPO / "tools/extract_gv100_firmware.py"),
|
|
"--rm-object", "/nonexistent/nv-kernel.o_binary"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertNotEqual(proc.returncode, 0)
|
|
self.assertIn("kernel object not found", proc.stderr)
|
|
self.assertNotIn("Traceback", proc.stderr)
|
|
|
|
def test_legacy_family_check_mode_does_not_write_and_detects_staleness(self):
|
|
if not STOCK_OBJ.is_file():
|
|
self.skipTest("580.178.04 nv-kernel.o_binary not present on host")
|
|
with tempfile.TemporaryDirectory() as td:
|
|
out_gv = Path(td) / "gv100.fw"
|
|
out_gm = Path(td) / "gm20x.fw"
|
|
manifest = Path(td) / "gv100.json"
|
|
|
|
# --check on missing gm20x.fw must fail and NOT create gm20x.fw
|
|
proc = subprocess.run(
|
|
[sys.executable, str(REPO / "tools/extract_gv100_firmware.py"),
|
|
"--rm-object", str(STOCK_OBJ), "--family", "gm20x",
|
|
"--out", str(out_gv), "--manifest", str(manifest), "--check"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertNotEqual(proc.returncode, 0)
|
|
self.assertFalse(out_gm.exists(), "--check must never write gm20x.fw")
|
|
|
|
# Extract gm20x.fw, then --check must pass
|
|
proc = subprocess.run(
|
|
[sys.executable, str(REPO / "tools/extract_gv100_firmware.py"),
|
|
"--rm-object", str(STOCK_OBJ), "--family", "gm20x",
|
|
"--out", str(out_gv), "--manifest", str(manifest)],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
self.assertTrue(out_gm.is_file())
|
|
|
|
proc = subprocess.run(
|
|
[sys.executable, str(REPO / "tools/extract_gv100_firmware.py"),
|
|
"--rm-object", str(STOCK_OBJ), "--family", "gm20x",
|
|
"--out", str(out_gv), "--manifest", str(manifest), "--check"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(proc.returncode, 0, proc.stderr)
|
|
|
|
# Corrupt one byte -> --check must fail
|
|
data = bytearray(out_gm.read_bytes())
|
|
data[-1] ^= 0xff
|
|
out_gm.write_bytes(bytes(data))
|
|
proc = subprocess.run(
|
|
[sys.executable, str(REPO / "tools/extract_gv100_firmware.py"),
|
|
"--rm-object", str(STOCK_OBJ), "--family", "gm20x",
|
|
"--out", str(out_gv), "--manifest", str(manifest), "--check"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertNotEqual(proc.returncode, 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|