2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-09-13 03:01:31 -04:00
|
|
|
"encoding/json"
|
2016-09-08 13:11:39 -04:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2017-09-07 17:50:44 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
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"
|
2017-06-15 12:28:08 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerPsContext(t *testing.T) {
|
|
|
|
containerID := stringid.GenerateRandomID()
|
|
|
|
unix := time.Now().Add(-65 * time.Second).Unix()
|
|
|
|
|
|
|
|
var ctx containerContext
|
|
|
|
cases := []struct {
|
|
|
|
container types.Container
|
|
|
|
trunc bool
|
|
|
|
expValue string
|
|
|
|
call func() string
|
|
|
|
}{
|
2017-02-03 19:48:46 -05:00
|
|
|
{types.Container{ID: containerID}, true, stringid.TruncateID(containerID), ctx.ID},
|
|
|
|
{types.Container{ID: containerID}, false, containerID, ctx.ID},
|
|
|
|
{types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", ctx.Names},
|
|
|
|
{types.Container{Image: "ubuntu"}, true, "ubuntu", ctx.Image},
|
|
|
|
{types.Container{Image: "verylongimagename"}, true, "verylongimagename", ctx.Image},
|
|
|
|
{types.Container{Image: "verylongimagename"}, false, "verylongimagename", ctx.Image},
|
2016-09-08 13:11:39 -04:00
|
|
|
{types.Container{
|
|
|
|
Image: "a5a665ff33eced1e0803148700880edab4",
|
|
|
|
ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
"a5a665ff33ec",
|
|
|
|
ctx.Image,
|
|
|
|
},
|
|
|
|
{types.Container{
|
|
|
|
Image: "a5a665ff33eced1e0803148700880edab4",
|
|
|
|
ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
"a5a665ff33eced1e0803148700880edab4",
|
|
|
|
ctx.Image,
|
|
|
|
},
|
2017-02-03 19:48:46 -05:00
|
|
|
{types.Container{Image: ""}, true, "<no image>", ctx.Image},
|
|
|
|
{types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, ctx.Command},
|
|
|
|
{types.Container{Created: unix}, true, time.Unix(unix, 0).String(), ctx.CreatedAt},
|
|
|
|
{types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", ctx.Ports},
|
|
|
|
{types.Container{Status: "RUNNING"}, true, "RUNNING", ctx.Status},
|
|
|
|
{types.Container{SizeRw: 10}, true, "10B", ctx.Size},
|
|
|
|
{types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10B (virtual 20B)", ctx.Size},
|
|
|
|
{types.Container{}, true, "", ctx.Labels},
|
|
|
|
{types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", ctx.Labels},
|
|
|
|
{types.Container{Created: unix}, true, "About a minute ago", ctx.RunningFor},
|
2016-09-08 13:11:39 -04:00
|
|
|
{types.Container{
|
|
|
|
Mounts: []types.MountPoint{
|
|
|
|
{
|
|
|
|
Name: "this-is-a-long-volume-name-and-will-be-truncated-if-trunc-is-set",
|
|
|
|
Driver: "local",
|
|
|
|
Source: "/a/path",
|
|
|
|
},
|
|
|
|
},
|
2017-10-13 05:01:32 -04:00
|
|
|
}, true, "this-is-a-long…", ctx.Mounts},
|
2016-09-08 13:11:39 -04:00
|
|
|
{types.Container{
|
|
|
|
Mounts: []types.MountPoint{
|
|
|
|
{
|
|
|
|
Driver: "local",
|
|
|
|
Source: "/a/path",
|
|
|
|
},
|
|
|
|
},
|
2017-02-03 19:48:46 -05:00
|
|
|
}, false, "/a/path", ctx.Mounts},
|
2016-09-08 13:11:39 -04:00
|
|
|
{types.Container{
|
|
|
|
Mounts: []types.MountPoint{
|
|
|
|
{
|
|
|
|
Name: "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203",
|
|
|
|
Driver: "local",
|
|
|
|
Source: "/a/path",
|
|
|
|
},
|
|
|
|
},
|
2017-02-03 19:48:46 -05:00
|
|
|
}, false, "733908409c91817de8e92b0096373245f329f19a88e2c849f02460e9b3d1c203", ctx.Mounts},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = containerContext{c: c.container, trunc: c.trunc}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c1 := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
|
|
|
|
ctx = containerContext{c: c1, trunc: true}
|
|
|
|
|
|
|
|
sid := ctx.Label("com.docker.swarm.swarm-id")
|
|
|
|
node := ctx.Label("com.docker.swarm.node_name")
|
|
|
|
if sid != "33" {
|
|
|
|
t.Fatalf("Expected 33, was %s\n", sid)
|
|
|
|
}
|
|
|
|
|
|
|
|
if node != "ubuntu" {
|
|
|
|
t.Fatalf("Expected ubuntu, was %s\n", node)
|
|
|
|
}
|
|
|
|
|
|
|
|
c2 := types.Container{}
|
|
|
|
ctx = containerContext{c: c2, trunc: true}
|
|
|
|
|
|
|
|
label := ctx.Label("anything.really")
|
|
|
|
if label != "" {
|
|
|
|
t.Fatalf("Expected an empty string, was %s", label)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerContextWrite(t *testing.T) {
|
|
|
|
unixTime := time.Now().AddDate(0, 0, -1).Unix()
|
|
|
|
expectedTime := time.Unix(unixTime, 0).String()
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
cases := []struct {
|
|
|
|
context Context
|
2016-09-08 13:11:39 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
// Errors
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: "{{InvalidFunction}}"},
|
2016-09-08 13:11:39 -04:00
|
|
|
`Template parsing error: template: :1: function "InvalidFunction" not defined
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: "{{nil}}"},
|
2016-09-08 13:11:39 -04:00
|
|
|
`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
// Table Format
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("table", false, true)},
|
2016-09-08 13:11:39 -04:00
|
|
|
`CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE
|
2017-02-07 15:58:56 -05:00
|
|
|
containerID1 ubuntu "" 24 hours ago foobar_baz 0B
|
|
|
|
containerID2 ubuntu "" 24 hours ago foobar_bar 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("table", false, false)},
|
2016-09-08 13:11:39 -04:00
|
|
|
`CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
|
|
|
containerID1 ubuntu "" 24 hours ago foobar_baz
|
|
|
|
containerID2 ubuntu "" 24 hours ago foobar_bar
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("table {{.Image}}", false, false)},
|
2016-09-08 13:11:39 -04:00
|
|
|
"IMAGE\nubuntu\nubuntu\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("table {{.Image}}", false, true)},
|
2016-09-08 13:11:39 -04:00
|
|
|
"IMAGE\nubuntu\nubuntu\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("table {{.Image}}", true, false)},
|
2016-09-08 13:11:39 -04:00
|
|
|
"IMAGE\nubuntu\nubuntu\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("table", true, false)},
|
2016-09-08 13:11:39 -04:00
|
|
|
"containerID1\ncontainerID2\n",
|
|
|
|
},
|
|
|
|
// Raw Format
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("raw", false, false)},
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Sprintf(`container_id: containerID1
|
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_baz
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
container_id: containerID2
|
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_bar
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
`, expectedTime, expectedTime),
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("raw", false, true)},
|
2016-09-08 13:11:39 -04:00
|
|
|
fmt.Sprintf(`container_id: containerID1
|
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_baz
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2017-02-07 15:58:56 -05:00
|
|
|
size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
container_id: containerID2
|
|
|
|
image: ubuntu
|
|
|
|
command: ""
|
|
|
|
created_at: %s
|
2016-09-12 16:59:18 -04:00
|
|
|
status:
|
2016-09-08 13:11:39 -04:00
|
|
|
names: foobar_bar
|
2016-09-12 16:59:18 -04:00
|
|
|
labels:
|
|
|
|
ports:
|
2017-02-07 15:58:56 -05:00
|
|
|
size: 0B
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
`, expectedTime, expectedTime),
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("raw", true, false)},
|
2016-09-08 13:11:39 -04:00
|
|
|
"container_id: containerID1\ncontainer_id: containerID2\n",
|
|
|
|
},
|
|
|
|
// Custom Format
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: "{{.Image}}"},
|
2016-09-08 13:11:39 -04:00
|
|
|
"ubuntu\nubuntu\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{Format: NewContainerFormat("{{.Image}}", false, true)},
|
2016-09-08 13:11:39 -04:00
|
|
|
"ubuntu\nubuntu\n",
|
|
|
|
},
|
2017-05-21 21:39:06 -04:00
|
|
|
// Special headers for customized table format
|
2017-02-03 23:23:00 -05:00
|
|
|
{
|
|
|
|
Context{Format: NewContainerFormat(`table {{truncate .ID 5}}\t{{json .Image}} {{.RunningFor}}/{{title .Status}}/{{pad .Ports 2 2}}.{{upper .Names}} {{lower .Status}}`, false, true)},
|
2017-09-07 17:50:44 -04:00
|
|
|
string(golden.Get(t, "container-context-write-special-headers.golden")),
|
2017-02-03 23:23:00 -05:00
|
|
|
},
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
for _, testcase := range cases {
|
2016-09-08 13:11:39 -04:00
|
|
|
containers := []types.Container{
|
|
|
|
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unixTime},
|
|
|
|
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unixTime},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
2016-09-12 16:59:18 -04:00
|
|
|
testcase.context.Output = out
|
|
|
|
err := ContainerWrite(testcase.context, containers)
|
|
|
|
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-09-12 16:59:18 -04: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-09-12 16:59:18 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
containers := []types.Container{}
|
|
|
|
|
|
|
|
contexts := []struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
context Context
|
2016-09-08 13:11:39 -04:00
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{
|
|
|
|
Format: "{{.Image}}",
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{
|
|
|
|
Format: "table {{.Image}}",
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
"IMAGE\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{
|
|
|
|
Format: NewContainerFormat("{{.Image}}", false, true),
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{
|
|
|
|
Format: NewContainerFormat("table {{.Image}}", false, true),
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
"IMAGE\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{
|
|
|
|
Format: "table {{.Image}}\t{{.Size}}",
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
"IMAGE SIZE\n",
|
|
|
|
},
|
|
|
|
{
|
2016-09-12 16:59:18 -04:00
|
|
|
Context{
|
|
|
|
Format: NewContainerFormat("table {{.Image}}\t{{.Size}}", false, true),
|
|
|
|
Output: out,
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
"IMAGE SIZE\n",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, context := range contexts {
|
2016-09-12 16:59:18 -04:00
|
|
|
ContainerWrite(context.context, containers)
|
|
|
|
assert.Equal(t, context.expected, out.String())
|
2016-09-08 13:11:39 -04:00
|
|
|
// Clean buffer
|
|
|
|
out.Reset()
|
|
|
|
}
|
|
|
|
}
|
2016-09-13 03:01:31 -04:00
|
|
|
|
|
|
|
func TestContainerContextWriteJSON(t *testing.T) {
|
|
|
|
unix := time.Now().Add(-65 * time.Second).Unix()
|
|
|
|
containers := []types.Container{
|
|
|
|
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu", Created: unix},
|
|
|
|
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu", Created: unix},
|
|
|
|
}
|
|
|
|
expectedCreated := time.Unix(unix, 0).String()
|
|
|
|
expectedJSONs := []map[string]interface{}{
|
2017-05-03 18:14:30 -04:00
|
|
|
{
|
|
|
|
"Command": "\"\"",
|
|
|
|
"CreatedAt": expectedCreated,
|
|
|
|
"ID": "containerID1",
|
|
|
|
"Image": "ubuntu",
|
|
|
|
"Labels": "",
|
|
|
|
"LocalVolumes": "0",
|
|
|
|
"Mounts": "",
|
|
|
|
"Names": "foobar_baz",
|
|
|
|
"Networks": "",
|
|
|
|
"Ports": "",
|
|
|
|
"RunningFor": "About a minute ago",
|
|
|
|
"Size": "0B",
|
|
|
|
"Status": "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Command": "\"\"",
|
|
|
|
"CreatedAt": expectedCreated,
|
|
|
|
"ID": "containerID2",
|
|
|
|
"Image": "ubuntu",
|
|
|
|
"Labels": "",
|
|
|
|
"LocalVolumes": "0",
|
|
|
|
"Mounts": "",
|
|
|
|
"Names": "foobar_bar",
|
|
|
|
"Networks": "",
|
|
|
|
"Ports": "",
|
|
|
|
"RunningFor": "About a minute ago",
|
|
|
|
"Size": "0B",
|
|
|
|
"Status": "",
|
|
|
|
},
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
err := ContainerWrite(Context{Format: "{{json .}}", Output: out}, containers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
2017-06-15 12:28:08 -04:00
|
|
|
msg := fmt.Sprintf("Output: line %d: %s", i, line)
|
2016-09-13 03:01:31 -04:00
|
|
|
var m map[string]interface{}
|
2017-06-15 12:28:08 -04:00
|
|
|
err := json.Unmarshal([]byte(line), &m)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, expectedJSONs[i], m, msg)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContainerContextWriteJSONField(t *testing.T) {
|
|
|
|
containers := []types.Container{
|
|
|
|
{ID: "containerID1", Names: []string{"/foobar_baz"}, Image: "ubuntu"},
|
|
|
|
{ID: "containerID2", Names: []string{"/foobar_bar"}, Image: "ubuntu"},
|
|
|
|
}
|
|
|
|
out := bytes.NewBufferString("")
|
|
|
|
err := ContainerWrite(Context{Format: "{{json .ID}}", Output: out}, containers)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for i, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
2017-06-15 12:28:08 -04:00
|
|
|
msg := fmt.Sprintf("Output: line %d: %s", i, line)
|
2016-09-13 03:01:31 -04:00
|
|
|
var s string
|
2017-06-15 12:28:08 -04:00
|
|
|
err := json.Unmarshal([]byte(line), &s)
|
|
|
|
require.NoError(t, err, msg)
|
|
|
|
assert.Equal(t, containers[i].ID, s, msg)
|
2016-09-13 03:01:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-11-12 11:10:27 -05:00
|
|
|
|
|
|
|
func TestContainerBackCompat(t *testing.T) {
|
2016-11-17 01:17:40 -05:00
|
|
|
containers := []types.Container{{ID: "brewhaha"}}
|
2016-11-12 11:10:27 -05:00
|
|
|
cases := []string{
|
|
|
|
"ID",
|
|
|
|
"Names",
|
|
|
|
"Image",
|
|
|
|
"Command",
|
|
|
|
"CreatedAt",
|
|
|
|
"RunningFor",
|
|
|
|
"Ports",
|
|
|
|
"Status",
|
|
|
|
"Size",
|
|
|
|
"Labels",
|
|
|
|
"Mounts",
|
|
|
|
}
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx := Context{Format: Format(fmt.Sprintf("{{ .%s }}", c)), Output: buf}
|
|
|
|
if err := ContainerWrite(ctx, containers); err != nil {
|
2016-11-17 01:17:40 -05:00
|
|
|
t.Logf("could not render template for field '%s': %v", c, err)
|
2016-11-12 11:10:27 -05:00
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|
2017-06-23 10:24:43 -04:00
|
|
|
|
|
|
|
type ports struct {
|
|
|
|
ports []types.Port
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
// nolint: lll
|
|
|
|
func TestDisplayablePorts(t *testing.T) {
|
|
|
|
cases := []ports{
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9988/tcp"},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9988/udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "0.0.0.0",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"0.0.0.0:0->9988/tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9988/tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"4.3.2.1:8899->9988/tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 9988,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"4.3.2.1:9988->9988/tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 9988,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9988/udp, 9988/udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 9998,
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 9999,
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"1.2.3.4:9998-9999->9998-9999/udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 8887,
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PublicPort: 8888,
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"1.2.3.4:8887->9998/udp, 1.2.3.4:8888->9999/udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 9998,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 9999,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9998-9999/udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 6677,
|
|
|
|
PublicPort: 7766,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9988/udp, 1.2.3.4:7766->6677/tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 2233,
|
|
|
|
PublicPort: 3322,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"4.3.2.1:3322->2233/tcp, 1.2.3.4:8899->9988/tcp, 1.2.3.4:8899->9988/udp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 9988,
|
|
|
|
PublicPort: 8899,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.2.3.4",
|
|
|
|
PrivatePort: 6677,
|
|
|
|
PublicPort: 7766,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "4.3.2.1",
|
|
|
|
PrivatePort: 2233,
|
|
|
|
PublicPort: 3322,
|
|
|
|
Type: "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"9988/udp, 4.3.2.1:3322->2233/tcp, 1.2.3.4:7766->6677/tcp",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]types.Port{
|
|
|
|
{
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "1.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 80,
|
|
|
|
PrivatePort: 1024,
|
|
|
|
Type: "udp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "tcp",
|
|
|
|
}, {
|
|
|
|
IP: "2.1.1.1",
|
|
|
|
PublicPort: 1024,
|
|
|
|
PrivatePort: 80,
|
|
|
|
Type: "udp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"80/tcp, 80/udp, 1024/tcp, 1024/udp, 1.1.1.1:1024->80/tcp, 1.1.1.1:1024->80/udp, 2.1.1.1:1024->80/tcp, 2.1.1.1:1024->80/udp, 1.1.1.1:80->1024/tcp, 1.1.1.1:80->1024/udp, 2.1.1.1:80->1024/tcp, 2.1.1.1:80->1024/udp",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, port := range cases {
|
|
|
|
actual := DisplayablePorts(port.ports)
|
|
|
|
assert.Equal(t, port.expected, actual)
|
|
|
|
}
|
|
|
|
}
|