Skip to content

Commit

Permalink
Better mock for docker.ImageID (#2461)
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot authored Jul 12, 2019
1 parent fa1e705 commit 3d4cefa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
14 changes: 12 additions & 2 deletions pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestImageID(t *testing.T) {
shouldErr bool
}{
{
description: "get digest",
description: "find by tag",
ref: "identifier:latest",
api: testutil.FakeAPIClient{
TagToImageID: map[string]string{
Expand All @@ -207,6 +207,16 @@ func TestImageID(t *testing.T) {
},
expected: "sha256:123abc",
},
{
description: "find by imageID",
ref: "sha256:123abc",
api: testutil.FakeAPIClient{
TagToImageID: map[string]string{
"identifier:latest": "sha256:123abc",
},
},
expected: "sha256:123abc",
},
{
description: "image inspect error",
ref: "test",
Expand All @@ -218,7 +228,7 @@ func TestImageID(t *testing.T) {
{
description: "not found",
ref: "somethingelse",
shouldErr: true,
expected: "",
},
}
for _, test := range tests {
Expand Down
27 changes: 18 additions & 9 deletions testutil/fake_image_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ type FakeAPIClient struct {
PushedImages []string
}

type notFoundError struct {
error
}

func (e notFoundError) NotFound() bool {
return true
}

type errReader struct{}

func (f errReader) Read([]byte) (int, error) { return 0, fmt.Errorf("") }
Expand Down Expand Up @@ -93,17 +101,18 @@ func (f *FakeAPIClient) ImageInspectWithRaw(_ context.Context, ref string) (type
return types.ImageInspect{}, nil, fmt.Errorf("")
}

id, ok := f.TagToImageID[ref]
if !ok {
return types.ImageInspect{}, nil, fmt.Errorf("")
}
for tag, imageID := range f.TagToImageID {
if tag == ref || imageID == ref {
rawConfig := []byte(fmt.Sprintf(`{"Config":{"Image":"%s"}}`, imageID))

rawConfig := []byte(fmt.Sprintf(`{"Config":{"Image":"%s"}}`, id))
return types.ImageInspect{
ID: imageID,
RepoDigests: f.RepoDigests,
}, rawConfig, nil
}
}

return types.ImageInspect{
ID: id,
RepoDigests: f.RepoDigests,
}, rawConfig, nil
return types.ImageInspect{}, nil, &notFoundError{}
}

func (f *FakeAPIClient) ImageTag(_ context.Context, image, ref string) error {
Expand Down

0 comments on commit 3d4cefa

Please sign in to comment.