okami.mom/strata

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

Niko1221Merge f-container-mem-core: memory checks that read a container's limit correctly6b63729a9

main
6.8 KiB175 linesraw
1"""Tests for setup.py's RAM figure in a container: ram_gb() takes the smaller of /proc/meminfo MemTotal (the host's,
2inside a container) and the cgroup memory limit, read as the engine's memory guard reads it.  Fake /proc and cgroup
3trees in a temporary folder; no container needed.
4
5    python -m unittest tools.test_setup_container_ram
6"""
7from __future__ import annotations
8
9import os
10import sys
11import tempfile
12import unittest
13from pathlib import Path
14from unittest import mock
15
16ROOT = Path(__file__).resolve().parents[1]
17sys.path.insert(0, str(ROOT))
18import setup  # noqa: E402
19
20GIB = 1 << 30
21HOST_KB = 527_000_000          # a 503 GiB host, as /proc/meminfo shows it inside the rented container
22POD_LIMIT = 61_998_571_520     # memory.max of that container: 57.74 GiB
23
24
25class Tree:
26    def __init__(self):
27        self.tmp = tempfile.TemporaryDirectory()
28        self.dir = Path(self.tmp.name)
29        (self.dir / "cg").mkdir()
30
31    def put(self, rel: str, text) -> None:
32        p = self.dir / rel
33        p.parent.mkdir(parents=True, exist_ok=True)
34        p.write_text(f"{text}\n")
35
36    def self_cgroup(self, text: str) -> None:
37        self.put("self_cgroup", text)
38
39    def limit(self) -> int | None:
40        return setup._cgroup_memory_limit(str(self.dir / "self_cgroup"), str(self.dir / "cg"))
41
42    def close(self):
43        self.tmp.cleanup()
44
45
46class CgroupLimit(unittest.TestCase):
47    def setUp(self):
48        self.t = Tree()
49
50    def tearDown(self):
51        self.t.close()
52
53    def test_container_limit_at_the_namespace_root(self):
54        self.t.self_cgroup("0::/")
55        self.t.put("cg/memory.max", POD_LIMIT)
56        self.t.put("cg/memory.current", 7 * GIB)
57        self.assertEqual(self.t.limit(), POD_LIMIT)
58
59    def test_no_limit_anywhere(self):
60        self.t.self_cgroup("0::/user.slice/u.scope")
61        self.t.put("cg/user.slice/memory.max", "max")
62        self.t.put("cg/user.slice/memory.current", 3 * GIB)
63        self.t.put("cg/user.slice/u.scope/memory.max", "max")
64        self.t.put("cg/user.slice/u.scope/memory.high", "max")
65        self.t.put("cg/user.slice/u.scope/memory.current", GIB)
66        self.assertIsNone(self.t.limit())
67
68    def test_limit_on_a_parent_level(self):
69        self.t.self_cgroup("0::/a/b")
70        self.t.put("cg/a/memory.max", 40 * GIB)
71        self.t.put("cg/a/memory.current", 10 * GIB)
72        self.t.put("cg/a/b/memory.max", "max")
73        self.t.put("cg/a/b/memory.current", 5 * GIB)
74        self.assertEqual(self.t.limit(), 40 * GIB)
75
76    def test_tightest_level_wins(self):
77        self.t.self_cgroup("0::/a/b")
78        self.t.put("cg/a/memory.max", 40 * GIB)
79        self.t.put("cg/a/memory.current", 10 * GIB)
80        self.t.put("cg/a/b/memory.max", 24 * GIB)
81        self.t.put("cg/a/b/memory.current", 5 * GIB)
82        self.assertEqual(self.t.limit(), 24 * GIB)
83
84    def test_memory_high_below_max_counts(self):
85        self.t.self_cgroup("0::/g")
86        self.t.put("cg/g/memory.max", 64 * GIB)
87        self.t.put("cg/g/memory.high", 48 * GIB)
88        self.t.put("cg/g/memory.current", GIB)
89        self.assertEqual(self.t.limit(), 48 * GIB)
90
91    def test_host_path_without_a_namespace(self):
92        # the container sees /docker/abc in /proc/self/cgroup, but only its own group at the mount
93        self.t.self_cgroup("0::/system.slice/docker-abc.scope")
94        self.t.put("cg/memory.max", 32 * GIB)
95        self.t.put("cg/memory.current", GIB)
96        self.assertEqual(self.t.limit(), 32 * GIB)
97
98    def test_level_without_memory_files_is_skipped(self):
99        self.t.self_cgroup("0::/a")
100        self.t.put("cg/cgroup.controllers", "cpu memory")   # the host's root group: no memory.max
101        self.t.put("cg/a/memory.max", 20 * GIB)
102        self.t.put("cg/a/memory.current", GIB)
103        self.assertEqual(self.t.limit(), 20 * GIB)
104
105    def test_cgroup_v1(self):
106        self.t.self_cgroup("12:memory:/docker/abc\n11:cpu,cpuacct:/docker/abc")
107        self.t.put("cg/memory/memory.limit_in_bytes", 9223372036854771712)   # v1's "unlimited"
108        self.t.put("cg/memory/docker/memory.limit_in_bytes", 9223372036854771712)
109        self.t.put("cg/memory/docker/abc/memory.limit_in_bytes", 16 * GIB)
110        self.assertEqual(self.t.limit(), 16 * GIB)
111
112    def test_cgroup_v1_unlimited(self):
113        self.t.self_cgroup("12:memory:/")
114        self.t.put("cg/memory/memory.limit_in_bytes", 9223372036854771712)
115        self.assertIsNone(self.t.limit())
116
117    def test_v2_files_present_v1_not_read(self):
118        # the engine reads v1 only when no v2 memory files are there
119        self.t.self_cgroup("0::/g\n12:memory:/g")
120        self.t.put("cg/g/memory.max", "max")
121        self.t.put("cg/g/memory.current", GIB)
122        self.t.put("cg/memory/g/memory.limit_in_bytes", 8 * GIB)
123        self.assertIsNone(self.t.limit())
124
125    def test_unreadable_values_give_no_limit(self):
126        self.t.self_cgroup("0::/g")
127        self.t.put("cg/g/memory.max", "garbage")
128        self.t.put("cg/g/memory.current", GIB)
129        self.assertIsNone(self.t.limit())
130
131    def test_no_self_cgroup_file_reads_the_mount_root(self):
132        self.t.put("cg/memory.max", 12 * GIB)
133        self.t.put("cg/memory.current", GIB)
134        self.assertEqual(self.t.limit(), 12 * GIB)
135
136    def test_dot_dot_is_refused(self):
137        self.t.self_cgroup("0::/../x")
138        self.t.put("cg/memory.max", 12 * GIB)
139        self.t.put("cg/memory.current", GIB)
140        self.assertIsNone(self.t.limit())
141
142
143class RamGb(unittest.TestCase):
144    def ram(self, meminfo_kb: int, limit) -> float:
145        def fake_open(path, *a, **kw):
146            if path == "/proc/meminfo":
147                import io
148                return io.StringIO(f"MemTotal:       {meminfo_kb} kB\nMemFree:        1000 kB\n")
149            raise OSError(path)
150        with mock.patch.object(setup, "WIN", False), mock.patch.object(setup.sys, "platform", "linux"), \
151                mock.patch("builtins.open", fake_open), mock.patch.object(setup, "_cgroup_memory_limit", lambda: limit):
152            return setup.ram_gb()
153
154    def test_container_takes_the_limit(self):
155        self.assertAlmostEqual(self.ram(HOST_KB, POD_LIMIT), POD_LIMIT / GIB, places=6)
156
157    def test_bare_metal_unchanged(self):
158        self.assertEqual(self.ram(HOST_KB, None), HOST_KB * 1024 / GIB)
159
160    def test_limit_above_memtotal_unchanged(self):
161        self.assertEqual(self.ram(65_000_000, 600 * GIB), 65_000_000 * 1024 / GIB)
162
163    def test_limit_equal_to_memtotal_unchanged(self):
164        self.assertEqual(self.ram(65_000_000, 65_000_000 * 1024), 65_000_000 * 1024 / GIB)
165
166    def test_no_meminfo_stays_zero(self):
167        def no_open(path, *a, **kw):
168            raise OSError(path)
169        with mock.patch.object(setup, "WIN", False), mock.patch.object(setup.sys, "platform", "linux"), \
170                mock.patch("builtins.open", no_open), mock.patch.object(setup, "_cgroup_memory_limit", lambda: 8 * GIB):
171            self.assertEqual(setup.ram_gb(), 0.0)
172
173
174if __name__ == "__main__":
175    unittest.main()