2016-07-15 18:37:05 -04:00
|
|
|
package container
|
|
|
|
|
2016-09-13 14:53:11 -04:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
|
|
)
|
2016-07-15 18:37:05 -04:00
|
|
|
|
|
|
|
func TestBuildContainerListOptions(t *testing.T) {
|
2016-09-13 14:53:11 -04:00
|
|
|
filters := opts.NewFilterOpt()
|
|
|
|
assert.NilError(t, filters.Set("foo=bar"))
|
|
|
|
assert.NilError(t, filters.Set("baz=foo"))
|
2016-07-15 18:37:05 -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-07-15 18:37:05 -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-07-15 18:37:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range contexts {
|
|
|
|
options, err := buildContainerListOptions(c.psOpts)
|
2016-09-13 14:53:11 -04:00
|
|
|
assert.NilError(t, err)
|
2016-07-15 18:37:05 -04:00
|
|
|
|
2016-09-13 14:53:11 -04:00
|
|
|
assert.Equal(t, c.expectedAll, options.All)
|
|
|
|
assert.Equal(t, c.expectedSize, options.Size)
|
|
|
|
assert.Equal(t, c.expectedLimit, options.Limit)
|
2016-11-01 10:01:16 -04:00
|
|
|
assert.Equal(t, options.Filters.Len(), len(c.expectedFilters))
|
2016-07-15 18:37:05 -04:00
|
|
|
|
|
|
|
for k, v := range c.expectedFilters {
|
2016-11-01 10:01:16 -04:00
|
|
|
f := options.Filters
|
2016-07-15 18:37:05 -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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|