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 <djordje.lukic@docker.com>
This commit is contained in:
Djordje Lukic 2023-07-18 10:02:01 +02:00
parent 536e3692c6
commit ad2760ec80
No known key found for this signature in database
2 changed files with 56 additions and 37 deletions

View file

@ -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"))

View file

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