From 3d4cefaec65dd04f50d63a86a401f32272e159ee Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 12 Jul 2019 21:24:46 +0200 Subject: [PATCH] Better mock for docker.ImageID (#2461) Signed-off-by: David Gageot --- pkg/skaffold/docker/image_test.go | 14 ++++++++++++-- testutil/fake_image_api.go | 27 ++++++++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/skaffold/docker/image_test.go b/pkg/skaffold/docker/image_test.go index a39c8699b7f..e106defe551 100644 --- a/pkg/skaffold/docker/image_test.go +++ b/pkg/skaffold/docker/image_test.go @@ -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{ @@ -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", @@ -218,7 +228,7 @@ func TestImageID(t *testing.T) { { description: "not found", ref: "somethingelse", - shouldErr: true, + expected: "", }, } for _, test := range tests { diff --git a/testutil/fake_image_api.go b/testutil/fake_image_api.go index 338c3e43e7d..f56afd709c9 100644 --- a/testutil/fake_image_api.go +++ b/testutil/fake_image_api.go @@ -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("") } @@ -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, ¬FoundError{} } func (f *FakeAPIClient) ImageTag(_ context.Context, image, ref string) error {