Skip to content

Commit

Permalink
test: make sure the exec funcction handles command exit code
Browse files Browse the repository at this point in the history
This test makes sure that the exec function does handle
docker command error results
  • Loading branch information
KnisterPeter authored and github-actions committed May 24, 2022
1 parent e2bd040 commit e1798b2
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion pkg/container/docker_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io"
"net"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -68,6 +69,11 @@ func (m *mockDockerClient) ContainerExecAttach(ctx context.Context, id string, o
return args.Get(0).(types.HijackedResponse), args.Error(1)
}

func (m *mockDockerClient) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
args := m.Called(ctx, execID)
return args.Get(0).(types.ContainerExecInspect), args.Error(1)
}

type endlessReader struct {
io.Reader
}
Expand All @@ -90,7 +96,7 @@ func (m *mockConn) Close() (err error) {
return nil
}

func TestDockerExec(t *testing.T) {
func TestDockerExecAbort(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())

conn := &mockConn{}
Expand Down Expand Up @@ -127,3 +133,33 @@ func TestDockerExec(t *testing.T) {
conn.AssertExpectations(t)
client.AssertExpectations(t)
}

func TestDockerExecFailure(t *testing.T) {
ctx := context.Background()

conn := &mockConn{}

client := &mockDockerClient{}
client.On("ContainerExecCreate", ctx, "123", mock.AnythingOfType("types.ExecConfig")).Return(types.IDResponse{ID: "id"}, nil)
client.On("ContainerExecAttach", ctx, "id", mock.AnythingOfType("types.ExecStartCheck")).Return(types.HijackedResponse{
Conn: conn,
Reader: bufio.NewReader(strings.NewReader("output")),
}, nil)
client.On("ContainerExecInspect", ctx, "id").Return(types.ContainerExecInspect{
ExitCode: 1,
}, nil)

cr := &containerReference{
id: "123",
cli: client,
input: &NewContainerInput{
Image: "image",
},
}

err := cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx)
assert.Error(t, err, "exit with `FAILURE`: 1")

conn.AssertExpectations(t)
client.AssertExpectations(t)
}

0 comments on commit e1798b2

Please sign in to comment.