Files
alzeo/tests/test_unit_lessons.py

47 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""What every unit template of this repo has learned, so that a new lane's starts with it: the two lanes beside were written
on 2026-11-20 and 2026-12-01 without two lessons the others had held for weeks (review of v1.60.0)."""
import pathlib
import re
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
UNITS = {p.name[:-len(".service.template")]: p.read_text() for p in sorted(REPO.glob("qwen38-*.service.template"))}
# docker stop sends SIGTERM to the container's first process, which the kernel gives no default for it there: an engine that
# handles none is killed after its wait, and docker run ends in 137, which systemd reads as a failure. These two end at once,
# with 0: vectors_server.py (test_clipstral_server.StoppedByItsUnit)
ENDS_WELL = {"qwen38-embed", "qwen38-clipstral"}
def lines(text, key):
return [ln.split("=", 1)[1] for ln in text.replace("\\\n", " ").splitlines() if ln.startswith(key + "=")]
class EachUnit(unittest.TestCase):
def test_there_are_units(self):
self.assertGreater(len(UNITS), 15)
self.assertTrue(ENDS_WELL <= set(UNITS))
def test_no_start_step_ends_in_78(self):
"""RestartPreventExitStatus holds the main process's exit alone (systemd.service(5)): from a start step the same 78 is
tried again every RestartSec, without end."""
for name, text in UNITS.items():
with self.subTest(name):
self.assertEqual([x for x in lines(text, "ExecStartPre") if re.search(r"\b78\b", x)], [])
if "78" in " ".join(lines(text, "RestartPreventExitStatus")):
self.assertRegex(" ".join(lines(text, "ExecStart")), r"exit 78|-files\.sh|launch-|engine-preflight", name)
def test_a_stop_is_no_failure(self):
for name, text in UNITS.items():
if "docker run" not in " ".join(lines(text, "ExecStart")):
continue
with self.subTest(name):
status = " ".join(lines(text, "SuccessExitStatus")).split()
self.assertEqual("137" in status, name not in ENDS_WELL, status)
if name in ENDS_WELL: # 137 is also the cgroup's kill: with Restart=on-failure it must stay a failure
self.assertEqual(lines(text, "Restart"), ["on-failure"])
if __name__ == "__main__":
unittest.main()