Skip to content

Commit

Permalink
feat: add dockertest package (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
colesnodgrass authored Jul 23, 2024
1 parent ab7078b commit 4b9dd0b
Show file tree
Hide file tree
Showing 6 changed files with 279 additions and 225 deletions.
99 changes: 6 additions & 93 deletions internal/cmd/local/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import (
"context"
"errors"
"github.com/airbytehq/abctl/internal/cmd/local/docker"
"github.com/airbytehq/abctl/internal/cmd/local/docker/dockertest"
"github.com/airbytehq/abctl/internal/cmd/local/localerr"
"github.com/airbytehq/abctl/internal/telemetry"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"io"
"net"
"strconv"
"strings"
Expand All @@ -27,8 +22,8 @@ func TestDockerInstalled(t *testing.T) {
})

dockerClient = &docker.Docker{
Client: mockDockerClient{
serverVersion: func(ctx context.Context) (types.Version, error) {
Client: dockertest.MockClient{
FnServerVersion: func(ctx context.Context) (types.Version, error) {
return types.Version{
Platform: struct{ Name string }{Name: "test"},
Version: "version",
Expand Down Expand Up @@ -60,8 +55,8 @@ func TestDockerInstalled_Error(t *testing.T) {
})

dockerClient = &docker.Docker{
Client: mockDockerClient{
serverVersion: func(ctx context.Context) (types.Version, error) {
Client: dockertest.MockClient{
FnServerVersion: func(ctx context.Context) (types.Version, error) {
return types.Version{}, errors.New("test")
},
},
Expand Down Expand Up @@ -116,89 +111,7 @@ func port(s string) int {
return p
}

// mocks

var _ docker.Client = (*mockDockerClient)(nil)

type mockDockerClient struct {
containerCreate func(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error)
containerInspect func(ctx context.Context, containerID string) (types.ContainerJSON, error)
containerRemove func(ctx context.Context, container string, options container.RemoveOptions) error
containerStart func(ctx context.Context, container string, options container.StartOptions) error
containerStop func(ctx context.Context, container string, options container.StopOptions) error
copyFromContainer func(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)

containerExecCreate func(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error)
containerExecInspect func(ctx context.Context, execID string) (types.ContainerExecInspect, error)
containerExecStart func(ctx context.Context, execID string, config types.ExecStartCheck) error

imageList func(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
imagePull func(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error)

serverVersion func(ctx context.Context) (types.Version, error)
volumeInspect func(ctx context.Context, volumeID string) (volume.Volume, error)
}

func (m mockDockerClient) ContainerCreate(ctx context.Context, config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, platform *ocispec.Platform, containerName string) (container.CreateResponse, error) {
return m.containerCreate(ctx, config, hostConfig, networkingConfig, platform, containerName)
}

func (m mockDockerClient) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
return m.containerInspect(ctx, containerID)
}

func (m mockDockerClient) ContainerRemove(ctx context.Context, container string, options container.RemoveOptions) error {
return m.containerRemove(ctx, container, options)
}

func (m mockDockerClient) ContainerStart(ctx context.Context, container string, options container.StartOptions) error {
return m.containerStart(ctx, container, options)
}

func (m mockDockerClient) ContainerStop(ctx context.Context, container string, options container.StopOptions) error {
return m.containerStop(ctx, container, options)
}

func (m mockDockerClient) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error) {
return m.copyFromContainer(ctx, container, srcPath)
}

func (m mockDockerClient) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error) {
return m.containerExecCreate(ctx, container, config)
}

func (m mockDockerClient) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error) {
return m.containerExecInspect(ctx, execID)
}

func (m mockDockerClient) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error {
return m.containerExecStart(ctx, execID, config)
}

func (m mockDockerClient) ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error) {
// by default return a list with one (empty) item in it
if m.imageList == nil {
return []image.Summary{{}}, nil
}
return m.imageList(ctx, options)
}

func (m mockDockerClient) ImagePull(ctx context.Context, img string, options image.PullOptions) (io.ReadCloser, error) {
// by default return a nop closer (with an empty string)
if m.imagePull == nil {
return io.NopCloser(strings.NewReader("")), nil
}
return m.imagePull(ctx, img, options)
}

func (m mockDockerClient) ServerVersion(ctx context.Context) (types.Version, error) {
return m.serverVersion(ctx)
}

func (m mockDockerClient) VolumeInspect(ctx context.Context, volumeID string) (volume.Volume, error) {
return m.volumeInspect(ctx, volumeID)
}

// --- mocks
var _ telemetry.Client = (*mockTelemetryClient)(nil)

type mockTelemetryClient struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/local/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ type Client interface {
ContainerRemove(ctx context.Context, container string, options container.RemoveOptions) error
ContainerStart(ctx context.Context, container string, options container.StartOptions) error
ContainerStop(ctx context.Context, container string, options container.StopOptions) error
CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)
CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, container.PathStat, error)

ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error)
ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error)
ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error
ContainerExecCreate(ctx context.Context, container string, config container.ExecOptions) (types.IDResponse, error)
ContainerExecInspect(ctx context.Context, execID string) (container.ExecInspect, error)
ContainerExecStart(ctx context.Context, execID string, config container.ExecStartOptions) error

ImageList(ctx context.Context, options image.ListOptions) ([]image.Summary, error)
ImagePull(ctx context.Context, refStr string, options image.PullOptions) (io.ReadCloser, error)
Expand Down
Loading

0 comments on commit 4b9dd0b

Please sign in to comment.