okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6b63729a9
main
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 5python -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 : 26def __init__ (self ): 27self .tmp = tempfile .TemporaryDirectory () 28self .dir = Path (self .tmp .name ) 29 (self .dir / "cg" ).mkdir () 30 31def put (self ,rel :str ,text )-> None : 32p = self .dir / rel 33p .parent .mkdir (parents = True ,exist_ok = True ) 34p .write_text (f" { text } \n" ) 35 36def self_cgroup (self ,text :str )-> None : 37self .put ("self_cgroup" ,text ) 38 39def limit (self )-> int | None : 40return setup ._cgroup_memory_limit (str (self .dir / "self_cgroup" ),str (self .dir / "cg" )) 41 42def close (self ): 43self .tmp .cleanup () 44 45 46class CgroupLimit (unittest .TestCase ): 47def setUp (self ): 48self .t = Tree () 49 50def tearDown (self ): 51self .t .close () 52 53def test_container_limit_at_the_namespace_root (self ): 54self .t .self_cgroup ("0::/" ) 55self .t .put ("cg/memory.max" ,POD_LIMIT ) 56self .t .put ("cg/memory.current" ,7 * GIB ) 57self .assertEqual (self .t .limit (),POD_LIMIT ) 58 59def test_no_limit_anywhere (self ): 60self .t .self_cgroup ("0::/user.slice/u.scope" ) 61self .t .put ("cg/user.slice/memory.max" ,"max" ) 62self .t .put ("cg/user.slice/memory.current" ,3 * GIB ) 63self .t .put ("cg/user.slice/u.scope/memory.max" ,"max" ) 64self .t .put ("cg/user.slice/u.scope/memory.high" ,"max" ) 65self .t .put ("cg/user.slice/u.scope/memory.current" ,GIB ) 66self .assertIsNone (self .t .limit ()) 67 68def test_limit_on_a_parent_level (self ): 69self .t .self_cgroup ("0::/a/b" ) 70self .t .put ("cg/a/memory.max" ,40 * GIB ) 71self .t .put ("cg/a/memory.current" ,10 * GIB ) 72self .t .put ("cg/a/b/memory.max" ,"max" ) 73self .t .put ("cg/a/b/memory.current" ,5 * GIB ) 74self .assertEqual (self .t .limit (),40 * GIB ) 75 76def test_tightest_level_wins (self ): 77self .t .self_cgroup ("0::/a/b" ) 78self .t .put ("cg/a/memory.max" ,40 * GIB ) 79self .t .put ("cg/a/memory.current" ,10 * GIB ) 80self .t .put ("cg/a/b/memory.max" ,24 * GIB ) 81self .t .put ("cg/a/b/memory.current" ,5 * GIB ) 82self .assertEqual (self .t .limit (),24 * GIB ) 83 84def test_memory_high_below_max_counts (self ): 85self .t .self_cgroup ("0::/g" ) 86self .t .put ("cg/g/memory.max" ,64 * GIB ) 87self .t .put ("cg/g/memory.high" ,48 * GIB ) 88self .t .put ("cg/g/memory.current" ,GIB ) 89self .assertEqual (self .t .limit (),48 * GIB ) 90 91def 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 93self .t .self_cgroup ("0::/system.slice/docker-abc.scope" ) 94self .t .put ("cg/memory.max" ,32 * GIB ) 95self .t .put ("cg/memory.current" ,GIB ) 96self .assertEqual (self .t .limit (),32 * GIB ) 97 98def test_level_without_memory_files_is_skipped (self ): 99self .t .self_cgroup ("0::/a" ) 100self .t .put ("cg/cgroup.controllers" ,"cpu memory" )# the host's root group: no memory.max 101self .t .put ("cg/a/memory.max" ,20 * GIB ) 102self .t .put ("cg/a/memory.current" ,GIB ) 103self .assertEqual (self .t .limit (),20 * GIB ) 104 105def test_cgroup_v1 (self ): 106self .t .self_cgroup ("12:memory:/docker/abc\n11:cpu,cpuacct:/docker/abc" ) 107self .t .put ("cg/memory/memory.limit_in_bytes" ,9223372036854771712 )# v1's "unlimited" 108self .t .put ("cg/memory/docker/memory.limit_in_bytes" ,9223372036854771712 ) 109self .t .put ("cg/memory/docker/abc/memory.limit_in_bytes" ,16 * GIB ) 110self .assertEqual (self .t .limit (),16 * GIB ) 111 112def test_cgroup_v1_unlimited (self ): 113self .t .self_cgroup ("12:memory:/" ) 114self .t .put ("cg/memory/memory.limit_in_bytes" ,9223372036854771712 ) 115self .assertIsNone (self .t .limit ()) 116 117def test_v2_files_present_v1_not_read (self ): 118# the engine reads v1 only when no v2 memory files are there 119self .t .self_cgroup ("0::/g\n12:memory:/g" ) 120self .t .put ("cg/g/memory.max" ,"max" ) 121self .t .put ("cg/g/memory.current" ,GIB ) 122self .t .put ("cg/memory/g/memory.limit_in_bytes" ,8 * GIB ) 123self .assertIsNone (self .t .limit ()) 124 125def test_unreadable_values_give_no_limit (self ): 126self .t .self_cgroup ("0::/g" ) 127self .t .put ("cg/g/memory.max" ,"garbage" ) 128self .t .put ("cg/g/memory.current" ,GIB ) 129self .assertIsNone (self .t .limit ()) 130 131def test_no_self_cgroup_file_reads_the_mount_root (self ): 132self .t .put ("cg/memory.max" ,12 * GIB ) 133self .t .put ("cg/memory.current" ,GIB ) 134self .assertEqual (self .t .limit (),12 * GIB ) 135 136def test_dot_dot_is_refused (self ): 137self .t .self_cgroup ("0::/../x" ) 138self .t .put ("cg/memory.max" ,12 * GIB ) 139self .t .put ("cg/memory.current" ,GIB ) 140self .assertIsNone (self .t .limit ()) 141 142 143class RamGb (unittest .TestCase ): 144def ram (self ,meminfo_kb :int ,limit )-> float : 145def fake_open (path ,* a ,** kw ): 146if path == "/proc/meminfo" : 147import io 148return io .StringIO (f"MemTotal: { meminfo_kb } kB\nMemFree: 1000 kB\n" ) 149raise OSError (path ) 150with mock .patch .object (setup ,"WIN" ,False ),mock .patch .object (setup .sys ,"platform" ,"linux" ), \ 151mock .patch ("builtins.open" ,fake_open ),mock .patch .object (setup ,"_cgroup_memory_limit" ,lambda :limit ): 152return setup .ram_gb () 153 154def test_container_takes_the_limit (self ): 155self .assertAlmostEqual (self .ram (HOST_KB ,POD_LIMIT ),POD_LIMIT / GIB ,places = 6 ) 156 157def test_bare_metal_unchanged (self ): 158self .assertEqual (self .ram (HOST_KB ,None ),HOST_KB * 1024 / GIB ) 159 160def test_limit_above_memtotal_unchanged (self ): 161self .assertEqual (self .ram (65_000_000 ,600 * GIB ),65_000_000 * 1024 / GIB ) 162 163def test_limit_equal_to_memtotal_unchanged (self ): 164self .assertEqual (self .ram (65_000_000 ,65_000_000 * 1024 ),65_000_000 * 1024 / GIB ) 165 166def test_no_meminfo_stays_zero (self ): 167def no_open (path ,* a ,** kw ): 168raise OSError (path ) 169with mock .patch .object (setup ,"WIN" ,False ),mock .patch .object (setup .sys ,"platform" ,"linux" ), \ 170mock .patch ("builtins.open" ,no_open ),mock .patch .object (setup ,"_cgroup_memory_limit" ,lambda :8 * GIB ): 171self .assertEqual (setup .ram_gb (),0.0 ) 172 173 174if __name__ == "__main__" : 175unittest .main ()