okami.mom/strata

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

Niko1221Merge f-0215-statewait: runtime: a start waiting for its owner reads an empty state file as not there yet (#5601)998077b33

main
2.6 KiB58 linesraw
1"""#5601: the start waits for the owner it launched to publish its state. The owner creates the state file as its lease and writes it a moment
2later, so a read in between saw 0 bytes and the start (an update's test start) failed with "runtime state file is unreadable"."""
3
4import inspect
5import os
6import tempfile
7import unittest
8from pathlib import Path
9from unittest import mock
10
11from strata_control import runtime
12from strata_control.profiles import ProfileError
13
14
15@unittest.skipUnless(os.name in ("posix", "nt"), "no qualified owner transport on this platform")
16class PendingState(unittest.TestCase):
17    def setUp(self):
18        temp = tempfile.TemporaryDirectory(prefix="strata-state-pending-")
19        self.addCleanup(temp.cleanup)
20        env = mock.patch.dict(os.environ, {"STRATA_RUNTIME_DIR": str(Path(temp.name) / "runtime")})
21        env.start()
22        self.addCleanup(env.stop)
23        self.path = runtime._paths("updtest")[0]
24        self.path.parent.mkdir(parents=True, exist_ok=True)
25
26    def write(self, data: bytes):
27        descriptor = runtime._write_new(self.path, data)       # the owner's own writer: a private file, the lease held
28        self.addCleanup(os.close, descriptor)
29
30    def test_an_empty_state_file_is_not_there_yet_for_the_waiting_start(self):
31        self.write(b"")
32        self.assertIsNone(runtime._read_state("updtest", pending_ok=True))
33        with self.assertRaisesRegex(ProfileError, "unreadable|not private or is invalid"):
34            runtime._read_state("updtest")                     # everywhere else an empty file is still an error, as before
35
36    def test_a_half_written_state_file_is_not_there_yet_for_the_waiting_start(self):
37        self.write(b'{"schema_version": 1, "prof')
38        self.assertIsNone(runtime._read_state("updtest", pending_ok=True))
39        with self.assertRaises(ProfileError):
40            runtime._read_state("updtest")
41
42    def test_a_written_state_of_another_shape_is_still_refused(self):
43        self.write(b'{"schema_version": 2}\n')
44        try:
45            runtime._read_state("updtest", pending_ok=True)
46        except ProfileError as exc:
47            self.assertIn("runtime state file", str(exc))
48        else:
49            self.fail("a complete state file of the wrong shape must not read as waiting")
50
51    def test_the_start_waits_with_pending_ok(self):
52        src = inspect.getsource(runtime.start)
53        self.assertIn("while _read_state(name, pending_ok=True) is None and proc.poll() is None", src)
54        self.assertIn("if _read_state(name, pending_ok=True) is None:", src)
55
56
57if __name__ == "__main__":
58    unittest.main()