"""Admission follows current process and ancestor cgroup ceilings.""" from types import SimpleNamespace import pytest from obliteratus.models import host_resources as resources def test_cgroup_ancestor_limit_dominates_unlimited_child(tmp_path): root = tmp_path / 'cgroup' child = root / 'workload' child.mkdir(parents=True) (root / 'cgroup.controllers').write_text('memory') (root / 'memory.max').write_text('1000') (root / 'memory.current').write_text('800') (child / 'memory.max').write_text('max') membership = tmp_path / 'membership' membership.write_text('0::/workload\n') assert resources._cgroup_available_bytes(root, membership) == 200 (child / 'memory.max').write_text('300') (child / 'memory.current').write_text('290') assert resources._cgroup_available_bytes(root, membership) == 10 def test_known_cgroup_limit_requires_readable_usage(tmp_path): (tmp_path / 'memory.max').write_text('1000') with pytest.raises(FileNotFoundError): resources._cgroup_available_bytes(tmp_path, tmp_path / 'absent') def test_host_admission_uses_minimum_of_all_current_limits(monkeypatch): import psutil resource = pytest.importorskip("resource") monkeypatch.setattr(psutil, 'virtual_memory', lambda: SimpleNamespace(total=10000, available=5000)) monkeypatch.setattr(resources, '_cgroup_available_bytes', lambda: 2000) monkeypatch.setattr(resource, 'getrlimit', lambda _: (3000, 3000)) monkeypatch.setattr(psutil, 'Process', lambda: SimpleNamespace(memory_info=lambda: SimpleNamespace(vms=2500))) assert resources.available_host_bytes() == 500