From b8cda96d117d8c2eb639869d373f3cff359a8bc0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 11 Sep 2025 13:58:50 +0200 Subject: [PATCH] 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 --- cli/command/container/formatter_stats_test.go | 146 +++++++++++++++--- 1 file changed, 124 insertions(+), 22 deletions(-) diff --git a/cli/command/container/formatter_stats_test.go b/cli/command/container/formatter_stats_test.go index 91c69a5d7c..220dc53d0a 100644 --- a/cli/command/container/formatter_stats_test.go +++ b/cli/command/container/formatter_stats_test.go @@ -14,36 +14,138 @@ func TestContainerStatsContext(t *testing.T) { containerID := test.RandomID() var ctx statsContext - tt := []struct { + tests := []struct { + name string stats StatsEntry osType string expValue string expHeader string call func() string }{ - {StatsEntry{Container: containerID}, "", containerID, containerHeader, ctx.Container}, - {StatsEntry{CPUPercentage: 5.5}, "", "5.50%", cpuPercHeader, ctx.CPUPerc}, - {StatsEntry{CPUPercentage: 5.5, IsInvalid: true}, "", "--", cpuPercHeader, ctx.CPUPerc}, - {StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3}, "", "0.31B / 12.3B", netIOHeader, ctx.NetIO}, - {StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true}, "", "--", netIOHeader, ctx.NetIO}, - {StatsEntry{BlockRead: 0.1, BlockWrite: 2.3}, "", "0.1B / 2.3B", blockIOHeader, ctx.BlockIO}, - {StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true}, "", "--", blockIOHeader, ctx.BlockIO}, - {StatsEntry{MemoryPercentage: 10.2}, "", "10.20%", memPercHeader, ctx.MemPerc}, - {StatsEntry{MemoryPercentage: 10.2, IsInvalid: true}, "", "--", memPercHeader, ctx.MemPerc}, - {StatsEntry{MemoryPercentage: 10.2}, "windows", "--", memPercHeader, ctx.MemPerc}, - {StatsEntry{Memory: 24, MemoryLimit: 30}, "", "24B / 30B", memUseHeader, ctx.MemUsage}, - {StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true}, "", "-- / --", memUseHeader, ctx.MemUsage}, - {StatsEntry{Memory: 24, MemoryLimit: 30}, "windows", "24B", winMemUseHeader, ctx.MemUsage}, - {StatsEntry{PidsCurrent: 10}, "", "10", pidsHeader, ctx.PIDs}, - {StatsEntry{PidsCurrent: 10, IsInvalid: true}, "", "--", pidsHeader, ctx.PIDs}, - {StatsEntry{PidsCurrent: 10}, "windows", "--", pidsHeader, ctx.PIDs}, + { + name: "Container", + stats: StatsEntry{Container: containerID}, + expValue: containerID, + expHeader: containerHeader, + call: ctx.Container, + }, + { + name: "CPUPerc", + stats: StatsEntry{CPUPercentage: 5.5}, + expValue: "5.50%", + expHeader: cpuPercHeader, + call: ctx.CPUPerc, + }, + { + name: "CPUPerc invalid", + stats: StatsEntry{CPUPercentage: 5.5, IsInvalid: true}, + expValue: "--", + expHeader: cpuPercHeader, + call: ctx.CPUPerc, + }, + { + name: "NetIO", + stats: StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3}, + expValue: "0.31B / 12.3B", + expHeader: netIOHeader, + call: ctx.NetIO, + }, + { + name: "NetIO invalid", + stats: StatsEntry{NetworkRx: 0.31, NetworkTx: 12.3, IsInvalid: true}, + expValue: "--", + expHeader: netIOHeader, + call: ctx.NetIO, + }, + { + name: "BlockIO", + stats: StatsEntry{BlockRead: 0.1, BlockWrite: 2.3}, + expValue: "0.1B / 2.3B", + expHeader: blockIOHeader, + call: ctx.BlockIO, + }, + { + name: "BlockIO invalid", + stats: StatsEntry{BlockRead: 0.1, BlockWrite: 2.3, IsInvalid: true}, + expValue: "--", + expHeader: blockIOHeader, + call: ctx.BlockIO, + }, + { + name: "MemPerc", + stats: StatsEntry{MemoryPercentage: 10.2}, + expValue: "10.20%", + expHeader: memPercHeader, + call: ctx.MemPerc, + }, + { + name: "MemPerc invalid", + stats: StatsEntry{MemoryPercentage: 10.2, IsInvalid: true}, + expValue: "--", + expHeader: memPercHeader, + call: ctx.MemPerc, + }, + { + name: "MemPerc windows", + stats: StatsEntry{MemoryPercentage: 10.2}, + osType: "windows", + expValue: "--", + expHeader: memPercHeader, + call: ctx.MemPerc, + }, + { + name: "MemUsage", + stats: StatsEntry{Memory: 24, MemoryLimit: 30}, + expValue: "24B / 30B", + expHeader: memUseHeader, + call: ctx.MemUsage, + }, + { + name: "MemUsage invalid", + stats: StatsEntry{Memory: 24, MemoryLimit: 30, IsInvalid: true}, + expValue: "-- / --", + expHeader: memUseHeader, + call: ctx.MemUsage, + }, + { + name: "MemUsage windows", + stats: StatsEntry{Memory: 24, MemoryLimit: 30}, + osType: "windows", + expValue: "24B", + expHeader: winMemUseHeader, + call: ctx.MemUsage, + }, + { + name: "PIDs", + stats: StatsEntry{PidsCurrent: 10}, + expValue: "10", + expHeader: pidsHeader, + call: ctx.PIDs, + }, + { + name: "PIDs invalid", + stats: StatsEntry{PidsCurrent: 10, IsInvalid: true}, + expValue: "--", + expHeader: pidsHeader, + call: ctx.PIDs, + }, + { + name: "PIDs windows", + stats: StatsEntry{PidsCurrent: 10}, + osType: "windows", + expValue: "--", + expHeader: pidsHeader, + call: ctx.PIDs, + }, } - for _, te := range tt { - ctx = statsContext{s: te.stats, os: te.osType} - if v := te.call(); v != te.expValue { - t.Fatalf("Expected %q, got %q", te.expValue, v) - } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ctx = statsContext{s: tc.stats, os: tc.osType} + if v := tc.call(); v != tc.expValue { + t.Fatalf("Expected %q, got %q", tc.expValue, v) + } + }) } }