2022-03-26 13:34:10 -04:00
|
|
|
//go:build !windows
|
2018-02-19 04:31:29 -05:00
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
package commandconn
|
2018-02-19 04:31:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2023-06-30 10:03:19 -04:00
|
|
|
"io/fs"
|
2018-02-19 04:31:29 -05:00
|
|
|
"testing"
|
2023-04-24 04:57:16 -04:00
|
|
|
"time"
|
2018-02-19 04:31:29 -05:00
|
|
|
|
2023-04-24 04:57:16 -04:00
|
|
|
"github.com/docker/docker/pkg/process"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-02-19 04:31:29 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// For https://github.com/docker/cli/pull/1014#issuecomment-409308139
|
2019-03-01 09:34:36 -05:00
|
|
|
func TestEOFWithError(t *testing.T) {
|
2018-02-19 04:31:29 -05:00
|
|
|
ctx := context.TODO()
|
2024-02-10 07:59:36 -05:00
|
|
|
c, err := New(ctx, "sh", "-c", "echo hello; echo some error >&2; exit 42")
|
2018-02-19 04:31:29 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
b := make([]byte, 32)
|
|
|
|
n, err := c.Read(b)
|
|
|
|
assert.Check(t, is.Equal(len("hello\n"), n))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
n, err = c.Read(b)
|
|
|
|
assert.Check(t, is.Equal(0, n))
|
|
|
|
assert.ErrorContains(t, err, "some error")
|
|
|
|
assert.ErrorContains(t, err, "42")
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
func TestEOFWithoutError(t *testing.T) {
|
2018-02-19 04:31:29 -05:00
|
|
|
ctx := context.TODO()
|
2024-02-10 07:59:36 -05:00
|
|
|
c, err := New(ctx, "sh", "-c", "echo hello; echo some debug log >&2; exit 0")
|
2018-02-19 04:31:29 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
b := make([]byte, 32)
|
|
|
|
n, err := c.Read(b)
|
|
|
|
assert.Check(t, is.Equal(len("hello\n"), n))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
n, err = c.Read(b)
|
|
|
|
assert.Check(t, is.Equal(0, n))
|
|
|
|
assert.Check(t, is.Equal(io.EOF, err))
|
|
|
|
}
|
2023-04-24 04:57:16 -04:00
|
|
|
|
|
|
|
func TestCloseRunningCommand(t *testing.T) {
|
2024-02-10 07:59:36 -05:00
|
|
|
ctx := context.TODO()
|
2023-04-24 04:57:16 -04:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
|
|
|
|
go func() {
|
2024-02-10 07:59:36 -05:00
|
|
|
c, err := New(ctx, "sh", "-c", "while true; sleep 1; done")
|
2023-04-24 04:57:16 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
cmdConn := c.(*commandConn)
|
|
|
|
assert.Check(t, process.Alive(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))
|
|
|
|
|
|
|
|
err = cmdConn.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
t.Error("test did not finish in time")
|
|
|
|
case <-done:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseTwice(t *testing.T) {
|
2024-02-10 07:59:36 -05:00
|
|
|
ctx := context.TODO()
|
2023-04-24 04:57:16 -04:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2024-02-10 07:59:36 -05:00
|
|
|
c, err := New(ctx, "sh", "-c", "echo hello; sleep 1; exit 0")
|
2023-04-24 04:57:16 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
cmdConn := c.(*commandConn)
|
|
|
|
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
b := make([]byte, 32)
|
|
|
|
n, err := c.Read(b)
|
|
|
|
assert.Check(t, is.Equal(len("hello\n"), n))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
err = cmdConn.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
err = cmdConn.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Error("test did not finish in time")
|
|
|
|
case <-done:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEOFTimeout(t *testing.T) {
|
2024-02-10 07:59:36 -05:00
|
|
|
ctx := context.TODO()
|
2023-04-24 04:57:16 -04:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2024-02-10 07:59:36 -05:00
|
|
|
c, err := New(ctx, "sh", "-c", "sleep 20")
|
2023-04-24 04:57:16 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
cmdConn := c.(*commandConn)
|
|
|
|
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
cmdConn.stdout = mockStdoutEOF{}
|
|
|
|
|
|
|
|
b := make([]byte, 32)
|
|
|
|
n, err := c.Read(b)
|
|
|
|
assert.Check(t, is.Equal(0, n))
|
|
|
|
assert.ErrorContains(t, err, "did not exit after EOF")
|
|
|
|
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// after receiving an EOF, we try to kill the command
|
|
|
|
// if it doesn't exit after 10s, we throw an error
|
|
|
|
select {
|
|
|
|
case <-time.After(12 * time.Second):
|
|
|
|
t.Error("test did not finish in time")
|
|
|
|
case <-done:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStdoutEOF struct{}
|
|
|
|
|
|
|
|
func (mockStdoutEOF) Read(_ []byte) (int, error) {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mockStdoutEOF) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseWhileWriting(t *testing.T) {
|
2024-02-10 07:59:36 -05:00
|
|
|
ctx := context.TODO()
|
|
|
|
c, err := New(ctx, "sh", "-c", "while true; sleep 1; done")
|
2023-04-24 04:57:16 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
cmdConn := c.(*commandConn)
|
|
|
|
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
writeErrC := make(chan error)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
n, err := c.Write([]byte("hello"))
|
|
|
|
if err != nil {
|
|
|
|
writeErrC <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Equal(t, n, len("hello"))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = c.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
writeErr := <-writeErrC
|
2023-06-30 10:03:19 -04:00
|
|
|
assert.ErrorContains(t, writeErr, "file already closed")
|
|
|
|
assert.Check(t, is.ErrorIs(writeErr, fs.ErrClosed))
|
2023-04-24 04:57:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCloseWhileReading(t *testing.T) {
|
2024-02-10 07:59:36 -05:00
|
|
|
ctx := context.TODO()
|
|
|
|
c, err := New(ctx, "sh", "-c", "while true; sleep 1; done")
|
2023-04-24 04:57:16 -04:00
|
|
|
assert.NilError(t, err)
|
|
|
|
cmdConn := c.(*commandConn)
|
|
|
|
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
readErrC := make(chan error)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
b := make([]byte, 32)
|
|
|
|
n, err := c.Read(b)
|
|
|
|
if err != nil {
|
|
|
|
readErrC <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.Check(t, is.Equal(0, n))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = cmdConn.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, !process.Alive(cmdConn.cmd.Process.Pid))
|
|
|
|
|
|
|
|
readErr := <-readErrC
|
2023-06-30 10:03:19 -04:00
|
|
|
assert.Check(t, is.ErrorIs(readErr, fs.ErrClosed))
|
2023-04-24 04:57:16 -04:00
|
|
|
}
|