From 978690e4f98fddcf5520ba211f067a707438f8d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Gronowski?= Date: Tue, 22 Nov 2022 13:12:11 +0100 Subject: [PATCH] client/list: Handle SharedSize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This makes the `ImageList` function to add `shared-size=1` to the url query when user caller sets the SharedSize. SharedSize support was introduced in API version 1.42. This field was added to the options struct, but client wasn't adjusted. Signed-off-by: Paweł Gronowski (cherry picked from commit 3d97f1e22d335a4db9348813ff25fe98cdd7d0cf) Signed-off-by: Paweł Gronowski --- client/image_list.go | 3 +++ client/image_list_test.go | 41 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/client/image_list.go b/client/image_list.go index a4d7505094..950d513334 100644 --- a/client/image_list.go +++ b/client/image_list.go @@ -34,6 +34,9 @@ func (cli *Client) ImageList(ctx context.Context, options types.ImageListOptions if options.All { query.Set("all", "1") } + if options.SharedSize && versions.GreaterThanOrEqualTo(cli.version, "1.42") { + query.Set("shared-size", "1") + } serverResp, err := cli.get(ctx, "/images/json", query, nil) defer ensureReaderClosed(serverResp) diff --git a/client/image_list_test.go b/client/image_list_test.go index 4619393ff3..b302d970c9 100644 --- a/client/image_list_test.go +++ b/client/image_list_test.go @@ -7,12 +7,15 @@ import ( "fmt" "io" "net/http" + "net/url" "strings" "testing" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" "github.com/docker/docker/errdefs" + "gotest.tools/v3/assert" + is "gotest.tools/v3/assert/cmp" ) func TestImageListError(t *testing.T) { @@ -158,3 +161,41 @@ func TestImageListApiBefore125(t *testing.T) { t.Fatalf("expected 2 images, got %v", images) } } + +// 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 types.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: types.ImageListOptions{SharedSize: true}, sharedSize: "1"}, + {name: "unset before 1.42, even if requested", version: "1.41", options: types.ImageListOptions{SharedSize: true}}, + } { + 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)) + }) + } +}