Files
computer-playbook/tests/unit/cli/mirror/test_util.py
T
Environment TestandClaude Sonnet 4.6 9ea2ad6ff1 refactor(docker): introduce utils/docker/image_discovery as single SPOT for image enumeration
- Move iter_role_images + all helpers + ImageRef from cli/mirror/util.py and
  cli/mirror/model.py into utils/docker/image_discovery.py; add source_file field
  to ImageRef to distinguish config/main.yml vs vars/main.yml entries
- Delete cli/mirror/util.py; update all callers to import from utils.docker.image_discovery
- Move utils into subpackages: utils/docker/ (image_ref, paths_utils, hadolint) and
  utils/roles/ (dependency_resolver, resource_validation); update all imports
- Reorganise tests into subpackages: tests/lint/docker/dockerfile/, tests/integration/docker/,
  tests/unit/utils/docker/, tests/unit/utils/roles/; fix parents[N] offsets after relocation
- tests/lint/docker/test_image_versions.py now uses iter_role_images (same SPOT as mirror)
- Add vars/main.yml images.<name>.{image,version} support for non-application roles
  (test-e2e-playwright, sys-ctl-hlth-csp); eliminate hardcoded duplicates in defaults/tasks
- Add web-app-matrix matrix-chatgpt-bot and web-app-mini-qr mini-qr entries in config/main.yml;
  replace hardcoded image strings in vars/templates with lookup('config', ...) references
- Fix sync/__main__.py log line to use correct registry prefix for non-Docker-Hub images
- Update docs/contributing/code/role/dockerfile.md with image declaration rules and SPOT reference

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-09 19:32:31 +02:00

120 lines
3.8 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
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 vars/main.yml → images and config/main.yml → compose.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:
import yaml
content = real_fs.get(path, "")
return yaml.safe_load(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/vars/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")
def test_ghcr_image_strips_registry_from_name(self):
refs = self._run(
{
"roles/web-app-matrix/vars/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")
def test_missing_image_or_version_is_skipped(self):
refs = self._run(
{
"roles/some-role/vars/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/vars/main.yml": """
some_var: value
""",
}
)
self.assertEqual(refs, [])
def test_config_main_yml_ghcr_image(self):
refs = self._run(
{
"roles/web-app-matrix/config/main.yml": """
compose:
services:
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")
if __name__ == "__main__": # pragma: no cover
unittest.main()