okami.mom/strata

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

Niko1221Merge f-022-refusals: environmental refusals repair or warn instead of blocking (audit items 1-4, 6, 8-10, 12-15)bcf88b738

main
2.5 KiB66 linesraw
1"""Disk-space checks stop only when the bytes to write do not fit; the working-room margin warns; a resumed download's partial file is not counted twice."""
2
3import tempfile
4import unittest
5from collections import namedtuple
6from pathlib import Path
7from unittest import mock
8
9from strata_control import updater
10
11Usage = namedtuple("Usage", "total used free")
12MiB = 1 << 20
13
14
15class Ctx:
16    def __init__(self, root):
17        self.components = root
18        self.said = []
19
20    def say(self, phase, message, **extra):
21        self.said.append((phase, message))
22
23
24class FreeSpace(unittest.TestCase):
25    def setUp(self):
26        self.tmp = tempfile.TemporaryDirectory()
27        self.root = Path(self.tmp.name)
28        self.addCleanup(self.tmp.cleanup)
29
30    def check(self, free_mib, need_mib, ctx=None, sha=""):
31        with mock.patch.object(updater.shutil, "disk_usage", return_value=Usage(10 ** 12, 0, free_mib * MiB)):
32            updater._free_space_check(self.root, need_mib * MiB, ctx, sha, "engine")
33
34    def test_plenty_of_room_is_silent(self):
35        ctx = Ctx(self.root)
36        self.check(5000, 1000, ctx)
37        self.assertEqual(ctx.said, [])
38
39    def test_between_the_real_need_and_the_margin_warns_and_goes_on(self):
40        ctx = Ctx(self.root)
41        self.check(1050, 1000, ctx)                      # 1.15x would be 1150
42        self.assertEqual(len(ctx.said), 1)
43        self.assertEqual(ctx.said[0][0], "warn")
44
45    def test_below_the_real_need_stops_and_names_the_drive_and_the_step(self):
46        with self.assertRaises(updater.UpdateError) as caught:
47            self.check(800, 1000, Ctx(self.root))
48        self.assertEqual(caught.exception.exit_code, 6)
49        self.assertIn("800 MiB free", str(caught.exception))
50        self.assertIn("Free up some space", str(caught.exception))
51
52    def test_the_partial_file_of_a_resumed_download_is_not_counted_twice(self):
53        ctx = Ctx(self.root)
54        sha = "ab" * 32
55        part = updater.updates_dir(self.root) / "partial"
56        part.mkdir(parents=True)
57        (part / f"{sha[:16]}.part").write_bytes(b"x" * 600)
58        with mock.patch.object(updater.shutil, "disk_usage", return_value=Usage(10 ** 12, 0, 500)):
59            updater._free_space_check(self.root, 1000, ctx, sha, "engine")        # 400 still to write, 500 free: fine and silent
60            self.assertEqual(ctx.said, [])
61            with self.assertRaises(updater.UpdateError):
62                updater._free_space_check(self.root, 1000, ctx, "")                # without the partial file's credit 1000 > 500
63
64
65if __name__ == "__main__":
66    unittest.main()