Files
computer-playbook/tests/unit/cli/mirror/test_resolver.py
T
kevinveenbirkenbachandClaude Opus 4.7 623035618c refactor(cache): route every yaml call in tests/ through utils.cache.yaml
Stops every direct `yaml.safe_load`/`safe_load_all`/`safe_dump`/`dump`
call across the test corpus from re-parsing files the production cache
already holds. Each migrated test loads role meta, group_vars, inventory
fixtures, etc. through the shared cache, so a single `make test` run
parses every YAML file at most once across all suites.

Cache module changes (utils/cache/yaml.py):
  - Unify single- and multi-doc cache: one `_CACHE: Dict[sig, list]`
    holding the `safe_load_all` payload. Single-doc helpers
    (load_yaml / load_yaml_any) unwrap docs[0] and raise on >1
    document, preserving the old strict semantics. A file consumed
    both as single-doc and multi-doc is now parsed exactly once.
  - Add load_yaml_all (path-cached) and load_yaml_all_str (string,
    no cache) so multi-doc consumers no longer need a direct
    `yaml.safe_load_all` plus a separate file read.
  - dump_yaml now accepts any safe-dumpable structure (dict, list,
    scalar) so test fixtures writing list-of-tasks YAML can use it.

Lint guard (tests/lint/repository/test_no_direct_yaml_calls.py):
  - Extend the scanned tree to include `tests/` so future test code
    is forced through the cache, with the existing
    `# noqa: direct-yaml` per-call opt-out for genuine raw-yaml
    fixture needs.

Test corpus:
  - 89 test files migrated to load_yaml / load_yaml_any /
    load_yaml_str / load_yaml_all / load_yaml_all_str / dump_yaml /
    dump_yaml_str. No `# noqa` shortcuts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 12:16:20 +02:00

122 lines
4.0 KiB
Python

from __future__ import annotations
import io
import unittest
from unittest.mock import patch
from utils.cache.yaml import load_yaml_str
import cli.mirror.resolver.__main__ as resolver_main
from cli.mirror.model import ImageRef
class TestResolverOutputFormat(unittest.TestCase):
"""resolver emits per-role services map and a separate images key."""
def _run_resolver(
self, images: list[ImageRef], extra_argv: list[str] | None = None
) -> dict:
argv = [
"resolver",
"--ghcr-namespace",
"acme",
"--ghcr-repository",
"myrepo",
]
if extra_argv:
argv += extra_argv
buf = io.StringIO()
with (
patch("cli.mirror.resolver.__main__.iter_role_images", return_value=images),
patch("sys.argv", argv),
patch("sys.stdout", buf),
):
result = resolver_main.main()
self.assertEqual(result, 0)
return load_yaml_str(buf.getvalue()) or {}
def test_compose_images_go_to_applications_key(self) -> None:
image = ImageRef(
role="web-app-nextcloud",
service="app",
name="nextcloud",
version="31.0.0",
source="docker.io/library/nextcloud:31.0.0",
registry="docker.io",
source_file="meta/services.yml",
)
out = self._run_resolver([image])
self.assertIn("applications", out)
self.assertIn("images", out)
svc = out["applications"]["web-app-nextcloud"]["services"]["app"]
self.assertEqual(svc["version"], "31.0.0")
self.assertIn("ghcr.io/acme/myrepo/mirror/docker.io/nextcloud", svc["image"])
self.assertEqual(out["images"], {})
def test_defaults_images_go_to_images_key(self) -> None:
image = ImageRef(
role="test-e2e-playwright",
service="playwright",
name="playwright",
version="v1.58.2-noble",
source="mcr.microsoft.com/playwright:v1.58.2-noble",
registry="mcr.microsoft.com",
source_file="defaults/main.yml",
)
out = self._run_resolver([image])
self.assertEqual(out["applications"], {})
svc = out["images"]["test-e2e-playwright"]["playwright"]
self.assertEqual(svc["version"], "v1.58.2-noble")
self.assertIn(
"ghcr.io/acme/myrepo/mirror/mcr.microsoft.com/playwright",
svc["image"],
)
def test_multiple_services_same_role_stay_grouped_per_source_type(self) -> None:
images = [
ImageRef(
role="web-app-nextcloud",
service="app",
name="nextcloud",
version="31.0.0",
source="docker.io/library/nextcloud:31.0.0",
registry="docker.io",
source_file="meta/services.yml",
),
ImageRef(
role="web-app-nextcloud",
service="proxy",
name="nginx",
version="alpine",
source="docker.io/library/nginx:alpine",
registry="docker.io",
source_file="meta/services.yml",
),
ImageRef(
role="web-app-nextcloud",
service="backup",
name="restic",
version="latest",
source="docker.io/restic/restic:latest",
registry="docker.io",
source_file="defaults/main.yml",
),
]
out = self._run_resolver(images)
self.assertIn("app", out["applications"]["web-app-nextcloud"]["services"])
self.assertIn("proxy", out["applications"]["web-app-nextcloud"]["services"])
self.assertIn("backup", out["images"]["web-app-nextcloud"])
def test_empty_images_yield_both_empty_top_level_keys(self) -> None:
out = self._run_resolver([])
self.assertEqual(out["applications"], {})
self.assertEqual(out["images"], {})
if __name__ == "__main__": # pragma: no cover
unittest.main()