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