Debian Codegraph @

tests/integration/pod_args/test_podman_compose_pod_args.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0
2
3 import json
4 import os
5 import unittest
6
7 from packaging import version
8
9 from tests.integration.test_utils import RunSubprocessMixin
10 from tests.integration.test_utils import get_podman_version
11
12
13 def base_path() -> str:
14 """Returns the base path for the project"""
15 return os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
16
17
18 def test_path() -> str:
19 """Returns the path to the tests directory"""
20 return os.path.join(base_path(), "tests/integration")
21
22
23 def podman_compose_path() -> str:
24 """Returns the path to the podman compose script"""
25 return os.path.join(base_path(), "podman_compose.py")
26
27
28 class TestPodmanComposePodArgs(unittest.TestCase, RunSubprocessMixin):
29 def load_pod_info(self, pod_name: str) -> dict:
30 output, _ = self.run_subprocess_assert_returncode([
31 "podman",
32 "pod",
33 "inspect",
34 pod_name,
35 ])
36 pod_info = json.loads(output.decode('utf-8'))
37 # Podman 5.0 changed pod inspect to always output a list.
38 # Check type to support both old and new version.
39 if isinstance(pod_info, list):
40 return pod_info[0]
41 return pod_info
42
43 def run_pod_args_test(self, config: str, args: list, expected: list) -> None:
44 """
45 Helper to run podman up with a docker-compose.yml config, additional
46 (--pod-args) arguments and compare the CreateCommand of the resulting
47 pod with an expected value
48 """
49 pod_name = "pod_" + config
50 command_up = (
51 [
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 "pod_args",
60 config,
61 "docker-compose.yml",
62 ),
63 ]
64 + args
65 + [
66 "up",
67 "--no-start",
68 ]
69 )
70
71 try:
72 self.run_subprocess_assert_returncode(command_up)
73
74 pod_info = self.load_pod_info(pod_name)
75 self.assertEqual(
76 pod_info['CreateCommand'],
77 ["podman", "pod", "create", "--name=" + pod_name] + expected,
78 )
79
80 finally:
81 command_rm_pod = ["podman", "pod", "rm", pod_name]
82 self.run_subprocess_assert_returncode(command_rm_pod)
83
84 def test_x_podman_pod_args_unset_unset(self) -> None:
85 """
86 Test that podman-compose will use the default pod-args when unset in
87 both docker-compose.yml and command line
88 """
89 self.run_pod_args_test(
90 "custom_pod_args_unset",
91 [],
92 ["--infra=false", "--share="],
93 )
94
95 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
96 def test_x_podman_pod_args_unset_empty(self) -> None:
97 """
98 Test that podman-compose will use empty pod-args when unset in
99 docker-compose.yml and passing an empty value on the command line
100 """
101 self.run_pod_args_test(
102 "custom_pod_args_unset",
103 ["--pod-args="],
104 [],
105 )
106
107 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
108 def test_x_podman_pod_args_unset_set(self) -> None:
109 """
110 Test that podman-compose will use the passed pod-args when unset in
111 docker-compose.yml and passing a non-empty value on the command line
112 """
113 self.run_pod_args_test(
114 "custom_pod_args_unset",
115 ["--pod-args=--infra=false --share= --cpus=1"],
116 ["--infra=false", "--share=", "--cpus=1"],
117 )
118
119 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
120 def test_x_podman_pod_args_empty_unset(self) -> None:
121 """
122 Test that podman-compose will use empty pod-args when set to an
123 empty value in docker-compose.yml and unset on the command line
124 """
125 self.run_pod_args_test(
126 "custom_pod_args_empty",
127 [],
128 [],
129 )
130
131 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
132 def test_x_podman_pod_args_empty_empty(self) -> None:
133 """
134 Test that podman-compose will use empty pod-args when set to an
135 empty value in both docker-compose.yml and command line
136 """
137 self.run_pod_args_test(
138 "custom_pod_args_empty",
139 ["--pod-args="],
140 [],
141 )
142
143 def test_x_podman_pod_args_empty_set(self) -> None:
144 """
145 Test that podman-compose will use the passed pod-args when set to an
146 empty value in docker-compose.yml and passing a non-empty value on the
147 command line
148 """
149 self.run_pod_args_test(
150 "custom_pod_args_empty",
151 ["--pod-args=--infra=false --share= --cpus=1"],
152 ["--infra=false", "--share=", "--cpus=1"],
153 )
154
155 def test_x_podman_pod_args_set_unset(self) -> None:
156 """
157 Test that podman-compose will use the set pod-args when set to a
158 non-empty value in docker-compose.yml and unset on the command line
159 """
160 self.run_pod_args_test(
161 "custom_pod_args_set",
162 [],
163 ["--infra=false", "--share=", "--cpus=2"],
164 )
165
166 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
167 def test_x_podman_pod_args_set_empty(self) -> None:
168 """
169 Test that podman-compose will use empty pod-args when set to a
170 non-empty value in docker-compose.yml and passing an empty value on
171 the command line
172 """
173 self.run_pod_args_test(
174 "custom_pod_args_set",
175 ["--pod-args="],
176 [],
177 )
178
179 def test_x_podman_pod_args_set_set(self) -> None:
180 """
181 Test that podman-compose will use the passed pod-args when set to a
182 non-empty value in both docker-compose.yml and command line
183 """
184 self.run_pod_args_test(
185 "custom_pod_args_set",
186 ["--pod-args=--infra=false --share= --cpus=1"],
187 ["--infra=false", "--share=", "--cpus=1"],
188 )