Skip to content

Commit b8cda96

Browse files
committed
cli/command/container: improve TestContainerStatsContext
- Don't use unnamed keys - Use sub-tests - Add test-cases for Name and ID fields Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 8228108 commit b8cda96

File tree

1 file changed

+124
-22
lines changed

1 file changed

+124
-22
lines changed

cli/command/container/formatter_stats_test.go

Lines changed: 124 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,138 @@ func TestContainerStatsContext(t *testing.T) {
1414
containerID := test.RandomID()
1515

1616
var ctx statsContext
17-
tt := []struct {
17+
tests := []struct {
18+
name string
1819
stats StatsEntry
1920
osType string
2021
expValue string
2122
expHeader string
2223
call func() string
2324
}{
24-
{StatsEntry{Container: containerID}, "", containerID, containerHeader, ctx.Container},
25-
{StatsEntry{CPUPercentage: 5.5}, "", "5.50%", cpuPercHeader, ctx.CPUPerc},
26-
{StatsEntry{CPUPercentage: 5.5, IsInvalid: true}, "", "--", cpuPercHeader, ctx.CPUPerc},
27-
{StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3}, "", "0.31B / 12.3B", netIOHeader, ctx.NetIO},
28-
{StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true}, "", "--", netIOHeader, ctx.NetIO},
29-
{StatsEntry{BlockRead: 0.1, BlockWrite: 2.3}, "", "0.1B / 2.3B", blockIOHeader, ctx.BlockIO},
30-
{StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true}, "", "--", blockIOHeader, ctx.BlockIO},
31-
{StatsEntry{MemoryPercentage: 10.2}, "", "10.20%", memPercHeader, ctx.MemPerc},
32-
{StatsEntry{MemoryPercentage: 10.2, IsInvalid: true}, "", "--", memPercHeader, ctx.MemPerc},
33-
{StatsEntry{MemoryPercentage: 10.2}, "windows", "--", memPercHeader, ctx.MemPerc},
34-
{StatsEntry{Memory: 24, MemoryLimit: 30}, "", "24B / 30B", memUseHeader, ctx.MemUsage},
35-
{StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true}, "", "-- / --", memUseHeader, ctx.MemUsage},
36-
{StatsEntry{Memory: 24, MemoryLimit: 30}, "windows", "24B", winMemUseHeader, ctx.MemUsage},
37-
{StatsEntry{PidsCurrent: 10}, "", "10", pidsHeader, ctx.PIDs},
38-
{StatsEntry{PidsCurrent: 10, IsInvalid: true}, "", "--", pidsHeader, ctx.PIDs},
39-
{StatsEntry{PidsCurrent: 10}, "windows", "--", pidsHeader, ctx.PIDs},
25+
{
26+
name: "Container",
27+
stats: StatsEntry{Container: containerID},
28+
expValue: containerID,
29+
expHeader: containerHeader,
30+
call: ctx.Container,
31+
},
32+
{
33+
name: "CPUPerc",
34+
stats: StatsEntry{CPUPercentage: 5.5},
35+
expValue: "5.50%",
36+
expHeader: cpuPercHeader,
37+
call: ctx.CPUPerc,
38+
},
39+
{
40+
name: "CPUPerc invalid",
41+
stats: StatsEntry{CPUPercentage: 5.5, IsInvalid: true},
42+
expValue: "--",
43+
expHeader: cpuPercHeader,
44+
call: ctx.CPUPerc,
45+
},
46+
{
47+
name: "NetIO",
48+
stats: StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3},
49+
expValue: "0.31B / 12.3B",
50+
expHeader: netIOHeader,
51+
call: ctx.NetIO,
52+
},
53+
{
54+
name: "NetIO invalid",
55+
stats: StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true},
56+
expValue: "--",
57+
expHeader: netIOHeader,
58+
call: ctx.NetIO,
59+
},
60+
{
61+
name: "BlockIO",
62+
stats: StatsEntry{BlockRead: 0.1, BlockWrite: 2.3},
63+
expValue: "0.1B / 2.3B",
64+
expHeader: blockIOHeader,
65+
call: ctx.BlockIO,
66+
},
67+
{
68+
name: "BlockIO invalid",
69+
stats: StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true},
70+
expValue: "--",
71+
expHeader: blockIOHeader,
72+
call: ctx.BlockIO,
73+
},
74+
{
75+
name: "MemPerc",
76+
stats: StatsEntry{MemoryPercentage: 10.2},
77+
expValue: "10.20%",
78+
expHeader: memPercHeader,
79+
call: ctx.MemPerc,
80+
},
81+
{
82+
name: "MemPerc invalid",
83+
stats: StatsEntry{MemoryPercentage: 10.2, IsInvalid: true},
84+
expValue: "--",
85+
expHeader: memPercHeader,
86+
call: ctx.MemPerc,
87+
},
88+
{
89+
name: "MemPerc windows",
90+
stats: StatsEntry{MemoryPercentage: 10.2},
91+
osType: "windows",
92+
expValue: "--",
93+
expHeader: memPercHeader,
94+
call: ctx.MemPerc,
95+
},
96+
{
97+
name: "MemUsage",
98+
stats: StatsEntry{Memory: 24, MemoryLimit: 30},
99+
expValue: "24B / 30B",
100+
expHeader: memUseHeader,
101+
call: ctx.MemUsage,
102+
},
103+
{
104+
name: "MemUsage invalid",
105+
stats: StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true},
106+
expValue: "-- / --",
107+
expHeader: memUseHeader,
108+
call: ctx.MemUsage,
109+
},
110+
{
111+
name: "MemUsage windows",
112+
stats: StatsEntry{Memory: 24, MemoryLimit: 30},
113+
osType: "windows",
114+
expValue: "24B",
115+
expHeader: winMemUseHeader,
116+
call: ctx.MemUsage,
117+
},
118+
{
119+
name: "PIDs",
120+
stats: StatsEntry{PidsCurrent: 10},
121+
expValue: "10",
122+
expHeader: pidsHeader,
123+
call: ctx.PIDs,
124+
},
125+
{
126+
name: "PIDs invalid",
127+
stats: StatsEntry{PidsCurrent: 10, IsInvalid: true},
128+
expValue: "--",
129+
expHeader: pidsHeader,
130+
call: ctx.PIDs,
131+
},
132+
{
133+
name: "PIDs windows",
134+
stats: StatsEntry{PidsCurrent: 10},
135+
osType: "windows",
136+
expValue: "--",
137+
expHeader: pidsHeader,
138+
call: ctx.PIDs,
139+
},
40140
}
41141

42-
for _, te := range tt {
43-
ctx = statsContext{s: te.stats, os: te.osType}
44-
if v := te.call(); v != te.expValue {
45-
t.Fatalf("Expected %q, got %q", te.expValue, v)
46-
}
142+
for _, tc := range tests {
143+
t.Run(tc.name, func(t *testing.T) {
144+
ctx = statsContext{s: tc.stats, os: tc.osType}
145+
if v := tc.call(); v != tc.expValue {
146+
t.Fatalf("Expected %q, got %q", tc.expValue, v)
147+
}
148+
})
47149
}
48150
}
49151

0 commit comments

Comments
 (0)