1
1
# SPDX-License-Identifier: GPL-2.0
2
2
import os
3
+ import time
3
4
import unittest
4
5
5
6
from tests .integration .test_utils import RunSubprocessMixin
7
+ from tests .integration .test_utils import is_systemd_available
6
8
from tests .integration .test_utils import podman_compose_path
7
9
from tests .integration .test_utils import test_path
8
10
@@ -14,7 +16,7 @@ def compose_yaml_path(suffix=""):
14
16
class TestComposeBaseDeps (unittest .TestCase , RunSubprocessMixin ):
15
17
def test_deps (self ):
16
18
try :
17
- output , error = self .run_subprocess_assert_returncode ([
19
+ output , _ = self .run_subprocess_assert_returncode ([
18
20
podman_compose_path (),
19
21
"-f" ,
20
22
compose_yaml_path (),
@@ -37,7 +39,7 @@ def test_deps(self):
37
39
38
40
def test_run_nodeps (self ):
39
41
try :
40
- output , error = self .run_subprocess_assert_returncode ([
42
+ output , _ = self .run_subprocess_assert_returncode ([
41
43
podman_compose_path (),
42
44
"-f" ,
43
45
compose_yaml_path (),
@@ -71,7 +73,7 @@ def test_up_nodeps(self):
71
73
"--detach" ,
72
74
"sleep" ,
73
75
])
74
- output , error = self .run_subprocess_assert_returncode ([
76
+ output , _ = self .run_subprocess_assert_returncode ([
75
77
podman_compose_path (),
76
78
"-f" ,
77
79
compose_yaml_path (),
@@ -144,7 +146,7 @@ class TestComposeConditionalDeps(unittest.TestCase, RunSubprocessMixin):
144
146
def test_deps_succeeds (self ):
145
147
suffix = "-conditional-succeeds"
146
148
try :
147
- output , error = self .run_subprocess_assert_returncode ([
149
+ output , _ = self .run_subprocess_assert_returncode ([
148
150
podman_compose_path (),
149
151
"-f" ,
150
152
compose_yaml_path (suffix ),
@@ -168,7 +170,7 @@ def test_deps_succeeds(self):
168
170
def test_deps_fails (self ):
169
171
suffix = "-conditional-fails"
170
172
try :
171
- output , error = self .run_subprocess_assert_returncode ([
173
+ output , _ = self .run_subprocess_assert_returncode ([
172
174
podman_compose_path (),
173
175
"-f" ,
174
176
compose_yaml_path (suffix ),
@@ -183,3 +185,74 @@ def test_deps_fails(self):
183
185
compose_yaml_path (suffix ),
184
186
"down" ,
185
187
])
188
+
189
+
190
+ class TestComposeConditionalDepsHealthy (unittest .TestCase , RunSubprocessMixin ):
191
+ def test_up_deps_healthy (self ):
192
+ suffix = "-conditional-healthy"
193
+ try :
194
+ self .run_subprocess_assert_returncode ([
195
+ podman_compose_path (),
196
+ "-f" ,
197
+ compose_yaml_path (suffix ),
198
+ "up" ,
199
+ "sleep" ,
200
+ "--detach" ,
201
+ ])
202
+
203
+ # The `podman wait --condition=healthy` is invalid prior to 4.6.0.
204
+ # Since the podman-compose project uses podman 4.3.1 in github actions, we
205
+ # use sleep as workaround to wait until the `sleep` container becomes running.
206
+ time .sleep (3 )
207
+
208
+ # self.run_subprocess_assert_returncode([
209
+ # "podman",
210
+ # "wait",
211
+ # "--condition=running",
212
+ # "deps_web_1",
213
+ # "deps_sleep_1",
214
+ # ])
215
+
216
+ # check both web and sleep are running
217
+ output , _ = self .run_subprocess_assert_returncode ([
218
+ podman_compose_path (),
219
+ "-f" ,
220
+ compose_yaml_path (),
221
+ "ps" ,
222
+ "--format" ,
223
+ "{{.ID}}\t {{.Names}}\t {{.Status}}\t {{.StartedAt}}" ,
224
+ ])
225
+ # extract container id of web
226
+ decoded_out = output .decode ('utf-8' )
227
+ web_cnt_id = ""
228
+ web_cnt_name = ""
229
+ web_cnt_status = ""
230
+ web_cnt_started = ""
231
+ sleep_cnt_id = ""
232
+ sleep_cnt_name = ""
233
+ sleep_cnt_started = ""
234
+ for line in decoded_out .split ("\n " ):
235
+ if "web" in line :
236
+ web_cnt_id , web_cnt_name , web_cnt_status , web_cnt_started = line .split ("\t " )
237
+ if "sleep" in line :
238
+ sleep_cnt_id , sleep_cnt_name , _ , sleep_cnt_started = line .split ("\t " )
239
+ self .assertNotEqual ("" , web_cnt_id )
240
+ self .assertEqual ("deps_web_1" , web_cnt_name )
241
+ self .assertNotEqual ("" , sleep_cnt_id )
242
+ self .assertEqual ("deps_sleep_1" , sleep_cnt_name )
243
+ if is_systemd_available ():
244
+ self .assertIn ("healthy" , web_cnt_status )
245
+ else :
246
+ # When test case is executed inside container like github actions, the absence of
247
+ # systemd prevents health check from working properly, resulting in failure to
248
+ # transit to healthy state. As a result, we only assert the `running` state
249
+ self .assertIn ("running" , web_cnt_status )
250
+ self .assertGreaterEqual (int (sleep_cnt_started ), int (web_cnt_started ))
251
+
252
+ finally :
253
+ self .run_subprocess_assert_returncode ([
254
+ podman_compose_path (),
255
+ "-f" ,
256
+ compose_yaml_path (),
257
+ "down" ,
258
+ ])
0 commit comments