2016-09-08 13:11:39 -04:00
|
|
|
package container
|
|
|
|
|
2016-09-13 14:53:11 -04:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-09-13 14:53:11 -04:00
|
|
|
)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
func TestBuildContainerListOptions(t *testing.T) {
|
2016-09-13 14:53:11 -04:00
|
|
|
filters := opts.NewFilterOpt()
|
2018-03-06 15:13:00 -05:00
|
|
|
assert.NilError(t, filters.Set("foo=bar"))
|
|
|
|
assert.NilError(t, filters.Set("baz=foo"))
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
contexts := []struct {
|
|
|
|
psOpts *psOptions
|
|
|
|
expectedAll bool
|
|
|
|
expectedSize bool
|
|
|
|
expectedLimit int
|
|
|
|
expectedFilters map[string]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
psOpts: &psOptions{
|
|
|
|
all: true,
|
|
|
|
size: true,
|
|
|
|
last: 5,
|
2016-09-13 14:53:11 -04:00
|
|
|
filter: filters,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
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),
|
|
|
|
},
|
2016-11-12 19:44:51 -05:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range contexts {
|
|
|
|
options, err := buildContainerListOptions(c.psOpts)
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
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()))
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
for k, v := range c.expectedFilters {
|
2016-11-01 10:01:16 -04:00
|
|
|
f := options.Filters
|
2016-09-08 13:11:39 -04:00
|
|
|
if !f.ExactMatch(k, v) {
|
|
|
|
t.Fatalf("Expected filter with key %s to be %s but got %s", k, v, f.Get(k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|