Skip to content

Commit

Permalink
ci: fix a race in TestExecIn and TestExecInTTY
Browse files Browse the repository at this point in the history
Signed-off-by: lifubang <[email protected]>
  • Loading branch information
lifubang committed Oct 14, 2024
1 parent 9112335 commit a6c839a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
57 changes: 42 additions & 15 deletions libcontainer/integration/execin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,33 @@ func TestExecIn(t *testing.T) {
// Execute a first process in the container
stdinR, stdinW, err := os.Pipe()
ok(t, err)
stdoutR, stdoutW, err := os.Pipe()
ok(t, err)
defer stdinR.Close() //nolint: errcheck
defer stdinW.Close() //nolint: errcheck
defer stdoutR.Close() //nolint: errcheck
defer stdoutW.Close() //nolint: errcheck

ch := waitStdOut(stdoutR)
process := &libcontainer.Process{
Cwd: "/",
Args: []string{"cat"},
Env: standardEnvironment,
Stdin: stdinR,
Init: true,
Cwd: "/",
Args: []string{"cat", "/proc/self/cmdline", "-"},
Env: standardEnvironment,
Stdin: stdinR,
Stdout: stdoutW,
Init: true,
}
err = container.Run(process)
_ = stdinR.Close()
defer stdinW.Close() //nolint: errcheck
defer func() {
stdinW.Write([]byte("hello"))

Check failure on line 51 in libcontainer/integration/execin_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `stdinW.Write` is not checked (errcheck)
_ = stdinW.Close()
if _, err := process.Wait(); err != nil {
t.Log(err)
}
}()
ok(t, err)

err = <-ch
ok(t, err)

buffers := newStdBuffers()
Expand All @@ -55,8 +72,6 @@ func TestExecIn(t *testing.T) {
err = container.Run(ps)
ok(t, err)
waitProcess(ps, t)
_ = stdinW.Close()
waitProcess(process, t)

out := buffers.Stdout.String()
if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") {
Expand Down Expand Up @@ -242,23 +257,35 @@ func TestExecInTTY(t *testing.T) {
// Execute a first process in the container
stdinR, stdinW, err := os.Pipe()
ok(t, err)
stdoutR, stdoutW, err := os.Pipe()
ok(t, err)
defer stdinR.Close() //nolint: errcheck
defer stdinW.Close() //nolint: errcheck
defer stdoutR.Close() //nolint: errcheck
defer stdoutW.Close() //nolint: errcheck

ch := waitStdOut(stdoutR)
process := &libcontainer.Process{
Cwd: "/",
Args: []string{"cat"},
Env: standardEnvironment,
Stdin: stdinR,
Init: true,
Cwd: "/",
Args: []string{"cat", "/proc/self/cmdline", "-"},
Env: standardEnvironment,
Stdin: stdinR,
Stdout: stdoutW,
Init: true,
}
err = container.Run(process)
_ = stdinR.Close()
defer func() {
stdinW.Write([]byte("hello"))

Check failure on line 278 in libcontainer/integration/execin_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `stdinW.Write` is not checked (errcheck)
_ = stdinW.Close()
if _, err := process.Wait(); err != nil {
t.Log(err)
}
}()
ok(t, err)

err = <-ch
ok(t, err)

ps := &libcontainer.Process{
Cwd: "/",
Args: []string{"ps"},
Expand Down
22 changes: 22 additions & 0 deletions libcontainer/integration/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,25 @@ func runContainerOk(t *testing.T, config *configs.Config, args ...string) *stdBu
func destroyContainer(container *libcontainer.Container) {
_ = container.Destroy()
}

func waitStdOut(stdout *os.File) chan error {
ch := make(chan error, 1)
buf := make([]byte, 1)
go func() {
defer close(ch)

for {
n, err := stdout.Read(buf)
if err != nil {
ch <- err
return
}

if n >= 0 {
ch <- nil
return
}
}
}()
return ch
}

0 comments on commit a6c839a

Please sign in to comment.