2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-25 07:10:34 -05:00
|
|
|
"io"
|
2017-03-30 20:21:14 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2023-10-13 14:34:32 -04:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewImagesCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
2023-10-13 14:34:32 -04:00
|
|
|
imageListFunc func(options types.ImageListOptions) ([]image.Summary, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
|
|
|
args: []string{"arg1", "arg2"},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires at most 1 argument.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "failed-list",
|
|
|
|
expectedError: "something went wrong",
|
2023-10-13 14:34:32 -04:00
|
|
|
imageListFunc: func(options types.ImageListOptions) ([]image.Summary, error) {
|
|
|
|
return []image.Summary{}, errors.Errorf("something went wrong")
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewImagesCommand(test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}))
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewImagesCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
imageFormat string
|
2023-10-13 14:34:32 -04:00
|
|
|
imageListFunc func(options types.ImageListOptions) ([]image.Summary, error)
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "format",
|
|
|
|
imageFormat: "raw",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "quiet-format",
|
|
|
|
args: []string{"-q"},
|
|
|
|
imageFormat: "table",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "match-name",
|
|
|
|
args: []string{"image"},
|
2023-10-13 14:34:32 -04:00
|
|
|
imageListFunc: func(options types.ImageListOptions) ([]image.Summary, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image", options.Filters.Get("reference")[0]))
|
2023-10-13 14:34:32 -04:00
|
|
|
return []image.Summary{}, nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filters",
|
|
|
|
args: []string{"--filter", "name=value"},
|
2023-10-13 14:34:32 -04:00
|
|
|
imageListFunc: func(options types.ImageListOptions) ([]image.Summary, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("value", options.Filters.Get("name")[0]))
|
2023-10-13 14:34:32 -04:00
|
|
|
return []image.Summary{}, nil
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-08-16 12:19:53 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc})
|
2017-07-05 14:34:16 -04:00
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{ImagesFormat: tc.imageFormat})
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd := NewImagesCommand(cli)
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-08-16 12:19:53 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), fmt.Sprintf("list-command-success.%s.golden", tc.name))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewListCommandAlias(t *testing.T) {
|
2017-07-05 14:34:16 -04:00
|
|
|
cmd := newListCommand(test.NewFakeCli(&fakeClient{}))
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, cmd.HasAlias("list"))
|
|
|
|
assert.Check(t, !cmd.HasAlias("other"))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|