okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
049ae1c67
main
1"""Round 0213 TUICFG, step 2: `strata launch --stop` exit status and message (#4398). 2Four cases scripted against a runtime double that tracks which fake PIDs are still alive: empty, successful, failed, mixed.""" 3from __future__import annotations 4 5import unittest 6from unittest import mock 7 8from strata_cli .launch import access 9from tests .terminal .launch_support import run_cli 10 11NOTHING = "Nothing from `strata launch` is running." 12 13 14class Runtime : 15"""profile id -> (alive?, stop succeeds?)""" 16 17def __init__ (self ,profiles ): 18self .alive = {p :ok is not None for p ,ok in profiles .items ()} 19self .stops_ok = {p :bool (ok )for p ,ok in profiles .items ()} 20 21def list_profiles (self ): 22return [{"id" :p }for p in self .alive ] 23 24def status (self ,prof ): 25return {"state" :"ready" if self .alive .get (prof )else "stopped" } 26 27def stop (self ,prof ,timeout = None ): 28if self .stops_ok .get (prof ): 29self .alive [prof ]= False 30return {"ok" :True } 31return {"ok" :False ,"error" :"leftover_process_not_ended" } 32 33def live (self ): 34return sorted (p for p ,a in self .alive .items ()if a ) 35 36 37class LaunchStopExit (unittest .TestCase ): 38def _run (self ,profiles ,* args ): 39rt = Runtime (profiles ) 40with mock .patch ("strata_control.profiles.list_profiles" ,rt .list_profiles ), \ 41mock .patch ("strata_control.runtime.status" ,rt .status ), \ 42mock .patch ("strata_control.runtime.stop" ,rt .stop ), \ 43mock .patch .object (access ,"stop_tunnel" ): 44code ,out ,err = run_cli (["launch" ,"--stop" ,* args ]) 45return rt ,code ,out ,err 46 47def test_empty_says_nothing_and_exits_zero (self ): 48rt ,code ,out ,_ = self ._run ({}) 49self .assertEqual (code ,0 ) 50self .assertIn (NOTHING ,out ) 51rt ,code ,out ,_ = self ._run ({"launch-office" :None })# a profile exists but nothing runs 52self .assertEqual ((code ,rt .live ()), (0 , [])) 53self .assertIn (NOTHING ,out ) 54 55def test_successful_stop_exits_zero_and_leaves_nothing_alive (self ): 56rt ,code ,out ,_ = self ._run ({"launch-office" :True ,"launch-home" :True }) 57self .assertEqual ((code ,rt .live ()), (0 , [])) 58self .assertIn ("Stopped office." ,out ) 59self .assertNotIn (NOTHING ,out ) 60 61def test_failed_stop_exits_nonzero_and_does_not_claim_nothing_runs (self ): 62for extra in ([], ["--preset" ,"Office" ]): 63with self .subTest (extra = extra ): 64rt ,code ,out ,_ = self ._run ({"launch-office" :False },* extra ) 65self .assertEqual (code ,1 ) 66self .assertEqual (rt .live (), ["launch-office" ]) 67self .assertIn ("Could not stop office: leftover_process_not_ended." ,out ) 68self .assertNotIn (NOTHING ,out ) 69 70def test_mixed_exits_nonzero_reports_both_and_only_the_failed_one_stays (self ): 71rt ,code ,out ,_ = self ._run ({"launch-office" :False ,"launch-home" :True }) 72self .assertEqual (code ,1 ) 73self .assertEqual (rt .live (), ["launch-office" ]) 74self .assertIn ("Could not stop office" ,out ) 75self .assertIn ("Stopped home." ,out ) 76self .assertNotIn (NOTHING ,out ) 77 78 79if __name__ == "__main__" : 80unittest .main ()