2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/testutil"
|
|
|
|
"github.com/docker/docker/pkg/testutil/golden"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
2017-04-25 19:24:07 -04:00
|
|
|
testutil.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) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "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) {
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.Equal(t, "value", options.Filters.Get("name")[0])
|
2017-03-30 20:21:14 -04:00
|
|
|
return []types.ImageSummary{{}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf := new(bytes.Buffer)
|
2017-07-05 14:19:52 -04:00
|
|
|
cli := test.NewFakeCliWithOutput(&fakeClient{imageListFunc: tc.imageListFunc}, buf)
|
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()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
actual := buf.String()
|
|
|
|
expected := string(golden.Get(t, []byte(actual), fmt.Sprintf("list-command-success.%s.golden", tc.name))[:])
|
|
|
|
testutil.EqualNormalizedString(t, testutil.RemoveSpace, actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewListCommandAlias(t *testing.T) {
|
2017-07-05 14:34:16 -04:00
|
|
|
cmd := newListCommand(test.NewFakeCli(&fakeClient{}))
|
2017-04-25 19:24:07 -04:00
|
|
|
assert.True(t, cmd.HasAlias("images"))
|
|
|
|
assert.True(t, cmd.HasAlias("list"))
|
|
|
|
assert.False(t, cmd.HasAlias("other"))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|