okami.mom/strata

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

Niko1221Trial2 merge f-0291-gh2-app (0dfa2d9b1)2e8fb221c

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