mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-11 13:17:07 +00:00
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>
124 lines
4.1 KiB
Python
124 lines
4.1 KiB
Python
from __future__ import annotations
|
|
|
|
import textwrap
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from utils.docker.image.discovery import iter_role_images
|
|
|
|
from utils.cache.yaml import load_yaml_str
|
|
|
|
|
|
def _make_fs(files: dict[str, str]) -> dict[Path, str]:
|
|
return {Path(k): textwrap.dedent(v) for k, v in files.items()}
|
|
|
|
|
|
class TestIterRoleImagesVarsImages(unittest.TestCase):
|
|
"""iter_role_images reads defaults/main.yml → images and meta/services.yml → services (any registry)."""
|
|
|
|
def _run(self, fs: dict[str, str]) -> list:
|
|
real_fs = _make_fs(fs)
|
|
|
|
def fake_glob(self, pattern: str):
|
|
return [p for p in real_fs if p.match(pattern)]
|
|
|
|
def fake_load(path: Path) -> dict:
|
|
|
|
content = real_fs.get(path, "")
|
|
return load_yaml_str(content) or {}
|
|
|
|
with (
|
|
patch.object(Path, "glob", fake_glob),
|
|
patch("utils.docker.image.discovery.load_yaml", side_effect=fake_load),
|
|
):
|
|
return list(iter_role_images(Path("/repo")))
|
|
|
|
def test_mcr_playwright_image(self):
|
|
refs = self._run(
|
|
{
|
|
"roles/test-e2e-playwright/defaults/main.yml": """
|
|
images:
|
|
playwright:
|
|
image: mcr.microsoft.com/playwright
|
|
version: v1.58.2-noble
|
|
""",
|
|
}
|
|
)
|
|
self.assertEqual(len(refs), 1)
|
|
ref = refs[0]
|
|
self.assertEqual(ref.role, "test-e2e-playwright")
|
|
self.assertEqual(ref.service, "playwright")
|
|
self.assertEqual(ref.name, "playwright")
|
|
self.assertEqual(ref.version, "v1.58.2-noble")
|
|
self.assertEqual(ref.registry, "mcr.microsoft.com")
|
|
self.assertEqual(ref.source, "mcr.microsoft.com/playwright:v1.58.2-noble")
|
|
self.assertEqual(ref.source_file, "defaults/main.yml")
|
|
|
|
def test_ghcr_image_strips_registry_from_name(self):
|
|
refs = self._run(
|
|
{
|
|
"roles/web-app-matrix/defaults/main.yml": """
|
|
images:
|
|
matrix-chatgpt-bot:
|
|
image: ghcr.io/matrixgpt/matrix-chatgpt-bot
|
|
version: latest
|
|
""",
|
|
}
|
|
)
|
|
self.assertEqual(len(refs), 1)
|
|
ref = refs[0]
|
|
self.assertEqual(ref.name, "matrixgpt/matrix-chatgpt-bot")
|
|
self.assertEqual(ref.registry, "ghcr.io")
|
|
self.assertEqual(ref.source, "ghcr.io/matrixgpt/matrix-chatgpt-bot:latest")
|
|
self.assertEqual(ref.source_file, "defaults/main.yml")
|
|
|
|
def test_missing_image_or_version_is_skipped(self):
|
|
refs = self._run(
|
|
{
|
|
"roles/some-role/defaults/main.yml": """
|
|
images:
|
|
no-version:
|
|
image: ghcr.io/foo/bar
|
|
no-image:
|
|
version: "1.0"
|
|
""",
|
|
}
|
|
)
|
|
self.assertEqual(refs, [])
|
|
|
|
def test_no_images_key_yields_nothing(self):
|
|
refs = self._run(
|
|
{
|
|
"roles/some-role/defaults/main.yml": """
|
|
some_var: value
|
|
""",
|
|
}
|
|
)
|
|
self.assertEqual(refs, [])
|
|
|
|
def test_config_main_yml_ghcr_image(self):
|
|
# Per req-008 the file root IS the services map (no compose envelope)
|
|
# and source_file reports the new path.
|
|
refs = self._run(
|
|
{
|
|
"roles/web-app-matrix/meta/services.yml": """
|
|
matrix-chatgpt-bot:
|
|
image: ghcr.io/matrixgpt/matrix-chatgpt-bot
|
|
version: latest
|
|
""",
|
|
}
|
|
)
|
|
self.assertEqual(len(refs), 1)
|
|
ref = refs[0]
|
|
self.assertEqual(ref.role, "web-app-matrix")
|
|
self.assertEqual(ref.service, "matrix-chatgpt-bot")
|
|
self.assertEqual(ref.name, "matrixgpt/matrix-chatgpt-bot")
|
|
self.assertEqual(ref.registry, "ghcr.io")
|
|
self.assertEqual(ref.source, "ghcr.io/matrixgpt/matrix-chatgpt-bot:latest")
|
|
self.assertEqual(ref.source_file, "meta/services.yml")
|
|
|
|
|
|
if __name__ == "__main__": # pragma: no cover
|
|
unittest.main()
|