|
| 1 | +from argparse import Namespace |
| 2 | +from unittest import IsolatedAsyncioTestCase |
| 3 | +from unittest.mock import AsyncMock |
| 4 | +from unittest.mock import Mock |
| 5 | +from unittest.mock import call |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +from parameterized import parameterized |
| 9 | + |
| 10 | +from podman_compose import PullImage |
| 11 | + |
| 12 | + |
| 13 | +class TestPullImage(IsolatedAsyncioTestCase): |
| 14 | + def test_unsupported_policy_fallback_to_missing(self) -> None: |
| 15 | + pull_image = PullImage("localhost/test:1", policy="unsupported") |
| 16 | + assert pull_image.policy == "missing" |
| 17 | + |
| 18 | + def test_update_policy(self) -> None: |
| 19 | + pull_image = PullImage("localhost/test:1", policy="never") |
| 20 | + assert pull_image.policy == "never" |
| 21 | + |
| 22 | + # not supported policy |
| 23 | + pull_image.update_policy("unsupported") |
| 24 | + assert pull_image.policy == "never" |
| 25 | + |
| 26 | + pull_image.update_policy("missing") |
| 27 | + assert pull_image.policy == "missing" |
| 28 | + |
| 29 | + pull_image.update_policy("newer") |
| 30 | + assert pull_image.policy == "newer" |
| 31 | + |
| 32 | + pull_image.update_policy("always") |
| 33 | + assert pull_image.policy == "always" |
| 34 | + |
| 35 | + # Ensure policy is not downgraded |
| 36 | + pull_image.update_policy("build") |
| 37 | + assert pull_image.policy == "always" |
| 38 | + |
| 39 | + def test_pull_args(self) -> None: |
| 40 | + pull_image = PullImage("localhost/test:1", policy="always", quiet=True) |
| 41 | + assert pull_image.pull_args == ["--policy", "always", "--quiet", "localhost/test:1"] |
| 42 | + |
| 43 | + pull_image.quiet = False |
| 44 | + assert pull_image.pull_args == ["--policy", "always", "localhost/test:1"] |
| 45 | + |
| 46 | + @patch("podman_compose.Podman") |
| 47 | + async def test_pull_success(self, podman_mock: Mock) -> None: |
| 48 | + pull_image = PullImage("localhost/test:1", policy="always", quiet=True) |
| 49 | + |
| 50 | + run_mock = AsyncMock() |
| 51 | + run_mock.return_value = 0 |
| 52 | + podman_mock.run = run_mock |
| 53 | + |
| 54 | + result = await pull_image.pull(podman_mock) |
| 55 | + assert result == 0 |
| 56 | + run_mock.assert_called_once_with( |
| 57 | + [], "pull", ["--policy", "always", "--quiet", "localhost/test:1"] |
| 58 | + ) |
| 59 | + |
| 60 | + @patch("podman_compose.Podman") |
| 61 | + async def test_pull_failed(self, podman_mock: Mock) -> None: |
| 62 | + pull_image = PullImage( |
| 63 | + "localhost/test:1", |
| 64 | + policy="always", |
| 65 | + quiet=True, |
| 66 | + ignore_pull_error=True, |
| 67 | + ) |
| 68 | + |
| 69 | + run_mock = AsyncMock() |
| 70 | + run_mock.return_value = 1 |
| 71 | + podman_mock.run = run_mock |
| 72 | + |
| 73 | + # with ignore_pull_error=True, should return 0 even if pull fails |
| 74 | + result = await pull_image.pull(podman_mock) |
| 75 | + assert result == 0 |
| 76 | + |
| 77 | + # with ignore_pull_error=False, should return the actual error code |
| 78 | + pull_image.ignore_pull_error = False |
| 79 | + result = await pull_image.pull(podman_mock) |
| 80 | + assert result == 1 |
| 81 | + |
| 82 | + @patch("podman_compose.Podman") |
| 83 | + async def test_pull_with_never_policy(self, podman_mock: Mock) -> None: |
| 84 | + pull_image = PullImage( |
| 85 | + "localhost/test:1", |
| 86 | + policy="never", |
| 87 | + quiet=True, |
| 88 | + ignore_pull_error=True, |
| 89 | + ) |
| 90 | + |
| 91 | + run_mock = AsyncMock() |
| 92 | + run_mock.return_value = 1 |
| 93 | + podman_mock.run = run_mock |
| 94 | + |
| 95 | + result = await pull_image.pull(podman_mock) |
| 96 | + assert result == 0 |
| 97 | + assert run_mock.call_count == 0 |
| 98 | + |
| 99 | + @parameterized.expand([ |
| 100 | + ( |
| 101 | + "Local image should not pull", |
| 102 | + Namespace(), |
| 103 | + [{"image": "localhost/a:latest"}], |
| 104 | + 0, |
| 105 | + [], |
| 106 | + ), |
| 107 | + ( |
| 108 | + "Remote image should pull", |
| 109 | + Namespace(), |
| 110 | + [{"image": "ghcr.io/a:latest"}], |
| 111 | + 1, |
| 112 | + [ |
| 113 | + call([], "pull", ["--policy", "missing", "ghcr.io/a:latest"]), |
| 114 | + ], |
| 115 | + ), |
| 116 | + ( |
| 117 | + "The same image in service should call once", |
| 118 | + Namespace(), |
| 119 | + [ |
| 120 | + {"image": "ghcr.io/a:latest"}, |
| 121 | + {"image": "ghcr.io/a:latest"}, |
| 122 | + {"image": "ghcr.io/b:latest"}, |
| 123 | + ], |
| 124 | + 2, |
| 125 | + [ |
| 126 | + call([], "pull", ["--policy", "missing", "ghcr.io/a:latest"]), |
| 127 | + call([], "pull", ["--policy", "missing", "ghcr.io/b:latest"]), |
| 128 | + ], |
| 129 | + ), |
| 130 | + ]) |
| 131 | + @patch("podman_compose.Podman") |
| 132 | + async def test_pull_images( |
| 133 | + self, |
| 134 | + desc: str, |
| 135 | + args: Namespace, |
| 136 | + services: list[dict], |
| 137 | + call_count: int, |
| 138 | + calls: list, |
| 139 | + podman_mock: Mock, |
| 140 | + ) -> None: |
| 141 | + run_mock = AsyncMock() |
| 142 | + run_mock.return_value = 0 |
| 143 | + podman_mock.run = run_mock |
| 144 | + |
| 145 | + assert await PullImage.pull_images(podman_mock, args, services) == 0 |
| 146 | + assert run_mock.call_count == call_count |
| 147 | + if calls: |
| 148 | + run_mock.assert_has_calls(calls, any_order=True) |
| 149 | + |
| 150 | + @patch("podman_compose.Podman") |
| 151 | + async def test_pull_images_with_build_section( |
| 152 | + self, |
| 153 | + podman_mock: Mock, |
| 154 | + ) -> None: |
| 155 | + run_mock = AsyncMock() |
| 156 | + run_mock.return_value = 1 |
| 157 | + podman_mock.run = run_mock |
| 158 | + |
| 159 | + args: Namespace = Namespace() |
| 160 | + services: list[dict] = [ |
| 161 | + {"image": "ghcr.io/a:latest", "build": {"context": "."}}, |
| 162 | + ] |
| 163 | + assert await PullImage.pull_images(podman_mock, args, services) == 0 |
| 164 | + assert run_mock.call_count == 1 |
| 165 | + run_mock.assert_called_with([], "pull", ["--policy", "missing", "ghcr.io/a:latest"]) |
0 commit comments