mirror of https://github.com/docker/cli.git
Fixes ImageList to be retro-compatible with older API
Make sure current client code can talk for ImageList can still talk to older daemon. Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
parent
36a1664cd4
commit
32f410cd35
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
|
"github.com/docker/docker/api/types/versions"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,8 +15,16 @@ func (cli *Client) ImageList(ctx context.Context, options types.ImageListOptions
|
||||||
var images []types.ImageSummary
|
var images []types.ImageSummary
|
||||||
query := url.Values{}
|
query := url.Values{}
|
||||||
|
|
||||||
if options.Filters.Len() > 0 {
|
optionFilters := options.Filters
|
||||||
filterJSON, err := filters.ToParamWithVersion(cli.version, options.Filters)
|
referenceFilters := optionFilters.Get("reference")
|
||||||
|
if versions.LessThan(cli.version, "1.25") && len(referenceFilters) > 0 {
|
||||||
|
query.Set("filter", referenceFilters[0])
|
||||||
|
for _, filterValue := range referenceFilters {
|
||||||
|
optionFilters.Del("reference", filterValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if optionFilters.Len() > 0 {
|
||||||
|
filterJSON, err := filters.ToParamWithVersion(cli.version, optionFilters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return images, err
|
return images, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,3 +109,51 @@ func TestImageList(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
content, err := json.Marshal([]types.ImageSummary{
|
||||||
|
{
|
||||||
|
ID: "image_id2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ID: "image_id2",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &http.Response{
|
||||||
|
StatusCode: http.StatusOK,
|
||||||
|
Body: ioutil.NopCloser(bytes.NewReader(content)),
|
||||||
|
}, nil
|
||||||
|
}),
|
||||||
|
version: "1.24",
|
||||||
|
}
|
||||||
|
|
||||||
|
filters := filters.NewArgs()
|
||||||
|
filters.Add("reference", "image:tag")
|
||||||
|
|
||||||
|
options := types.ImageListOptions{
|
||||||
|
Filters: filters,
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue