From 72ef4563870cb3ec8897c625946df7c24d4ff429 Mon Sep 17 00:00:00 2001 From: Wayne Cheng Date: Mon, 1 Jan 2024 16:27:52 +0800 Subject: [PATCH] test cases for `docker manifest ls` Signed-off-by: Wayne Cheng --- cli/command/manifest/list_test.go | 102 ++++++++++++++++++ .../manifest-list-with-quiet-option.golden | 2 + .../manifest/testdata/manifest-list.golden | 3 + 3 files changed, 107 insertions(+) create mode 100644 cli/command/manifest/list_test.go create mode 100644 cli/command/manifest/testdata/manifest-list-with-quiet-option.golden create mode 100644 cli/command/manifest/testdata/manifest-list.golden diff --git a/cli/command/manifest/list_test.go b/cli/command/manifest/list_test.go new file mode 100644 index 0000000000..bd7ee46224 --- /dev/null +++ b/cli/command/manifest/list_test.go @@ -0,0 +1,102 @@ +package manifest + +import ( + "io" + "testing" + + "github.com/docker/cli/cli/manifest/store" + "github.com/docker/cli/internal/test" + "github.com/docker/docker/api/types" + "github.com/docker/docker/api/types/filters" + + "gotest.tools/v3/assert" + "gotest.tools/v3/golden" +) + +func TestListErrors(t *testing.T) { + manifestStore := store.NewStore(t.TempDir()) + + testCases := []struct { + description string + args []string + flags map[string]string + expectedError string + }{ + { + description: "too many arguments", + args: []string{"foo"}, + expectedError: "accepts no arguments", + }, + { + description: "invalid format", + args: []string{}, + flags: map[string]string{ + "format": "{{invalid format}}", + }, + expectedError: "template parsing error", + }, + } + + for _, tc := range testCases { + cli := test.NewFakeCli(nil) + cli.SetManifestStore(manifestStore) + cmd := newListCommand(cli) + cmd.SetArgs(tc.args) + for key, value := range tc.flags { + cmd.Flags().Set(key, value) + } + cmd.SetOut(io.Discard) + assert.ErrorContains(t, cmd.Execute(), tc.expectedError) + } +} + +func TestList(t *testing.T) { + manifestStore := store.NewStore(t.TempDir()) + + list1 := ref(t, "first:1") + namedRef := ref(t, "alpine:3.0") + err := manifestStore.Save(list1, namedRef, fullImageManifest(t, namedRef)) + assert.NilError(t, err) + namedRef = ref(t, "alpine:3.1") + err = manifestStore.Save(list1, namedRef, fullImageManifest(t, namedRef)) + assert.NilError(t, err) + + list2 := ref(t, "second:2") + namedRef = ref(t, "alpine:3.2") + err = manifestStore.Save(list2, namedRef, fullImageManifest(t, namedRef)) + assert.NilError(t, err) + + testCases := []struct { + description string + args []string + flags map[string]string + golden string + listFunc func(filter filters.Args) (types.PluginsListResponse, error) + }{ + { + description: "list with no additional flags", + args: []string{}, + golden: "manifest-list.golden", + }, + { + description: "list with quiet option", + args: []string{}, + flags: map[string]string{ + "quiet": "true", + }, + golden: "manifest-list-with-quiet-option.golden", + }, + } + + for _, tc := range testCases { + cli := test.NewFakeCli(nil) + cli.SetManifestStore(manifestStore) + cmd := newListCommand(cli) + cmd.SetArgs(tc.args) + for key, value := range tc.flags { + cmd.Flags().Set(key, value) + } + assert.NilError(t, cmd.Execute()) + golden.Assert(t, cli.OutBuffer().String(), tc.golden) + } +} diff --git a/cli/command/manifest/testdata/manifest-list-with-quiet-option.golden b/cli/command/manifest/testdata/manifest-list-with-quiet-option.golden new file mode 100644 index 0000000000..a91a0080a6 --- /dev/null +++ b/cli/command/manifest/testdata/manifest-list-with-quiet-option.golden @@ -0,0 +1,2 @@ +example.com/first:1 +example.com/second:2 diff --git a/cli/command/manifest/testdata/manifest-list.golden b/cli/command/manifest/testdata/manifest-list.golden new file mode 100644 index 0000000000..d4e6ddeb5e --- /dev/null +++ b/cli/command/manifest/testdata/manifest-list.golden @@ -0,0 +1,3 @@ +REPOSITORY TAG +example.com/first 1 +example.com/second 2