Browse code

Add unit tests to cli/command/formatter/stats.go

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>

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