@@ -18,11 +18,14 @@ package container
1818
1919import (
2020 "errors"
21+ "strings"
2122 "testing"
23+ "time"
2224
2325 "github.com/containerd/nerdctl/mod/tigron/expect"
2426 "github.com/containerd/nerdctl/mod/tigron/require"
2527 "github.com/containerd/nerdctl/mod/tigron/test"
28+ "github.com/containerd/nerdctl/mod/tigron/tig"
2629
2730 "github.com/containerd/nerdctl/v2/pkg/testutil"
2831 "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
@@ -89,18 +92,35 @@ func TestRunWithSystemdTrueEnabled(t *testing.T) {
8992 Output : expect .Contains ("SIGRTMIN+3" ),
9093 })
9194
92- // waits for systemd to become ready and lists systemd jobs
93- return helpers .Command ("exec" , data .Identifier (), "sh" , "-c" , "--" , `tries=0
94- until systemctl is-system-running >/dev/null 2>&1; do
95- >&2 printf "Waiting for systemd to come up...\n"
96- sleep 1s
97- tries=$(( tries + 1))
98- [ $tries -lt 10 ] || {
99- >&2 printf "systemd failed to come up in a reasonable amount of time\n"
100- exit 1
101- }
102- done
103- systemctl list-jobs` )
95+ // Poll for systemd to become ready using the same pattern as EnsureContainerStarted
96+ const maxRetry = 30
97+ const sleep = 5 * time .Second
98+ systemdReady := false
99+
100+ for i := 0 ; i < maxRetry && ! systemdReady ; i ++ {
101+ helpers .Command ("exec" , data .Identifier (), "sh" , "-c" , "--" , "systemctl is-system-running" ).
102+ Run (& test.Expected {
103+ ExitCode : expect .ExitCodeNoCheck ,
104+ Output : func (stdout string , t tig.T ) {
105+ // Silent return on error or empty output
106+ if stdout == "" {
107+ return
108+ }
109+ // Check for systemd ready states
110+ if strings .Contains (stdout , "running" ) || strings .Contains (stdout , "degraded" ) {
111+ systemdReady = true
112+ }
113+ },
114+ })
115+ time .Sleep (sleep )
116+ }
117+
118+ if ! systemdReady {
119+ helpers .T ().Log ("systemd did not become ready after 30 seconds" )
120+ helpers .T ().FailNow ()
121+ }
122+
123+ return helpers .Command ("exec" , data .Identifier (), "sh" , "-c" , "--" , "systemctl list-jobs" )
104124 }
105125
106126 testCase .Expected = test .Expects (expect .ExitCodeSuccess , nil , expect .Contains ("jobs" ))
0 commit comments