Debian Codegraph @

ansible_collections/containers/podman/tests/unit/plugins/modules/test_podman_build.py

raw
1 from __future__ import absolute_import
2
3 __metaclass__ = type
4
5 import pytest
6
7 from ansible_collections.containers.podman.plugins.module_utils.podman.quadlet import (
8 BuildQuadlet,
9 )
10
11
12 def _default_params(**overrides):
13 params = dict(
14 annotation=None,
15 arch=None,
16 authfile=None,
17 build_args=None,
18 ca_cert_dir=None,
19 cache=None,
20 cmd_args=None,
21 dns=None,
22 dns_opt=None,
23 dns_search=None,
24 env=None,
25 file=None,
26 force_rm=None,
27 group_add=None,
28 ignore_file=None,
29 name=None,
30 labels=None,
31 network=None,
32 password=None,
33 pull=None,
34 retry=None,
35 retry_delay=None,
36 secret=None,
37 set_working_directory=None,
38 target=None,
39 username=None,
40 validate_certs=None,
41 variant=None,
42 volume=None,
43 quadlet_options=None,
44 )
45 params.update(overrides)
46 return params
47
48
49 def _build_content(**overrides):
50 return BuildQuadlet(_default_params(**overrides)).create_quadlet_content()
51
52
53 class TestBuildQuadletMinimal:
54
55 def test_minimal_with_file(self):
56 content = _build_content(name="localhost/myimage:v1", file="/tmp/Containerfile")
57 assert "[Build]\n" in content
58 assert "File=/tmp/Containerfile\n" in content
59 assert "ImageTag=localhost/myimage:v1\n" in content
60
61 def test_minimal_with_working_directory(self):
62 content = _build_content(name="myimage", set_working_directory="/src")
63 assert "SetWorkingDirectory=/src\n" in content
64 assert "ImageTag=myimage\n" in content
65
66
67 class TestBuildQuadletLabels:
68
69 def test_labels_use_singular_key(self):
70 content = _build_content(
71 name="myimage",
72 file="/tmp/Containerfile",
73 labels={"maintainer": "testuser", "version": "1.0"},
74 )
75 assert "Label=maintainer=testuser\n" in content
76 assert "Label=version=1.0\n" in content
77 assert "Labels=" not in content
78
79 def test_single_label(self):
80 content = _build_content(name="myimage", file="/tmp/Containerfile", labels={"app": "web"})
81 assert "Label=app=web\n" in content
82
83
84 class TestBuildQuadletAnnotations:
85
86 def test_annotations_expanded(self):
87 content = _build_content(
88 name="myimage",
89 file="/tmp/Containerfile",
90 annotation={"com.example.key": "value1", "com.example.key2": "value2"},
91 )
92 assert "Annotation=com.example.key=value1\n" in content
93 assert "Annotation=com.example.key2=value2\n" in content
94
95
96 class TestBuildQuadletBuildArgs:
97
98 def test_build_args_expanded(self):
99 content = _build_content(
100 name="myimage",
101 file="/tmp/Containerfile",
102 build_args={"HTTP_PROXY": "http://proxy:8080", "VERSION": "3.0"},
103 )
104 assert "BuildArg=HTTP_PROXY=http://proxy:8080\n" in content
105 assert "BuildArg=VERSION=3.0\n" in content
106
107
108 class TestBuildQuadletEnvironment:
109
110 def test_env_expanded(self):
111 content = _build_content(
112 name="myimage",
113 file="/tmp/Containerfile",
114 env={"FOO": "bar", "DEBUG": "1"},
115 )
116 assert "Environment=FOO=bar\n" in content
117 assert "Environment=DEBUG=1\n" in content
118
119
120 class TestBuildQuadletNetwork:
121
122 def test_multiple_networks(self):
123 content = _build_content(
124 name="myimage",
125 file="/tmp/Containerfile",
126 network=["host", "mynet"],
127 )
128 assert "Network=host\n" in content
129 assert "Network=mynet\n" in content
130
131
132 class TestBuildQuadletDNS:
133
134 def test_dns_options(self):
135 content = _build_content(
136 name="myimage",
137 file="/tmp/Containerfile",
138 dns=["8.8.8.8", "1.1.1.1"],
139 dns_opt=["ndots:5"],
140 dns_search=["example.com"],
141 )
142 assert "DNS=8.8.8.8\n" in content
143 assert "DNS=1.1.1.1\n" in content
144 assert "DNSOption=ndots:5\n" in content
145 assert "DNSSearch=example.com\n" in content
146
147
148 class TestBuildQuadletSecrets:
149
150 def test_single_secret(self):
151 content = _build_content(
152 name="myimage",
153 file="/tmp/Containerfile",
154 secret=["id=mysecret,src=/run/secrets/mysecret"],
155 )
156 assert "Secret=id=mysecret,src=/run/secrets/mysecret\n" in content
157
158 def test_multiple_secrets(self):
159 content = _build_content(
160 name="myimage",
161 file="/tmp/Containerfile",
162 secret=["id=aws,src=/root/.aws/creds", "id=ssh,src=/root/.ssh/id_rsa"],
163 )
164 assert "Secret=id=aws,src=/root/.aws/creds\n" in content
165 assert "Secret=id=ssh,src=/root/.ssh/id_rsa\n" in content
166
167
168 class TestBuildQuadletCredentials:
169
170 def test_username_password_becomes_podman_args(self):
171 content = _build_content(
172 name="myimage",
173 file="/tmp/Containerfile",
174 username="user",
175 password="pass",
176 )
177 assert "PodmanArgs=--creds user:pass\n" in content
178
179 def test_no_creds_without_both(self):
180 content = _build_content(name="myimage", file="/tmp/Containerfile", username=None, password=None)
181 assert "--creds" not in content
182
183
184 class TestBuildQuadletCacheAndCertDir:
185
186 def test_cache_false_adds_no_cache(self):
187 content = _build_content(name="myimage", file="/tmp/Containerfile", cache=False)
188 assert "PodmanArgs=--no-cache\n" in content
189
190 def test_cache_true_no_flag(self):
191 content = _build_content(name="myimage", file="/tmp/Containerfile", cache=True)
192 assert "--no-cache" not in content
193
194 def test_ca_cert_dir(self):
195 content = _build_content(name="myimage", file="/tmp/Containerfile", ca_cert_dir="/etc/certs")
196 assert "PodmanArgs=--cert-dir /etc/certs\n" in content
197
198
199 class TestBuildQuadletCmdArgs:
200
201 def test_cmd_args_appended_to_podman_args(self):
202 content = _build_content(
203 name="myimage",
204 file="/tmp/Containerfile",
205 cmd_args=["--log-level=debug", "--storage-driver=overlay"],
206 )
207 assert "PodmanArgs=--log-level=debug\n" in content
208 assert "PodmanArgs=--storage-driver=overlay\n" in content
209
210
211 class TestBuildQuadletScalarParams:
212
213 @pytest.mark.parametrize(
214 "param, value, expected_key, expected_value",
215 [
216 ("arch", "amd64", "Arch", "amd64"),
217 ("authfile", "/run/auth.json", "AuthFile", "/run/auth.json"),
218 ("force_rm", True, "ForceRM", "true"),
219 ("force_rm", False, "ForceRM", "false"),
220 ("pull", "always", "Pull", "always"),
221 ("retry", 5, "Retry", "5"),
222 ("retry_delay", "10s", "RetryDelay", "10s"),
223 ("target", "builder", "Target", "builder"),
224 ("validate_certs", True, "TLSVerify", "true"),
225 ("validate_certs", False, "TLSVerify", "false"),
226 ("variant", "v8", "Variant", "v8"),
227 ("ignore_file", "/tmp/.containerignore", "IgnoreFile", "/tmp/.containerignore"),
228 ],
229 )
230 def test_scalar_param(self, param, value, expected_key, expected_value):
231 content = _build_content(name="myimage", file="/tmp/Containerfile", **{param: value})
232 assert f"{expected_key}={expected_value}\n" in content
233
234
235 class TestBuildQuadletVolumes:
236
237 def test_multiple_volumes(self):
238 content = _build_content(
239 name="myimage",
240 file="/tmp/Containerfile",
241 volume=["/src:/src:ro", "/cache:/cache"],
242 )
243 assert "Volume=/src:/src:ro\n" in content
244 assert "Volume=/cache:/cache\n" in content
245
246
247 class TestBuildQuadletOptions:
248
249 def test_quadlet_options_extra_section(self):
250 content = _build_content(
251 name="myimage",
252 file="/tmp/Containerfile",
253 quadlet_options=["[Install]\nWantedBy=default.target"],
254 )
255 assert "[Install]\nWantedBy=default.target" in content
256
257 def test_quadlet_options_plain_key_value(self):
258 content = _build_content(
259 name="myimage",
260 file="/tmp/Containerfile",
261 quadlet_options=["SomeKey=SomeValue"],
262 )
263 assert "SomeKey=SomeValue\n" in content
264
265
266 class TestBuildQuadletNoneParamsOmitted:
267
268 def test_none_params_not_in_output(self):
269 content = _build_content(name="myimage", file="/tmp/Containerfile")
270 for key in [
271 "Annotation=",
272 "Arch=",
273 "BuildArg=",
274 "DNS=",
275 "DNSOption=",
276 "DNSSearch=",
277 "Environment=",
278 "ForceRM=",
279 "GroupAdd=",
280 "IgnoreFile=",
281 "Label=",
282 "Network=",
283 "Pull=",
284 "Retry=",
285 "RetryDelay=",
286 "Secret=",
287 "Target=",
288 "TLSVerify=",
289 "Variant=",
290 "Volume=",
291 "PodmanArgs=",
292 ]:
293 assert key not in content