2016-11-22 19:23:21 -05:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-11-22 19:23:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPluginContext(t *testing.T) {
|
|
|
|
pluginID := stringid.GenerateRandomID()
|
|
|
|
|
|
|
|
var ctx pluginContext
|
|
|
|
cases := []struct {
|
|
|
|
pluginCtx pluginContext
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
|
|
|
{pluginContext{
|
|
|
|
p: types.Plugin{ID: pluginID},
|
|
|
|
trunc: false,
|
2017-02-03 19:48:46 -05:00
|
|
|
}, pluginID, ctx.ID},
|
2016-11-22 19:23:21 -05:00
|
|
|
{pluginContext{
|
|
|
|
p: types.Plugin{ID: pluginID},
|
|
|
|
trunc: true,
|
2017-02-03 19:48:46 -05:00
|
|
|
}, stringid.TruncateID(pluginID), ctx.ID},
|
2016-11-22 19:23:21 -05:00
|
|
|
{pluginContext{
|
|
|
|
p: types.Plugin{Name: "plugin_name"},
|
2017-02-03 19:48:46 -05:00
|
|
|
}, "plugin_name", ctx.Name},
|
2016-11-22 19:23:21 -05:00
|
|
|
{pluginContext{
|
|
|
|
p: types.Plugin{Config: types.PluginConfig{Description: "plugin_description"}},
|
2017-02-03 19:48:46 -05:00
|
|
|
}, "plugin_description", ctx.Description},
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = c.pluginCtx
|
|
|
|
v := c.call()
|
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
compareMultipleValues(t, v, c.expValue)
|
|
|
|
} else if v != c.expValue {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginContextWrite(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
context Context
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
|
|
|
|
// Errors
|
|
|
|
{
|
|
|
|
Context{Format: "{{InvalidFunction}}"},
|
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: "{{nil}}"},
|
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Table format
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("table", false)},
|
|
|
|
`ID NAME DESCRIPTION ENABLED
|
|
|
|
pluginID1 foobar_baz description 1 true
|
|
|
|
pluginID2 foobar_bar description 2 false
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("table", true)},
|
|
|
|
`pluginID1
|
|
|
|
pluginID2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("table {{.Name}}", false)},
|
|
|
|
`NAME
|
|
|
|
foobar_baz
|
|
|
|
foobar_bar
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("table {{.Name}}", true)},
|
|
|
|
`NAME
|
|
|
|
foobar_baz
|
|
|
|
foobar_bar
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Raw Format
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("raw", false)},
|
|
|
|
`plugin_id: pluginID1
|
|
|
|
name: foobar_baz
|
|
|
|
description: description 1
|
|
|
|
enabled: true
|
|
|
|
|
|
|
|
plugin_id: pluginID2
|
|
|
|
name: foobar_bar
|
|
|
|
description: description 2
|
|
|
|
enabled: false
|
|
|
|
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("raw", true)},
|
|
|
|
`plugin_id: pluginID1
|
|
|
|
plugin_id: pluginID2
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Custom Format
|
|
|
|
{
|
|
|
|
Context{Format: NewPluginFormat("{{.Name}}", false)},
|
|
|
|
`foobar_baz
|
|
|
|
foobar_bar
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range cases {
|
|
|
|
plugins := []*types.Plugin{
|
|
|
|
{ID: "pluginID1", Name: "foobar_baz", Config: types.PluginConfig{Description: "description 1"}, Enabled: true},
|
|
|
|
{ID: "pluginID2", Name: "foobar_bar", Config: types.PluginConfig{Description: "description 2"}, Enabled: false},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
testcase.context.Output = out
|
|
|
|
err := PluginWrite(testcase.context, plugins)
|
|
|
|
if err != nil {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.EqualError(t, err, testcase.expected)
|
2016-11-22 19:23:21 -05:00
|
|
|
} else {
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, testcase.expected, out.String())
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginContextWriteJSON(t *testing.T) {
|
|
|
|
plugins := []*types.Plugin{
|
|
|
|
{ID: "pluginID1", Name: "foobar_baz"},
|
|
|
|
{ID: "pluginID2", Name: "foobar_bar"},
|
|
|
|
}
|
|
|
|
expectedJSONs := []map[string]interface{}{
|
2017-01-28 19:54:32 -05:00
|
|
|
{"Description": "", "Enabled": false, "ID": "pluginID1", "Name": "foobar_baz", "PluginReference": ""},
|
|
|
|
{"Description": "", "Enabled": false, "ID": "pluginID2", "Name": "foobar_bar", "PluginReference": ""},
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
err := PluginWrite(Context{Format: "{{json .}}", Output: out}, plugins)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := json.Unmarshal([]byte(line), &m); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, expectedJSONs[i], m)
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPluginContextWriteJSONField(t *testing.T) {
|
|
|
|
plugins := []*types.Plugin{
|
|
|
|
{ID: "pluginID1", Name: "foobar_baz"},
|
|
|
|
{ID: "pluginID2", Name: "foobar_bar"},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
err := PluginWrite(Context{Format: "{{json .ID}}", Output: out}, plugins)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal([]byte(line), &s); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
Remove pkg/testutil/assert in favor of testify
I noticed that we're using a homegrown package for assertions. The
functions are extremely similar to testify, but with enough slight
differences to be confusing (for example, Equal takes its arguments in a
different order). We already vendor testify, and it's used in a few
places by tests.
I also found some problems with pkg/testutil/assert. For example, the
NotNil function seems to be broken. It checks the argument against
"nil", which only works for an interface. If you pass in a nil map or
slice, the equality check will fail.
In the interest of avoiding NIH, I'm proposing replacing
pkg/testutil/assert with testify. The test code looks almost the same,
but we avoid the confusion of having two similar but slightly different
assertion packages, and having to maintain our own package instead of
using a commonly-used one.
In the process, I found a few places where the tests should halt if an
assertion fails, so I've made those cases (that I noticed) use "require"
instead of "assert", and I've vendored the "require" package from
testify alongside the already-present "assert" package.
Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
2017-04-13 18:45:37 -04:00
|
|
|
assert.Equal(t, plugins[i].ID, s)
|
2016-11-22 19:23:21 -05:00
|
|
|
}
|
|
|
|
}
|