okami.mom/strata

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

Niko1221update: a test start that misses a system library names it and keeps the engine's output4746b6846

main
8.4 KiB141 linesraw
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:
22    if os.name == "nt":
23        path = folder / "strata.cmd"
24        path.write_text("@echo off\r\n" + body_nt, encoding="utf-8")
25    else:
26        path = folder / "strata"
27        path.write_text("#!/bin/sh\n" + body_posix, encoding="utf-8")
28        path.chmod(path.stat().st_mode | stat.S_IXUSR)
29    return path
30
31
32class Words(unittest.TestCase):
33    def test_the_loader_line_names_the_library(self):
34        self.assertEqual(updater.missing_library(LOADER), "libatomic.so.1")
35        self.assertIsNone(updater.missing_library("No device of requested type available"))
36        self.assertIsNone(updater.missing_library(None))
37
38    def test_the_family_comes_from_os_release(self):
39        with tempfile.TemporaryDirectory() as tmp:
40            path = Path(tmp) / "os-release"
41            for 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)):
44                path.write_text(text)
45                self.assertEqual(updater.linux_family(str(path)), family, text)
46            self.assertIsNone(updater.linux_family(str(Path(tmp) / "missing")))
47
48    def test_one_next_step_with_the_package(self):
49        self.assertEqual(updater.library_hint("libatomic.so.1", "debian"), "Install it with `sudo apt install libatomic1`, then run `strata update` again.")
50        self.assertEqual(updater.library_hint("libatomic.so.1", "fedora"), "Install it with `sudo dnf install libatomic`, then run `strata update` again.")
51        self.assertEqual(updater.library_hint("libatomic.so.1", "arch"), "Install it with `sudo pacman -S gcc-libs`, then run `strata update` again.")
52        self.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.")
55        self.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
58    def test_the_plain_message_names_the_library(self):
59        text = str(updater.TestStartFailed("0.1.60", f"--version exited 127: {LOADER}", busy=False, hint="NEXT"))
60        self.assertIn("The new engine 0.1.60 did not start correctly on this computer, so it was not installed.", text)
61        self.assertIn("The engine needs the system library libatomic.so.1, and this computer does not have it.", text)
62        self.assertNotIn("The engine program did not run on this computer.", text)
63        self.assertTrue(text.endswith("NEXT"))
64
65    def test_other_failures_keep_their_words(self):
66        self.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):
70    def setUp(self):
71        tmp = tempfile.TemporaryDirectory()
72        self.addCleanup(tmp.cleanup)
73        self.root = Path(tmp.name)
74        self.engine = self.root / "engine"
75        self.engine.mkdir()
76        self.components = self.root / "components"
77        self.ctx = SimpleNamespace(components=self.components)
78
79    def component(self, exe):
80        return SimpleNamespace(exe=exe, root=self.engine, version="0.1.60")
81
82    def test_a_missing_library_is_named_with_the_package_and_the_log_is_kept(self):
83        exe = make_engine(self.engine, f"echo '{LOADER}' >&2\nexit 127\n", f"echo {LOADER} 1>&2\r\nexit /b 127\r\n")
84        with mock.patch.object(updater, "linux_family", return_value="debian"), self.assertRaises(updater.TestStartFailed) as caught:
85            updater.binary_smoke(self.component(exe), ctx=self.ctx)
86        log = self.components / updater.STATE_DIR / "logs" / "test-start-0.1.60.log"
87        text = str(caught.exception)
88        self.assertIn("The engine needs the system library libatomic.so.1, and this computer does not have it.", text)
89        self.assertIn("Install it with `sudo apt install libatomic1`, then run `strata update` again.", text)
90        self.assertIn(f"The engine's log from this test is in {log}.", text)
91        self.assertFalse(caught.exception.busy)
92        self.assertIn("libatomic.so.1", caught.exception.reason)
93        kept = log.read_text()
94        self.assertIn("exit code 127", kept)
95        self.assertIn("libatomic.so.1: cannot open shared object file", kept)
96
97    def test_another_failure_keeps_the_log_and_says_where(self):
98        exe = 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")
100        with self.assertRaises(updater.TestStartFailed) as caught:
101            updater.binary_smoke(self.component(exe), ctx=self.ctx)
102        log = self.components / updater.STATE_DIR / "logs" / "test-start-0.1.60.log"
103        self.assertIn("The engine program did not run on this computer.", str(caught.exception))
104        self.assertIn(f"report it with the log: {log}", str(caught.exception))
105        self.assertIn("No device of requested type", log.read_text())
106
107    def test_without_a_context_nothing_is_written_and_the_words_are_as_before(self):
108        exe = make_engine(self.engine, "echo 'bad' >&2\nexit 3\n", "echo bad 1>&2\r\nexit /b 3\r\n")
109        with self.assertRaises(updater.TestStartFailed) as caught:
110            updater.binary_smoke(self.component(exe))
111        self.assertIn("Run `strata update --force` to try again; if it fails again, report it with the log.", str(caught.exception))
112        self.assertFalse(self.components.exists())
113
114    def test_the_default_test_start_with_no_model_keeps_the_log(self):
115        exe = make_engine(self.engine, f"echo '{LOADER}' >&2\nexit 127\n", f"echo {LOADER} 1>&2\r\nexit /b 127\r\n")
116        ctx = SimpleNamespace(components=self.components, run_config=None, environ={}, allow_no_model_smoke=True)
117        with mock.patch.object(updater, "discover_run_config", return_value=None), mock.patch.object(updater, "linux_family", return_value=None), \
118                self.assertRaises(updater.TestStartFailed) as caught:
119            updater.default_test_start(self.root / "stage", self.component(exe), ctx)
120        self.assertIn("libatomic1 on Debian and Ubuntu", str(caught.exception))
121        self.assertTrue((self.components / updater.STATE_DIR / "logs" / "test-start-0.1.60.log").is_file())
122
123
124class FullTestStart(unittest.TestCase):
125    def test_a_loader_error_in_the_engine_log_is_named(self):
126        with tempfile.TemporaryDirectory() as tmp:
127            components = Path(tmp)
128            log = components / updater.STATE_DIR / "logs" / "test-start-0.1.60.log"
129            log.parent.mkdir(parents=True)
130            log.write_text(LOADER + "\n")
131            ctx = SimpleNamespace(components=components, test_timeout=60)
132            with mock.patch.object(updater, "linux_family", return_value="fedora"):
133                failure = updater._test_start_failure(SimpleNamespace(version="0.1.60"), {"ok": False, "error": "exited", "state": "failed"}, ctx, "")
134            text = str(failure)
135            self.assertIn("The engine needs the system library libatomic.so.1", text)
136            self.assertIn("`sudo dnf install libatomic`", text)
137            self.assertIn(str(log), text)
138
139
140if __name__ == "__main__":
141    unittest.main()