2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-09-06 18:46:37 +00:00
|
|
|
"net/http"
|
2022-11-22 12:12:11 +00:00
|
|
|
"net/url"
|
2016-09-06 18:46:37 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2022-11-24 18:04:31 +00:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2018-12-31 17:22:43 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2022-11-22 12:12:11 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestImageListError(t *testing.T) {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(errorMock(http.StatusInternalServerError, "Server error")),
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:40:24 +00:00
|
|
|
_, err := client.ImageList(context.Background(), image.ListOptions{})
|
2023-05-10 11:17:40 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsSystem))
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 11:20:06 +00:00
|
|
|
// 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(), image.ListOptions{})
|
|
|
|
assert.Check(t, is.ErrorType(err, IsErrConnectionFailed))
|
|
|
|
}
|
|
|
|
|
2016-09-06 18:46:37 +00:00
|
|
|
func TestImageList(t *testing.T) {
|
2023-04-25 13:11:32 +00:00
|
|
|
const expectedURL = "/images/json"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
listCases := []struct {
|
2023-07-07 11:40:24 +00:00
|
|
|
options image.ListOptions
|
2016-09-06 18:46:37 +00:00
|
|
|
expectedQueryParams map[string]string
|
|
|
|
}{
|
|
|
|
{
|
2023-07-07 11:40:24 +00:00
|
|
|
options: image.ListOptions{},
|
2016-09-06 18:46:37 +00:00
|
|
|
expectedQueryParams: map[string]string{
|
|
|
|
"all": "",
|
|
|
|
"filter": "",
|
|
|
|
"filters": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-07 11:40:24 +00:00
|
|
|
options: image.ListOptions{
|
2023-04-25 13:11:32 +00:00
|
|
|
Filters: filters.NewArgs(
|
|
|
|
filters.Arg("label", "label1"),
|
|
|
|
filters.Arg("label", "label2"),
|
|
|
|
filters.Arg("dangling", "true"),
|
|
|
|
),
|
2016-09-06 18:46:37 +00:00
|
|
|
},
|
|
|
|
expectedQueryParams: map[string]string{
|
|
|
|
"all": "",
|
|
|
|
"filter": "",
|
|
|
|
"filters": `{"dangling":{"true":true},"label":{"label1":true,"label2":true}}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-07 11:40:24 +00:00
|
|
|
options: image.ListOptions{
|
2023-04-25 13:11:32 +00:00
|
|
|
Filters: filters.NewArgs(filters.Arg("dangling", "false")),
|
2016-09-06 18:46:37 +00:00
|
|
|
},
|
|
|
|
expectedQueryParams: map[string]string{
|
|
|
|
"all": "",
|
|
|
|
"filter": "",
|
|
|
|
"filters": `{"dangling":{"false":true}}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, listCase := range listCases {
|
|
|
|
client := &Client{
|
2016-09-09 03:44:25 +00:00
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
2016-09-06 18:46:37 +00:00
|
|
|
if !strings.HasPrefix(req.URL.Path, expectedURL) {
|
|
|
|
return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-11-24 18:04:31 +00:00
|
|
|
content, err := json.Marshal([]image.Summary{
|
2016-09-06 18:46:37 +00:00
|
|
|
{
|
|
|
|
ID: "image_id2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "image_id2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(content)),
|
2016-09-06 18:46:37 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
images, err := client.ImageList(context.Background(), listCase.options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(images) != 2 {
|
|
|
|
t.Fatalf("expected 2 images, got %v", images)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 21:15:50 +00:00
|
|
|
|
|
|
|
func TestImageListApiBefore125(t *testing.T) {
|
|
|
|
expectedFilter := "image:tag"
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
query := req.URL.Query()
|
|
|
|
actualFilter := query.Get("filter")
|
|
|
|
if actualFilter != expectedFilter {
|
|
|
|
return nil, fmt.Errorf("filter not set in URL query properly. Expected '%s', got %s", expectedFilter, actualFilter)
|
|
|
|
}
|
|
|
|
actualFilters := query.Get("filters")
|
|
|
|
if actualFilters != "" {
|
|
|
|
return nil, fmt.Errorf("filters should have not been present, were with value: %s", actualFilters)
|
|
|
|
}
|
2022-11-24 18:04:31 +00:00
|
|
|
content, err := json.Marshal([]image.Summary{
|
2016-11-28 21:15:50 +00:00
|
|
|
{
|
|
|
|
ID: "image_id2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "image_id2",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
2021-08-24 10:10:50 +00:00
|
|
|
Body: io.NopCloser(bytes.NewReader(content)),
|
2016-11-28 21:15:50 +00:00
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
version: "1.24",
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:40:24 +00:00
|
|
|
options := image.ListOptions{
|
2023-04-25 13:11:32 +00:00
|
|
|
Filters: filters.NewArgs(filters.Arg("reference", "image:tag")),
|
2016-11-28 21:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
images, err := client.ImageList(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(images) != 2 {
|
|
|
|
t.Fatalf("expected 2 images, got %v", images)
|
|
|
|
}
|
|
|
|
}
|
2022-11-22 12:12:11 +00:00
|
|
|
|
|
|
|
// 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
|
2023-07-07 11:40:24 +00:00
|
|
|
options image.ListOptions
|
2022-11-22 12:12:11 +00:00
|
|
|
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"},
|
2023-07-07 11:40:24 +00:00
|
|
|
{name: "set after 1.42, if requested", version: "1.42", options: image.ListOptions{SharedSize: true}, sharedSize: "1"},
|
|
|
|
{name: "unset before 1.42, even if requested", version: "1.41", options: image.ListOptions{SharedSize: true}},
|
2022-11-22 12:12:11 +00:00
|
|
|
} {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var query url.Values
|
|
|
|
client := &Client{
|
|
|
|
client: newMockClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
query = req.URL.Query()
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: io.NopCloser(strings.NewReader("[]")),
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
version: tc.version,
|
|
|
|
}
|
|
|
|
_, err := client.ImageList(context.Background(), tc.options)
|
|
|
|
assert.Check(t, err)
|
|
|
|
expectedSet := tc.sharedSize != ""
|
|
|
|
assert.Check(t, is.Equal(query.Has(sharedSize), expectedSet))
|
|
|
|
assert.Check(t, is.Equal(query.Get(sharedSize), tc.sharedSize))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|