312cc7eebd
This fix is an attempt to address the issue raised in 28339. In `docker ps`, the formatter needs to expose all fields of `types.Container` to `preProcessor` so that template could be executed. This direct exposing is unreliable and could cause issues as user may incorrectly assume all fields in `types.Container` will be available for templating. However, the purpose of `preProcessor` is to only find out if `.Size` is defined (so that opts.size could be set accordingly). This fix defines `preProcessor` as `map[string]bool` with a func `Size()`. In this way, any unknown fields will be ignored. This fix adds several test cases to the existing `TestBuildContainerListOptions`. This fix fixes 28339. Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
118 lines
2.4 KiB
Go
118 lines
2.4 KiB
Go
package container
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/docker/docker/opts"
|
|
"github.com/docker/docker/pkg/testutil/assert"
|
|
)
|
|
|
|
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.Equal(t, c.expectedAll, options.All)
|
|
assert.Equal(t, c.expectedSize, options.Size)
|
|
assert.Equal(t, c.expectedLimit, options.Limit)
|
|
assert.Equal(t, options.Filters.Len(), len(c.expectedFilters))
|
|
|
|
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))
|
|
}
|
|
}
|
|
}
|
|
}
|