Debian Codegraph @

tests/unit/test_pull_image.py

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