okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2e8fb221c
main
1"""#4824: a runtime host left in "failed" (its server exited, the host did not) is cleared before the tuning and before the start, a start that still 2meets one says so and what to do, and a launch preset asked for with --save-preset is kept when the start fails after the tuning.""" 3 4import unittest 5from pathlib import Path 6from unittest import mock 7 8from strata_cli .launch import store 9from strata_cli .launch import settings as S 10from strata_cli .launch .runner import LEFTOVER_STATES ,LaunchError ,Runner ,_log_hint 11from strata_cli .launch .catalog import Catalog 12from tests .terminal .launch_support import HW_12 ,Home ,run_cli 13 14 15def bare_runner (): 16class Bare (Runner ): 17profile = "launch-x" 18r = Bare .__new__ (Bare ) 19r .s = S .LaunchSettings (name = "x" ) 20r .restart_after = True 21r ._stop_first = [] 22r .said = [] 23r .emit = lambda * a ,** k :r .said .append (a [1 ]) 24return r 25 26 27class Leftover (unittest .TestCase ): 28def test_every_state_that_blocks_a_start_is_one_that_launch_stop_ends (self ): 29self .assertEqual (set (LEFTOVER_STATES ), {"ready" ,"starting" ,"failed" ,"stale" }) 30 31def test_a_failed_host_is_cleared_before_the_tuning_and_the_line_says_so (self ): 32for state in ("failed" ,"stale" ): 33r = bare_runner () 34with mock .patch ("strata_control.runtime.status" ,return_value = {"state" :state }),mock .patch ("strata_control.runtime.stop" )as stop : 35r .stop_own_server_for_tuning () 36stop .assert_called_once () 37self .assertEqual (r .said , ["Clearing a stopped server of this preset before measuring." ]) 38 39def test_a_running_server_keeps_its_own_line (self ): 40r = bare_runner () 41with mock .patch ("strata_control.runtime.status" ,return_value = {"state" :"ready" }),mock .patch ("strata_control.runtime.stop" ): 42r .stop_own_server_for_tuning () 43self .assertEqual (r .said , ["Stopping the model to measure; it starts again afterwards." ]) 44 45def test_the_start_clears_a_failed_host_before_it_starts (self ): 46r = bare_runner () 47r .cancel = mock .Mock (is_set = lambda :False ) 48r .config_path = None 49r .stop_others = lambda :None 50r .check_port_free = lambda :None 51r .log_marks = lambda : {} 52r .start_wait = lambda :5 53r .report_drafting = lambda :None 54calls = [] 55with mock .patch ("strata_control.runtime.status" ,return_value = {"state" :"failed" ,"error" :"server_exited" }), \ 56mock .patch ("strata_control.runtime.stop" ,side_effect = lambda * a ,** k :calls .append ("stop" )), \ 57mock .patch ("strata_control.runtime.start" ,side_effect = lambda * a ,** k :calls .append ("start" )or {"ok" :True }): 58r .start () 59self .assertEqual (calls , ["stop" ,"start" ]) 60self .assertTrue (any ("stopped earlier and was never cleared" in line for line in r .said ),r .said ) 61 62def test_the_message_for_a_profile_that_is_still_held_names_the_cure_and_the_retune (self ): 63hint = _log_hint (None ,"this profile already has a live managed runtime" ) 64self .assertIn ("`strata launch --stop`" ,hint ) 65self .assertIn ("without --retune" ,hint ) 66self .assertNotIn ("Run the command again" ,hint ) 67 68 69class SavedPreset (unittest .TestCase ): 70def setUp (self ): 71self .home = Home ().__enter__ () 72self .addCleanup (self .home .__exit__ ,None ,None ,None ) 73p = mock .patch .object (Catalog ,"hardware" ,lambda self ,fresh = False :dict (HW_12 ,disk_free_gb = 500.0 )) 74p .start () 75self .addCleanup (p .stop ) 76 77def test_a_start_that_fails_after_the_tuning_keeps_the_preset_that_was_asked_for (self ): 78def fails (self_ ,** _ ): 79self_ .config_path = Path ("/x" )# the settings were written: the failure came later 80raise LaunchError ("The server could not start: busy." ,"Run `strata launch --stop`." ,6 ) 81with mock .patch .object (Runner ,"run" ,fails ): 82code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--save-preset" ,"Office" ]) 83self .assertEqual (code ,6 ,err ) 84self .assertEqual ([p .name for p in store .list_presets ()], ["Office" ]) 85self .assertIn ("saved as the launch preset Office" ,out ) 86self .assertIn ("strata launch --preset Office" ,out ) 87self .assertIn ("The server could not start" ,err ) 88 89def test_a_failure_before_the_settings_were_written_saves_nothing (self ): 90with mock .patch .object (Runner ,"run" ,side_effect = LaunchError ("The engine could not be installed." ,"Check your internet." ,6 )): 91code ,out ,err = run_cli (["launch" ,"--no-tui" ,"--model" ,"q2_0" ,"--save-preset" ,"Office" ]) 92self .assertEqual (code ,6 ,err ) 93self .assertEqual (store .list_presets (), []) 94 95 96if __name__ == "__main__" : 97unittest .main ()