mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-28 22:06:31 +00:00
test_render.py imported unittest both as a module and through from-import, which tests/lint/repository/python/test_import_and_import_from.py rejects; quality-high on main 2f1fc493cd failed test-lint on it. The test now imports unittest.mock as mock.
🎯 Fix: 97 %
💥 Break: 1 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 96
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %
Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
4.4 KiB
Python
131 lines
4.4 KiB
Python
"""Unit tests for writing the generated package-cache files.
|
|
|
|
The shape measured in a gate run: a container had rendered
|
|
compose/package-cache/pip.conf under its own uid, and the next host-side
|
|
render died on it with PermissionError while the directory stayed writable.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import stat
|
|
import tempfile
|
|
import unittest
|
|
import unittest.mock as mock
|
|
from pathlib import Path
|
|
|
|
from utils.cache.hosts import host_records
|
|
from utils.cache.render import _write, upstreams_conf
|
|
|
|
OLLAMA_HOST = "registry.ollama.ai"
|
|
OLLAMA_REPO = "raw-ollama-registry"
|
|
|
|
|
|
class TestWrite(unittest.TestCase):
|
|
"""A file the renderer cannot overwrite is replaced, not given up on."""
|
|
|
|
def test_an_unwritable_file_is_replaced(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
target = Path(directory, "pip.conf")
|
|
target.write_text("stale", encoding="utf-8")
|
|
target.chmod(stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
|
|
|
|
_write(target, "fresh")
|
|
|
|
self.assertEqual(
|
|
target.read_text(
|
|
encoding="utf-8"
|
|
), # nocheck: cache-read tempdir fixture rewritten in this test
|
|
"fresh",
|
|
)
|
|
|
|
def test_a_writable_file_keeps_its_inode(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
target = Path(directory, "arch.list")
|
|
target.write_text("stale", encoding="utf-8")
|
|
before = target.stat().st_ino
|
|
|
|
_write(target, "fresh")
|
|
|
|
self.assertEqual(
|
|
target.read_text(
|
|
encoding="utf-8"
|
|
), # nocheck: cache-read tempdir fixture rewritten in this test
|
|
"fresh",
|
|
)
|
|
self.assertEqual(target.stat().st_ino, before)
|
|
|
|
def test_a_missing_parent_is_created(self) -> None:
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
target = Path(directory, "apt", "debian.list")
|
|
|
|
_write(target, "fresh")
|
|
|
|
self.assertEqual(
|
|
target.read_text(
|
|
encoding="utf-8"
|
|
), # nocheck: cache-read tempdir fixture rewritten in this test
|
|
"fresh",
|
|
)
|
|
|
|
|
|
class TestOllamaRegistry(unittest.TestCase):
|
|
def test_the_probe_requests_a_path_under_v2(self) -> None:
|
|
self.assertEqual(
|
|
host_records()[OLLAMA_HOST]["routes"],
|
|
[{"path": "/v2/", "repo": OLLAMA_REPO}],
|
|
f"{OLLAMA_HOST} answers 503 on its bare root, so the package-cache "
|
|
"probe must request a path under /v2/, which it serves",
|
|
)
|
|
|
|
def test_the_tls_frontend_still_proxies_the_whole_host(self) -> None:
|
|
block = next(
|
|
block
|
|
for block in upstreams_conf().split("server {")
|
|
if f"server_name {OLLAMA_HOST};" in block
|
|
)
|
|
|
|
self.assertIn("location / {", block)
|
|
self.assertIn(f"/repository/{OLLAMA_REPO}/;", block)
|
|
self.assertNotIn("location /v2/", block)
|
|
|
|
|
|
def _servers(http2: bool | str) -> tuple[list[str], list[str]]:
|
|
"""Return the TLS and the plain server blocks of the rendered frontend.
|
|
|
|
Args:
|
|
http2: the value ``WEBSERVER_HTTP2_ENABLED`` is rendered with.
|
|
"""
|
|
with mock.patch("utils.cache.render._webserver_http2", return_value=http2):
|
|
blocks = upstreams_conf().split("server {")[1:]
|
|
tls = [block for block in blocks if " ssl;" in block]
|
|
return tls, [block for block in blocks if " ssl;" not in block]
|
|
|
|
|
|
class TestHttp2(unittest.TestCase):
|
|
def test_no_listener_serves_http2_while_the_switch_is_off(self) -> None:
|
|
tls, plain = _servers(False)
|
|
|
|
self.assertTrue(tls)
|
|
self.assertFalse([block for block in tls + plain if "http2" in block])
|
|
|
|
def test_every_tls_listener_serves_http2_once_the_switch_is_on(self) -> None:
|
|
tls, _plain = _servers(True)
|
|
|
|
self.assertTrue(tls)
|
|
self.assertEqual([block for block in tls if "\n\thttp2 on;\n" not in block], [])
|
|
|
|
def test_a_string_false_from_extra_vars_keeps_http2_off(self) -> None:
|
|
tls, _plain = _servers("false")
|
|
|
|
self.assertFalse([block for block in tls if "http2" in block])
|
|
|
|
def test_a_plain_listener_never_serves_http2(self) -> None:
|
|
_tls, plain = _servers(True)
|
|
|
|
self.assertTrue(plain)
|
|
self.assertFalse([block for block in plain if "http2" in block])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|