66 lines
2.9 KiB
Python
66 lines
2.9 KiB
Python
import hashlib
|
|
import importlib.util
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
|
|
MODULE_PATH = Path(__file__).parents[2] / "autoinstall/offline/prepare-python-profile-operation.py"
|
|
SPEC = importlib.util.spec_from_file_location("prepare_python_profile_operation", MODULE_PATH)
|
|
MODULE = importlib.util.module_from_spec(SPEC)
|
|
assert SPEC.loader
|
|
SPEC.loader.exec_module(MODULE)
|
|
|
|
|
|
class PreparePythonProfileOperationTests(unittest.TestCase):
|
|
def test_prepares_and_resumes_without_mutating_target(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
target = root / "venv"
|
|
target.mkdir()
|
|
(target / "old").write_text("old")
|
|
wheels = root / "wheels"
|
|
wheels.mkdir()
|
|
wheel = wheels / "torch-cpu.whl"
|
|
wheel.write_bytes(b"cpu")
|
|
contract = {"profiles": {"cpu-only": {
|
|
"required": {wheel.name: hashlib.sha256(b"cpu").hexdigest()},
|
|
"forbidden_filename_patterns": ["cu12"],
|
|
}}}
|
|
receipt = {
|
|
"schema": "com.cezen.nexus-one.profile-membership-verification-receipt.v1",
|
|
"environment": "test", "result": "verified", "manifest_sha256": "a" * 64,
|
|
"namespace": "offline-workstation/python-runtime-profiles", "profiles": ["cpu-only"],
|
|
}
|
|
args = ({"architecture": "amd64"}, receipt, "auto", "op-1", root / "journal.json",
|
|
target, root / "checkpoint", contract, wheels, True)
|
|
first = MODULE.prepare(*args)
|
|
second = MODULE.prepare(*args)
|
|
self.assertEqual(first["checkpoint_id"], second["checkpoint_id"])
|
|
self.assertEqual((target / "old").read_text(), "old")
|
|
self.assertFalse(first["mutation_performed"])
|
|
|
|
def test_invalid_payload_creates_no_journal_or_checkpoint(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
wheels = root / "wheels"
|
|
wheels.mkdir()
|
|
receipt = {
|
|
"schema": "com.cezen.nexus-one.profile-membership-verification-receipt.v1",
|
|
"environment": "test", "result": "verified", "manifest_sha256": "a" * 64,
|
|
"namespace": "offline-workstation/python-runtime-profiles", "profiles": ["cpu-only"],
|
|
}
|
|
journal = root / "journal.json"
|
|
checkpoint = root / "checkpoint"
|
|
with self.assertRaisesRegex(ValueError, "PROFILE_PAYLOAD_INVALID"):
|
|
MODULE.prepare({"architecture": "amd64"}, receipt, "auto", "op-1", journal,
|
|
root / "target", checkpoint,
|
|
{"profiles": {"cpu-only": {"required": {"missing.whl": "0" * 64}}}},
|
|
wheels, True)
|
|
self.assertFalse(journal.exists())
|
|
self.assertFalse(checkpoint.exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|