Debian Codegraph @

tests/integration/in_pod/test_podman_compose_in_pod.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0
2
3 import os
4 import unittest
5
6 from packaging import version
7
8 from tests.integration.test_utils import RunSubprocessMixin
9 from tests.integration.test_utils import get_podman_version
10
11
12 def base_path() -> str:
13 """Returns the base path for the project"""
14 return os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
15
16
17 def test_path() -> str:
18 """Returns the path to the tests directory"""
19 return os.path.join(base_path(), "tests/integration")
20
21
22 def podman_compose_path() -> str:
23 """Returns the path to the podman compose script"""
24 return os.path.join(base_path(), "podman_compose.py")
25
26
27 def is_root() -> bool:
28 return os.geteuid() == 0
29
30
31 def failure_exitcode_when_rootful() -> int:
32 if is_root():
33 return 125
34 return 0
35
36
37 # If a compose file has userns_mode set, setting in_pod to True, results in error.
38 # Default in_pod setting is True, unless compose file provides otherwise.
39 # Compose file provides custom in_pod option, which can be overridden by command line in_pod option.
40 # Test all combinations of command line argument in_pod and compose file argument in_pod.
41 class TestPodmanComposeInPod(unittest.TestCase, RunSubprocessMixin):
42 # compose file provides x-podman in_pod=false
43 @unittest.skipIf(
44 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
45 )
46 def test_x_podman_in_pod_false_command_line_in_pod_not_exists(self) -> None:
47 """
48 Test that podman-compose will not create a pod, when x-podman in_pod=false and command line
49 does not provide this option
50 """
51 command_up = [
52 "python3",
53 os.path.join(base_path(), "podman_compose.py"),
54 "-f",
55 os.path.join(
56 base_path(),
57 "tests",
58 "integration",
59 "in_pod",
60 "custom_x-podman_false",
61 "docker-compose.yml",
62 ),
63 "up",
64 "-d",
65 ]
66
67 down_cmd = [
68 "python3",
69 podman_compose_path(),
70 "-f",
71 os.path.join(
72 base_path(),
73 "tests",
74 "integration",
75 "in_pod",
76 "custom_x-podman_false",
77 "docker-compose.yml",
78 ),
79 "down",
80 ]
81
82 try:
83 self.run_subprocess_assert_returncode(command_up, failure_exitcode_when_rootful())
84
85 finally:
86 self.run_subprocess_assert_returncode(down_cmd)
87 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_false"]
88 # throws an error, can not actually find this pod because it was not created
89 self.run_subprocess_assert_returncode(command_rm_pod, expected_returncode=1)
90
91 def test_x_podman_in_pod_false_command_line_in_pod_true(self) -> None:
92 """
93 Test that podman-compose does not allow pod creating even with command line in_pod=True
94 when --userns and --pod are set together: throws an error
95 """
96 # FIXME: creates a pod anyway, although it should not
97 command_up = [
98 "python3",
99 os.path.join(base_path(), "podman_compose.py"),
100 "--in-pod=True",
101 "-f",
102 os.path.join(
103 base_path(),
104 "tests",
105 "integration",
106 "in_pod",
107 "custom_x-podman_false",
108 "docker-compose.yml",
109 ),
110 "up",
111 "-d",
112 ]
113
114 try:
115 out, err = self.run_subprocess_assert_returncode(command_up, 125)
116 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
117
118 finally:
119 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_false"]
120 # should throw an error of not being able to find this pod (because it should not have
121 # been created) and have expected_returncode=1 (see FIXME above)
122 self.run_subprocess_assert_returncode(command_rm_pod)
123
124 @unittest.skipIf(
125 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
126 )
127 def test_x_podman_in_pod_false_command_line_in_pod_false(self) -> None:
128 """
129 Test that podman-compose will not create a pod as command line sets in_pod=False
130 """
131 command_up = [
132 "python3",
133 os.path.join(base_path(), "podman_compose.py"),
134 "--in-pod=False",
135 "-f",
136 os.path.join(
137 base_path(),
138 "tests",
139 "integration",
140 "in_pod",
141 "custom_x-podman_false",
142 "docker-compose.yml",
143 ),
144 "up",
145 "-d",
146 ]
147
148 down_cmd = [
149 "python3",
150 podman_compose_path(),
151 "-f",
152 os.path.join(
153 base_path(),
154 "tests",
155 "integration",
156 "in_pod",
157 "custom_x-podman_false",
158 "docker-compose.yml",
159 ),
160 "down",
161 ]
162
163 try:
164 self.run_subprocess_assert_returncode(command_up, failure_exitcode_when_rootful())
165
166 finally:
167 self.run_subprocess_assert_returncode(down_cmd)
168 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_false"]
169 # can not actually find this pod because it was not created
170 self.run_subprocess_assert_returncode(command_rm_pod, 1)
171
172 @unittest.skipIf(
173 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
174 )
175 def test_x_podman_in_pod_false_command_line_in_pod_empty_string(self) -> None:
176 """
177 Test that podman-compose will not create a pod, when x-podman in_pod=false and command line
178 command line in_pod=""
179 """
180 command_up = [
181 "python3",
182 os.path.join(base_path(), "podman_compose.py"),
183 "--in-pod=",
184 "-f",
185 os.path.join(
186 base_path(),
187 "tests",
188 "integration",
189 "in_pod",
190 "custom_x-podman_false",
191 "docker-compose.yml",
192 ),
193 "up",
194 "-d",
195 ]
196
197 down_cmd = [
198 "python3",
199 podman_compose_path(),
200 "-f",
201 os.path.join(
202 base_path(),
203 "tests",
204 "integration",
205 "in_pod",
206 "custom_x-podman_false",
207 "docker-compose.yml",
208 ),
209 "down",
210 ]
211
212 try:
213 self.run_subprocess_assert_returncode(command_up, failure_exitcode_when_rootful())
214
215 finally:
216 self.run_subprocess_assert_returncode(down_cmd)
217 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_false"]
218 # can not actually find this pod because it was not created
219 self.run_subprocess_assert_returncode(command_rm_pod, 1)
220
221 # compose file provides x-podman in_pod=true
222 def test_x_podman_in_pod_true_command_line_in_pod_not_exists(self) -> None:
223 """
224 Test that podman-compose does not allow pod creating when --userns and --pod are set
225 together even when x-podman in_pod=true: throws an error
226 """
227 # FIXME: creates a pod anyway, although it should not
228 # Container is not created, so command 'down' is not needed
229 command_up = [
230 "python3",
231 os.path.join(base_path(), "podman_compose.py"),
232 "-f",
233 os.path.join(
234 base_path(),
235 "tests",
236 "integration",
237 "in_pod",
238 "custom_x-podman_true",
239 "docker-compose.yml",
240 ),
241 "up",
242 "-d",
243 ]
244
245 try:
246 out, err = self.run_subprocess_assert_returncode(command_up, 125)
247 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
248
249 finally:
250 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_true"]
251 # should throw an error of not being able to find this pod (it should not have been
252 # created) and have expected_returncode=1 (see FIXME above)
253 self.run_subprocess_assert_returncode(command_rm_pod)
254
255 def test_x_podman_in_pod_true_command_line_in_pod_true(self) -> None:
256 """
257 Test that podman-compose does not allow pod creating when --userns and --pod are set
258 together even when x-podman in_pod=true and command line in_pod=True: throws an error
259 """
260 # FIXME: creates a pod anyway, although it should not
261 # Container is not created, so command 'down' is not needed
262 command_up = [
263 "python3",
264 os.path.join(base_path(), "podman_compose.py"),
265 "--in-pod=True",
266 "-f",
267 os.path.join(
268 base_path(),
269 "tests",
270 "integration",
271 "in_pod",
272 "custom_x-podman_true",
273 "docker-compose.yml",
274 ),
275 "up",
276 "-d",
277 ]
278
279 try:
280 out, err = self.run_subprocess_assert_returncode(command_up, 125)
281 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
282
283 finally:
284 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_true"]
285 # should throw an error of not being able to find this pod (because it should not have
286 # been created) and have expected_returncode=1 (see FIXME above)
287 self.run_subprocess_assert_returncode(command_rm_pod)
288
289 @unittest.skipIf(
290 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
291 )
292 def test_x_podman_in_pod_true_command_line_in_pod_false(self) -> None:
293 """
294 Test that podman-compose will not create a pod as command line sets in_pod=False
295 """
296 command_up = [
297 "python3",
298 os.path.join(base_path(), "podman_compose.py"),
299 "--in-pod=False",
300 "-f",
301 os.path.join(
302 base_path(),
303 "tests",
304 "integration",
305 "in_pod",
306 "custom_x-podman_true",
307 "docker-compose.yml",
308 ),
309 "up",
310 "-d",
311 ]
312
313 down_cmd = [
314 "python3",
315 podman_compose_path(),
316 "-f",
317 os.path.join(
318 base_path(),
319 "tests",
320 "integration",
321 "in_pod",
322 "custom_x-podman_true",
323 "docker-compose.yml",
324 ),
325 "down",
326 ]
327
328 try:
329 self.run_subprocess_assert_returncode(command_up, failure_exitcode_when_rootful())
330
331 finally:
332 self.run_subprocess_assert_returncode(down_cmd)
333 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_false"]
334 # can not actually find this pod because it was not created
335 self.run_subprocess_assert_returncode(command_rm_pod, 1)
336
337 def test_x_podman_in_pod_true_command_line_in_pod_empty_string(self) -> None:
338 """
339 Test that podman-compose does not allow pod creating when --userns and --pod are set
340 together even when x-podman in_pod=true and command line in_pod="": throws an error
341 """
342 # FIXME: creates a pod anyway, although it should not
343 # Container is not created, so command 'down' is not needed
344 command_up = [
345 "python3",
346 os.path.join(base_path(), "podman_compose.py"),
347 "--in-pod=",
348 "-f",
349 os.path.join(
350 base_path(),
351 "tests",
352 "integration",
353 "in_pod",
354 "custom_x-podman_true",
355 "docker-compose.yml",
356 ),
357 "up",
358 "-d",
359 ]
360
361 try:
362 out, err = self.run_subprocess_assert_returncode(command_up, 125)
363 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
364
365 finally:
366 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_true"]
367 # should throw an error of not being able to find this pod (because it should not have
368 # been created) and have expected_returncode=1 (see FIXME above)
369 self.run_subprocess_assert_returncode(command_rm_pod)
370
371 # compose file does not provide x-podman in_pod
372 def test_x_podman_in_pod_not_exists_command_line_in_pod_not_exists(self) -> None:
373 """
374 Test that podman-compose does not allow pod creating when --userns and --pod are set
375 together: throws an error
376 """
377 # FIXME: creates a pod anyway, although it should not
378 # Container is not created, so command 'down' is not needed
379 command_up = [
380 "python3",
381 os.path.join(base_path(), "podman_compose.py"),
382 "-f",
383 os.path.join(
384 base_path(),
385 "tests",
386 "integration",
387 "in_pod",
388 "custom_x-podman_not_exists",
389 "docker-compose.yml",
390 ),
391 "up",
392 "-d",
393 ]
394
395 try:
396 out, err = self.run_subprocess_assert_returncode(command_up, 125)
397 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
398
399 finally:
400 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
401 # should throw an error of not being able to find this pod (it should not have been
402 # created) and have expected_returncode=1 (see FIXME above)
403 self.run_subprocess_assert_returncode(command_rm_pod)
404
405 def test_x_podman_in_pod_not_exists_command_line_in_pod_true(self) -> None:
406 """
407 Test that podman-compose does not allow pod creating when --userns and --pod are set
408 together even when x-podman in_pod=true: throws an error
409 """
410 # FIXME: creates a pod anyway, although it should not
411 # Container was not created, so command 'down' is not needed
412 command_up = [
413 "python3",
414 os.path.join(base_path(), "podman_compose.py"),
415 "--in-pod=True",
416 "-f",
417 os.path.join(
418 base_path(),
419 "tests",
420 "integration",
421 "in_pod",
422 "custom_x-podman_not_exists",
423 "docker-compose.yml",
424 ),
425 "up",
426 "-d",
427 ]
428
429 try:
430 out, err = self.run_subprocess_assert_returncode(command_up, 125)
431 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
432
433 finally:
434 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
435 # should throw an error of not being able to find this pod (because it should not have
436 # been created) and have expected_returncode=1 (see FIXME above)
437 self.run_subprocess_assert_returncode(command_rm_pod)
438
439 @unittest.skipIf(
440 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
441 )
442 def test_x_podman_in_pod_not_exists_command_line_in_pod_false(self) -> None:
443 """
444 Test that podman-compose will not create a pod as command line sets in_pod=False
445 """
446 command_up = [
447 "python3",
448 os.path.join(base_path(), "podman_compose.py"),
449 "--in-pod=False",
450 "-f",
451 os.path.join(
452 base_path(),
453 "tests",
454 "integration",
455 "in_pod",
456 "custom_x-podman_not_exists",
457 "docker-compose.yml",
458 ),
459 "up",
460 "-d",
461 ]
462
463 down_cmd = [
464 "python3",
465 podman_compose_path(),
466 "-f",
467 os.path.join(
468 base_path(),
469 "tests",
470 "integration",
471 "in_pod",
472 "custom_x-podman_not_exists",
473 "docker-compose.yml",
474 ),
475 "down",
476 ]
477
478 try:
479 self.run_subprocess_assert_returncode(command_up, failure_exitcode_when_rootful())
480
481 finally:
482 self.run_subprocess_assert_returncode(down_cmd)
483
484 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
485 # can not actually find this pod because it was not created
486 self.run_subprocess_assert_returncode(command_rm_pod, 1)
487
488 @unittest.skipIf(
489 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
490 )
491 def test_x_podman_in_pod_not_exists_command_line_in_pod_not_exists_docker_compat(self) -> None:
492 """
493 Test that podman-compose will not create a pod when docker compat is requested.
494 """
495 command_up = [
496 "python3",
497 os.path.join(base_path(), "podman_compose.py"),
498 "-f",
499 os.path.join(
500 base_path(),
501 "tests",
502 "integration",
503 "in_pod",
504 "custom_x-podman_not_exists",
505 "docker-compose.yml",
506 ),
507 "up",
508 "-d",
509 ]
510
511 down_cmd = [
512 "python3",
513 podman_compose_path(),
514 "-f",
515 os.path.join(
516 base_path(),
517 "tests",
518 "integration",
519 "in_pod",
520 "custom_x-podman_not_exists",
521 "docker-compose.yml",
522 ),
523 "down",
524 ]
525
526 env = {
527 "PODMAN_COMPOSE_DOCKER_COMPOSE_COMPAT": "1",
528 }
529
530 try:
531 self.run_subprocess_assert_returncode(
532 command_up, failure_exitcode_when_rootful(), env=env
533 )
534
535 finally:
536 self.run_subprocess_assert_returncode(down_cmd, env=env)
537
538 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
539 # can not actually find this pod because it was not created
540 self.run_subprocess_assert_returncode(command_rm_pod, 1)
541
542 @unittest.skipIf(
543 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
544 )
545 def test_x_podman_in_pod_not_exists_command_line_in_pod_not_exists_env_var(self) -> None:
546 """
547 Test that podman-compose will not create a pod when env var is set.
548 """
549 command_up = [
550 "python3",
551 os.path.join(base_path(), "podman_compose.py"),
552 "-f",
553 os.path.join(
554 base_path(),
555 "tests",
556 "integration",
557 "in_pod",
558 "custom_x-podman_not_exists",
559 "docker-compose.yml",
560 ),
561 "up",
562 "-d",
563 ]
564
565 down_cmd = [
566 "python3",
567 podman_compose_path(),
568 "-f",
569 os.path.join(
570 base_path(),
571 "tests",
572 "integration",
573 "in_pod",
574 "custom_x-podman_not_exists",
575 "docker-compose.yml",
576 ),
577 "down",
578 ]
579
580 env = {
581 "PODMAN_COMPOSE_IN_POD": "0",
582 }
583
584 try:
585 self.run_subprocess_assert_returncode(
586 command_up, failure_exitcode_when_rootful(), env=env
587 )
588
589 finally:
590 self.run_subprocess_assert_returncode(down_cmd, env=env)
591
592 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
593 # can not actually find this pod because it was not created
594 self.run_subprocess_assert_returncode(command_rm_pod, 1)
595
596 def test_x_podman_in_pod_custom_name(self) -> None:
597 """
598 Test that podman-compose will create a pod with a custom name
599 """
600 command_up = [
601 "python3",
602 os.path.join(base_path(), "podman_compose.py"),
603 "-f",
604 os.path.join(
605 base_path(),
606 "tests",
607 "integration",
608 "in_pod",
609 "custom_x-podman_custom_name",
610 "docker-compose.yml",
611 ),
612 "up",
613 "--no-start",
614 ]
615
616 try:
617 self.run_subprocess_assert_returncode(command_up)
618
619 finally:
620 command_rm_pod = ["podman", "pod", "rm", "custom_test_pod_name"]
621 self.run_subprocess_assert_returncode(command_rm_pod)
622
623 def test_x_podman_in_pod_not_exists_command_line_in_pod_empty_string(self) -> None:
624 """
625 Test that podman-compose does not allow pod creating when --userns and --pod are set
626 together: throws an error
627 """
628 # FIXME: creates a pod anyway, although it should not
629 # Container was not created, so command 'down' is not needed
630 command_up = [
631 "python3",
632 os.path.join(base_path(), "podman_compose.py"),
633 "--in-pod=",
634 "-f",
635 os.path.join(
636 base_path(),
637 "tests",
638 "integration",
639 "in_pod",
640 "custom_x-podman_not_exists",
641 "docker-compose.yml",
642 ),
643 "up",
644 "-d",
645 ]
646
647 try:
648 out, err = self.run_subprocess_assert_returncode(command_up, 125)
649 self.assertEqual(b"Error: --userns and --pod cannot be set together" in err, True)
650
651 finally:
652 command_rm_pod = ["podman", "pod", "rm", "pod_custom_x-podman_not_exists"]
653 # should throw an error of not being able to find this pod (because it should not have
654 # been created) and have expected_returncode=1 (see FIXME above)
655 self.run_subprocess_assert_returncode(command_rm_pod)