okami.mom/strata

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

Niko1221Merge f-0213-life (final 576d06553) into f-0213-int66331f9bc

main
4.5 KiB96 linesraw
1"""#4463: `strata update` on a source install.
2
3Gap 1: with no folder named, it used the per-user component folder even when the checkout has its own engine store (`engine-feed`, which setup.py
4fills); the download, its half-written state and its part file then sat in a folder the checkout never read.
5Gap 2: the busy check did not see an engine the desktop app's source install had started (it runs serve/server.py directly, which took no lease), so
6`strata update` test-started a second copy of the model beside it.
7
8    python -m pytest tests/terminal/test_update_source_install.py
9"""
10import os
11import tempfile
12import unittest
13from pathlib import Path
14from unittest import mock
15
16from serve import server
17from strata_cli import update_cmd
18from strata_control import engine_component as ec
19from strata_control import updater
20
21
22class DefaultFolder(unittest.TestCase):
23    def setUp(self):
24        self.temp = tempfile.TemporaryDirectory(prefix="strata-updsrc-")
25        self.addCleanup(self.temp.cleanup)
26        self.root = Path(self.temp.name)
27        self.user = self.root / "user-components"
28        self.store = self.root / "checkout" / "engine-feed"
29        for folder in (self.user, self.store):
30            folder.mkdir(parents=True)
31        env = mock.patch.dict(os.environ, {}, clear=False)
32        env.start()
33        self.addCleanup(env.stop)
34        os.environ.pop(ec.ENV_DIR, None)
35        self.patches = [mock.patch.object(ec, "components_dir", lambda *a, **k: self.user)]
36        for patch in self.patches:
37            patch.start()
38            self.addCleanup(patch.stop)
39
40    def _scan(self, user_has, store_has):
41        def scan(root=None):
42            has = user_has if Path(root) == self.user else store_has if Path(root) == self.store else False
43            return ([object()] if has else []), []
44        return mock.patch.object(ec, "scan", scan)
45
46    def test_a_checkout_with_its_own_store_updates_it_when_the_user_folder_has_no_engine(self):
47        with self._scan(False, True), mock.patch.object(update_cmd, "source_store", return_value=self.store):
48            self.assertEqual(update_cmd.default_components_dir(ec), self.store)
49
50    def test_a_user_folder_that_holds_an_engine_is_kept(self):
51        with self._scan(True, True), mock.patch.object(update_cmd, "source_store", return_value=self.store):
52            self.assertEqual(update_cmd.default_components_dir(ec), self.user)
53
54    def test_no_store_in_the_checkout_changes_nothing(self):
55        with self._scan(False, False), mock.patch.object(update_cmd, "source_store", return_value=None):
56            self.assertEqual(update_cmd.default_components_dir(ec), self.user)
57
58    def test_an_explicit_folder_in_the_environment_always_wins(self):
59        with self._scan(False, True), mock.patch.object(update_cmd, "source_store", return_value=self.store), \
60                mock.patch.dict(os.environ, {ec.ENV_DIR: str(self.root / "named")}):
61            self.assertEqual(update_cmd.default_components_dir(ec), self.user)      # (components_dir() itself returns the named folder)
62
63    def test_a_store_without_an_engine_package_is_not_one(self):
64        with mock.patch.object(ec, "scan", return_value=([], [])):
65            self.assertIsNone(update_cmd.source_store(self.root / "checkout"))
66
67
68class AppLaunchedEngine(unittest.TestCase):
69    def setUp(self):
70        self.temp = tempfile.TemporaryDirectory(prefix="strata-updsrc-rt-")
71        self.addCleanup(self.temp.cleanup)
72        env = mock.patch.dict(os.environ, {"STRATA_RUNTIME_DIR": str(Path(self.temp.name) / "runtime")})
73        env.start()
74        self.addCleanup(env.stop)
75
76    def test_a_server_started_by_running_serve_server_is_seen_by_the_busy_check(self):
77        seen = {}
78
79        def serving(*args, **kwargs):
80            seen["during"] = updater.serving_now()
81            return 0
82        self.assertEqual(updater.serving_now(), [])
83        with mock.patch.object(server, "_main", serving):
84            self.assertEqual(server.main([]), 0)
85        self.assertTrue(seen["during"], "the update's busy check did not see the running server")
86        self.assertEqual(updater.serving_now(), [], "the lease is released when the server ends")
87
88    def test_serving_does_not_depend_on_the_lease(self):
89        from strata_control import runtime
90        with mock.patch.object(runtime, "acquire_activity_lease", side_effect=runtime.ProfileError("a managed runtime or calibration owns the local engine lease", 6)), \
91                mock.patch.object(server, "_main", return_value=7):
92            self.assertEqual(server.main([]), 7)
93
94
95if __name__ == "__main__":
96    unittest.main()