50 lines
2.6 KiB
Python
50 lines
2.6 KiB
Python
from pathlib import Path
|
|||
|
|
import tempfile
|
||
|
|
import unittest
|
||
|
|
from unittest.mock import patch
|
||
|
|
|
||
|
|
import macos_runtime
|
||
|
|
|
||
|
|
|
||
|
|
class MacRuntimeTests(unittest.TestCase):
|
||
|
|
def inspect_fixture(self, dependency=None, sections=1, arches="arm64"):
|
||
|
|
with tempfile.TemporaryDirectory(prefix="mac metadata ") as directory:
|
||
|
|
worker = Path(directory) / "searchi-gemma-worker"
|
||
|
|
worker.write_bytes(b"\xcf\xfa\xed\xfe" + b"metadata fixture, not executable")
|
||
|
|
libraries = f"{worker}:\n\t/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 100.0.0)\n"
|
||
|
|
libraries += "\t" + (dependency or "/System/Library/Frameworks/Metal.framework/Versions/A/Metal") + " (compatibility version 1.0.0, current version 100.0.0)\n"
|
||
|
|
loads = "sectname __ggml_metallib\nsegname __DATA\naddr 0x1000\nsize 0x100\n" * sections
|
||
|
|
with patch.object(macos_runtime.subprocess, "check_output", side_effect=[arches, libraries, loads]):
|
||
|
|
return macos_runtime.inspect(worker)
|
||
|
|
|
||
|
|
def test_static_system_metal_inventory_has_no_packaged_dylibs(self):
|
||
|
|
result = self.inspect_fixture()
|
||
|
|
self.assertEqual(result["architectures"], ["arm64"])
|
||
|
|
self.assertEqual(result["libraries"], [])
|
||
|
|
self.assertTrue(result["metal_embedded"])
|
||
|
|
|
||
|
|
def test_external_library_and_missing_metal_are_rejected(self):
|
||
|
|
for dependency in ("/private/build path/libggml.dylib", "/usr/lib/libc++.1.dylib"):
|
||
|
|
with self.subTest(dependency=dependency), self.assertRaises(RuntimeError):
|
||
|
|
self.inspect_fixture(dependency=dependency)
|
||
|
|
|
||
|
|
def test_all_architectures_require_embedded_sources(self):
|
||
|
|
with self.assertRaisesRegex(RuntimeError, "Each Mach-O architecture"):
|
||
|
|
self.inspect_fixture(sections=0)
|
||
|
|
with self.assertRaisesRegex(RuntimeError, "Each Mach-O architecture"):
|
||
|
|
self.inspect_fixture(sections=1, arches="arm64 x86_64")
|
||
|
|
self.assertEqual(self.inspect_fixture(sections=2, arches="arm64 x86_64")["architectures"], ["arm64", "x86_64"])
|
||
|
|
|
||
|
|
def test_malformed_inventory_and_foreign_artifact_fail(self):
|
||
|
|
with tempfile.TemporaryDirectory() as directory:
|
||
|
|
worker = Path(directory) / "worker"
|
||
|
|
worker.write_bytes(b"\xcf\xfa\xed\xfe")
|
||
|
|
with self.assertRaises(RuntimeError):
|
||
|
|
macos_runtime.validate_inventory({"architectures": [["arm64"]]}, worker)
|
||
|
|
worker.write_bytes(b"MZxx")
|
||
|
|
with self.assertRaisesRegex(RuntimeError, "Mach-O"):
|
||
|
|
macos_runtime.validate_inventory({}, worker)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
unittest.main()
|