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