Debian Codegraph @

tests/unit/test_container_to_build_args.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0
2
3 import os
4 import unittest
5 from unittest import mock
6
7 from podman_compose import container_to_build_args
8
9
10 def create_compose_mock(project_name='test_project_name'):
11 compose = mock.Mock()
12 compose.project_name = project_name
13 compose.dirname = 'test_dirname'
14 compose.container_names_by_service.get = mock.Mock(return_value=None)
15 compose.prefer_volume_over_mount = False
16 compose.default_net = None
17 compose.networks = {}
18 compose.x_podman = {}
19 return compose
20
21
22 def get_minimal_container():
23 return {
24 'name': 'project_name_service_name1',
25 'service_name': 'service_name',
26 'image': 'new-image',
27 'build': {},
28 }
29
30
31 def get_minimal_args():
32 args = mock.Mock()
33 args.build_arg = []
34 args.pull = None
35 return args
36
37
38 class TestContainerToBuildArgs(unittest.TestCase):
39 def test_minimal(self):
40 c = create_compose_mock()
41
42 cnt = get_minimal_container()
43 args = get_minimal_args()
44
45 args = container_to_build_args(c, cnt, args, lambda path: True)
46 self.assertEqual(
47 args,
48 [
49 '-f',
50 'Containerfile',
51 '-t',
52 'new-image',
53 '--no-cache',
54 '.',
55 ],
56 )
57
58 def test_platform(self):
59 c = create_compose_mock()
60
61 cnt = get_minimal_container()
62 cnt['platform'] = 'linux/amd64'
63 args = get_minimal_args()
64
65 args = container_to_build_args(c, cnt, args, lambda path: True)
66 self.assertEqual(
67 args,
68 [
69 '-f',
70 'Containerfile',
71 '-t',
72 'new-image',
73 '--platform',
74 'linux/amd64',
75 '--no-cache',
76 '.',
77 ],
78 )
79
80 def test_tags(self):
81 c = create_compose_mock()
82
83 cnt = get_minimal_container()
84 cnt['build']['tags'] = ['some-tag1', 'some-tag2:2']
85 args = get_minimal_args()
86
87 args = container_to_build_args(c, cnt, args, lambda path: True)
88 self.assertEqual(
89 args,
90 [
91 '-f',
92 'Containerfile',
93 '-t',
94 'new-image',
95 '-t',
96 'some-tag1',
97 '-t',
98 'some-tag2:2',
99 '--no-cache',
100 '.',
101 ],
102 )
103
104 def test_labels(self):
105 c = create_compose_mock()
106
107 cnt = get_minimal_container()
108 cnt['build']['labels'] = ['some-label1', 'some-label2.2']
109 args = get_minimal_args()
110
111 args = container_to_build_args(c, cnt, args, lambda path: True)
112 self.assertEqual(
113 args,
114 [
115 '-f',
116 'Containerfile',
117 '-t',
118 'new-image',
119 '--label',
120 'some-label1',
121 '--label',
122 'some-label2.2',
123 '--no-cache',
124 '.',
125 ],
126 )
127
128 def test_caches(self):
129 c = create_compose_mock()
130
131 cnt = get_minimal_container()
132 cnt['build']['cache_from'] = ['registry/image1', 'registry/image2']
133 cnt['build']['cache_to'] = ['registry/image3', 'registry/image4']
134 args = get_minimal_args()
135
136 args = container_to_build_args(c, cnt, args, lambda path: True)
137 self.assertEqual(
138 args,
139 [
140 '-f',
141 'Containerfile',
142 '-t',
143 'new-image',
144 '--no-cache',
145 '--cache-from',
146 'registry/image1',
147 '--cache-from',
148 'registry/image2',
149 '--cache-to',
150 'registry/image3',
151 '--cache-to',
152 'registry/image4',
153 '.',
154 ],
155 )
156
157 def test_dockerfile_inline(self):
158 c = create_compose_mock()
159
160 cnt = get_minimal_container()
161 cnt['build']['dockerfile_inline'] = "FROM busybox\nRUN echo 'hello world'"
162 args = get_minimal_args()
163
164 cleanup_callbacks = []
165 args = container_to_build_args(
166 c, cnt, args, lambda path: True, cleanup_callbacks=cleanup_callbacks
167 )
168
169 temp_dockerfile = args[args.index("-f") + 1]
170 self.assertTrue(os.path.exists(temp_dockerfile))
171
172 with open(temp_dockerfile) as file:
173 contents = file.read()
174 self.assertEqual(contents, "FROM busybox\n" + "RUN echo 'hello world'")
175
176 for c in cleanup_callbacks:
177 c()
178 self.assertFalse(os.path.exists(temp_dockerfile))
179
180 def test_context_git_url(self):
181 c = create_compose_mock()
182
183 cnt = get_minimal_container()
184 cnt['build']['context'] = "https://github.com/test_repo.git"
185 args = get_minimal_args()
186
187 args = container_to_build_args(c, cnt, args, lambda path: False)
188 self.assertEqual(
189 args,
190 [
191 '-t',
192 'new-image',
193 '--no-cache',
194 'https://github.com/test_repo.git',
195 ],
196 )
197
198 def test_context_invalid_git_url_git_is_not_prefix(self):
199 c = create_compose_mock()
200
201 cnt = get_minimal_container()
202 cnt['build']['context'] = "not_prefix://github.com/test_repo"
203 args = get_minimal_args()
204
205 with self.assertRaises(OSError):
206 container_to_build_args(c, cnt, args, lambda path: False)
207
208 def test_build_ssh_absolute_path(self):
209 c = create_compose_mock()
210
211 cnt = get_minimal_container()
212 cnt['build']['ssh'] = ["id1=/test1"]
213 args = get_minimal_args()
214
215 args = container_to_build_args(c, cnt, args, lambda path: True)
216 self.assertEqual(
217 args,
218 [
219 '-f',
220 'Containerfile',
221 '-t',
222 'new-image',
223 '--ssh',
224 'id1=/test1',
225 '--no-cache',
226 '.',
227 ],
228 )
229
230 def test_build_ssh_relative_path(self):
231 c = create_compose_mock()
232
233 cnt = get_minimal_container()
234 cnt['build']['ssh'] = ["id1=id1/test1"]
235 args = get_minimal_args()
236
237 args = container_to_build_args(c, cnt, args, lambda path: True)
238 self.assertEqual(
239 args,
240 [
241 '-f',
242 'Containerfile',
243 '-t',
244 'new-image',
245 '--ssh',
246 'id1=test_dirname/id1/test1',
247 '--no-cache',
248 '.',
249 ],
250 )
251
252 def test_build_ssh_working_dir(self):
253 c = create_compose_mock()
254
255 cnt = get_minimal_container()
256 cnt['build']['ssh'] = ["id1=./test1"]
257 args = get_minimal_args()
258
259 args = container_to_build_args(c, cnt, args, lambda path: True)
260 self.assertEqual(
261 args,
262 [
263 '-f',
264 'Containerfile',
265 '-t',
266 'new-image',
267 '--ssh',
268 'id1=test_dirname/./test1',
269 '--no-cache',
270 '.',
271 ],
272 )
273
274 @mock.patch.dict(os.environ, {"HOME": "/home/user"}, clear=True)
275 def test_build_ssh_path_home_dir(self):
276 c = create_compose_mock()
277
278 cnt = get_minimal_container()
279 cnt['build']['ssh'] = ["id1=~/test1"]
280 args = get_minimal_args()
281
282 args = container_to_build_args(c, cnt, args, lambda path: True)
283 self.assertEqual(
284 args,
285 [
286 '-f',
287 'Containerfile',
288 '-t',
289 'new-image',
290 '--ssh',
291 'id1=/home/user/test1',
292 '--no-cache',
293 '.',
294 ],
295 )
296
297 def test_build_ssh_map(self):
298 c = create_compose_mock()
299
300 cnt = get_minimal_container()
301 cnt['build']['ssh'] = {"id1": "test1", "id2": "test2"}
302 args = get_minimal_args()
303
304 args = container_to_build_args(c, cnt, args, lambda path: True)
305 self.assertEqual(
306 args,
307 [
308 '-f',
309 'Containerfile',
310 '-t',
311 'new-image',
312 '--ssh',
313 'id1=test_dirname/test1',
314 '--ssh',
315 'id2=test_dirname/test2',
316 '--no-cache',
317 '.',
318 ],
319 )
320
321 def test_build_ssh_array(self):
322 c = create_compose_mock()
323
324 cnt = get_minimal_container()
325 cnt['build']['ssh'] = ['id1=test1', 'id2=test2']
326 args = get_minimal_args()
327
328 args = container_to_build_args(c, cnt, args, lambda path: True)
329 self.assertEqual(
330 args,
331 [
332 '-f',
333 'Containerfile',
334 '-t',
335 'new-image',
336 '--ssh',
337 'id1=test_dirname/test1',
338 '--ssh',
339 'id2=test_dirname/test2',
340 '--no-cache',
341 '.',
342 ],
343 )
344
345 def test_pull_always(self):
346 c = create_compose_mock()
347 cnt = get_minimal_container()
348 args = get_minimal_args()
349 args.pull = 'always'
350
351 args = container_to_build_args(c, cnt, args, lambda path: True)
352 self.assertEqual(
353 args,
354 [
355 '-f',
356 'Containerfile',
357 '-t',
358 'new-image',
359 '--no-cache',
360 '--pull=always',
361 '.',
362 ],
363 )
364
365 def test_containerfile_in_context(self):
366 c = create_compose_mock()
367
368 cnt = get_minimal_container()
369 cnt['build']['context'] = "./subdir"
370 args = get_minimal_args()
371 args = container_to_build_args(c, cnt, args, lambda path: True)
372 self.assertEqual(
373 args,
374 [
375 '-f',
376 'subdir/Containerfile',
377 '-t',
378 'new-image',
379 '--no-cache',
380 './subdir',
381 ],
382 )
383
384 def test_build_environment_secret_no_target(self):
385 c = create_compose_mock()
386 c.declared_secrets = {'my_secret': {'environment': 'MY_VAR'}}
387 cnt = get_minimal_container()
388 cnt['build']['secrets'] = ['my_secret']
389 args = get_minimal_args()
390 args = container_to_build_args(c, cnt, args, lambda path: True)
391 self.assertEqual(
392 args,
393 [
394 '-f',
395 'Containerfile',
396 '-t',
397 'new-image',
398 '--secret',
399 'id=my_secret,env=MY_VAR',
400 '--no-cache',
401 '.',
402 ],
403 )
404
405 def test_build_environment_secret_with_target(self):
406 c = create_compose_mock()
407 c.declared_secrets = {'my_secret': {'environment': 'MY_VAR'}}
408 cnt = get_minimal_container()
409 cnt['build']['secrets'] = [{'source': 'my_secret', 'target': 'custom_id'}]
410 args = get_minimal_args()
411 args = container_to_build_args(c, cnt, args, lambda path: True)
412 self.assertEqual(
413 args,
414 [
415 '-f',
416 'Containerfile',
417 '-t',
418 'new-image',
419 '--secret',
420 'id=custom_id,env=MY_VAR',
421 '--no-cache',
422 '.',
423 ],
424 )