|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +"""Exercise AITER cache seeding without importing AITER or accessing a GPU.""" |
| 4 | + |
| 5 | +import importlib.util |
| 6 | +import json |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | +from pathlib import Path |
| 10 | +from types import SimpleNamespace |
| 11 | +from unittest.mock import patch |
| 12 | + |
| 13 | + |
| 14 | +class AiterCacheTests(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + temporary = tempfile.TemporaryDirectory() |
| 17 | + self.addCleanup(temporary.cleanup) |
| 18 | + self.root = Path(temporary.name).resolve() |
| 19 | + self.source = self.root / "base/aiter/jit" |
| 20 | + self.source.mkdir(parents=True) |
| 21 | + self.module = self.source / "module.so" |
| 22 | + self.module.write_bytes(b"pinned prebuilt") |
| 23 | + self.destination = self.root / "job/cache" |
| 24 | + path = Path(__file__).resolve().parents[1] / "rocm/prepare_aiter_cache.py" |
| 25 | + spec = importlib.util.spec_from_file_location("barite_aiter_test", path) |
| 26 | + self.helper = importlib.util.module_from_spec(spec) |
| 27 | + spec.loader.exec_module(self.helper) |
| 28 | + |
| 29 | + def test_seed_is_idempotent_and_rebuild_does_not_modify_source(self): |
| 30 | + records = self.helper.seed_prebuilt_modules(self.source, self.destination) |
| 31 | + self.assertEqual( |
| 32 | + records, self.helper.seed_prebuilt_modules(self.source, self.destination) |
| 33 | + ) |
| 34 | + link = self.destination / "module.so" |
| 35 | + self.assertTrue(link.is_symlink()) |
| 36 | + self.assertEqual(link.resolve(), self.module) |
| 37 | + self.assertEqual( |
| 38 | + records, |
| 39 | + [ |
| 40 | + { |
| 41 | + "name": "module.so", |
| 42 | + "target": str(self.module), |
| 43 | + "size": len(b"pinned prebuilt"), |
| 44 | + } |
| 45 | + ], |
| 46 | + ) |
| 47 | + # AITER 0.1.19 removes its output before installing a rebuilt extension. |
| 48 | + link.unlink() |
| 49 | + link.write_bytes(b"rebuilt for this job") |
| 50 | + self.assertEqual(self.module.read_bytes(), b"pinned prebuilt") |
| 51 | + self.assertEqual(link.read_bytes(), b"rebuilt for this job") |
| 52 | + with self.assertRaisesRegex(ValueError, "without source provenance"): |
| 53 | + self.helper.seed_prebuilt_modules(self.source, self.destination) |
| 54 | + self.assertEqual(link.read_bytes(), b"rebuilt for this job") |
| 55 | + |
| 56 | + def test_escaping_source_symlink_is_rejected(self): |
| 57 | + outside = self.root / "outside.so" |
| 58 | + outside.write_bytes(b"untrusted") |
| 59 | + (self.source / "escape.so").symlink_to(outside) |
| 60 | + with self.assertRaisesRegex(ValueError, "escapes installed"): |
| 61 | + self.helper.seed_prebuilt_modules(self.source, self.destination) |
| 62 | + self.assertFalse((self.destination / "escape.so").exists()) |
| 63 | + |
| 64 | + def test_conflicting_destination_and_empty_source_are_rejected(self): |
| 65 | + for destination in (self.source, self.root / "alias"): |
| 66 | + if destination != self.source: |
| 67 | + destination.symlink_to(self.root / "base", target_is_directory=True) |
| 68 | + with ( |
| 69 | + self.subTest(destination=destination), |
| 70 | + self.assertRaisesRegex(ValueError, "separate writable"), |
| 71 | + ): |
| 72 | + self.helper.seed_prebuilt_modules(self.source, destination) |
| 73 | + self.destination.mkdir(parents=True) |
| 74 | + wrong = self.root / "wrong.so" |
| 75 | + wrong.write_bytes(b"wrong") |
| 76 | + (self.destination / "module.so").symlink_to(wrong) |
| 77 | + with self.assertRaisesRegex(ValueError, "Unexpected AITER cache symlink"): |
| 78 | + self.helper.seed_prebuilt_modules(self.source, self.destination) |
| 79 | + self.module.unlink() |
| 80 | + with self.assertRaisesRegex(ValueError, "no prebuilt"): |
| 81 | + self.helper.seed_prebuilt_modules(self.source, self.destination) |
| 82 | + |
| 83 | + def test_metadata_gate_and_evidence(self): |
| 84 | + output = self.root / "evidence.json" |
| 85 | + distribution = SimpleNamespace( |
| 86 | + version="0.1.19", locate_file=lambda _: self.source |
| 87 | + ) |
| 88 | + with ( |
| 89 | + patch.object( |
| 90 | + self.helper.sys, |
| 91 | + "argv", |
| 92 | + [ |
| 93 | + "prepare", |
| 94 | + "--directory", |
| 95 | + str(self.destination), |
| 96 | + "--output", |
| 97 | + str(output), |
| 98 | + ], |
| 99 | + ), |
| 100 | + patch.object(self.helper.sys, "base_prefix", str(self.root / "base")), |
| 101 | + patch.object( |
| 102 | + self.helper.metadata, "distribution", return_value=distribution |
| 103 | + ), |
| 104 | + patch("builtins.print"), |
| 105 | + ): |
| 106 | + self.helper.main() |
| 107 | + evidence = json.loads(output.read_text()) |
| 108 | + self.assertEqual(evidence["source"], str(self.source)) |
| 109 | + self.assertEqual(evidence["version"], "0.1.19") |
| 110 | + self.assertEqual(len(evidence["modules"]), 1) |
| 111 | + distribution.version = "0.1.20" |
| 112 | + with self.assertRaisesRegex(ValueError, "Expected amd-aiter"): |
| 113 | + self.helper.main() |
| 114 | + distribution.version = "0.1.19" |
| 115 | + with ( |
| 116 | + patch.object(self.helper.sys, "base_prefix", str(self.root / "other")), |
| 117 | + self.assertRaisesRegex(ValueError, "installed base interpreter"), |
| 118 | + ): |
| 119 | + self.helper.main() |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + unittest.main() |
0 commit comments