Skip to content

Commit ee42499

Browse files
committed
ci: fix a race in TestExecIn and TestExecInTTY
Signed-off-by: lifubang <[email protected]>
1 parent 9112335 commit ee42499

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

libcontainer/integration/execin_test.go

+42-15
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,33 @@ func TestExecIn(t *testing.T) {
3030
// Execute a first process in the container
3131
stdinR, stdinW, err := os.Pipe()
3232
ok(t, err)
33+
stdoutR, stdoutW, err := os.Pipe()
34+
ok(t, err)
35+
defer stdinR.Close() //nolint: errcheck
36+
defer stdinW.Close() //nolint: errcheck
37+
defer stdoutR.Close() //nolint: errcheck
38+
defer stdoutW.Close() //nolint: errcheck
39+
40+
ch := waitStdOut(stdoutR)
3341
process := &libcontainer.Process{
34-
Cwd: "/",
35-
Args: []string{"cat"},
36-
Env: standardEnvironment,
37-
Stdin: stdinR,
38-
Init: true,
42+
Cwd: "/",
43+
Args: []string{"cat", "/proc/self/cmdline", "-"},
44+
Env: standardEnvironment,
45+
Stdin: stdinR,
46+
Stdout: stdoutW,
47+
Init: true,
3948
}
4049
err = container.Run(process)
41-
_ = stdinR.Close()
42-
defer stdinW.Close() //nolint: errcheck
50+
defer func() {
51+
_, _ = stdinW.Write([]byte("hello"))
52+
_ = stdinW.Close()
53+
if _, err := process.Wait(); err != nil {
54+
t.Log(err)
55+
}
56+
}()
57+
ok(t, err)
58+
59+
err = <-ch
4360
ok(t, err)
4461

4562
buffers := newStdBuffers()
@@ -55,8 +72,6 @@ func TestExecIn(t *testing.T) {
5572
err = container.Run(ps)
5673
ok(t, err)
5774
waitProcess(ps, t)
58-
_ = stdinW.Close()
59-
waitProcess(process, t)
6075

6176
out := buffers.Stdout.String()
6277
if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") {
@@ -242,23 +257,35 @@ func TestExecInTTY(t *testing.T) {
242257
// Execute a first process in the container
243258
stdinR, stdinW, err := os.Pipe()
244259
ok(t, err)
260+
stdoutR, stdoutW, err := os.Pipe()
261+
ok(t, err)
262+
defer stdinR.Close() //nolint: errcheck
263+
defer stdinW.Close() //nolint: errcheck
264+
defer stdoutR.Close() //nolint: errcheck
265+
defer stdoutW.Close() //nolint: errcheck
266+
267+
ch := waitStdOut(stdoutR)
245268
process := &libcontainer.Process{
246-
Cwd: "/",
247-
Args: []string{"cat"},
248-
Env: standardEnvironment,
249-
Stdin: stdinR,
250-
Init: true,
269+
Cwd: "/",
270+
Args: []string{"cat", "/proc/self/cmdline", "-"},
271+
Env: standardEnvironment,
272+
Stdin: stdinR,
273+
Stdout: stdoutW,
274+
Init: true,
251275
}
252276
err = container.Run(process)
253-
_ = stdinR.Close()
254277
defer func() {
278+
_, _ = stdinW.Write([]byte("hello"))
255279
_ = stdinW.Close()
256280
if _, err := process.Wait(); err != nil {
257281
t.Log(err)
258282
}
259283
}()
260284
ok(t, err)
261285

286+
err = <-ch
287+
ok(t, err)
288+
262289
ps := &libcontainer.Process{
263290
Cwd: "/",
264291
Args: []string{"ps"},

libcontainer/integration/utils_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,25 @@ func runContainerOk(t *testing.T, config *configs.Config, args ...string) *stdBu
231231
func destroyContainer(container *libcontainer.Container) {
232232
_ = container.Destroy()
233233
}
234+
235+
func waitStdOut(stdout *os.File) chan error {
236+
ch := make(chan error, 1)
237+
buf := make([]byte, 1)
238+
go func() {
239+
defer close(ch)
240+
241+
for {
242+
n, err := stdout.Read(buf)
243+
if err != nil {
244+
ch <- err
245+
return
246+
}
247+
248+
if n >= 0 {
249+
ch <- nil
250+
return
251+
}
252+
}
253+
}()
254+
return ch
255+
}

0 commit comments

Comments
 (0)