okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-142c-small into f-142407ea8192

main
3.3 KiB82 linesraw
1"""Tests for serve/server.py's effort_end_args (#458): "effort_position": "end" is used when the engine binary knows
2--tail-role-token.  With the Intel launcher (sycl/serve/strata-sycl.sh) as the configured exe, the check reads the
3binary the launcher runs (STRATA_SYCL_BIN, default build-sycl-aot/strata), not the shell script, which never names
4the option.  Fake files in a temp repo - nothing is built or run.
5
6    python -m unittest tools.test_effort_end_args
7"""
8from __future__ import annotations
9
10import contextlib
11import io
12import os
13import sys
14import tempfile
15import unittest
16from pathlib import Path
17from unittest import mock
18
19ROOT = Path(__file__).resolve().parents[1]
20sys.path.insert(0, str(ROOT))
21from serve import server as S  # noqa: E402
22
23
24class Tok:
25    def encode(self, text, parse_special=False):
26        return [42] if text == "system" else [1, 2]
27
28
29ENGINE_WITH = b"\x7fELF ... --tail-role-token ... --serve ..."
30ENGINE_WITHOUT = b"\x7fELF ... --serve ..."
31
32
33class EffortEndArgs(unittest.TestCase):
34    def setUp(self):
35        self.tmp = tempfile.TemporaryDirectory()
36        self.repo = Path(self.tmp.name) / "engine-repo"
37        (self.repo / "sycl" / "serve").mkdir(parents=True)
38        self.launcher = self.repo / "sycl" / "serve" / "strata-sycl.sh"
39        self.launcher.write_text("#!/usr/bin/env bash\nexec ${STRATA_SYCL_BIN:-build-sycl-aot/strata}\n")
40        (self.repo / "build-sycl-aot").mkdir()
41
42    def tearDown(self):
43        self.tmp.cleanup()
44
45    def call(self, cfg, exe):
46        with contextlib.redirect_stdout(io.StringIO()), mock.patch.dict(os.environ, {}, clear=False):
47            os.environ.pop("STRATA_SYCL_BIN", None)
48            return S.effort_end_args(cfg, str(exe), Tok())
49
50    def test_sycl_launcher_reads_the_binary(self):
51        (self.repo / "build-sycl-aot" / "strata").write_bytes(ENGINE_WITH)
52        self.assertEqual(self.call({"effort_position": "end"}, self.launcher), ["--tail-role-token", "42"])
53
54    def test_sycl_binary_without_the_option(self):
55        (self.repo / "build-sycl-aot" / "strata").write_bytes(ENGINE_WITHOUT)
56        self.assertIsNone(self.call({"effort_position": "end"}, self.launcher))
57
58    def test_sycl_bin_from_the_config_env(self):
59        (self.repo / "build-sycl-aot" / "strata").write_bytes(ENGINE_WITHOUT)
60        (self.repo / "other").mkdir()
61        (self.repo / "other" / "strata").write_bytes(ENGINE_WITH)
62        cfg = {"effort_position": "end", "env": {"STRATA_SYCL_BIN": "other/strata"}}
63        self.assertEqual(self.call(cfg, self.launcher), ["--tail-role-token", "42"])
64
65    def test_sycl_binary_missing_falls_back_to_the_launcher(self):
66        self.assertIsNone(self.call({"effort_position": "end"}, self.launcher))
67
68    def test_a_plain_engine_is_read_as_before(self):
69        exe = Path(self.tmp.name) / "strata"
70        exe.write_bytes(ENGINE_WITH)
71        self.assertEqual(self.call({"effort_position": "end"}, exe), ["--tail-role-token", "42"])
72        exe.write_bytes(ENGINE_WITHOUT)
73        self.assertIsNone(self.call({"effort_position": "end"}, exe))
74
75    def test_start_is_the_default(self):
76        (self.repo / "build-sycl-aot" / "strata").write_bytes(ENGINE_WITH)
77        self.assertIsNone(self.call({}, self.launcher))
78        self.assertIsNone(self.call({"effort_position": "start"}, self.launcher))
79
80
81if __name__ == "__main__":
82    unittest.main()