cli/connhelper/commandconn: inline variables

Inline the variables used to define the command + args used in the
tests, which makes it slightly easier to see what's run.

Also explicitly define a context, in case we want to add telemetry
to these tests.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2024-02-10 13:59:36 +01:00
parent 34797d1678
commit 8bae662713
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 12 additions and 26 deletions

View File

@ -17,9 +17,7 @@ import (
// For https://github.com/docker/cli/pull/1014#issuecomment-409308139 // For https://github.com/docker/cli/pull/1014#issuecomment-409308139
func TestEOFWithError(t *testing.T) { func TestEOFWithError(t *testing.T) {
ctx := context.TODO() ctx := context.TODO()
cmd := "sh" c, err := New(ctx, "sh", "-c", "echo hello; echo some error >&2; exit 42")
args := []string{"-c", "echo hello; echo some error >&2; exit 42"}
c, err := New(ctx, cmd, args...)
assert.NilError(t, err) assert.NilError(t, err)
b := make([]byte, 32) b := make([]byte, 32)
n, err := c.Read(b) n, err := c.Read(b)
@ -33,9 +31,7 @@ func TestEOFWithError(t *testing.T) {
func TestEOFWithoutError(t *testing.T) { func TestEOFWithoutError(t *testing.T) {
ctx := context.TODO() ctx := context.TODO()
cmd := "sh" c, err := New(ctx, "sh", "-c", "echo hello; echo some debug log >&2; exit 0")
args := []string{"-c", "echo hello; echo some debug log >&2; exit 0"}
c, err := New(ctx, cmd, args...)
assert.NilError(t, err) assert.NilError(t, err)
b := make([]byte, 32) b := make([]byte, 32)
n, err := c.Read(b) n, err := c.Read(b)
@ -47,14 +43,12 @@ func TestEOFWithoutError(t *testing.T) {
} }
func TestCloseRunningCommand(t *testing.T) { func TestCloseRunningCommand(t *testing.T) {
cmd := "sh" ctx := context.TODO()
args := []string{"-c", "while true; sleep 1; done"}
done := make(chan struct{}) done := make(chan struct{})
defer close(done) defer close(done)
go func() { go func() {
c, err := New(context.TODO(), cmd, args...) c, err := New(ctx, "sh", "-c", "while true; sleep 1; done")
assert.NilError(t, err) assert.NilError(t, err)
cmdConn := c.(*commandConn) cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
@ -79,12 +73,10 @@ func TestCloseRunningCommand(t *testing.T) {
} }
func TestCloseTwice(t *testing.T) { func TestCloseTwice(t *testing.T) {
cmd := "sh" ctx := context.TODO()
args := []string{"-c", "echo hello; sleep 1; exit 0"}
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
c, err := New(context.TODO(), cmd, args...) c, err := New(ctx, "sh", "-c", "echo hello; sleep 1; exit 0")
assert.NilError(t, err) assert.NilError(t, err)
cmdConn := c.(*commandConn) cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
@ -113,12 +105,10 @@ func TestCloseTwice(t *testing.T) {
} }
func TestEOFTimeout(t *testing.T) { func TestEOFTimeout(t *testing.T) {
cmd := "sh" ctx := context.TODO()
args := []string{"-c", "sleep 20"}
done := make(chan struct{}) done := make(chan struct{})
go func() { go func() {
c, err := New(context.TODO(), cmd, args...) c, err := New(ctx, "sh", "-c", "sleep 20")
assert.NilError(t, err) assert.NilError(t, err)
cmdConn := c.(*commandConn) cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
@ -154,10 +144,8 @@ func (mockStdoutEOF) Close() error {
} }
func TestCloseWhileWriting(t *testing.T) { func TestCloseWhileWriting(t *testing.T) {
cmd := "sh" ctx := context.TODO()
args := []string{"-c", "while true; sleep 1; done"} c, err := New(ctx, "sh", "-c", "while true; sleep 1; done")
c, err := New(context.TODO(), cmd, args...)
assert.NilError(t, err) assert.NilError(t, err)
cmdConn := c.(*commandConn) cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))
@ -184,10 +172,8 @@ func TestCloseWhileWriting(t *testing.T) {
} }
func TestCloseWhileReading(t *testing.T) { func TestCloseWhileReading(t *testing.T) {
cmd := "sh" ctx := context.TODO()
args := []string{"-c", "while true; sleep 1; done"} c, err := New(ctx, "sh", "-c", "while true; sleep 1; done")
c, err := New(context.TODO(), cmd, args...)
assert.NilError(t, err) assert.NilError(t, err)
cmdConn := c.(*commandConn) cmdConn := c.(*commandConn)
assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid)) assert.Check(t, process.Alive(cmdConn.cmd.Process.Pid))