Debian Codegraph @

tests/integration/deps/test_podman_compose_deps.py

raw symbols SCIP
1 # SPDX-License-Identifier: GPL-2.0
2 import os
3 import unittest
4
5 from packaging import version
6
7 from tests.integration.test_utils import PodmanAwareRunSubprocessMixin
8 from tests.integration.test_utils import RunSubprocessMixin
9 from tests.integration.test_utils import get_podman_version
10 from tests.integration.test_utils import is_systemd_available
11 from tests.integration.test_utils import podman_compose_path
12 from tests.integration.test_utils import test_path
13
14
15 def compose_yaml_path(suffix: str = "") -> str:
16 return os.path.join(os.path.join(test_path(), "deps"), f"docker-compose{suffix}.yaml")
17
18
19 class TestComposeBaseDeps(unittest.TestCase, RunSubprocessMixin):
20 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
21 def test_deps(self) -> None:
22 try:
23 output, _ = self.run_subprocess_assert_returncode([
24 podman_compose_path(),
25 "-f",
26 compose_yaml_path(),
27 "run",
28 "--rm",
29 "sleep",
30 "/bin/sh",
31 "-c",
32 "wget -O - http://web:8000/hosts",
33 ])
34 self.assertIn(b"HTTP request sent, awaiting response... 200 OK", output)
35 self.assertIn(b"deps_web_1", output)
36 finally:
37 self.run_subprocess_assert_returncode([
38 podman_compose_path(),
39 "-f",
40 compose_yaml_path(),
41 "down",
42 ])
43
44 def test_run_nodeps(self) -> None:
45 try:
46 output, _ = self.run_subprocess_assert_returncode([
47 podman_compose_path(),
48 "-f",
49 compose_yaml_path(),
50 "run",
51 "--rm",
52 "--no-deps",
53 "sleep",
54 "/bin/sh",
55 "-c",
56 "wget -O - http://web:8000/hosts || echo Failed to connect",
57 ])
58 self.assertNotIn(b"HTTP request sent, awaiting response... 200 OK", output)
59 self.assertNotIn(b"deps_web_1", output)
60 self.assertIn(b"Failed to connect", output)
61 finally:
62 self.run_subprocess_assert_returncode([
63 podman_compose_path(),
64 "-f",
65 compose_yaml_path(),
66 "down",
67 ])
68
69 def test_up_nodeps(self) -> None:
70 try:
71 self.run_subprocess_assert_returncode([
72 podman_compose_path(),
73 "-f",
74 compose_yaml_path(),
75 "up",
76 "--no-deps",
77 "--detach",
78 "sleep",
79 ])
80 output, _ = self.run_subprocess_assert_returncode([
81 podman_compose_path(),
82 "-f",
83 compose_yaml_path(),
84 "ps",
85 ])
86 self.assertNotIn(b"deps_web_1", output)
87 self.assertIn(b"deps_sleep_1", output)
88 finally:
89 self.run_subprocess_assert_returncode([
90 podman_compose_path(),
91 "-f",
92 compose_yaml_path(),
93 "down",
94 ])
95
96 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
97 def test_podman_compose_run(self) -> None:
98 """
99 This will test depends_on as well
100 """
101 run_cmd = [
102 "coverage",
103 "run",
104 podman_compose_path(),
105 "-f",
106 os.path.join(test_path(), "deps", "docker-compose.yaml"),
107 "run",
108 "--rm",
109 "sleep",
110 "/bin/sh",
111 "-c",
112 "wget -q -O - http://web:8000/hosts",
113 ]
114
115 out, _ = self.run_subprocess_assert_returncode(run_cmd)
116 self.assertIn(b"127.0.0.1\tlocalhost", out)
117
118 # Run it again to make sure we can run it twice. I saw an issue where a second run, with
119 # the container left up, would fail
120 run_cmd = [
121 "coverage",
122 "run",
123 podman_compose_path(),
124 "-f",
125 os.path.join(test_path(), "deps", "docker-compose.yaml"),
126 "run",
127 "--rm",
128 "sleep",
129 "/bin/sh",
130 "-c",
131 "wget -q -O - http://web:8000/hosts",
132 ]
133
134 out, _ = self.run_subprocess_assert_returncode(run_cmd)
135 self.assertIn(b"127.0.0.1\tlocalhost", out)
136
137 # This leaves a container running. Not sure it's intended, but it matches docker-compose
138 down_cmd = [
139 "coverage",
140 "run",
141 podman_compose_path(),
142 "-f",
143 os.path.join(test_path(), "deps", "docker-compose.yaml"),
144 "down",
145 ]
146
147 self.run_subprocess_assert_returncode(down_cmd)
148
149
150 class TestComposeConditionalDeps(unittest.TestCase, RunSubprocessMixin):
151 @unittest.skipIf(get_podman_version() >= version.parse("5.0.0"), "Breaks as of podman-5.4.2.")
152 def test_deps_succeeds(self) -> None:
153 suffix = "-conditional-succeeds"
154 try:
155 output, _ = self.run_subprocess_assert_returncode([
156 podman_compose_path(),
157 "-f",
158 compose_yaml_path(suffix),
159 "run",
160 "--rm",
161 "sleep",
162 "/bin/sh",
163 "-c",
164 "wget -O - http://web:8000/hosts",
165 ])
166 self.assertIn(b"HTTP request sent, awaiting response... 200 OK", output)
167 self.assertIn(b"deps_web_1", output)
168 finally:
169 self.run_subprocess_assert_returncode([
170 podman_compose_path(),
171 "-f",
172 compose_yaml_path(suffix),
173 "down",
174 ])
175
176 def test_deps_fails(self) -> None:
177 suffix = "-conditional-fails"
178 try:
179 output, _ = self.run_subprocess_assert_returncode([
180 podman_compose_path(),
181 "-f",
182 compose_yaml_path(suffix),
183 "ps",
184 ])
185 self.assertNotIn(b"HTTP request sent, awaiting response... 200 OK", output)
186 self.assertNotIn(b"deps_web_1", output)
187 finally:
188 self.run_subprocess_assert_returncode([
189 podman_compose_path(),
190 "-f",
191 compose_yaml_path(suffix),
192 "down",
193 ])
194
195
196 class TestComposeConditionalDepsHealthy(unittest.TestCase, PodmanAwareRunSubprocessMixin):
197 def setUp(self) -> None:
198 self.podman_version = self.retrieve_podman_version()
199
200 @unittest.skipIf(
201 get_podman_version() > version.parse("4.4.0"), "Breaks as of podman-4.9.5 and podman-5.4.2."
202 )
203 def test_up_deps_healthy(self) -> None:
204 suffix = "-conditional-healthy"
205 try:
206 self.run_subprocess_assert_returncode([
207 podman_compose_path(),
208 "-f",
209 compose_yaml_path(suffix),
210 "up",
211 "sleep",
212 "--detach",
213 ])
214
215 # Since the command `podman wait --condition=healthy` is invalid prior to 4.6.0,
216 # we only validate healthy status for podman 4.6.0+, which won't be tested in the
217 # CI pipeline of the podman-compose project where podman 4.3.1 is employed.
218 podman_ver_major, podman_ver_minor, podman_ver_patch = self.podman_version
219 if podman_ver_major >= 4 and podman_ver_minor >= 6 and podman_ver_patch >= 0:
220 self.run_subprocess_assert_returncode([
221 "podman",
222 "wait",
223 "--condition=running",
224 "deps_web_1",
225 "deps_sleep_1",
226 ])
227
228 # check both web and sleep are running
229 output, _ = self.run_subprocess_assert_returncode([
230 podman_compose_path(),
231 "-f",
232 compose_yaml_path(),
233 "ps",
234 "--format",
235 "{{.ID}}\t{{.Names}}\t{{.Status}}\t{{.StartedAt}}",
236 ])
237
238 # extract container id of web
239 decoded_out = output.decode('utf-8')
240 lines = decoded_out.split("\n")
241
242 web_lines = [line for line in lines if "web" in line]
243 self.assertTrue(web_lines)
244 self.assertEqual(1, len(web_lines))
245 web_cnt_id, web_cnt_name, web_cnt_status, web_cnt_started = web_lines[0].split("\t")
246 self.assertNotEqual("", web_cnt_id)
247 self.assertEqual("deps_web_1", web_cnt_name)
248
249 sleep_lines = [line for line in lines if "sleep" in line]
250 self.assertTrue(sleep_lines)
251 self.assertEqual(1, len(sleep_lines))
252 sleep_cnt_id, sleep_cnt_name, _, sleep_cnt_started = sleep_lines[0].split("\t")
253 self.assertNotEqual("", sleep_cnt_id)
254 self.assertEqual("deps_sleep_1", sleep_cnt_name)
255
256 # When test case is executed inside container like github actions, the absence of
257 # systemd prevents health check from working properly, resulting in failure to
258 # transit to healthy state. As a result, we only assert the `healthy` state where
259 # systemd is functioning.
260 if (
261 is_systemd_available()
262 and podman_ver_major >= 4
263 and podman_ver_minor >= 6
264 and podman_ver_patch >= 0
265 ):
266 self.assertIn("healthy", web_cnt_status)
267 self.assertGreaterEqual(int(sleep_cnt_started), int(web_cnt_started))
268
269 finally:
270 self.run_subprocess_assert_returncode([
271 podman_compose_path(),
272 "-f",
273 compose_yaml_path(suffix),
274 "down",
275 ])