okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
998077b33
main
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 ): 17def setUp (self ): 18temp = tempfile .TemporaryDirectory (prefix = "strata-state-pending-" ) 19self .addCleanup (temp .cleanup ) 20env = mock .patch .dict (os .environ , {"STRATA_RUNTIME_DIR" :str (Path (temp .name )/ "runtime" )}) 21env .start () 22self .addCleanup (env .stop ) 23self .path = runtime ._paths ("updtest" )[0 ] 24self .path .parent .mkdir (parents = True ,exist_ok = True ) 25 26def write (self ,data :bytes ): 27descriptor = runtime ._write_new (self .path ,data )# the owner's own writer: a private file, the lease held 28self .addCleanup (os .close ,descriptor ) 29 30def test_an_empty_state_file_is_not_there_yet_for_the_waiting_start (self ): 31self .write (b"" ) 32self .assertIsNone (runtime ._read_state ("updtest" ,pending_ok = True )) 33with self .assertRaisesRegex (ProfileError ,"unreadable|not private or is invalid" ): 34runtime ._read_state ("updtest" )# everywhere else an empty file is still an error, as before 35 36def test_a_half_written_state_file_is_not_there_yet_for_the_waiting_start (self ): 37self .write (b'{"schema_version": 1, "prof' ) 38self .assertIsNone (runtime ._read_state ("updtest" ,pending_ok = True )) 39with self .assertRaises (ProfileError ): 40runtime ._read_state ("updtest" ) 41 42def test_a_written_state_of_another_shape_is_still_refused (self ): 43self .write (b'{"schema_version": 2}\n' ) 44try : 45runtime ._read_state ("updtest" ,pending_ok = True ) 46except ProfileError as exc : 47self .assertIn ("runtime state file" ,str (exc )) 48else : 49self .fail ("a complete state file of the wrong shape must not read as waiting" ) 50 51def test_the_start_waits_with_pending_ok (self ): 52src = inspect .getsource (runtime .start ) 53self .assertIn ("while _read_state(name, pending_ok=True) is None and proc.poll() is None" ,src ) 54self .assertIn ("if _read_state(name, pending_ok=True) is None:" ,src ) 55 56 57if __name__ == "__main__" : 58unittest .main ()