2015-05-01 17:23:27 -04:00
|
|
|
package ps
|
|
|
|
|
|
|
|
import (
|
2015-07-17 00:03:16 -04:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2015-05-01 17:23:27 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerContextID(t *testing.T) {
|
2015-07-20 20:55:30 -04:00
|
|
|
containerID := stringid.GenerateRandomID()
|
2015-05-01 17:23:27 -04:00
|
|
|
unix := time.Now().Unix()
|
|
|
|
|
|
|
|
var ctx containerContext
|
|
|
|
cases := []struct {
|
|
|
|
container types.Container
|
|
|
|
trunc bool
|
|
|
|
expValue string
|
|
|
|
expHeader string
|
|
|
|
call func() string
|
|
|
|
}{
|
2015-07-20 20:55:30 -04:00
|
|
|
{types.Container{ID: containerID}, true, stringid.TruncateID(containerID), idHeader, ctx.ID},
|
2015-05-01 17:23:27 -04:00
|
|
|
{types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", namesHeader, ctx.Names},
|
|
|
|
{types.Container{Image: "ubuntu"}, true, "ubuntu", imageHeader, ctx.Image},
|
|
|
|
{types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
|
|
|
|
{types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
|
|
|
|
{types.Container{Created: int(unix)}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
|
2015-07-17 00:03:16 -04:00
|
|
|
{types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports},
|
2015-05-01 17:23:27 -04:00
|
|
|
{types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status},
|
|
|
|
{types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size},
|
|
|
|
{types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size},
|
|
|
|
{types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
ctx = containerContext{c: c.container, trunc: c.trunc}
|
|
|
|
v := c.call()
|
2015-07-17 00:03:16 -04:00
|
|
|
if strings.Contains(v, ",") {
|
|
|
|
// comma-separated values means probably a map input, which won't
|
|
|
|
// be guaranteed to have the same order as our expected value
|
|
|
|
// We'll create maps and use reflect.DeepEquals to check instead:
|
|
|
|
entriesMap := make(map[string]string)
|
|
|
|
expMap := make(map[string]string)
|
|
|
|
entries := strings.Split(v, ",")
|
|
|
|
expectedEntries := strings.Split(c.expValue, ",")
|
|
|
|
for _, entry := range entries {
|
|
|
|
keyval := strings.Split(entry, "=")
|
|
|
|
entriesMap[keyval[0]] = keyval[1]
|
|
|
|
}
|
|
|
|
for _, expected := range expectedEntries {
|
|
|
|
keyval := strings.Split(expected, "=")
|
|
|
|
expMap[keyval[0]] = keyval[1]
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expMap, entriesMap) {
|
|
|
|
t.Fatalf("Expected entries: %v, got: %v", c.expValue, v)
|
|
|
|
}
|
|
|
|
} else if v != c.expValue {
|
2015-05-01 17:23:27 -04:00
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
h := ctx.fullHeader()
|
|
|
|
if h != c.expHeader {
|
|
|
|
t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
|
|
|
|
ctx = containerContext{c: c, trunc: true}
|
|
|
|
|
|
|
|
sid := ctx.Label("com.docker.swarm.swarm-id")
|
|
|
|
node := ctx.Label("com.docker.swarm.node_name")
|
|
|
|
if sid != "33" {
|
2015-07-17 00:03:16 -04:00
|
|
|
t.Fatalf("Expected 33, was %s\n", sid)
|
2015-05-01 17:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if node != "ubuntu" {
|
2015-07-17 00:03:16 -04:00
|
|
|
t.Fatalf("Expected ubuntu, was %s\n", node)
|
2015-05-01 17:23:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
h := ctx.fullHeader()
|
|
|
|
if h != "SWARM ID\tNODE NAME" {
|
2015-07-17 00:03:16 -04:00
|
|
|
t.Fatalf("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h)
|
2015-05-01 17:23:27 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|