2018-02-15 11:58:17 -05:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-25 07:05:59 -05:00
|
|
|
"io"
|
2018-02-15 11:58:17 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
|
|
"github.com/docker/cli/internal/test"
|
2019-04-02 11:20:21 -04:00
|
|
|
. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
|
2019-12-20 08:57:54 -05:00
|
|
|
"github.com/docker/cli/opts"
|
2018-02-15 11:58:17 -05:00
|
|
|
"github.com/docker/docker/api/types"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2018-02-15 11:58:17 -05:00
|
|
|
)
|
|
|
|
|
2019-12-20 08:57:54 -05:00
|
|
|
func TestContainerListBuildContainerListOptions(t *testing.T) {
|
|
|
|
filters := opts.NewFilterOpt()
|
|
|
|
assert.NilError(t, filters.Set("foo=bar"))
|
|
|
|
assert.NilError(t, filters.Set("baz=foo"))
|
|
|
|
|
|
|
|
contexts := []struct {
|
|
|
|
psOpts *psOptions
|
|
|
|
expectedAll bool
|
|
|
|
expectedSize bool
|
|
|
|
expectedLimit int
|
|
|
|
expectedFilters map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
psOpts: &psOptions{
|
|
|
|
all: true,
|
|
|
|
size: true,
|
|
|
|
last: 5,
|
|
|
|
filter: filters,
|
|
|
|
},
|
|
|
|
expectedAll: true,
|
|
|
|
expectedSize: true,
|
|
|
|
expectedLimit: 5,
|
|
|
|
expectedFilters: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
psOpts: &psOptions{
|
|
|
|
all: true,
|
|
|
|
size: true,
|
|
|
|
last: -1,
|
|
|
|
nLatest: true,
|
|
|
|
},
|
|
|
|
expectedAll: true,
|
|
|
|
expectedSize: true,
|
|
|
|
expectedLimit: 1,
|
|
|
|
expectedFilters: make(map[string]string),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
psOpts: &psOptions{
|
|
|
|
all: true,
|
|
|
|
size: false,
|
|
|
|
last: 5,
|
|
|
|
filter: filters,
|
|
|
|
// With .Size, size should be true
|
|
|
|
format: "{{.Size}}",
|
|
|
|
},
|
|
|
|
expectedAll: true,
|
|
|
|
expectedSize: true,
|
|
|
|
expectedLimit: 5,
|
|
|
|
expectedFilters: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
psOpts: &psOptions{
|
|
|
|
all: true,
|
|
|
|
size: false,
|
|
|
|
last: 5,
|
|
|
|
filter: filters,
|
|
|
|
// With .Size, size should be true
|
2019-12-24 10:30:23 -05:00
|
|
|
format: "{{.Size}} {{.CreatedAt}} {{upper .Networks}}",
|
2019-12-20 08:57:54 -05:00
|
|
|
},
|
|
|
|
expectedAll: true,
|
|
|
|
expectedSize: true,
|
|
|
|
expectedLimit: 5,
|
|
|
|
expectedFilters: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
psOpts: &psOptions{
|
|
|
|
all: true,
|
|
|
|
size: false,
|
|
|
|
last: 5,
|
|
|
|
filter: filters,
|
|
|
|
// Without .Size, size should be false
|
|
|
|
format: "{{.CreatedAt}} {{.Networks}}",
|
|
|
|
},
|
|
|
|
expectedAll: true,
|
|
|
|
expectedSize: false,
|
|
|
|
expectedLimit: 5,
|
|
|
|
expectedFilters: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range contexts {
|
|
|
|
options, err := buildContainerListOptions(c.psOpts)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
assert.Check(t, is.Equal(c.expectedAll, options.All))
|
|
|
|
assert.Check(t, is.Equal(c.expectedSize, options.Size))
|
|
|
|
assert.Check(t, is.Equal(c.expectedLimit, options.Limit))
|
|
|
|
assert.Check(t, is.Equal(len(c.expectedFilters), options.Filters.Len()))
|
|
|
|
|
|
|
|
for k, v := range c.expectedFilters {
|
|
|
|
f := options.Filters
|
|
|
|
if !f.ExactMatch(k, v) {
|
|
|
|
t.Fatalf("Expected filter with key %s to be %s but got %s", k, v, f.Get(k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 11:58:17 -05:00
|
|
|
func TestContainerListErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
flags map[string]string
|
|
|
|
containerListFunc func(types.ContainerListOptions) ([]types.Container, error)
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
flags: map[string]string{
|
|
|
|
"format": "{{invalid}}",
|
|
|
|
},
|
|
|
|
expectedError: `function "invalid" not defined`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
flags: map[string]string{
|
|
|
|
"format": "{{join}}",
|
|
|
|
},
|
|
|
|
expectedError: `wrong number of args for join`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return nil, fmt.Errorf("error listing containers")
|
|
|
|
},
|
|
|
|
expectedError: "error listing containers",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cmd := newListCommand(
|
|
|
|
test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: tc.containerListFunc,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
for key, value := range tc.flags {
|
|
|
|
cmd.Flags().Set(key, value)
|
|
|
|
}
|
2022-02-25 07:05:59 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2018-02-15 11:58:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerListWithoutFormat(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return []types.Container{
|
|
|
|
*Container("c1"),
|
|
|
|
*Container("c2", WithName("foo")),
|
|
|
|
*Container("c3", WithPort(80, 80, TCP), WithPort(81, 81, TCP), WithPort(82, 82, TCP)),
|
|
|
|
*Container("c4", WithPort(81, 81, UDP)),
|
|
|
|
*Container("c5", WithPort(82, 82, IP("8.8.8.8"), TCP)),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-02-15 11:58:17 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-without-format.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerListNoTrunc(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return []types.Container{
|
|
|
|
*Container("c1"),
|
|
|
|
*Container("c2", WithName("foo/bar")),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.Flags().Set("no-trunc", "true")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-02-15 11:58:17 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-without-format-no-trunc.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for GitHub issue docker/docker#21772
|
|
|
|
func TestContainerListNamesMultipleTime(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return []types.Container{
|
|
|
|
*Container("c1"),
|
|
|
|
*Container("c2", WithName("foo/bar")),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.Flags().Set("format", "{{.Names}} {{.Names}}")
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-02-15 11:58:17 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-format-name-name.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test for GitHub issue docker/docker#30291
|
|
|
|
func TestContainerListFormatTemplateWithArg(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return []types.Container{
|
|
|
|
*Container("c1", WithLabel("some.label", "value")),
|
|
|
|
*Container("c2", WithName("foo/bar"), WithLabel("foo", "bar")),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.Flags().Set("format", `{{.Names}} {{.Label "some.label"}}`)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-02-15 11:58:17 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-format-with-arg.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerListFormatSizeSetsOption(t *testing.T) {
|
2023-03-02 11:05:28 -05:00
|
|
|
tests := []struct {
|
|
|
|
doc, format, sizeFlag string
|
|
|
|
sizeExpected bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
doc: "detect with all fields",
|
|
|
|
format: `{{json .}}`,
|
|
|
|
sizeExpected: true,
|
2018-02-15 11:58:17 -05:00
|
|
|
},
|
2023-03-02 11:05:28 -05:00
|
|
|
{
|
|
|
|
doc: "detect with explicit field",
|
|
|
|
format: `{{.Size}}`,
|
|
|
|
sizeExpected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "detect no size",
|
|
|
|
format: `{{.Names}}`,
|
|
|
|
sizeExpected: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "override enable",
|
|
|
|
format: `{{.Names}}`,
|
|
|
|
sizeFlag: "true",
|
|
|
|
sizeExpected: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
doc: "override disable",
|
|
|
|
format: `{{.Size}}`,
|
|
|
|
sizeFlag: "false",
|
|
|
|
sizeExpected: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.doc, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(options types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
assert.Check(t, is.Equal(options.Size, tc.sizeExpected))
|
|
|
|
return []types.Container{}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
cmd.Flags().Set("format", tc.format)
|
|
|
|
if tc.sizeFlag != "" {
|
|
|
|
cmd.Flags().Set("size", tc.sizeFlag)
|
|
|
|
}
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
})
|
|
|
|
}
|
2018-02-15 11:58:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerListWithConfigFormat(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return []types.Container{
|
2022-05-30 21:33:19 -04:00
|
|
|
*Container("c1", WithLabel("some.label", "value"), WithSize(10700000)),
|
|
|
|
*Container("c2", WithName("foo/bar"), WithLabel("foo", "bar"), WithSize(3200000)),
|
2018-02-15 11:58:17 -05:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cli.SetConfigFile(&configfile.ConfigFile{
|
2022-05-30 21:33:19 -04:00
|
|
|
PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }} {{ .Size}}",
|
2018-02-15 11:58:17 -05:00
|
|
|
})
|
|
|
|
cmd := newListCommand(cli)
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, cmd.Execute())
|
2018-02-15 11:58:17 -05:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-with-config-format.golden")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerListWithFormat(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
|
|
|
|
return []types.Container{
|
|
|
|
*Container("c1", WithLabel("some.label", "value")),
|
|
|
|
*Container("c2", WithName("foo/bar"), WithLabel("foo", "bar")),
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
2022-06-07 04:14:31 -04:00
|
|
|
|
|
|
|
t.Run("with format", func(t *testing.T) {
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}"))
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-with-format.golden")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with format and quiet", func(t *testing.T) {
|
|
|
|
cli.OutBuffer().Reset()
|
|
|
|
cmd := newListCommand(cli)
|
|
|
|
assert.Check(t, cmd.Flags().Set("format", "{{ .Names }} {{ .Image }} {{ .Labels }}"))
|
|
|
|
assert.Check(t, cmd.Flags().Set("quiet", "true"))
|
|
|
|
assert.NilError(t, cmd.Execute())
|
2023-04-09 19:14:21 -04:00
|
|
|
assert.Equal(t, cli.ErrBuffer().String(), "WARNING: Ignoring custom format, because both --format and --quiet are set.\n")
|
2022-06-07 04:14:31 -04:00
|
|
|
golden.Assert(t, cli.OutBuffer().String(), "container-list-quiet.golden")
|
|
|
|
})
|
2018-02-15 11:58:17 -05:00
|
|
|
}
|