okami.mom/strata

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

Niko1221terminal: strata_cli, strata_control, bin/strata and the terminal tests64a91dcdc

main
3.0 KiB69 linesraw
1"""Ready-file lifecycle checked against a live mock Strata API process."""
2
3import json
4import os
5import signal
6import stat
7import subprocess
8import sys
9import tempfile
10import time
11import unittest
12from pathlib import Path
13
14from serve.ready import preflight, publish, remove_if_owned
15
16
17class ReadyFileTests(unittest.TestCase):
18    def test_owner_only_file_is_never_removed_for_another_instance(self):
19        with tempfile.TemporaryDirectory() as directory:
20            path = Path(directory) / "ready.json"
21            self.assertEqual(preflight(str(path)), path)
22            publish(path, {"schema_version": 1, "instance_id": "one", "pid": os.getpid()})
23            if os.name != "nt":
24                self.assertEqual(stat.S_IMODE(path.stat().st_mode), 0o600)
25            with self.assertRaisesRegex(ValueError, "already exists"):
26                preflight(str(path))
27            remove_if_owned(path, "other")
28            self.assertTrue(path.exists())
29            remove_if_owned(path, "one")
30            self.assertFalse(path.exists())
31
32    @unittest.skipIf(os.name == "nt", "Windows console stop needs a platform-specific integration runner")
33    def test_mock_server_publishes_bound_port_and_cleans_on_sigterm(self):
34        root = Path(__file__).resolve().parents[2]
35        with tempfile.TemporaryDirectory() as directory:
36            path = Path(directory) / "ready.json"
37            env = os.environ.copy()
38            env.pop("STRATA_API_KEY", None)
39            env.pop("STRATA_TELEMETRY_KEY", None)
40            process = subprocess.Popen([sys.executable, "-m", "serve.server", "--engine", "mock", "--port", "0",
41                                        "--ready-file", str(path)], cwd=root, env=env,
42                                       stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
43            try:
44                deadline = time.monotonic() + 10
45                record = None
46                while time.monotonic() < deadline and process.poll() is None:
47                    try:
48                        record = json.loads(path.read_text(encoding="utf-8"))
49                        break
50                    except (FileNotFoundError, json.JSONDecodeError):
51                        time.sleep(0.05)
52                self.assertIsNotNone(record, process.stderr.read().decode("utf-8", "replace") if process.poll() is not None else "")
53                self.assertEqual(record["pid"], process.pid)
54                self.assertEqual(record["api_state"], "ready")
55                self.assertEqual(record["model_state"], "ready")
56                self.assertGreater(record["bound_port"], 0)
57                self.assertNotIn("key", json.dumps(record).lower())
58                process.send_signal(signal.SIGTERM)
59                self.assertEqual(process.wait(timeout=10), 0)
60                self.assertFalse(path.exists())
61            finally:
62                if process.poll() is None:
63                    process.kill()
64                    process.wait(timeout=5)
65                process.stderr.close()
66
67
68if __name__ == "__main__":
69    unittest.main()