Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions cli/connhelper/commandconn/commandconn_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ package commandconn

import (
"context"
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strconv"
"syscall"
"testing"
"time"

"github.com/docker/docker/pkg/process"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
Expand Down Expand Up @@ -51,16 +56,16 @@ func TestCloseRunningCommand(t *testing.T) {
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
assert.NilError(t, err)
cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))

n, err := c.Write([]byte("hello"))
assert.Check(t, is.Equal(len("hello"), n))
assert.NilError(t, err)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))

err = cmdConn.Close()
assert.NilError(t, err)
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
done <- struct{}{}
}()

Expand All @@ -79,7 +84,7 @@ func TestCloseTwice(t *testing.T) {
c, err := New(ctx, "sh", "-c", "echo hello; sleep 1; exit 0")
assert.NilError(t, err)
cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))

b := make([]byte, 32)
n, err := c.Read(b)
Expand All @@ -88,11 +93,11 @@ func TestCloseTwice(t *testing.T) {

err = cmdConn.Close()
assert.NilError(t, err)
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))

err = cmdConn.Close()
assert.NilError(t, err)
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))
done <- struct{}{}
}()

Expand All @@ -111,7 +116,7 @@ func TestEOFTimeout(t *testing.T) {
c, err := New(ctx, "sh", "-c", "sleep 20")
assert.NilError(t, err)
cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))

cmdConn.stdout = mockStdoutEOF{}

Expand Down Expand Up @@ -148,7 +153,7 @@ func TestCloseWhileWriting(t *testing.T) {
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
assert.NilError(t, err)
cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))

writeErrC := make(chan error)
go func() {
Expand All @@ -164,7 +169,7 @@ func TestCloseWhileWriting(t *testing.T) {

err = c.Close()
assert.NilError(t, err)
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))

writeErr := <-writeErrC
assert.ErrorContains(t, writeErr, "file already closed")
Expand All @@ -176,7 +181,7 @@ func TestCloseWhileReading(t *testing.T) {
c, err := New(ctx, "sh", "-c", "while true; do sleep 1; done")
assert.NilError(t, err)
cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, processAlive(cmdConn.cmd.Process.Pid))

readErrC := make(chan error)
go func() {
Expand All @@ -193,8 +198,37 @@ func TestCloseWhileReading(t *testing.T) {

err = cmdConn.Close()
assert.NilError(t, err)
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
assert.Check(t, !processAlive(cmdConn.cmd.Process.Pid))

readErr := <-readErrC
assert.Check(t, is.ErrorIs(readErr, fs.ErrClosed))
}

// processAlive returns true if a process with a given pid is running. It only considers
// positive PIDs; 0 (all processes in the current process group), -1 (all processes
// with a PID larger than 1), and negative (-n, all processes in process group
// "n") values for pid are never considered to be alive.
//
// It was forked from https://github.com/moby/moby/blob/v28.3.3/pkg/process/process_unix.go#L17-L42
func processAlive(pid int) bool {
if pid < 1 {
return false
}
switch runtime.GOOS {
case "darwin":
// OS X does not have a proc filesystem. Use kill -0 pid to judge if the
// process exists. From KILL(2): https://www.freebsd.org/cgi/man.cgi?query=kill&sektion=2&manpath=OpenDarwin+7.2.1
//
// Sig may be one of the signals specified in sigaction(2) or it may
// be 0, in which case error checking is performed but no signal is
// actually sent. This can be used to check the validity of pid.
err := syscall.Kill(pid, 0)

// Either the PID was found (no error), or we get an EPERM, which means
// the PID exists, but we don't have permissions to signal it.
return err == nil || errors.Is(err, syscall.EPERM)
default:
_, err := os.Stat(filepath.Join("/proc", strconv.Itoa(pid)))
return err == nil
}
}
3 changes: 0 additions & 3 deletions vendor/github.com/docker/docker/pkg/process/doc.go

This file was deleted.

82 changes: 0 additions & 82 deletions vendor/github.com/docker/docker/pkg/process/process_unix.go

This file was deleted.

45 changes: 0 additions & 45 deletions vendor/github.com/docker/docker/pkg/process/process_windows.go

This file was deleted.

1 change: 0 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ github.com/docker/docker/internal/lazyregexp
github.com/docker/docker/internal/multierror
github.com/docker/docker/pkg/homedir
github.com/docker/docker/pkg/jsonmessage
github.com/docker/docker/pkg/process
github.com/docker/docker/pkg/progress
github.com/docker/docker/pkg/stdcopy
github.com/docker/docker/pkg/streamformatter
Expand Down
Loading