2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
|
|
|
"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
|
|
|
|
imageListFunc func(options types.ImageListOptions) ([]types.ImageSummary, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
imageListFunc: func(options types.ImageListOptions) ([]types.ImageSummary, error) {
|
|
|
|
return []types.ImageSummary{{}}, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewImagesCommand(test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc}))
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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
|
|
|
|
imageListFunc func(options types.ImageListOptions) ([]types.ImageSummary, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "format",
|
|
|
|
imageFormat: "raw",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "quiet-format",
|
|
|
|
args: []string{"-q"},
|
|
|
|
imageFormat: "table",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "match-name",
|
|
|
|
args: []string{"image"},
|
|
|
|
imageListFunc: func(options types.ImageListOptions) ([]types.ImageSummary, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("image", options.Filters.Get("reference")[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageSummary{{}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "filters",
|
|
|
|
args: []string{"--filter", "name=value"},
|
|
|
|
imageListFunc: func(options types.ImageListOptions) ([]types.ImageSummary, error) {
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal("value", options.Filters.Get("name")[0]))
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageSummary{{}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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("images"))
|
|
|
|
assert.Check(t, cmd.HasAlias("list"))
|
|
|
|
assert.Check(t, !cmd.HasAlias("other"))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|