Skip to content

Commit

Permalink
update docker mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
colesnodgrass committed Jun 24, 2024
1 parent 52bc429 commit a9e5139
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 6 deletions.
56 changes: 54 additions & 2 deletions internal/cmd/local/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import (
"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/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 Down Expand Up @@ -115,18 +120,65 @@ func port(s string) int {
var _ docker.Client = (*mockDockerClient)(nil)

type mockDockerClient struct {
serverVersion func(ctx context.Context) (types.Version, error)
containerInspect func(ctx context.Context, containerID string) (types.ContainerJSON, error)
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

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) 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)
}

var _ telemetry.Client = (*mockTelemetryClient)(nil)

type mockTelemetryClient struct {
Expand Down
61 changes: 57 additions & 4 deletions internal/cmd/local/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import (
"errors"
"github.com/airbytehq/abctl/internal/cmd/local/localerr"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/google/go-cmp/cmp"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"io"
"testing"
)

Expand Down Expand Up @@ -313,13 +318,61 @@ func TestPort_Err(t *testing.T) {
}

// -- mocks
var _ pinger = (*mockPinger)(nil)

type mockPinger struct {
containerInspect func(ctx context.Context, containerID string) (types.ContainerJSON, error)
serverVersion func(ctx context.Context) (types.Version, error)
ping func(ctx context.Context) (types.Ping, error)
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

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

ping func(ctx context.Context) (types.Ping, error)
}

var _ pinger = (*mockPinger)(nil)
func (m mockPinger) 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 mockPinger) ContainerRemove(ctx context.Context, container string, options container.RemoveOptions) error {
return m.containerRemove(ctx, container, options)
}

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

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

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

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

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

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

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

func (m mockPinger) ContainerInspect(ctx context.Context, containerID string) (types.ContainerJSON, error) {
if m.containerInspect == nil {
Expand Down

0 comments on commit a9e5139

Please sign in to comment.