@@ -126,7 +126,7 @@ container2 --
126126}
127127
128128func TestContainerStatsContextWriteWindows (t * testing.T ) {
129- tt := []struct {
129+ cases := []struct {
130130 context formatter.Context
131131 expected string
132132 }{
@@ -150,51 +150,54 @@ container2 -- --
150150` ,
151151 },
152152 }
153+ stats := []StatsEntry {
154+ {
155+ Container : "container1" ,
156+ CPUPercentage : 20 ,
157+ Memory : 20 ,
158+ MemoryLimit : 20 ,
159+ MemoryPercentage : 20 ,
160+ NetworkRx : 20 ,
161+ NetworkTx : 20 ,
162+ BlockRead : 20 ,
163+ BlockWrite : 20 ,
164+ PidsCurrent : 2 ,
165+ IsInvalid : false ,
166+ },
167+ {
168+ Container : "container2" ,
169+ CPUPercentage : 30 ,
170+ Memory : 30 ,
171+ MemoryLimit : 30 ,
172+ MemoryPercentage : 30 ,
173+ NetworkRx : 30 ,
174+ NetworkTx : 30 ,
175+ BlockRead : 30 ,
176+ BlockWrite : 30 ,
177+ PidsCurrent : 3 ,
178+ IsInvalid : true ,
179+ },
180+ }
153181
154- for _ , te := range tt {
155- stats := []StatsEntry {
156- {
157- Container : "container1" ,
158- CPUPercentage : 20 ,
159- Memory : 20 ,
160- MemoryLimit : 20 ,
161- MemoryPercentage : 20 ,
162- NetworkRx : 20 ,
163- NetworkTx : 20 ,
164- BlockRead : 20 ,
165- BlockWrite : 20 ,
166- PidsCurrent : 2 ,
167- IsInvalid : false ,
168- },
169- {
170- Container : "container2" ,
171- CPUPercentage : 30 ,
172- Memory : 30 ,
173- MemoryLimit : 30 ,
174- MemoryPercentage : 30 ,
175- NetworkRx : 30 ,
176- NetworkTx : 30 ,
177- BlockRead : 30 ,
178- BlockWrite : 30 ,
179- PidsCurrent : 3 ,
180- IsInvalid : true ,
181- },
182- }
183- var out bytes.Buffer
184- te .context .Output = & out
185- err := statsFormatWrite (te .context , stats , "windows" , false )
186- if err != nil {
187- assert .Error (t , err , te .expected )
188- } else {
189- assert .Check (t , is .Equal (te .expected , out .String ()))
190- }
182+ for _ , tc := range cases {
183+ tc := tc
184+ t .Run (string (tc .context .Format ), func (t * testing.T ) {
185+ var out bytes.Buffer
186+ tc .context .Output = & out
187+ err := statsFormatWrite (tc .context , stats , "windows" , false )
188+ if err != nil {
189+ assert .Error (t , err , tc .expected )
190+ } else {
191+ assert .Equal (t , out .String (), tc .expected )
192+ }
193+ })
191194 }
192195}
193196
194197func TestContainerStatsContextWriteWithNoStats (t * testing.T ) {
195198 var out bytes.Buffer
196199
197- contexts := []struct {
200+ cases := []struct {
198201 context formatter.Context
199202 expected string
200203 }{
@@ -217,22 +220,26 @@ func TestContainerStatsContextWriteWithNoStats(t *testing.T) {
217220 Format : "table {{.Container}}\t {{.CPUPerc}}" ,
218221 Output : & out ,
219222 },
220- "CONTAINER CPU %\n " ,
223+ "CONTAINER CPU %\n " ,
221224 },
222225 }
223226
224- for _ , context := range contexts {
225- statsFormatWrite (context .context , []StatsEntry {}, "linux" , false )
226- assert .Check (t , is .Equal (context .expected , out .String ()))
227- // Clean buffer
228- out .Reset ()
227+ for _ , tc := range cases {
228+ tc := tc
229+ t .Run (string (tc .context .Format ), func (t * testing.T ) {
230+ err := statsFormatWrite (tc .context , []StatsEntry {}, "linux" , false )
231+ assert .NilError (t , err )
232+ assert .Equal (t , out .String (), tc .expected )
233+ // Clean buffer
234+ out .Reset ()
235+ })
229236 }
230237}
231238
232239func TestContainerStatsContextWriteWithNoStatsWindows (t * testing.T ) {
233240 var out bytes.Buffer
234241
235- contexts := []struct {
242+ cases := []struct {
236243 context formatter.Context
237244 expected string
238245 }{
@@ -248,22 +255,25 @@ func TestContainerStatsContextWriteWithNoStatsWindows(t *testing.T) {
248255 Format : "table {{.Container}}\t {{.MemUsage}}" ,
249256 Output : & out ,
250257 },
251- "CONTAINER PRIV WORKING SET\n " ,
258+ "CONTAINER PRIV WORKING SET\n " ,
252259 },
253260 {
254261 formatter.Context {
255262 Format : "table {{.Container}}\t {{.CPUPerc}}\t {{.MemUsage}}" ,
256263 Output : & out ,
257264 },
258- "CONTAINER CPU % PRIV WORKING SET\n " ,
265+ "CONTAINER CPU % PRIV WORKING SET\n " ,
259266 },
260267 }
261268
262- for _ , context := range contexts {
263- statsFormatWrite (context .context , []StatsEntry {}, "windows" , false )
264- assert .Check (t , is .Equal (context .expected , out .String ()))
265- // Clean buffer
266- out .Reset ()
269+ for _ , tc := range cases {
270+ tc := tc
271+ t .Run (string (tc .context .Format ), func (t * testing.T ) {
272+ err := statsFormatWrite (tc .context , []StatsEntry {}, "windows" , false )
273+ assert .NilError (t , err )
274+ assert .Equal (t , out .String (), tc .expected )
275+ out .Reset ()
276+ })
267277 }
268278}
269279
0 commit comments