Skip to content

Commit

Permalink
Update tests for PullImage:
Browse files Browse the repository at this point in the history
With the change in behavior, the tests needed
to be updated.

Signed-off-by: Jacob Weinstock <[email protected]>
  • Loading branch information
jacobweinstock committed Aug 29, 2024
1 parent a54bcce commit c0998f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
19 changes: 17 additions & 2 deletions cmd/tink-worker/worker/container_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,32 @@ type fakeDockerClient struct {
statusCode int
err error
waitErr error
imageInspectErr error
}

func newFakeDockerClient(containerID, imagePullContent string, delay time.Duration, statusCode int, err, waitErr error) *fakeDockerClient {
return &fakeDockerClient{
type dockerClientOpt func(*fakeDockerClient)

func withImageInspectErr(err error) dockerClientOpt {
return func(c *fakeDockerClient) {
c.imageInspectErr = err
}
}

func newFakeDockerClient(containerID, imagePullContent string, delay time.Duration, statusCode int, err, waitErr error, opts ...dockerClientOpt) *fakeDockerClient {
f := &fakeDockerClient{
containerID: containerID,
imagePullContent: imagePullContent,
delay: delay,
statusCode: statusCode,
err: err,
waitErr: waitErr,
}

for _, opt := range opts {
opt(f)
}

return f
}

func (c *fakeDockerClient) ContainerCreate(
Expand Down
16 changes: 15 additions & 1 deletion cmd/tink-worker/worker/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/go-logr/zapr"
"go.uber.org/zap"
Expand All @@ -19,6 +20,10 @@ func (c *fakeDockerClient) ImagePull(context.Context, string, image.PullOptions)
return io.NopCloser(strings.NewReader(c.imagePullContent)), nil
}

func (c *fakeDockerClient) ImageInspectWithRaw(context.Context, string) (types.ImageInspect, []byte, error) {
return types.ImageInspect{}, nil, c.imageInspectErr
}

func TestContainerManagerPullImage(t *testing.T) {
cases := []struct {
name string
Expand All @@ -27,6 +32,7 @@ func TestContainerManagerPullImage(t *testing.T) {
registry RegistryConnDetails
clientErr error
wantErr error
imageInspectErr error
}{
{
name: "Happy Path",
Expand All @@ -39,19 +45,27 @@ func TestContainerManagerPullImage(t *testing.T) {
responseContent: "{",
clientErr: errors.New("You missed the shot"),
wantErr: errors.New("DOCKER PULL: You missed the shot"),
imageInspectErr: errors.New("Image not in local cache"),
},
{
name: "pull error",
image: "yav.in/4/deathstar:nomedalforchewie",
responseContent: `{"error": "You missed the shot"}`,
wantErr: errors.New("DOCKER PULL: You missed the shot"),
imageInspectErr: errors.New("Image not in local cache"),
},
{
name: "image already exists, no error",
image: "yav.in/4/deathstar:nomedalforchewie",
clientErr: errors.New("You missed the shot"),
wantErr: nil,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
logger := zapr.NewLogger(zap.Must(zap.NewDevelopment()))
mgr := NewContainerManager(logger, newFakeDockerClient("", tc.responseContent, 0, 0, tc.clientErr, nil), tc.registry)
mgr := NewContainerManager(logger, newFakeDockerClient("", tc.responseContent, 0, 0, tc.clientErr, nil, withImageInspectErr(tc.imageInspectErr)), tc.registry)

ctx := context.Background()
gotErr := mgr.PullImage(ctx, tc.image)
Expand Down

0 comments on commit c0998f9

Please sign in to comment.