Files
moby/client/image_list_test.go
Sebastiaan van Stijn f7ed1b84d2 client: ImageList: don't discard reference filter on API < 1.25
the "reference" filter was introduced in [moby@820b809] (docker 1.13.0-rc1)
to replace the "filter" query argument. That commit initially included a
version-gate anticipating the API version to be used for v17.12, but as
this was yet unknown, the version-gate was removed in [moby@0f9d22c].
A later PR re-introduced a version-gate in [moby@4a19009], reflecting the
API version in which the deprecation was (finally) completed.

For the client, [moby@c6e3145] added a fallback was added for older daemons
(docker 1.12.0 and older, using API < v1.25) that did not support the new
filter.

Looking at the above, any version of docker 1.13.0 or above handles the
"reference" filter, but (depending on the docker version) may also handle
the old filter on API < 1.28 or API < 1.41. Removing this option will only
impact daemon versions older than 1.13.0, which are long obsolete.

Given that current clients forcibly remove the "reference" filter and replace
it with the old "filter" when using API v1.24, we keep support on the daemon
side, but update the version to v1.24, and only if no reference filter is
set.

[moby@820b809]: 820b809e70
[moby@c6e3145]: c6e31454ba
[moby@0f9d22c]: 0f9d22cd66
[moby@4a19009]: 4a1900915a

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-18 18:15:42 +02:00

151 lines
4.3 KiB
Go

package client
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/filters"
"github.com/moby/moby/api/types/image"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestImageListError(t *testing.T) {
client, err := NewClientWithOpts(WithMockClient(errorMock(http.StatusInternalServerError, "Server error")))
assert.NilError(t, err)
_, err = client.ImageList(context.Background(), ImageListOptions{})
assert.Check(t, is.ErrorType(err, cerrdefs.IsInternal))
}
// TestImageListConnectionError verifies that connection errors occurring
// during API-version negotiation are not shadowed by API-version errors.
//
// Regression test for https://github.com/docker/cli/issues/4890
func TestImageListConnectionError(t *testing.T) {
client, err := NewClientWithOpts(WithAPIVersionNegotiation(), WithHost("tcp://no-such-host.invalid"))
assert.NilError(t, err)
_, err = client.ImageList(context.Background(), ImageListOptions{})
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
}
func TestImageList(t *testing.T) {
const expectedURL = "/images/json"
listCases := []struct {
options ImageListOptions
expectedQueryParams map[string]string
}{
{
options: ImageListOptions{},
expectedQueryParams: map[string]string{
"all": "",
"filter": "",
"filters": "",
},
},
{
options: ImageListOptions{
Filters: filters.NewArgs(
filters.Arg("label", "label1"),
filters.Arg("label", "label2"),
filters.Arg("dangling", "true"),
),
},
expectedQueryParams: map[string]string{
"all": "",
"filter": "",
"filters": `{"dangling":{"true":true},"label":{"label1":true,"label2":true}}`,
},
},
{
options: ImageListOptions{
Filters: filters.NewArgs(filters.Arg("dangling", "false")),
},
expectedQueryParams: map[string]string{
"all": "",
"filter": "",
"filters": `{"dangling":{"false":true}}`,
},
},
}
for _, listCase := range listCases {
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
if err := assertRequest(req, http.MethodGet, expectedURL); err != nil {
return nil, err
}
query := req.URL.Query()
for key, expected := range listCase.expectedQueryParams {
actual := query.Get(key)
if actual != expected {
return nil, fmt.Errorf("%s not set in URL query properly. Expected '%s', got %s", key, expected, actual)
}
}
content, err := json.Marshal([]image.Summary{
{
ID: "image_id2",
},
{
ID: "image_id2",
},
})
if err != nil {
return nil, err
}
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewReader(content)),
}, nil
}))
assert.NilError(t, err)
images, err := client.ImageList(context.Background(), listCase.options)
assert.NilError(t, err)
assert.Check(t, is.Len(images, 2))
}
}
// Checks if shared-size query parameter is set/not being set correctly
// for /images/json.
func TestImageListWithSharedSize(t *testing.T) {
t.Parallel()
const sharedSize = "shared-size"
for _, tc := range []struct {
name string
version string
options ImageListOptions
sharedSize string // expected value for the shared-size query param, or empty if it should not be set.
}{
{name: "unset after 1.42, no options set", version: "1.42"},
{name: "set after 1.42, if requested", version: "1.42", options: ImageListOptions{SharedSize: true}, sharedSize: "1"},
{name: "unset before 1.42, even if requested", version: "1.41", options: ImageListOptions{SharedSize: true}},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var query url.Values
client, err := NewClientWithOpts(WithMockClient(func(req *http.Request) (*http.Response, error) {
query = req.URL.Query()
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(strings.NewReader("[]")),
}, nil
}), WithVersion(tc.version))
assert.NilError(t, err)
_, err = client.ImageList(context.Background(), tc.options)
assert.NilError(t, err)
expectedSet := tc.sharedSize != ""
assert.Check(t, is.Equal(query.Has(sharedSize), expectedSet))
assert.Check(t, is.Equal(query.Get(sharedSize), tc.sharedSize))
})
}
}