Skip to content

Commit a8ab5cd

Browse files
authored
Merge pull request #1220 from TianZong48/ci/flaky-test
wait for detached container running before exec
2 parents b140bc1 + 15decf3 commit a8ab5cd

File tree

8 files changed

+31
-2
lines changed

8 files changed

+31
-2
lines changed

cmd/nerdctl/commit_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func TestCommit(t *testing.T) {
4040
"false",
4141
} {
4242
base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "infinity").AssertOK()
43+
base.EnsureContainerStarted(testContainer)
4344
base.Cmd("exec", testContainer, "sh", "-euxc", `echo hello-test-commit > /foo`).AssertOK()
4445
base.Cmd(
4546
"commit",

cmd/nerdctl/exec_linux_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ func TestExecWithUser(t *testing.T) {
2626
t.Parallel()
2727
base := testutil.NewBase(t)
2828
testContainer := testutil.Identifier(t)
29-
defer base.Cmd("rm", "-f", testContainer).Run()
3029

30+
defer base.Cmd("rm", "-f", testContainer).Run()
3131
base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "infinity").AssertOK()
32+
base.EnsureContainerStarted(testContainer)
3233

3334
testCases := map[string]string{
3435
"": "uid=0(root) gid=0(root)",

cmd/nerdctl/exec_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestExec(t *testing.T) {
3030
defer base.Cmd("rm", "-f", testContainer).Run()
3131

3232
base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK()
33+
base.EnsureContainerStarted(testContainer)
3334

3435
base.Cmd("exec", testContainer, "echo", "success").AssertOutExactly("success\n")
3536
}
@@ -42,6 +43,7 @@ func TestExecWithDoubleDash(t *testing.T) {
4243
defer base.Cmd("rm", "-f", testContainer).Run()
4344

4445
base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK()
46+
base.EnsureContainerStarted(testContainer)
4547

4648
base.Cmd("exec", testContainer, "--", "echo", "success").AssertOutExactly("success\n")
4749
}
@@ -56,6 +58,7 @@ func TestExecStdin(t *testing.T) {
5658
testContainer := testutil.Identifier(t)
5759
defer base.Cmd("rm", "-f", testContainer).Run()
5860
base.Cmd("run", "-d", "--name", testContainer, testutil.CommonImage, "sleep", "1h").AssertOK()
61+
base.EnsureContainerStarted(testContainer)
5962

6063
const testStr = "test-exec-stdin"
6164
opts := []func(*testutil.Cmd){

cmd/nerdctl/ps_linux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func prepareTest(t *testing.T) (*testutil.Base, string) {
4444
testutil.CommonImage,
4545
"top",
4646
}...).AssertOK()
47+
base.EnsureContainerStarted(testContainerName)
4748

4849
// dd if=/dev/zero of=test_file bs=1M count=25
4950
// let the container occupy 25MiB space.

cmd/nerdctl/rename_linux_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ func TestRenameUpdateHosts(t *testing.T) {
4545

4646
defer base.Cmd("rm", "-f", testContainerName).Run()
4747
base.Cmd("run", "-d", "--name", testContainerName, testutil.CommonImage, "sleep", "infinity").AssertOK()
48+
base.EnsureContainerStarted(testContainerName)
4849

4950
defer base.Cmd("rm", "-f", testContainerName+"_1").Run()
5051
base.Cmd("run", "-d", "--name", testContainerName+"_1", testutil.CommonImage, "sleep", "infinity").AssertOK()
52+
base.EnsureContainerStarted(testContainerName + "_1")
5153

5254
defer base.Cmd("rm", "-f", testContainerName+"_new").Run()
5355
base.Cmd("exec", testContainerName, "cat", "/etc/hosts").AssertOutContains(testContainerName + "_1")

cmd/nerdctl/run_cgroup_linux_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ func TestRunCgroupV2(t *testing.T) {
109109
"cat", "cpu.max", "memory.max", "memory.swap.max",
110110
"pids.max", "cpu.weight", "cpuset.cpus", "cpuset.mems").AssertOutExactly(expected1)
111111

112+
defer base.Cmd("rm", "-f", testutil.Identifier(t)+"-testUpdate2").Run()
112113
base.Cmd("run", "--name", testutil.Identifier(t)+"-testUpdate2", "-w", "/sys/fs/cgroup", "-d",
113114
testutil.AlpineImage, "sleep", "infinity").AssertOK()
114-
defer base.Cmd("rm", "-f", testutil.Identifier(t)+"-testUpdate2").Run()
115+
base.EnsureContainerStarted(testutil.Identifier(t) + "-testUpdate2")
116+
115117
base.Cmd("update", "--cpu-quota", "42000", "--cpuset-mems", "0", "--cpu-period", "100000",
116118
"--memory", "42m", "--memory-reservation", "6m", "--memory-swap", "100m",
117119
"--pids-limit", "42", "--cpu-shares", "2000", "--cpuset-cpus", "0-1",

cmd/nerdctl/run_mount_linux_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,12 @@ RUN echo -n "rev0" > /mnt/file
223223
base.Cmd("run", "-d", "--name", containerName, "-v", volumeName+":/mnt", imageName, "sleep", "infinity").AssertOK()
224224
}
225225
runContainer()
226+
base.EnsureContainerStarted(containerName)
226227
base.Cmd("exec", containerName, "cat", "/mnt/file").AssertOutExactly("rev0")
227228
base.Cmd("exec", containerName, "sh", "-euc", "echo -n \"rev1\" >/mnt/file").AssertOK()
228229
base.Cmd("rm", "-f", containerName).AssertOK()
229230
runContainer()
231+
base.EnsureContainerStarted(containerName)
230232
base.Cmd("exec", containerName, "cat", "/mnt/file").AssertOutExactly("rev1")
231233
}
232234

pkg/testutil/testutil.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ func (b *Base) InfoNative() native.Info {
255255
}
256256
return info
257257
}
258+
func (b *Base) EnsureContainerStarted(con string) {
259+
b.T.Helper()
260+
261+
const (
262+
maxRetry = 5
263+
sleep = time.Second
264+
)
265+
for i := 0; i < maxRetry; i++ {
266+
if b.InspectContainer(con).State.Running {
267+
b.T.Logf("container %s is now running", con)
268+
return
269+
}
270+
b.T.Logf("(retry=%d)", i+1)
271+
time.Sleep(sleep)
272+
}
273+
b.T.Fatalf("conainer %s not running", con)
274+
}
258275

259276
type Cmd struct {
260277
icmd.Cmd

0 commit comments

Comments
 (0)