mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
Edit the global CDI mount list as YAML and atomically publish a complete spec. Retain the generated content when parsing fails or an existing mount conflicts.
149 lines
5.5 KiB
Python
149 lines
5.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise the installed CDI helper with temporary specs."""
|
|
import importlib.util
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import yaml
|
|
|
|
|
|
HELPER = Path(__file__).resolve().parents[2] / "tools" / "patch_cdi.py"
|
|
MODULE = importlib.util.spec_from_file_location("patch_cdi", HELPER)
|
|
cdi = importlib.util.module_from_spec(MODULE)
|
|
MODULE.loader.exec_module(cdi)
|
|
CHAR_MOUNT = {
|
|
"hostPath": "/dev/char",
|
|
"containerPath": "/dev/char",
|
|
"options": ["ro", "nosuid", "rbind"],
|
|
}
|
|
DEVICE_FIRST = """\
|
|
cdiVersion: 0.5.0
|
|
kind: nvidia.com/gpu
|
|
devices:
|
|
- name: "0"
|
|
containerEdits:
|
|
deviceNodes:
|
|
- path: /dev/nvidia0
|
|
"""
|
|
|
|
|
|
class CdiPatchTests(unittest.TestCase):
|
|
def test_adds_root_mount_when_device_edits_precede_it(self):
|
|
document = DEVICE_FIRST + """\
|
|
containerEdits:
|
|
env:
|
|
- NVIDIA_VISIBLE_DEVICES=void
|
|
"""
|
|
before = yaml.safe_load(document)
|
|
after = yaml.safe_load(cdi.add_char_mount(document))
|
|
self.assertEqual(after["devices"], before["devices"])
|
|
self.assertEqual(after["containerEdits"]["env"], before["containerEdits"]["env"])
|
|
self.assertEqual(after["containerEdits"]["mounts"], [CHAR_MOUNT])
|
|
|
|
def test_creates_global_edits_without_using_device_mount(self):
|
|
document = DEVICE_FIRST + """\
|
|
mounts:
|
|
- hostPath: /dev/char
|
|
containerPath: /dev/char
|
|
options: [ro, rbind]
|
|
"""
|
|
before = yaml.safe_load(document)
|
|
after = yaml.safe_load(cdi.add_char_mount(document))
|
|
self.assertEqual(after["devices"], before["devices"])
|
|
self.assertEqual(after["containerEdits"]["mounts"], [CHAR_MOUNT])
|
|
|
|
def test_appends_after_existing_global_mount_and_ignores_prefix(self):
|
|
document = DEVICE_FIRST + """\
|
|
containerEdits:
|
|
mounts:
|
|
- hostPath: /dev/char/195:0
|
|
containerPath: /dev/char/195:0
|
|
options: [ro, bind]
|
|
"""
|
|
before = yaml.safe_load(document)
|
|
after = yaml.safe_load(cdi.add_char_mount(document))
|
|
self.assertEqual(
|
|
after["containerEdits"]["mounts"],
|
|
before["containerEdits"]["mounts"] + [CHAR_MOUNT],
|
|
)
|
|
|
|
def test_accepts_quoted_and_flow_style_without_rewriting(self):
|
|
document = DEVICE_FIRST + """\
|
|
containerEdits: {mounts: [{hostPath: '/dev/char', containerPath: "/dev/char", options: [ro, bind]}]}
|
|
"""
|
|
self.assertIsNone(cdi.add_char_mount(document))
|
|
|
|
def test_shared_edits_alias_does_not_gain_device_mounts(self):
|
|
document = DEVICE_FIRST.replace("containerEdits:", "containerEdits: &edits")
|
|
document += "containerEdits: *edits\n"
|
|
before = yaml.safe_load(document)
|
|
after = yaml.safe_load(cdi.add_char_mount(document))
|
|
self.assertEqual(after["devices"], before["devices"])
|
|
self.assertEqual(after["containerEdits"]["mounts"], [CHAR_MOUNT])
|
|
|
|
def test_unchanged_file_for_invalid_and_conflicting_input(self):
|
|
examples = (
|
|
"containerEdits: [malformed",
|
|
"containerEdits: {mounts: {} }\n",
|
|
"containerEdits: {}\ncontainerEdits: {}\n",
|
|
"containerEdits: {<<: {mounts: []}}\n",
|
|
"""\
|
|
containerEdits:
|
|
mounts:
|
|
- hostPath: /dev/char
|
|
containerPath: /dev/char
|
|
options: [rw, bind]
|
|
""",
|
|
)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
spec = Path(tmp) / "nvidia.yaml"
|
|
for example in examples:
|
|
with self.subTest(example=example):
|
|
original = (DEVICE_FIRST + example).encode("utf-8")
|
|
spec.write_bytes(original)
|
|
result = subprocess.run(
|
|
[sys.executable, str(HELPER), str(spec)],
|
|
capture_output=True, text=True,
|
|
)
|
|
self.assertEqual(result.returncode, 1)
|
|
self.assertIn("nvdriver-cdi-patch", result.stderr)
|
|
self.assertEqual(spec.read_bytes(), original)
|
|
|
|
def test_updates_atomically_and_second_run_does_not_replace(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
spec = Path(tmp) / "nvidia.yaml"
|
|
spec.write_text(DEVICE_FIRST, encoding="utf-8")
|
|
spec.chmod(0o640)
|
|
original = spec.stat()
|
|
with patch.object(cdi.os, "replace", wraps=cdi.os.replace) as replace:
|
|
cdi.patch_file(spec)
|
|
replace.assert_called_once()
|
|
updated = spec.stat()
|
|
content = spec.read_bytes()
|
|
cdi.patch_file(spec)
|
|
replace.assert_called_once()
|
|
self.assertEqual(spec.read_bytes(), content)
|
|
self.assertEqual(yaml.safe_load(content)["containerEdits"]["mounts"], [CHAR_MOUNT])
|
|
self.assertEqual(updated.st_mode, original.st_mode)
|
|
self.assertEqual((updated.st_uid, updated.st_gid), (original.st_uid, original.st_gid))
|
|
self.assertEqual(list(spec.parent.iterdir()), [spec])
|
|
|
|
def test_failed_replace_removes_temporary_file(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
spec = Path(tmp) / "nvidia.yaml"
|
|
original = DEVICE_FIRST.encode("utf-8")
|
|
spec.write_bytes(original)
|
|
with patch.object(cdi.os, "replace", side_effect=OSError("test failure")):
|
|
with self.assertRaises(OSError):
|
|
cdi.patch_file(spec)
|
|
self.assertEqual(spec.read_bytes(), original)
|
|
self.assertEqual(list(spec.parent.iterdir()), [spec])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|