mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-29 06:17:25 +00:00
The cache renderer and twelve unit tests built their Jinja2 environments with autoescape=False (CodeQL py/jinja2/autoescape-false, alert 958 and fifteen test alerts) behind `# noqa: S701` markers. They now use select_autoescape(default_for_string=False), the form the repository already uses in over forty other modules. Every template the cache renderer loads ends in .j2, so nothing is escaped: its output stays byte-identical for all five distros, also with markup characters injected into every variable, while a control run with autoescape=True differs in exactly those injected cases. 🎯 Fix: 95 % 💥 Break: 2 % 💸 Cost: – ⏱️ Time: – 🛡️ Security: 100 % 🏆 Score: 93 🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
"""The gpu gate of web-svc-libretranslate.
|
|
|
|
`lookup('config', ...)` returned false for the same declaration the service
|
|
carries, which silently shipped a CPU image twice. The image tag and the
|
|
assert task both hang off this one expression, so what it asks and which
|
|
service key it names is what this guards. Whether a declared flag and a
|
|
present device add up to a yes belongs to the ``gpu`` lookup's own tests.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
from pathlib import Path
|
|
from typing import ClassVar
|
|
|
|
from jinja2 import Environment, StrictUndefined, select_autoescape
|
|
|
|
from utils import PROJECT_ROOT
|
|
from utils.cache.yaml import load_yaml
|
|
from utils.roles.mapping import ROLE_FILE_META_SERVICES, ROLE_FILE_VARS_MAIN
|
|
|
|
ROLE = "web-svc-libretranslate"
|
|
SERVICE = "libretranslate"
|
|
ROLE_DIR = Path(PROJECT_ROOT) / "roles" / ROLE
|
|
VARS = ROLE_DIR / ROLE_FILE_VARS_MAIN
|
|
SERVICES = ROLE_DIR / ROLE_FILE_META_SERVICES
|
|
|
|
|
|
def _ansible_bool(value) -> bool:
|
|
if isinstance(value, str):
|
|
return value.strip().lower() in {"y", "yes", "true", "on", "1"}
|
|
return bool(value)
|
|
|
|
|
|
class TestGpuGate(unittest.TestCase):
|
|
services: ClassVar[dict] = load_yaml(str(SERVICES))
|
|
|
|
def _render(self, expression: str, answer) -> tuple[str, list]:
|
|
"""Render ``expression`` against a recording ``lookup``.
|
|
|
|
Args:
|
|
expression: the var expression to render.
|
|
answer: what the ``gpu`` lookup answers.
|
|
|
|
Returns:
|
|
The rendered string and the ``(name, terms)`` of every lookup.
|
|
"""
|
|
asked: list = []
|
|
env = Environment(
|
|
undefined=StrictUndefined,
|
|
autoescape=select_autoescape(default_for_string=False),
|
|
)
|
|
env.filters["bool"] = _ansible_bool
|
|
|
|
def _lookup(name, *terms, **_kwargs):
|
|
asked.append((name, terms))
|
|
return answer if name == "gpu" else {}
|
|
|
|
env.globals["lookup"] = _lookup
|
|
return env.from_string(expression).render(application_id=ROLE), asked
|
|
|
|
def test_the_role_declares_the_gpu_flag(self) -> None:
|
|
self.assertTrue(self.services[SERVICE].get("gpu"))
|
|
|
|
def test_the_gate_asks_the_gpu_lookup_for_this_service(self) -> None:
|
|
expression = load_yaml(str(VARS))["LIBRETRANSLATE_GPU"]
|
|
|
|
_rendered, asked = self._render(expression, True)
|
|
|
|
self.assertIn(("gpu", (ROLE, SERVICE)), asked)
|
|
|
|
def test_a_yes_from_the_lookup_opens_the_gate(self) -> None:
|
|
expression = load_yaml(str(VARS))["LIBRETRANSLATE_GPU"]
|
|
|
|
rendered, _asked = self._render(expression, True)
|
|
|
|
self.assertTrue(_ansible_bool(rendered))
|
|
|
|
def test_a_no_from_the_lookup_keeps_the_gate_closed(self) -> None:
|
|
expression = load_yaml(str(VARS))["LIBRETRANSLATE_GPU"]
|
|
|
|
rendered, _asked = self._render(expression, False)
|
|
|
|
self.assertFalse(_ansible_bool(rendered))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|