diff --git a/roles/svc-cache-package/meta/services.yml b/roles/svc-cache-package/meta/services.yml index ea71b5814f..590f432178 100644 --- a/roles/svc-cache-package/meta/services.yml +++ b/roles/svc-cache-package/meta/services.yml @@ -77,8 +77,6 @@ frontend: image: nginx version: 1.31-alpine name: package-cache-frontend - # nocheck: dynamic-flag the throwaway container that issues the CA and leaf certs before nginx starts, not a deployed service - init_image: alpine:latest min_storage: 64MB cpus: "0.5" mem_reservation: 64m diff --git a/roles/svc-cache-package/vars/main.yml b/roles/svc-cache-package/vars/main.yml index cdd51cbbd9..980a17e774 100644 --- a/roles/svc-cache-package/vars/main.yml +++ b/roles/svc-cache-package/vars/main.yml @@ -19,7 +19,7 @@ CACHE_PACKAGE_UPSTREAMS: "{{ lookup('cache_upstreams') }}" CACHE_PACKAGE_REPOS: "{{ lookup('cache_repos') }}" CACHE_PACKAGE_UPSTREAM_RECORDS: "{{ lookup('cache_records') }}" -CACHE_PACKAGE_INIT_IMAGE: "{{ lookup('config', application_id, 'services.frontend.init_image') }}" +CACHE_PACKAGE_INIT_IMAGE: "{{ lookup('image', application_id, 'frontend') }}" CACHE_PACKAGE_BLOBSTORE_MAX: "{{ lookup('config', application_id, 'services.package.blobstore_max') }}" CACHE_PACKAGE_MAX_AGE_MIN: "{{ lookup('config', application_id, 'services.package.content_max_age_min') }}" CACHE_PACKAGE_METADATA_MAX_AGE_MIN: "{{ lookup('config', application_id, 'services.package.metadata_max_age_min') }}" diff --git a/tests/unit/python/roles/svc-cache-package/__init__.py b/tests/unit/python/roles/svc-cache-package/__init__.py new file mode 100644 index 0000000000..aa7a30d2e5 --- /dev/null +++ b/tests/unit/python/roles/svc-cache-package/__init__.py @@ -0,0 +1,5 @@ +from pathlib import Path + +PROJECT_ROOT: Path = Path(__file__).resolve().parents[5] + +__all__ = ["PROJECT_ROOT"] diff --git a/tests/unit/python/roles/svc-cache-package/test_cert_issuer_image.py b/tests/unit/python/roles/svc-cache-package/test_cert_issuer_image.py new file mode 100644 index 0000000000..69fce853e5 --- /dev/null +++ b/tests/unit/python/roles/svc-cache-package/test_cert_issuer_image.py @@ -0,0 +1,77 @@ +from __future__ import annotations + +import unittest +import unittest.mock as mock +from typing import Any + +from jinja2 import Environment, StrictUndefined, select_autoescape + +from plugins.lookup.image import LookupModule as ImageLookup +from utils.cache.files import read_text +from utils.cache.yaml import load_yaml_str +from utils.docker.image.discovery import canonical_image_name, iter_role_images +from utils.roles.mapping import ROLE_FILE_META_SERVICES, ROLE_FILE_VARS_MAIN + +from . import PROJECT_ROOT + +APP = "svc-cache-package" +ROLE = PROJECT_ROOT / "roles" / APP +APPLICATIONS = { + APP: {"services": load_yaml_str(read_text(str(ROLE / ROLE_FILE_META_SERVICES)))} +} +INIT_IMAGE = load_yaml_str(read_text(str(ROLE / ROLE_FILE_VARS_MAIN)))[ + "CACHE_PACKAGE_INIT_IMAGE" +] + + +def _lookup(name: str, *terms: str, **kwargs: Any) -> Any: + """Resolve the lookups the role's vars use against its own services map. + + Args: + name: lookup plugin name, ``image`` or ``config``. + terms: the lookup's positional terms. + kwargs: forwarded to the ``image`` lookup. + """ + if name == "image": + with mock.patch("plugins.lookup.image.lookup_loader") as loader: + loader.get.return_value = mock.MagicMock( + run=lambda *_a, **_k: [APPLICATIONS] + ) + return ImageLookup().run( + list(terms), variables={"DEPLOYMENT_MODE": "compose"}, **kwargs + )[0] + if name == "config": + application_id, path = terms + value: Any = APPLICATIONS[application_id] + for key in path.split("."): + value = value[key] + return value + raise AssertionError(f"unexpected lookup '{name}'") + + +def _issuer_image() -> str: + env = Environment( + autoescape=select_autoescape(default_for_string=False), + undefined=StrictUndefined, + ) + return env.from_string(INIT_IMAGE).render(application_id=APP, lookup=_lookup) + + +class TestCertIssuerImage(unittest.TestCase): + def test_the_issuer_image_is_one_the_image_discovery_yields(self) -> None: + discovered = { + (ref.name, ref.version) + for ref in iter_role_images(PROJECT_ROOT) + if ref.role == APP + } + name, _, version = _issuer_image().rpartition(":") + self.assertIn((canonical_image_name(name), version), discovered) + + def test_the_issuer_reuses_the_frontend_image_so_it_pulls_nothing_extra( + self, + ) -> None: + self.assertEqual(_lookup("image", APP, "frontend"), _issuer_image()) + + +if __name__ == "__main__": + unittest.main()