2018-02-19 04:31:29 -05:00
|
|
|
// +build !windows
|
|
|
|
|
2019-03-01 09:34:36 -05:00
|
|
|
package commandconn
|
2018-02-19 04:31:29 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
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()
|
|
|
|
cmd := "sh"
|
|
|
|
args := []string{"-c", "echo hello; echo some error >&2; exit 42"}
|
2019-03-01 09:34:36 -05:00
|
|
|
c, err := New(ctx, cmd, args...)
|
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()
|
|
|
|
cmd := "sh"
|
|
|
|
args := []string{"-c", "echo hello; echo some debug log >&2; exit 0"}
|
2019-03-01 09:34:36 -05:00
|
|
|
c, err := New(ctx, cmd, args...)
|
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))
|
|
|
|
}
|