github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/formatter/custom_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/docker/docker/pkg/stringid"
    10  	"github.com/docker/engine-api/types"
    11  )
    12  
    13  func TestContainerPsContext(t *testing.T) {
    14  	containerID := stringid.GenerateRandomID()
    15  	unix := time.Now().Add(-65 * time.Second).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), containerIDHeader, ctx.ID},
    26  		{types.Container{ID: containerID}, false, containerID, containerIDHeader, 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, "verylongimagename", imageHeader, ctx.Image},
    30  		{types.Container{Image: "verylongimagename"}, false, "verylongimagename", imageHeader, ctx.Image},
    31  		{types.Container{
    32  			Image:   "a5a665ff33eced1e0803148700880edab4",
    33  			ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
    34  		},
    35  			true,
    36  			"a5a665ff33ec",
    37  			imageHeader,
    38  			ctx.Image,
    39  		},
    40  		{types.Container{
    41  			Image:   "a5a665ff33eced1e0803148700880edab4",
    42  			ImageID: "a5a665ff33eced1e0803148700880edab4269067ed77e27737a708d0d293fbf5",
    43  		},
    44  			false,
    45  			"a5a665ff33eced1e0803148700880edab4",
    46  			imageHeader,
    47  			ctx.Image,
    48  		},
    49  		{types.Container{Image: ""}, true, "<no image>", imageHeader, ctx.Image},
    50  		{types.Container{Command: "sh -c 'ls -la'"}, true, `"sh -c 'ls -la'"`, commandHeader, ctx.Command},
    51  		{types.Container{Created: unix}, true, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
    52  		{types.Container{Ports: []types.Port{{PrivatePort: 8080, PublicPort: 8080, Type: "tcp"}}}, true, "8080/tcp", portsHeader, ctx.Ports},
    53  		{types.Container{Status: "RUNNING"}, true, "RUNNING", statusHeader, ctx.Status},
    54  		{types.Container{SizeRw: 10}, true, "10 B", sizeHeader, ctx.Size},
    55  		{types.Container{SizeRw: 10, SizeRootFs: 20}, true, "10 B (virtual 20 B)", sizeHeader, ctx.Size},
    56  		{types.Container{}, true, "", labelsHeader, ctx.Labels},
    57  		{types.Container{Labels: map[string]string{"cpu": "6", "storage": "ssd"}}, true, "cpu=6,storage=ssd", labelsHeader, ctx.Labels},
    58  		{types.Container{Created: unix}, true, "About a minute", runningForHeader, ctx.RunningFor},
    59  	}
    60  
    61  	for _, c := range cases {
    62  		ctx = containerContext{c: c.container, trunc: c.trunc}
    63  		v := c.call()
    64  		if strings.Contains(v, ",") {
    65  			compareMultipleValues(t, v, c.expValue)
    66  		} else if v != c.expValue {
    67  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
    68  		}
    69  
    70  		h := ctx.fullHeader()
    71  		if h != c.expHeader {
    72  			t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
    73  		}
    74  	}
    75  
    76  	c1 := types.Container{Labels: map[string]string{"com.docker.swarm.swarm-id": "33", "com.docker.swarm.node_name": "ubuntu"}}
    77  	ctx = containerContext{c: c1, trunc: true}
    78  
    79  	sid := ctx.Label("com.docker.swarm.swarm-id")
    80  	node := ctx.Label("com.docker.swarm.node_name")
    81  	if sid != "33" {
    82  		t.Fatalf("Expected 33, was %s\n", sid)
    83  	}
    84  
    85  	if node != "ubuntu" {
    86  		t.Fatalf("Expected ubuntu, was %s\n", node)
    87  	}
    88  
    89  	h := ctx.fullHeader()
    90  	if h != "SWARM ID\tNODE NAME" {
    91  		t.Fatalf("Expected %s, was %s\n", "SWARM ID\tNODE NAME", h)
    92  
    93  	}
    94  
    95  	c2 := types.Container{}
    96  	ctx = containerContext{c: c2, trunc: true}
    97  
    98  	label := ctx.Label("anything.really")
    99  	if label != "" {
   100  		t.Fatalf("Expected an empty string, was %s", label)
   101  	}
   102  
   103  	ctx = containerContext{c: c2, trunc: true}
   104  	fullHeader := ctx.fullHeader()
   105  	if fullHeader != "" {
   106  		t.Fatalf("Expected fullHeader to be empty, was %s", fullHeader)
   107  	}
   108  
   109  }
   110  
   111  func TestImagesContext(t *testing.T) {
   112  	imageID := stringid.GenerateRandomID()
   113  	unix := time.Now().Unix()
   114  
   115  	var ctx imageContext
   116  	cases := []struct {
   117  		imageCtx  imageContext
   118  		expValue  string
   119  		expHeader string
   120  		call      func() string
   121  	}{
   122  		{imageContext{
   123  			i:     types.Image{ID: imageID},
   124  			trunc: true,
   125  		}, stringid.TruncateID(imageID), imageIDHeader, ctx.ID},
   126  		{imageContext{
   127  			i:     types.Image{ID: imageID},
   128  			trunc: false,
   129  		}, imageID, imageIDHeader, ctx.ID},
   130  		{imageContext{
   131  			i:     types.Image{Size: 10},
   132  			trunc: true,
   133  		}, "10 B", sizeHeader, ctx.Size},
   134  		{imageContext{
   135  			i:     types.Image{Created: unix},
   136  			trunc: true,
   137  		}, time.Unix(unix, 0).String(), createdAtHeader, ctx.CreatedAt},
   138  		// FIXME
   139  		// {imageContext{
   140  		// 	i:     types.Image{Created: unix},
   141  		// 	trunc: true,
   142  		// }, units.HumanDuration(time.Unix(unix, 0)), createdSinceHeader, ctx.CreatedSince},
   143  		{imageContext{
   144  			i:    types.Image{},
   145  			repo: "busybox",
   146  		}, "busybox", repositoryHeader, ctx.Repository},
   147  		{imageContext{
   148  			i:   types.Image{},
   149  			tag: "latest",
   150  		}, "latest", tagHeader, ctx.Tag},
   151  		{imageContext{
   152  			i:      types.Image{},
   153  			digest: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a",
   154  		}, "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a", digestHeader, ctx.Digest},
   155  	}
   156  
   157  	for _, c := range cases {
   158  		ctx = c.imageCtx
   159  		v := c.call()
   160  		if strings.Contains(v, ",") {
   161  			compareMultipleValues(t, v, c.expValue)
   162  		} else if v != c.expValue {
   163  			t.Fatalf("Expected %s, was %s\n", c.expValue, v)
   164  		}
   165  
   166  		h := ctx.fullHeader()
   167  		if h != c.expHeader {
   168  			t.Fatalf("Expected %s, was %s\n", c.expHeader, h)
   169  		}
   170  	}
   171  }
   172  
   173  func compareMultipleValues(t *testing.T, value, expected string) {
   174  	// comma-separated values means probably a map input, which won't
   175  	// be guaranteed to have the same order as our expected value
   176  	// We'll create maps and use reflect.DeepEquals to check instead:
   177  	entriesMap := make(map[string]string)
   178  	expMap := make(map[string]string)
   179  	entries := strings.Split(value, ",")
   180  	expectedEntries := strings.Split(expected, ",")
   181  	for _, entry := range entries {
   182  		keyval := strings.Split(entry, "=")
   183  		entriesMap[keyval[0]] = keyval[1]
   184  	}
   185  	for _, expected := range expectedEntries {
   186  		keyval := strings.Split(expected, "=")
   187  		expMap[keyval[0]] = keyval[1]
   188  	}
   189  	if !reflect.DeepEqual(expMap, entriesMap) {
   190  		t.Fatalf("Expected entries: %v, got: %v", expected, value)
   191  	}
   192  }