144 lines
7.8 KiB
Python
144 lines
7.8 KiB
Python
"""Redistributable preparation tests; no native builds or GPU execution."""
|
|
from pathlib import Path
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import build_runtime
|
|
from build_runtime import bundle_windows_crt, WINDOWS_CRT_FILES
|
|
|
|
|
|
class LinuxRuntimeTests(unittest.TestCase):
|
|
def test_only_normal_linux_vulkan_modes_use_shared_dispatch(self):
|
|
with patch.object(build_runtime.sys, "platform", "linux"), patch.object(build_runtime.platform, "machine", return_value="x86_64"):
|
|
for backend in ["auto", "vulkan"]:
|
|
self.assertTrue(build_runtime.linux_dispatch(backend))
|
|
for backend in ["cpu", "cuda", "metal"]:
|
|
self.assertFalse(build_runtime.linux_dispatch(backend))
|
|
with patch.object(build_runtime.sys, "platform", "darwin"):
|
|
self.assertFalse(build_runtime.linux_dispatch("auto"))
|
|
with patch.object(build_runtime.sys, "platform", "linux"), patch.object(build_runtime.platform, "machine", return_value="aarch64"):
|
|
self.assertFalse(build_runtime.linux_dispatch("auto"))
|
|
self.assertFalse(build_runtime.linux_dispatch("vulkan"))
|
|
|
|
def test_readelf_parses_needed_soname_and_runpath_without_executing_worker(self):
|
|
output = " 0x1 (NEEDED) Shared library: [libggml.so.0]\n 0xe (SONAME) Library soname: [libggml-cpu-x64.so]\n 0x1d (RUNPATH) Library runpath: [$ORIGIN]\n"
|
|
with patch.object(build_runtime.subprocess, "check_output", return_value=output) as command:
|
|
result = build_runtime.elf_dynamic(Path("worker"))
|
|
self.assertEqual(result, {"NEEDED": ["libggml.so.0"], "SONAME": ["libggml-cpu-x64.so"], "RUNPATH": ["$ORIGIN"]})
|
|
command.assert_called_once_with(["readelf", "--dynamic", "worker"], text=True)
|
|
|
|
def fixture(self, root):
|
|
root = Path(root)
|
|
(root / "bin").mkdir()
|
|
(root / "searchi-gemma-worker").write_bytes(b"worker")
|
|
for name in ["ggml", "ggml-base", "llama", "mtmd"]:
|
|
(root / "bin" / f"lib{name}.so").write_bytes(name.encode())
|
|
for variant in build_runtime.LINUX_CPU_VARIANTS:
|
|
(root / f"libggml-cpu-{variant}.so").write_bytes(variant.encode())
|
|
(root / "libggml-vulkan.so").write_bytes(b"vulkan")
|
|
return root
|
|
|
|
def metadata(self, path):
|
|
result = {"RUNPATH": ["$ORIGIN"], "NEEDED": ["libstdc++.so.6", "libc.so.6"]}
|
|
if path.parent.name == "bin":
|
|
result["SONAME"] = [path.name + ".0"]
|
|
if path.name == "libggml-vulkan.so":
|
|
result["NEEDED"].append("libvulkan.so.1")
|
|
return result
|
|
|
|
def test_flattened_core_and_bounded_modules_have_hashed_inventory(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = self.fixture(directory)
|
|
with patch.object(build_runtime, "elf_dynamic", side_effect=self.metadata):
|
|
build_runtime.bundle_linux_libraries(root)
|
|
manifest = json.loads((root / "runtime-libraries.json").read_text())
|
|
self.assertEqual(manifest["platform"], "linux")
|
|
self.assertEqual(len(manifest["libraries"]), 5 + len(build_runtime.LINUX_CPU_VARIANTS))
|
|
self.assertTrue((root / "libllama.so.0").is_file())
|
|
self.assertTrue(all(not (root / item["path"]).is_symlink() for item in manifest["libraries"]))
|
|
self.assertTrue(all(len(item["sha256"]) == 64 for item in manifest["libraries"]))
|
|
|
|
def test_startup_gpu_dependency_and_build_tree_rpath_are_rejected(self):
|
|
for problem in ["gpu", "rpath", "missing"]:
|
|
with self.subTest(problem=problem), tempfile.TemporaryDirectory() as directory:
|
|
root = self.fixture(directory)
|
|
previous = root / "libllama.so.0"
|
|
previous.write_bytes(b"previous flattened core")
|
|
def metadata(path):
|
|
result = self.metadata(path)
|
|
if path.name == "searchi-gemma-worker":
|
|
if problem == "gpu":
|
|
result["NEEDED"].append("libvulkan.so.1")
|
|
elif problem == "rpath":
|
|
result["RUNPATH"] = ["/private/build"]
|
|
else:
|
|
result["NEEDED"].append("libunpackaged.so.9")
|
|
return result
|
|
with patch.object(build_runtime, "elf_dynamic", side_effect=metadata), self.assertRaises(RuntimeError):
|
|
build_runtime.bundle_linux_libraries(root)
|
|
self.assertFalse((root / "runtime-libraries.json").exists())
|
|
self.assertEqual(previous.read_bytes(), b"previous flattened core")
|
|
|
|
|
|
class CrtTests(unittest.TestCase):
|
|
def fixture(self, directory):
|
|
root = Path(directory)
|
|
installation = root / "Visual Studio"
|
|
auxiliary = installation / "VC/Auxiliary/Build"
|
|
auxiliary.mkdir(parents=True)
|
|
(auxiliary / "Microsoft.VCRedistVersion.default.txt").write_text("14.44.35112")
|
|
(auxiliary / "Microsoft.VCToolsVersion.default.txt").write_text("14.44.35207")
|
|
source = installation / "VC/Redist/MSVC/14.44.35112/x64/Microsoft.VC143.CRT"
|
|
source.mkdir(parents=True)
|
|
for name in WINDOWS_CRT_FILES:
|
|
(source / name).write_bytes(name.encode())
|
|
(source / "unlisted-debug.dll").write_bytes(b"do not distribute")
|
|
build = root / "build"
|
|
(build / "Release").mkdir(parents=True)
|
|
(build / "CMakeCache.txt").write_text(f"CMAKE_GENERATOR_INSTANCE:INTERNAL={installation}\n")
|
|
return build, source, auxiliary
|
|
|
|
def test_copies_only_fixed_unmodified_release_files_and_public_provenance(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
build, source, _ = self.fixture(directory)
|
|
bundle_windows_crt(build)
|
|
for name in WINDOWS_CRT_FILES:
|
|
self.assertEqual((build / "Release" / name).read_bytes(), (source / name).read_bytes())
|
|
self.assertFalse((build / "Release/unlisted-debug.dll").exists())
|
|
notice = (build / "Release/MSVC-REDISTRIBUTION.txt").read_text(encoding="utf-8")
|
|
self.assertIn("14.44.35112", notice)
|
|
self.assertIn("https://learn.microsoft.com/", notice)
|
|
self.assertNotIn(directory, notice)
|
|
|
|
def test_missing_or_mismatched_crt_preserves_existing_files(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
build, source, auxiliary = self.fixture(directory)
|
|
existing = build / "Release/msvcp140.dll"
|
|
existing.write_bytes(b"previous CRT")
|
|
(source / "vcruntime140.dll").unlink()
|
|
with self.assertRaisesRegex(RuntimeError, "redistributable is missing"):
|
|
bundle_windows_crt(build)
|
|
self.assertEqual(existing.read_bytes(), b"previous CRT")
|
|
(auxiliary / "Microsoft.VCRedistVersion.default.txt").write_text("14.43.12345")
|
|
with self.assertRaisesRegex(RuntimeError, "does not match"):
|
|
bundle_windows_crt(build)
|
|
self.assertEqual(existing.read_bytes(), b"previous CRT")
|
|
|
|
def test_custom_toolset_compiler_mismatch_rejects_before_copy(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
build, _, _ = self.fixture(directory)
|
|
with (build / "CMakeCache.txt").open("a") as stream:
|
|
stream.write("CMAKE_CACHE_MAJOR_VERSION:INTERNAL=4\nCMAKE_CACHE_MINOR_VERSION:INTERNAL=2\nCMAKE_CACHE_PATCH_VERSION:INTERNAL=0\n")
|
|
compiler = build / "CMakeFiles/4.2.0/CMakeCXXCompiler.cmake"
|
|
compiler.parent.mkdir(parents=True)
|
|
compiler.write_text('set(CMAKE_CXX_COMPILER_VERSION "19.45.12345")\n')
|
|
with self.assertRaisesRegex(RuntimeError, "actual selected compiler"):
|
|
bundle_windows_crt(build)
|
|
self.assertFalse((build / "Release/msvcp140.dll").exists())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|