diff --git a/runtime/test/check_device_filter_tools.py b/runtime/test/check_device_filter_tools.py index 299529482b..734635acec 100644 --- a/runtime/test/check_device_filter_tools.py +++ b/runtime/test/check_device_filter_tools.py @@ -7,10 +7,16 @@ python -I check_device_filter_tools.py Exit 0 when both tools refuse the filter, 1 when one does not or when the device list fails for another reason, and -77 (skip) on a machine with no Vulkan loader, driver or device, where no tool can choose one. Standard library only. +77 (skip) on a machine with no Vulkan loader, driver or device, where no tool can choose one, and on a non-Windows host +without wine for the Windows tools below. Standard library only. + +The tools are Windows executables even when this runs on Linux, so they go through wine there (LEONIDA_WINE names +another loader) and run directly on Windows. Without that the ctest entry was a host python launching a PE, which is +an Exec format error rather than the tool's answer (#1150). """ import os import re +import shutil import struct import subprocess import sys @@ -19,6 +25,20 @@ UNSUPPORTED = 77 +def launcher(exe, host=None, which=None): + """The prefix that runs `exe` on this host: [] for a native binary, ["wine"] for a Windows PE on a non-Windows + host. `host` and `which` are injectable so the choice is tested where it cannot happen.""" + if (host or os.name) == "nt": + return [] + try: + with open(exe, "rb") as f: + if f.read(2) != b"MZ": + return [] + except OSError: + return [] # a missing tool fails on its own, with a better message than "cannot read" + return [os.environ.get("LEONIDA_WINE") or (which or shutil.which)("wine") or "wine"] + + def run(cmd, env=None): p = subprocess.run(cmd, capture_output=True, text=True, errors="replace", env=env, timeout=120) return p.returncode, p.stdout + p.stderr @@ -44,9 +64,15 @@ def main(argv): bench, sandbox, work = argv[1:] os.makedirs(work, exist_ok=True) + prefixes = {bench: launcher(bench), sandbox: launcher(sandbox)} + for exe, prefix in prefixes.items(): + if prefix and not shutil.which(prefix[0]): + print(f"SKIP: {os.path.basename(exe)} is a Windows executable and {prefix[0]} is not on PATH") + return UNSUPPORTED + # The device list ignores the filter, so it shows whether there is any device to choose from. plain = {k: v for k, v in os.environ.items() if k != "LEONIDA_VK_DEVICE"} - code, out = run([bench, "--list-devices"], env=plain) + code, out = run(prefixes[bench] + [bench, "--list-devices"], env=plain) reason = no_vulkan(code, out) if reason: print(f"SKIP: no Vulkan device to choose from ({reason})") @@ -65,7 +91,7 @@ def main(argv): failures = [] for name, cmd in (("vk_descriptor_bench", [bench]), ("vk_sandbox", [sandbox, scene, missing, missing, "--out", os.path.join(work, "out.ppm")])): - code, out = run(cmd, env=env) + code, out = run(prefixes[cmd[0]] + cmd, env=env) named = f"LEONIDA_VK_DEVICE={FILTER}" in out if code == 0 or not named or "device:" in out: failures.append(f"{name}: exit {code}, {'names' if named else 'does not name'} the filter:\n{out.strip()}") diff --git a/tests/test_check_device_filter_tools.py b/tests/test_check_device_filter_tools.py new file mode 100644 index 0000000000..8221afe208 --- /dev/null +++ b/tests/test_check_device_filter_tools.py @@ -0,0 +1,71 @@ +"""Synthetic tests for the wine launcher of runtime/test/check_device_filter_tools.py (issue #1150). No GPU, no wine.""" +import importlib.util +import io +import os +import tempfile +import unittest +from contextlib import redirect_stdout +from unittest import mock + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +SCRIPT = os.path.join(ROOT, "runtime", "test", "check_device_filter_tools.py") + + +def load(): + spec = importlib.util.spec_from_file_location("check_device_filter_tools", SCRIPT) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +class LauncherTest(unittest.TestCase): + def setUp(self): + self.mod = load() + self.tmp = tempfile.TemporaryDirectory(prefix="device-filter-") + self.pe = os.path.join(self.tmp.name, "tool.exe") + with open(self.pe, "wb") as f: + f.write(b"MZ\x90\x00rest") + self.elf = os.path.join(self.tmp.name, "tool") + with open(self.elf, "wb") as f: + f.write(b"\x7fELFrest") + self.env = mock.patch.dict(os.environ, {}, clear=False) + self.env.start() + os.environ.pop("LEONIDA_WINE", None) + + def tearDown(self): + self.env.stop() + self.tmp.cleanup() + + def test_windows_runs_the_tool_directly(self): + self.assertEqual(self.mod.launcher(self.pe, host="nt"), []) + + def test_posix_runs_a_pe_through_wine(self): + self.assertEqual(self.mod.launcher(self.pe, host="posix", which=lambda n: "/usr/bin/" + n), + ["/usr/bin/wine"]) + + def test_posix_runs_a_non_pe_directly(self): + self.assertEqual(self.mod.launcher(self.elf, host="posix", which=lambda n: "/usr/bin/" + n), []) + + def test_posix_without_wine_still_names_a_loader(self): + # main turns this into a skip with a message; the launcher itself does not decide. + self.assertEqual(self.mod.launcher(self.pe, host="posix", which=lambda n: None), ["wine"]) + + def test_leonida_wine_overrides_the_binary(self): + os.environ["LEONIDA_WINE"] = "wine64" + self.assertEqual(self.mod.launcher(self.pe, host="posix", which=lambda n: "/usr/bin/wine"), ["wine64"]) + + def test_missing_tool_has_no_prefix(self): + self.assertEqual(self.mod.launcher(os.path.join(self.tmp.name, "absent.exe"), host="posix"), []) + + def test_main_skips_when_wine_is_absent(self): + out = io.StringIO() + with mock.patch.object(self.mod, "launcher", return_value=["no-such-wine-loader"]), \ + mock.patch.object(self.mod.shutil, "which", return_value=None), \ + redirect_stdout(out): + code = self.mod.main(["check", self.pe, self.pe, self.tmp.name]) + self.assertEqual(code, 77) + self.assertIn("no-such-wine-loader is not on PATH", out.getvalue()) + + +if __name__ == "__main__": + unittest.main()