mirror of https://github.com/docker/cli.git
command/container: unify list tests in a single file
Move the remaining test with the others, and rename it from `TestBuildContainerListOptions` to `TestContainerListBuildContainerListOptions`, so that it has the same prefix as the other tests. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
ebca141311
commit
b062726313
|
@ -8,11 +8,123 @@ import (
|
||||||
"github.com/docker/cli/cli/config/configfile"
|
"github.com/docker/cli/cli/config/configfile"
|
||||||
"github.com/docker/cli/internal/test"
|
"github.com/docker/cli/internal/test"
|
||||||
. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
|
. "github.com/docker/cli/internal/test/builders" // Import builders to get the builder function as package function
|
||||||
|
"github.com/docker/cli/opts"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"gotest.tools/assert"
|
"gotest.tools/assert"
|
||||||
|
is "gotest.tools/assert/cmp"
|
||||||
"gotest.tools/golden"
|
"gotest.tools/golden"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
format: "{{.Size}} {{.CreatedAt}} {{.Networks}}",
|
||||||
|
},
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestContainerListErrors(t *testing.T) {
|
func TestContainerListErrors(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
args []string
|
args []string
|
||||||
|
|
|
@ -1,119 +0,0 @@
|
||||||
package container
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/docker/cli/opts"
|
|
||||||
"gotest.tools/assert"
|
|
||||||
is "gotest.tools/assert/cmp"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestBuildContainerListOptions(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
|
|
||||||
format: "{{.Size}} {{.CreatedAt}} {{.Networks}}",
|
|
||||||
},
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue