github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/cli/command/formatter/stats_test.go (about)

     1  package formatter
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/pkg/stringid"
     8  	"github.com/docker/docker/pkg/testutil/assert"
     9  )
    10  
    11  func TestContainerStatsContext(t *testing.T) {
    12  	containerID := stringid.GenerateRandomID()
    13  
    14  	var ctx containerStatsContext
    15  	tt := []struct {
    16  		stats     StatsEntry
    17  		osType    string
    18  		expValue  string
    19  		expHeader string
    20  		call      func() string
    21  	}{
    22  		{StatsEntry{Container: containerID}, "", containerID, containerHeader, ctx.Container},
    23  		{StatsEntry{CPUPercentage: 5.5}, "", "5.50%", cpuPercHeader, ctx.CPUPerc},
    24  		{StatsEntry{CPUPercentage: 5.5, IsInvalid: true}, "", "--", cpuPercHeader, ctx.CPUPerc},
    25  		{StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3}, "", "0.31B / 12.3B", netIOHeader, ctx.NetIO},
    26  		{StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true}, "", "--", netIOHeader, ctx.NetIO},
    27  		{StatsEntry{BlockRead: 0.1, BlockWrite: 2.3}, "", "0.1B / 2.3B", blockIOHeader, ctx.BlockIO},
    28  		{StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true}, "", "--", blockIOHeader, ctx.BlockIO},
    29  		{StatsEntry{MemoryPercentage: 10.2}, "", "10.20%", memPercHeader, ctx.MemPerc},
    30  		{StatsEntry{MemoryPercentage: 10.2, IsInvalid: true}, "", "--", memPercHeader, ctx.MemPerc},
    31  		{StatsEntry{MemoryPercentage: 10.2}, "windows", "--", memPercHeader, ctx.MemPerc},
    32  		{StatsEntry{Memory: 24, MemoryLimit: 30}, "", "24B / 30B", memUseHeader, ctx.MemUsage},
    33  		{StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true}, "", "-- / --", memUseHeader, ctx.MemUsage},
    34  		{StatsEntry{Memory: 24, MemoryLimit: 30}, "windows", "24B", winMemUseHeader, ctx.MemUsage},
    35  		{StatsEntry{PidsCurrent: 10}, "", "10", pidsHeader, ctx.PIDs},
    36  		{StatsEntry{PidsCurrent: 10, IsInvalid: true}, "", "--", pidsHeader, ctx.PIDs},
    37  		{StatsEntry{PidsCurrent: 10}, "windows", "--", pidsHeader, ctx.PIDs},
    38  	}
    39  
    40  	for _, te := range tt {
    41  		ctx = containerStatsContext{s: te.stats, os: te.osType}
    42  		if v := te.call(); v != te.expValue {
    43  			t.Fatalf("Expected %q, got %q", te.expValue, v)
    44  		}
    45  
    46  		h := ctx.FullHeader()
    47  		if h != te.expHeader {
    48  			t.Fatalf("Expected %q, got %q", te.expHeader, h)
    49  		}
    50  	}
    51  }
    52  
    53  func TestContainerStatsContextWrite(t *testing.T) {
    54  	tt := []struct {
    55  		context  Context
    56  		expected string
    57  	}{
    58  		{
    59  			Context{Format: "{{InvalidFunction}}"},
    60  			`Template parsing error: template: :1: function "InvalidFunction" not defined
    61  `,
    62  		},
    63  		{
    64  			Context{Format: "{{nil}}"},
    65  			`Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
    66  `,
    67  		},
    68  		{
    69  			Context{Format: "table {{.MemUsage}}"},
    70  			`MEM USAGE / LIMIT
    71  20B / 20B
    72  -- / --
    73  `,
    74  		},
    75  		{
    76  			Context{Format: "{{.Container}}  {{.ID}}  {{.Name}}"},
    77  			`container1  abcdef  foo
    78  container2    --
    79  `,
    80  		},
    81  		{
    82  			Context{Format: "{{.Container}}  {{.CPUPerc}}"},
    83  			`container1  20.00%
    84  container2  --
    85  `,
    86  		},
    87  	}
    88  
    89  	for _, te := range tt {
    90  		stats := []StatsEntry{
    91  			{
    92  				Container:        "container1",
    93  				ID:               "abcdef",
    94  				Name:             "/foo",
    95  				CPUPercentage:    20,
    96  				Memory:           20,
    97  				MemoryLimit:      20,
    98  				MemoryPercentage: 20,
    99  				NetworkRx:        20,
   100  				NetworkTx:        20,
   101  				BlockRead:        20,
   102  				BlockWrite:       20,
   103  				PidsCurrent:      2,
   104  				IsInvalid:        false,
   105  			},
   106  			{
   107  				Container:        "container2",
   108  				CPUPercentage:    30,
   109  				Memory:           30,
   110  				MemoryLimit:      30,
   111  				MemoryPercentage: 30,
   112  				NetworkRx:        30,
   113  				NetworkTx:        30,
   114  				BlockRead:        30,
   115  				BlockWrite:       30,
   116  				PidsCurrent:      3,
   117  				IsInvalid:        true,
   118  			},
   119  		}
   120  		var out bytes.Buffer
   121  		te.context.Output = &out
   122  		err := ContainerStatsWrite(te.context, stats, "linux")
   123  		if err != nil {
   124  			assert.Error(t, err, te.expected)
   125  		} else {
   126  			assert.Equal(t, out.String(), te.expected)
   127  		}
   128  	}
   129  }
   130  
   131  func TestContainerStatsContextWriteWindows(t *testing.T) {
   132  	tt := []struct {
   133  		context  Context
   134  		expected string
   135  	}{
   136  		{
   137  			Context{Format: "table {{.MemUsage}}"},
   138  			`PRIV WORKING SET
   139  20B
   140  -- / --
   141  `,
   142  		},
   143  		{
   144  			Context{Format: "{{.Container}}  {{.CPUPerc}}"},
   145  			`container1  20.00%
   146  container2  --
   147  `,
   148  		},
   149  		{
   150  			Context{Format: "{{.Container}}  {{.MemPerc}}  {{.PIDs}}"},
   151  			`container1  --  --
   152  container2  --  --
   153  `,
   154  		},
   155  	}
   156  
   157  	for _, te := range tt {
   158  		stats := []StatsEntry{
   159  			{
   160  				Container:        "container1",
   161  				CPUPercentage:    20,
   162  				Memory:           20,
   163  				MemoryLimit:      20,
   164  				MemoryPercentage: 20,
   165  				NetworkRx:        20,
   166  				NetworkTx:        20,
   167  				BlockRead:        20,
   168  				BlockWrite:       20,
   169  				PidsCurrent:      2,
   170  				IsInvalid:        false,
   171  			},
   172  			{
   173  				Container:        "container2",
   174  				CPUPercentage:    30,
   175  				Memory:           30,
   176  				MemoryLimit:      30,
   177  				MemoryPercentage: 30,
   178  				NetworkRx:        30,
   179  				NetworkTx:        30,
   180  				BlockRead:        30,
   181  				BlockWrite:       30,
   182  				PidsCurrent:      3,
   183  				IsInvalid:        true,
   184  			},
   185  		}
   186  		var out bytes.Buffer
   187  		te.context.Output = &out
   188  		err := ContainerStatsWrite(te.context, stats, "windows")
   189  		if err != nil {
   190  			assert.Error(t, err, te.expected)
   191  		} else {
   192  			assert.Equal(t, out.String(), te.expected)
   193  		}
   194  	}
   195  }
   196  
   197  func TestContainerStatsContextWriteWithNoStats(t *testing.T) {
   198  	var out bytes.Buffer
   199  
   200  	contexts := []struct {
   201  		context  Context
   202  		expected string
   203  	}{
   204  		{
   205  			Context{
   206  				Format: "{{.Container}}",
   207  				Output: &out,
   208  			},
   209  			"",
   210  		},
   211  		{
   212  			Context{
   213  				Format: "table {{.Container}}",
   214  				Output: &out,
   215  			},
   216  			"CONTAINER\n",
   217  		},
   218  		{
   219  			Context{
   220  				Format: "table {{.Container}}\t{{.CPUPerc}}",
   221  				Output: &out,
   222  			},
   223  			"CONTAINER           CPU %\n",
   224  		},
   225  	}
   226  
   227  	for _, context := range contexts {
   228  		ContainerStatsWrite(context.context, []StatsEntry{}, "linux")
   229  		assert.Equal(t, context.expected, out.String())
   230  		// Clean buffer
   231  		out.Reset()
   232  	}
   233  }
   234  
   235  func TestContainerStatsContextWriteWithNoStatsWindows(t *testing.T) {
   236  	var out bytes.Buffer
   237  
   238  	contexts := []struct {
   239  		context  Context
   240  		expected string
   241  	}{
   242  		{
   243  			Context{
   244  				Format: "{{.Container}}",
   245  				Output: &out,
   246  			},
   247  			"",
   248  		},
   249  		{
   250  			Context{
   251  				Format: "table {{.Container}}\t{{.MemUsage}}",
   252  				Output: &out,
   253  			},
   254  			"CONTAINER           PRIV WORKING SET\n",
   255  		},
   256  		{
   257  			Context{
   258  				Format: "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}",
   259  				Output: &out,
   260  			},
   261  			"CONTAINER           CPU %               PRIV WORKING SET\n",
   262  		},
   263  	}
   264  
   265  	for _, context := range contexts {
   266  		ContainerStatsWrite(context.context, []StatsEntry{}, "windows")
   267  		assert.Equal(t, out.String(), context.expected)
   268  		// Clean buffer
   269  		out.Reset()
   270  	}
   271  }