Files
computer-playbook/tests/unit/python/utils/docker/test_mirror.py
T
kevinveenbirkenbachandClaude Opus 5 575a5a87ac refactor(layout): sort shipped scripts by language and split the unit suite
Every script a role ships now lives under `files/<language>/`, and the unit
suite is split the same way: `tests/unit/python/` beside new sibling trees for
the other languages. `tests/lint/filesystem/test_role_files_layout.py` keeps it
that way; directories whose shape is dictated from outside carry a `.nocheck`
naming the constraint (Docker build contexts, WordPress mu-plugins, the Joomla
extension, Odoo addons, Playwright suites, the `files/test/test.sh` harness
that `test-e2e-cli` resolves generically).

This cannot be applied in halves. Moving a file and repointing what names it
are the same change: 14 references resolve only at deploy time, so the tree is
green either before or after, never between.

Those 14 were found by static resolution over every reference class, not by the
test suite -- a full `make quality-high` was green while all of them were
broken:

  * 7 `src:` values built from a role variable holding a bare filename
    (svc-bkp-remote-2-local, decidim, espocrm, friendica, joomla, pixelfed x2).
    The variable doubles as the destination name inside the container, so the
    language segment belongs at the call site, not in the variable.
  * `sys-service/tasks/04_base.yml` resolved every consumer's systemd script
    through `first_found(..., errors='strict')` over `files/script.{sh,py}`.
    18 roles ship exactly that and have no template fallback: the play would
    have died on each.
  * `sys-front-inj-javascript` fell back to `<role>/files/javascript.js`; the
    interface now reads `files/javascript/javascript.js`.
  * `sys-front-inj-logout/templates/head_sub.j2` built the cache-busting mtime
    path through `path_absolute(... 'files', ...)`.
  * 4 `ansible.builtin.script:` invocations naming a bare command
    (pull.py, prune.sh, set_postgres_superuser_password.sh, ganesha-serve.sh).
  * `dev-python/files/shell/install.sh` pointed at a sibling role's script.

Depth-dependent paths inside the moved trees were bumped accordingly: 59
`parents[N]` computations, 12 relative climbs, and the per-role recover CLI,
whose path `cli/administration/recover/recoverers.py` still assembled from the
pre-move location.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-22 01:31:09 +02:00

151 lines
5.1 KiB
Python

from __future__ import annotations
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path
from utils.docker import mirror
_CONFIG_WITH_FORK = """\
[core]
\trepositoryformatversion = 0
[remote "origin"]
\turl = git@github.com:upstream-org/core.git
[remote "fork"]
\turl = https://github.com/a-user/a-repo.git
[remote]
\tpushDefault = fork
[branch "main"]
\tvscode-merge-base = origin/main
\tvscode-merge-base = fork/main
"""
_CONFIG_ORIGIN_ONLY = """\
[remote "origin"]
\turl = git@github.com:upstream-org/core.git
"""
class _RepoConfig(unittest.TestCase):
def _with_config(self, body: str | None):
root = tempfile.TemporaryDirectory()
self.addCleanup(root.cleanup)
if body is not None:
git_dir = Path(root.name) / ".git"
git_dir.mkdir()
(git_dir / "config").write_text(body, encoding="utf-8")
return mock.patch.object(mirror, "PROJECT_ROOT", Path(root.name))
class TestRemoteUrl(_RepoConfig, unittest.TestCase):
def test_push_default_wins_over_origin(self) -> None:
with self._with_config(_CONFIG_WITH_FORK):
self.assertEqual(
mirror._remote_url(), "https://github.com/a-user/a-repo.git"
)
def test_origin_is_used_without_a_push_default(self) -> None:
with self._with_config(_CONFIG_ORIGIN_ONLY):
self.assertEqual(
mirror._remote_url(), "git@github.com:upstream-org/core.git"
)
def test_duplicate_keys_do_not_break_parsing(self) -> None:
with self._with_config(_CONFIG_WITH_FORK):
self.assertIn("branch.main.vscode-merge-base", mirror._git_config())
def test_missing_config_yields_no_url(self) -> None:
with self._with_config(None):
self.assertIsNone(mirror._remote_url())
class TestOwnerAndRepository(_RepoConfig, unittest.TestCase):
def test_github_environment_wins(self) -> None:
with (
self._with_config(_CONFIG_WITH_FORK),
mock.patch.dict(
mirror.os.environ,
{"GITHUB_REPOSITORY": "Env-Owner/Env-Repo"},
clear=True,
),
):
self.assertEqual(mirror._owner_and_repository(), ("env-owner", "env-repo"))
def test_repository_owner_wins_over_the_slug(self) -> None:
with (
self._with_config(_CONFIG_WITH_FORK),
mock.patch.dict(
mirror.os.environ,
{
"GITHUB_REPOSITORY": "Env-Owner/Env-Repo",
"GITHUB_REPOSITORY_OWNER": "Explicit-Owner",
},
clear=True,
),
):
self.assertEqual(
mirror._owner_and_repository(), ("explicit-owner", "env-repo")
)
def test_ssh_remote_is_parsed(self) -> None:
with (
self._with_config(_CONFIG_ORIGIN_ONLY),
mock.patch.dict(mirror.os.environ, {}, clear=True),
):
self.assertEqual(mirror._owner_and_repository(), ("upstream-org", "core"))
def test_https_remote_is_parsed(self) -> None:
with (
self._with_config(_CONFIG_WITH_FORK),
mock.patch.dict(mirror.os.environ, {}, clear=True),
):
self.assertEqual(mirror._owner_and_repository(), ("a-user", "a-repo"))
class TestMirrorImage(_RepoConfig, unittest.TestCase):
def _env(self, **overrides):
env = {"INFINITO_GHCR_MIRROR_PREFIX": "mirror"}
env.update(overrides)
return mock.patch.dict(mirror.os.environ, env, clear=True)
def test_implicit_docker_hub_image(self) -> None:
with self._with_config(_CONFIG_WITH_FORK), self._env():
self.assertEqual(
mirror.mirror_image("postgres"),
"ghcr.io/a-user/a-repo/mirror/docker.io/postgres",
)
def test_explicit_registry_is_kept_in_the_path(self) -> None:
with self._with_config(_CONFIG_WITH_FORK), self._env():
self.assertEqual(
mirror.mirror_image("quay.io/keycloak/keycloak"),
"ghcr.io/a-user/a-repo/mirror/quay.io/keycloak/keycloak",
)
def test_docker_hub_aliases_normalise(self) -> None:
with self._with_config(_CONFIG_WITH_FORK), self._env():
self.assertEqual(
mirror.mirror_image("docker.io/library/nginx"),
"ghcr.io/a-user/a-repo/mirror/docker.io/library/nginx",
)
def test_no_prefix_means_no_mirror(self) -> None:
with (
self._with_config(_CONFIG_WITH_FORK),
self._env(INFINITO_GHCR_MIRROR_PREFIX=""),
):
self.assertIsNone(mirror.mirror_image("postgres"))
def test_unresolvable_namespace_means_no_mirror(self) -> None:
with self._with_config(None), self._env():
self.assertIsNone(mirror.mirror_image("postgres"))
def test_malformed_image_means_no_mirror(self) -> None:
with self._with_config(_CONFIG_WITH_FORK), self._env():
self.assertIsNone(mirror.mirror_image("Not A Valid Image"))
if __name__ == "__main__":
unittest.main()