Files
ProxMenux/oci/tests/test_container_memory.py
MacRimi a552da6f91 fix(oci): a Compose reservation is not the memory limit of the container
What a Compose file reserves is the least the application is promised, not the most it may use. 81 recipes had taken it as the memory of the container, as low as 128 MB, and the kernel stopped applications such as Home Assistant Official and UniFi Network.

Without a limit in the file the container gets the standard 1024 MB, or the reservation when it is larger. A limit the file sets is kept.
2026-10-11 12:02:04 +02:00

48 lines
1.7 KiB
Python

"""What a Compose file reserves for an application is the least it is promised,
not the most it may use. A container limited to that figure is stopped by the
kernel as soon as the application needs more."""
from pathlib import Path
import sys
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "src"))
from proxmenux_oci.casaos import _memory_mb
from proxmenux_oci.catalog import Catalog
def service(reservation=None, limit=None):
resources = {}
if reservation:
resources["reservations"] = {"memory": reservation}
if limit:
resources["limits"] = {"memory": limit}
return {"deploy": {"resources": resources}} if resources else {}
class ContainerMemoryTests(unittest.TestCase):
def test_a_reservation_is_not_the_memory_of_the_container(self):
self.assertEqual(_memory_mb(service(reservation="64M")), 1024)
self.assertEqual(_memory_mb(service(reservation="256M")), 1024)
def test_a_larger_reservation_raises_it(self):
self.assertEqual(_memory_mb(service(reservation="2G")), 2048)
def test_a_limit_the_file_sets_is_the_memory_of_the_container(self):
self.assertEqual(_memory_mb(service(limit="512M")), 512)
self.assertEqual(_memory_mb(service(reservation="128M", limit="128M")), 128)
def test_nothing_stated_is_the_default(self):
self.assertEqual(_memory_mb({}), 1024)
def test_the_recipes_that_only_reserve_memory_get_the_default(self):
catalog = Catalog(ROOT)
for app in ("homeassistant-official", "unifi-controller", "grafana", "gitea", "vaultwarden", "uptimekuma"):
self.assertGreaterEqual(catalog.compose(app)["proxmox"]["defaults"]["memory_mb"], 1024, app)
if __name__ == "__main__":
unittest.main()