Files
computer-playbook/tests/unit/python/cli/contributing/mirror/test_sync.py
T
kevinveenbirkenbachandClaude Opus 5.5 20fae00bdf refactor(cli): import ImageRef from its module instead of the mirror shim
py/unused-import on PR #625 (mirror/model.py:10): the module only re-imported ImageRef from utils.docker.image.discovery. providers.py and the four mirror tests import it from there, as wait/__main__.py already did; no other importer exists.

🎯 Fix: 97 %
💥 Break: 2 %
💸 Cost: –
⏱️ Time: –
🛡️ Security: ~100 %
🏆 Score: 95
🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 %

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 17:07:20 +00:00

122 lines
3.9 KiB
Python

from __future__ import annotations
import subprocess
import unittest
from unittest.mock import patch
import cli.contributing.mirror.sync.__main__ as sync_main
from utils.docker.image.discovery import ImageRef
class TestMirrorSync(unittest.TestCase):
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",
)
def _run_only_missing(self, *, stale: bool, mirror_error=None):
with (
patch(
"cli.contributing.mirror.sync.__main__.iter_role_images",
return_value=[self.IMAGE],
),
patch(
"cli.contributing.mirror.sync.__main__.iter_runner_images",
return_value=[],
),
patch.object(
sync_main.GHCRProvider,
"image_base",
return_value="ghcr.io/acme/myrepo/mirror/docker.io/nextcloud",
),
patch.object(
sync_main.GHCRProvider, "tag_exists", return_value=True
) as mock_tag_exists,
patch.object(
sync_main.GHCRProvider, "is_stale", return_value=stale
) as mock_is_stale,
patch.object(
sync_main.GHCRProvider, "mirror", side_effect=mirror_error
) as mock_mirror,
patch(
"sys.argv",
[
"mirror-sync",
"--ghcr-namespace",
"acme",
"--ghcr-repository",
"myrepo",
"--only-missing",
],
),
):
result = sync_main.main()
mock_tag_exists.assert_called_once_with(self.IMAGE)
mock_is_stale.assert_called_once_with(self.IMAGE)
return result, mock_mirror
def test_only_missing_skips_current_destination(self) -> None:
result, mock_mirror = self._run_only_missing(stale=False)
self.assertEqual(result, 0)
mock_mirror.assert_not_called()
def test_only_missing_refreshes_a_stale_destination(self) -> None:
result, mock_mirror = self._run_only_missing(stale=True)
self.assertEqual(result, 0)
mock_mirror.assert_called_once_with(self.IMAGE)
def test_failed_refresh_keeps_the_existing_copy_without_failing(self) -> None:
error = subprocess.CalledProcessError(1, ["skopeo", "copy"])
result, mock_mirror = self._run_only_missing(stale=True, mirror_error=error)
self.assertEqual(result, 0)
mock_mirror.assert_called_once_with(self.IMAGE)
def test_runner_images_are_mirrored_with_the_role_images(self) -> None:
image = ImageRef(
role="runner",
service="coredns",
name="coredns/coredns",
version="1.14.4",
source="docker.io/coredns/coredns:1.14.4",
registry="docker.io",
source_file="compose.yml",
)
with (
patch(
"cli.contributing.mirror.sync.__main__.iter_role_images",
return_value=[],
),
patch(
"cli.contributing.mirror.sync.__main__.iter_runner_images",
return_value=[image],
),
patch.object(sync_main.GHCRProvider, "mirror") as mock_mirror,
patch(
"sys.argv",
[
"mirror-sync",
"--ghcr-namespace",
"acme",
"--ghcr-repository",
"myrepo",
],
),
):
result = sync_main.main()
self.assertEqual(result, 0)
mock_mirror.assert_called_once_with(image)
if __name__ == "__main__": # pragma: no cover
unittest.main()