okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
4746b6846
main
1"""A test start that fails because the system lacks a shared library (boxia 11-15: the hip-rx6000 engine exited 127, "error while loading shared 2libraries: libatomic.so.1: cannot open shared object file", and the update only said "The engine program did not run on this computer"). 3 4Proves: the message names the library in plain words with one next step (the package for this Linux family, or the names per family when it is 5not known); the engine's output is kept in .updates/logs and the message says where; other failures keep their words; the full test start (a model 6installed) names the library too.""" 7 8import os 9import stat 10import tempfile 11import unittest 12from pathlib import Path 13from types import SimpleNamespace 14from unittest import mock 15 16from strata_control import updater 17 18LOADER = "./strata: error while loading shared libraries: libatomic.so.1: cannot open shared object file: No such file or directory" 19 20 21def make_engine (folder :Path ,body_posix :str ,body_nt :str )-> Path : 22if os .name == "nt" : 23path = folder / "strata.cmd" 24path .write_text ("@echo off\r\n" + body_nt ,encoding = "utf-8" ) 25else : 26path = folder / "strata" 27path .write_text ("#!/bin/sh\n" + body_posix ,encoding = "utf-8" ) 28path .chmod (path .stat ().st_mode | stat .S_IXUSR ) 29return path 30 31 32class Words (unittest .TestCase ): 33def test_the_loader_line_names_the_library (self ): 34self .assertEqual (updater .missing_library (LOADER ),"libatomic.so.1" ) 35self .assertIsNone (updater .missing_library ("No device of requested type available" )) 36self .assertIsNone (updater .missing_library (None )) 37 38def test_the_family_comes_from_os_release (self ): 39with tempfile .TemporaryDirectory ()as tmp : 40path = Path (tmp )/ "os-release" 41for text ,family in (('ID=ubuntu\nID_LIKE=debian\n' ,"debian" ), ('ID=debian\n' ,"debian" ), ('ID="fedora"\n' ,"fedora" ), 42 ('ID="rocky"\nID_LIKE="rhel centos fedora"\n' ,"fedora" ), ('ID=arch\n' ,"arch" ), ('ID=manjaro\nID_LIKE=arch\n' ,"arch" ), 43 ('ID=opensuse-tumbleweed\nID_LIKE="opensuse suse"\n' ,None )): 44path .write_text (text ) 45self .assertEqual (updater .linux_family (str (path )),family ,text ) 46self .assertIsNone (updater .linux_family (str (Path (tmp )/ "missing" ))) 47 48def test_one_next_step_with_the_package (self ): 49self .assertEqual (updater .library_hint ("libatomic.so.1" ,"debian" ),"Install it with `sudo apt install libatomic1`, then run `strata update` again." ) 50self .assertEqual (updater .library_hint ("libatomic.so.1" ,"fedora" ),"Install it with `sudo dnf install libatomic`, then run `strata update` again." ) 51self .assertEqual (updater .library_hint ("libatomic.so.1" ,"arch" ),"Install it with `sudo pacman -S gcc-libs`, then run `strata update` again." ) 52self .assertEqual (updater .library_hint ("libatomic.so.1" ,None ), 53"Install it with your system's package manager (libatomic1 on Debian and Ubuntu, libatomic on Fedora, gcc-libs on Arch), " 54"then run `strata update` again." ) 55self .assertEqual (updater .library_hint ("libfoo.so.3" ,"debian" ), 56"Install the package that has it with your system's package manager, then run `strata update` again." ) 57 58def test_the_plain_message_names_the_library (self ): 59text = str (updater .TestStartFailed ("0.1.60" ,f"--version exited 127: { LOADER } " ,busy = False ,hint = "NEXT" )) 60self .assertIn ("The new engine 0.1.60 did not start correctly on this computer, so it was not installed." ,text ) 61self .assertIn ("The engine needs the system library libatomic.so.1, and this computer does not have it." ,text ) 62self .assertNotIn ("The engine program did not run on this computer." ,text ) 63self .assertTrue (text .endswith ("NEXT" )) 64 65def test_other_failures_keep_their_words (self ): 66self .assertIn ("The engine program did not run on this computer." ,str (updater .TestStartFailed ("0.1.48" ,"--version exited 3: dll missing" ))) 67 68 69class BinarySmoke (unittest .TestCase ): 70def setUp (self ): 71tmp = tempfile .TemporaryDirectory () 72self .addCleanup (tmp .cleanup ) 73self .root = Path (tmp .name ) 74self .engine = self .root / "engine" 75self .engine .mkdir () 76self .components = self .root / "components" 77self .ctx = SimpleNamespace (components = self .components ) 78 79def component (self ,exe ): 80return SimpleNamespace (exe = exe ,root = self .engine ,version = "0.1.60" ) 81 82def test_a_missing_library_is_named_with_the_package_and_the_log_is_kept (self ): 83exe = make_engine (self .engine ,f"echo ' { LOADER } ' >&2\nexit 127\n" ,f"echo { LOADER } 1>&2\r\nexit /b 127\r\n" ) 84with mock .patch .object (updater ,"linux_family" ,return_value = "debian" ),self .assertRaises (updater .TestStartFailed )as caught : 85updater .binary_smoke (self .component (exe ),ctx = self .ctx ) 86log = self .components / updater .STATE_DIR / "logs" / "test-start-0.1.60.log" 87text = str (caught .exception ) 88self .assertIn ("The engine needs the system library libatomic.so.1, and this computer does not have it." ,text ) 89self .assertIn ("Install it with `sudo apt install libatomic1`, then run `strata update` again." ,text ) 90self .assertIn (f"The engine's log from this test is in { log } ." ,text ) 91self .assertFalse (caught .exception .busy ) 92self .assertIn ("libatomic.so.1" ,caught .exception .reason ) 93kept = log .read_text () 94self .assertIn ("exit code 127" ,kept ) 95self .assertIn ("libatomic.so.1: cannot open shared object file" ,kept ) 96 97def test_another_failure_keeps_the_log_and_says_where (self ): 98exe = make_engine (self .engine ,"echo 'No device of requested type available' >&2\nexit 250\n" , 99"echo No device of requested type available 1>&2\r\nexit /b 250\r\n" ) 100with self .assertRaises (updater .TestStartFailed )as caught : 101updater .binary_smoke (self .component (exe ),ctx = self .ctx ) 102log = self .components / updater .STATE_DIR / "logs" / "test-start-0.1.60.log" 103self .assertIn ("The engine program did not run on this computer." ,str (caught .exception )) 104self .assertIn (f"report it with the log: { log } " ,str (caught .exception )) 105self .assertIn ("No device of requested type" ,log .read_text ()) 106 107def test_without_a_context_nothing_is_written_and_the_words_are_as_before (self ): 108exe = make_engine (self .engine ,"echo 'bad' >&2\nexit 3\n" ,"echo bad 1>&2\r\nexit /b 3\r\n" ) 109with self .assertRaises (updater .TestStartFailed )as caught : 110updater .binary_smoke (self .component (exe )) 111self .assertIn ("Run `strata update --force` to try again; if it fails again, report it with the log." ,str (caught .exception )) 112self .assertFalse (self .components .exists ()) 113 114def test_the_default_test_start_with_no_model_keeps_the_log (self ): 115exe = make_engine (self .engine ,f"echo ' { LOADER } ' >&2\nexit 127\n" ,f"echo { LOADER } 1>&2\r\nexit /b 127\r\n" ) 116ctx = SimpleNamespace (components = self .components ,run_config = None ,environ = {},allow_no_model_smoke = True ) 117with mock .patch .object (updater ,"discover_run_config" ,return_value = None ),mock .patch .object (updater ,"linux_family" ,return_value = None ), \ 118self .assertRaises (updater .TestStartFailed )as caught : 119updater .default_test_start (self .root / "stage" ,self .component (exe ),ctx ) 120self .assertIn ("libatomic1 on Debian and Ubuntu" ,str (caught .exception )) 121self .assertTrue ((self .components / updater .STATE_DIR / "logs" / "test-start-0.1.60.log" ).is_file ()) 122 123 124class FullTestStart (unittest .TestCase ): 125def test_a_loader_error_in_the_engine_log_is_named (self ): 126with tempfile .TemporaryDirectory ()as tmp : 127components = Path (tmp ) 128log = components / updater .STATE_DIR / "logs" / "test-start-0.1.60.log" 129log .parent .mkdir (parents = True ) 130log .write_text (LOADER + "\n" ) 131ctx = SimpleNamespace (components = components ,test_timeout = 60 ) 132with mock .patch .object (updater ,"linux_family" ,return_value = "fedora" ): 133failure = updater ._test_start_failure (SimpleNamespace (version = "0.1.60" ), {"ok" :False ,"error" :"exited" ,"state" :"failed" },ctx ,"" ) 134text = str (failure ) 135self .assertIn ("The engine needs the system library libatomic.so.1" ,text ) 136self .assertIn ("`sudo dnf install libatomic`" ,text ) 137self .assertIn (str (log ),text ) 138 139 140if __name__ == "__main__" : 141unittest .main ()