From ad2760ec80f2c3c9863f5d534b0d16366b1f870e Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Tue, 18 Jul 2023 10:02:01 +0200 Subject: [PATCH] integration: Move image filter tests to integration - use assert.Check to continue the test even if a check fails - assert the total number of images returned, not only their RepoTags - use subtests Signed-off-by: Djordje Lukic --- integration-cli/docker_api_images_test.go | 37 --------------- integration/image/list_test.go | 56 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 37 deletions(-) diff --git a/integration-cli/docker_api_images_test.go b/integration-cli/docker_api_images_test.go index 1078cf5d6d..f22afb4034 100644 --- a/integration-cli/docker_api_images_test.go +++ b/integration-cli/docker_api_images_test.go @@ -8,7 +8,6 @@ import ( "testing" "github.com/docker/docker/api/types" - "github.com/docker/docker/api/types/filters" "github.com/docker/docker/client" "github.com/docker/docker/integration-cli/cli" "github.com/docker/docker/integration-cli/cli/build" @@ -16,42 +15,6 @@ import ( "gotest.tools/v3/assert" ) -func (s *DockerAPISuite) TestAPIImagesFilter(c *testing.T) { - apiClient, err := client.NewClientWithOpts(client.FromEnv) - assert.NilError(c, err) - defer apiClient.Close() - - name := "utest:tag1" - name2 := "utest/docker:tag2" - name3 := "utest:5000/docker:tag3" - for _, n := range []string{name, name2, name3} { - dockerCmd(c, "tag", "busybox", n) - } - getImages := func(filter string) []types.ImageSummary { - options := types.ImageListOptions{ - All: false, - Filters: filters.NewArgs(filters.Arg("reference", filter)), - } - images, err := apiClient.ImageList(context.Background(), options) - assert.NilError(c, err) - - return images - } - - // incorrect number of matches returned - images := getImages("utest*/*") - assert.Equal(c, len(images[0].RepoTags), 2) - - images = getImages("utest") - assert.Equal(c, len(images[0].RepoTags), 1) - - images = getImages("utest*") - assert.Equal(c, len(images[0].RepoTags), 1) - - images = getImages("*5000*/*") - assert.Equal(c, len(images[0].RepoTags), 1) -} - func (s *DockerAPISuite) TestAPIImagesSaveAndLoad(c *testing.T) { testRequires(c, Network) buildImageSuccessfully(c, "saveandload", build.WithDockerfile("FROM busybox\nENV FOO bar")) diff --git a/integration/image/list_test.go b/integration/image/list_test.go index a63277335e..2f9e71aa35 100644 --- a/integration/image/list_test.go +++ b/integration/image/list_test.go @@ -93,3 +93,59 @@ func TestImagesFilterBeforeSince(t *testing.T) { // the assertion must therefore be order-independent. assert.DeepEqual(t, listedIDs, imgs[1:len(imgs)-1], cmpopts.SortSlices(func(a, b string) bool { return a < b })) } + +func TestAPIImagesFilters(t *testing.T) { + t.Cleanup(setupTest(t)) + client := testEnv.APIClient() + ctx := context.Background() + + for _, n := range []string{"utest:tag1", "utest/docker:tag2", "utest:5000/docker:tag3"} { + err := client.ImageTag(ctx, "busybox:latest", n) + assert.NilError(t, err) + } + + testcases := []struct { + name string + filters []filters.KeyValuePair + expectedImages int + expectedRepoTags int + }{ + { + name: "repository regex", + filters: []filters.KeyValuePair{filters.Arg("reference", "utest*/*")}, + expectedImages: 1, + expectedRepoTags: 2, + }, + { + name: "image name regex", + filters: []filters.KeyValuePair{filters.Arg("reference", "utest*")}, + expectedImages: 1, + expectedRepoTags: 1, + }, + { + name: "image name without a tag", + filters: []filters.KeyValuePair{filters.Arg("reference", "utest")}, + expectedImages: 1, + expectedRepoTags: 1, + }, + { + name: "registry port regex", + filters: []filters.KeyValuePair{filters.Arg("reference", "*5000*/*")}, + expectedImages: 1, + expectedRepoTags: 1, + }, + } + + for _, tc := range testcases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + images, err := client.ImageList(context.Background(), types.ImageListOptions{ + Filters: filters.NewArgs(tc.filters...), + }) + assert.Check(t, err) + assert.Assert(t, is.Len(images, tc.expectedImages)) + assert.Check(t, is.Len(images[0].RepoTags, tc.expectedRepoTags)) + }) + } +}