Files
computer-playbook/tests/unit/python/roles/sys-svc-container/test_gpu_runtime.py
T
kevinveenbirkenbachandClaude Opus 5 7e1c2d1273 test(gpu): make the lookup's consumers assert wiring instead of passing vacuously
Collapsing the three GPU call sites into one lookup moved the whole decision behind lookup('gpu', ...), but both consumers' tests still stubbed lookup with a constant. One test per file went red; three more kept passing for the wrong reason, because the stub's {} is falsy and every negative case was therefore true no matter what the template did.

The stubs now branch on the lookup name and record what they were asked, so each file guards what it still owns: the answer reaches the compose key or the role variable, and the lookup is asked for this service. The declared-flag-times-device matrix stays with the lookup's own tests, which is also where the boolean() over bool() rationale now lives, checked rather than written beside the call it protects.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-10-05 20:34:19 +02:00

71 lines
1.9 KiB
Python

"""The ``runtime: nvidia`` key of the shared resource template.
The template asks the ``gpu`` lookup and decides nothing itself: combining
the declared flag with the host's device is that lookup's job, and its own
tests own the matrix.
"""
import unittest
from pathlib import Path
from jinja2 import (
Environment,
FileSystemLoader,
StrictUndefined,
select_autoescape,
)
from utils import PROJECT_ROOT
TEMPLATES = Path(PROJECT_ROOT) / "roles" / "sys-svc-container" / "templates"
BASE = {
"application_id": "web-svc-libretranslate",
"service_name": "libretranslate",
"RESOURCE_CPUS": "1",
"RESOURCE_HOST_CPUS": "8",
"RESOURCE_MEM_LIMIT": "1g",
"RESOURCE_MEM_RESERVATION": "256m",
"RESOURCE_PIDS_LIMIT": "512",
}
class TestGpuRuntimeRegistration(unittest.TestCase):
def _render(self, *, gpu):
env = Environment(
loader=FileSystemLoader(str(TEMPLATES)),
trim_blocks=True,
lstrip_blocks=False,
undefined=StrictUndefined,
autoescape=select_autoescape(),
)
env.filters["resource_filter"] = (
lambda _apps, _id, _key, _svc, default, **_k: default
)
self.asked = []
def _lookup(name, *terms, **_kwargs):
self.asked.append((name, terms))
return gpu if name == "gpu" else {}
env.globals["lookup"] = _lookup
return env.get_template("resource.yml.j2").render(BASE)
def test_the_lookups_yes_reaches_the_runtime_key(self):
self.assertIn("runtime: nvidia", self._render(gpu=True))
def test_the_lookups_no_leaves_the_runtime_key_out(self):
self.assertNotIn("runtime:", self._render(gpu=False))
def test_the_lookup_is_asked_for_this_service(self):
self._render(gpu=True)
self.assertIn(
("gpu", ("web-svc-libretranslate", "libretranslate")),
self.asked,
)
if __name__ == "__main__":
unittest.main()