2018-11-10 03:00:26 +00:00
|
|
|
package image // import "github.com/docker/docker/integration/image"
|
|
|
|
|
|
|
|
import (
|
2022-11-21 21:44:57 +00:00
|
|
|
"fmt"
|
2020-05-18 15:26:44 +00:00
|
|
|
"strings"
|
2018-11-10 03:00:26 +00:00
|
|
|
"testing"
|
2022-11-21 21:44:57 +00:00
|
|
|
"time"
|
2018-11-10 03:00:26 +00:00
|
|
|
|
2023-08-25 18:15:25 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2018-11-10 03:00:26 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2023-07-07 11:40:24 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2022-11-21 21:44:57 +00:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2023-07-14 18:02:38 +00:00
|
|
|
"github.com/docker/docker/testutil"
|
2022-11-21 21:44:57 +00:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2018-11-10 03:00:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Regression : #38171
|
|
|
|
func TestImagesFilterMultiReference(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
|
|
|
|
2019-01-02 13:16:25 +00:00
|
|
|
client := testEnv.APIClient()
|
2018-11-10 03:00:26 +00:00
|
|
|
|
2020-05-18 15:26:44 +00:00
|
|
|
name := strings.ToLower(t.Name())
|
2018-11-10 03:00:26 +00:00
|
|
|
repoTags := []string{
|
|
|
|
name + ":v1",
|
|
|
|
name + ":v2",
|
|
|
|
name + ":v3",
|
|
|
|
name + ":v4",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repoTag := range repoTags {
|
|
|
|
err := client.ImageTag(ctx, "busybox:latest", repoTag)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("reference", repoTags[0])
|
|
|
|
filter.Add("reference", repoTags[1])
|
|
|
|
filter.Add("reference", repoTags[2])
|
2023-07-07 11:40:24 +00:00
|
|
|
options := image.ListOptions{
|
2018-11-10 03:00:26 +00:00
|
|
|
Filters: filter,
|
|
|
|
}
|
|
|
|
images, err := client.ImageList(ctx, options)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2023-07-21 11:47:49 +00:00
|
|
|
assert.Assert(t, is.Len(images, 1))
|
|
|
|
assert.Check(t, is.Len(images[0].RepoTags, 3))
|
2018-11-10 03:00:26 +00:00
|
|
|
for _, repoTag := range images[0].RepoTags {
|
|
|
|
if repoTag != repoTags[0] && repoTag != repoTags[1] && repoTag != repoTags[2] {
|
|
|
|
t.Errorf("list images doesn't match any repoTag we expected, repoTag: %s", repoTag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 21:44:57 +00:00
|
|
|
|
2023-10-01 17:34:38 +00:00
|
|
|
func TestImagesFilterUntil(t *testing.T) {
|
|
|
|
ctx := setupTest(t)
|
|
|
|
|
|
|
|
client := testEnv.APIClient()
|
|
|
|
|
|
|
|
name := strings.ToLower(t.Name())
|
|
|
|
ctr := container.Create(ctx, t, client, container.WithName(name))
|
|
|
|
|
|
|
|
imgs := make([]string, 5)
|
|
|
|
for i := range imgs {
|
|
|
|
if i > 0 {
|
2024-03-06 14:15:01 +00:00
|
|
|
// Make sure each image has a distinct timestamp.
|
2023-10-01 17:34:38 +00:00
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
id, err := client.ContainerCommit(ctx, ctr, containertypes.CommitOptions{Reference: fmt.Sprintf("%s:v%d", name, i)})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
imgs[i] = id.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
olderImage, _, err := client.ImageInspectWithRaw(ctx, imgs[2])
|
|
|
|
assert.NilError(t, err)
|
|
|
|
olderUntil := olderImage.Created
|
|
|
|
|
|
|
|
laterImage, _, err := client.ImageInspectWithRaw(ctx, imgs[3])
|
|
|
|
assert.NilError(t, err)
|
|
|
|
laterUntil := laterImage.Created
|
|
|
|
|
|
|
|
filter := filters.NewArgs(
|
|
|
|
filters.Arg("since", imgs[0]),
|
|
|
|
filters.Arg("until", olderUntil),
|
|
|
|
filters.Arg("until", laterUntil),
|
|
|
|
filters.Arg("before", imgs[len(imgs)-1]),
|
|
|
|
)
|
2023-07-07 11:40:24 +00:00
|
|
|
list, err := client.ImageList(ctx, image.ListOptions{Filters: filter})
|
2023-10-01 17:34:38 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
var listedIDs []string
|
|
|
|
for _, i := range list {
|
|
|
|
t.Logf("ImageList: ID=%v RepoTags=%v", i.ID, i.RepoTags)
|
|
|
|
listedIDs = append(listedIDs, i.ID)
|
|
|
|
}
|
|
|
|
assert.DeepEqual(t, listedIDs, imgs[1:2], cmpopts.SortSlices(func(a, b string) bool { return a < b }))
|
|
|
|
}
|
|
|
|
|
2022-11-21 21:44:57 +00:00
|
|
|
func TestImagesFilterBeforeSince(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
|
|
|
|
2022-11-21 21:44:57 +00:00
|
|
|
client := testEnv.APIClient()
|
|
|
|
|
|
|
|
name := strings.ToLower(t.Name())
|
|
|
|
ctr := container.Create(ctx, t, client, container.WithName(name))
|
|
|
|
|
|
|
|
imgs := make([]string, 5)
|
|
|
|
for i := range imgs {
|
|
|
|
if i > 0 {
|
2024-03-06 14:15:01 +00:00
|
|
|
// Make sure each image has a distinct timestamp.
|
2022-11-21 21:44:57 +00:00
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
2023-08-25 18:15:25 +00:00
|
|
|
id, err := client.ContainerCommit(ctx, ctr, containertypes.CommitOptions{Reference: fmt.Sprintf("%s:v%d", name, i)})
|
2022-11-21 21:44:57 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
imgs[i] = id.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
filter := filters.NewArgs(
|
|
|
|
filters.Arg("since", imgs[0]),
|
|
|
|
filters.Arg("before", imgs[len(imgs)-1]),
|
|
|
|
)
|
2023-07-07 11:40:24 +00:00
|
|
|
list, err := client.ImageList(ctx, image.ListOptions{Filters: filter})
|
2022-11-21 21:44:57 +00:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
var listedIDs []string
|
|
|
|
for _, i := range list {
|
|
|
|
t.Logf("ImageList: ID=%v RepoTags=%v", i.ID, i.RepoTags)
|
|
|
|
listedIDs = append(listedIDs, i.ID)
|
|
|
|
}
|
|
|
|
// The ImageList API sorts the list by created timestamp... truncated to
|
|
|
|
// 1-second precision. Since all the images were created within
|
|
|
|
// milliseconds of each other, listedIDs is effectively unordered and
|
|
|
|
// 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 }))
|
|
|
|
}
|
2023-07-18 08:02:01 +00:00
|
|
|
|
|
|
|
func TestAPIImagesFilters(t *testing.T) {
|
2023-07-14 18:02:38 +00:00
|
|
|
ctx := setupTest(t)
|
2023-07-18 08:02:01 +00:00
|
|
|
client := testEnv.APIClient()
|
|
|
|
|
|
|
|
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()
|
2023-07-14 18:02:38 +00:00
|
|
|
|
|
|
|
ctx := testutil.StartSpan(ctx, t)
|
2023-07-07 11:40:24 +00:00
|
|
|
images, err := client.ImageList(ctx, image.ListOptions{
|
2023-07-18 08:02:01 +00:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|