github.com/campoy/docker@v1.8.0-rc1/api/client/ps/custom_test.go (about) 1 package ps 2 3 import ( 4 "reflect" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/docker/docker/api/types" 10 "github.com/docker/docker/pkg/stringid" 11 ) 12 13 func TestContainerContextID(t *testing.T) { 14 containerId := stringid.GenerateRandomID() 15 unix := time.Now().Unix() 16 17 var ctx containerContext 18 cases := []struct { 19 container types.Container 20 trunc bool 21 expValue string 22 expHeader string 23 call func() string 24 }{ 25 {types.Container{ID: containerId}, true, stringid.TruncateID(containerId), idHeader, ctx.ID}, 26 {types.Container{Names: []string{"/foobar_baz"}}, true, "foobar_baz", namesHeader, ctx.Names}, 27 {types.Container{Image: "ubuntu"}, true, "ubuntu", imageHeader, ctx.Image}, 28 {types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image}, 29 {types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command}, 30 {types.Container{Created: int(unix)}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt}, 31 {types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports}, 32 {types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status}, 33 {types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size}, 34 {types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size}, 35 {types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels}, 36 } 37 38 for _, c := range cases { 39 ctx = containerContext{c: c.container, trunc: c.trunc} 40 v := c.call() 41 if strings.Contains(v, ",") { 42 // comma-separated values means probably a map input, which won't 43 // be guaranteed to have the same order as our expected value 44 // We'll create maps and use reflect.DeepEquals to check instead: 45 entriesMap := make(map[string]string) 46 expMap := make(map[string]string) 47 entries := strings.Split(v, ",") 48 expectedEntries := strings.Split(c.expValue, ",") 49 for _, entry := range entries { 50 keyval := strings.Split(entry, "=") 51 entriesMap[keyval[0]] = keyval[1] 52 } 53 for _, expected := range expectedEntries { 54 keyval := strings.Split(expected, "=") 55 expMap[keyval[0]] = keyval[1] 56 } 57 if !reflect.DeepEqual(expMap, entriesMap) { 58 t.Fatalf("Expected entries: %v, got: %v", c.expValue, v) 59 } 60 } else if v != c.expValue { 61 t.Fatalf("Expected %s, was %s\n", c.expValue, v) 62 } 63 64 h := ctx.fullHeader() 65 if h != c.expHeader { 66 t.Fatalf("Expected %s, was %s\n", c.expHeader, h) 67 } 68 } 69 70 c := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}} 71 ctx = containerContext{c: c, trunc: true} 72 73 sid := ctx.Label("com.docker.swarm.swarm-id") 74 node := ctx.Label("com.docker.swarm.node_name") 75 if sid != "33" { 76 t.Fatalf("Expected 33, was %s\n", sid) 77 } 78 79 if node != "ubuntu" { 80 t.Fatalf("Expected ubuntu, was %s\n", node) 81 } 82 83 h := ctx.fullHeader() 84 if h != "SWARM ID\tNODE NAME" { 85 t.Fatalf("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h) 86 87 } 88 }