github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/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 TestContainerPsContext(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{ID: containerID}, false, 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: "verylongimagename"}, true, "verylongimag", imageHeader, ctx.Image},
    30  		{types.Container{Image: "verylongimagename"}, false, "verylongimagename", imageHeader, ctx.Image},
    31  		{types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
    32  		{types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
    33  		{types.Container{Created: unix}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
    34  		{types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports},
    35  		{types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status},
    36  		{types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size},
    37  		{types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size},
    38  		{types.Container{}, true, "", labelsHeader, ctx.Labels},
    39  		{types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels},
    40  		{types.Container{Created: unix}, true, "Less than a second", runningForHeader, ctx.RunningFor},
    41  	}
    42  
    43  	for _, c := range cases {
    44  		ctx = containerContext{c: c.container, trunc: c.trunc}
    45  		v := c.call()
    46  		if strings.Contains(v, ",") {
    47  			// comma-separated values means probably a map input, which won't
    48  			// be guaranteed to have the same order as our expected value
    49  			// We'll create maps and use reflect.DeepEquals to check instead:
    50  			entriesMap := make(map[string]string)
    51  			expMap := make(map[string]string)
    52  			entries := strings.Split(v, ",")
    53  			expectedEntries := strings.Split(c.expValue, ",")
    54  			for _, entry := range entries {
    55  				keyval := strings.Split(entry, "=")
    56  				entriesMap[keyval[0]] = keyval[1]
    57  			}
    58  			for _, expected := range expectedEntries {
    59  				keyval := strings.Split(expected, "=")
    60  				expMap[keyval[0]] = keyval[1]
    61  			}
    62  			if !reflect.DeepEqual(expMap, entriesMap) {
    63  				t.Fatalf("Expected entries: %v, got: %v", c.expValue, v)
    64  			}
    65  		} else if v != c.expValue {
    66  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
    67  		}
    68  
    69  		h := ctx.fullHeader()
    70  		if h != c.expHeader {
    71  			t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
    72  		}
    73  	}
    74  
    75  	c1 := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
    76  	ctx = containerContext{c: c1, trunc: true}
    77  
    78  	sid := ctx.Label("com.docker.swarm.swarm-id")
    79  	node := ctx.Label("com.docker.swarm.node_name")
    80  	if sid != "33" {
    81  		t.Fatalf("Expected 33, was %s\n", sid)
    82  	}
    83  
    84  	if node != "ubuntu" {
    85  		t.Fatalf("Expected ubuntu, was %s\n", node)
    86  	}
    87  
    88  	h := ctx.fullHeader()
    89  	if h != "SWARM ID\tNODE NAME" {
    90  		t.Fatalf("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h)
    91  
    92  	}
    93  
    94  	c2 := types.Container{}
    95  	ctx = containerContext{c: c2, trunc: true}
    96  
    97  	label := ctx.Label("anything.really")
    98  	if label != "" {
    99  		t.Fatalf("Expected an empty string, was %s", label)
   100  	}
   101  
   102  	ctx = containerContext{c: c2, trunc: true}
   103  	fullHeader := ctx.fullHeader()
   104  	if fullHeader != "" {
   105  		t.Fatalf("Expected fullHeader to be empty, was %s", fullHeader)
   106  	}
   107  
   108  }