diff --git a/pkg/cmd/resourcecmd.go b/pkg/cmd/resourcecmd.go index 4aea45c..9fe3b52 100644 --- a/pkg/cmd/resourcecmd.go +++ b/pkg/cmd/resourcecmd.go @@ -277,6 +277,11 @@ func printResourceRow(name string, res gjson.Result, unit string) { if ratio == 1.0 { ratioStr = "1.0x" } + if name == "network" && res.Get("source").String() == "unknown" { + capStr = "unknown" + effStr, availStr = "unlimited", "unlimited" + ratioStr = "n/a" + } fmt.Printf("%-10s %-14s %-14s %-14s %-14s %s\n", name, capStr, effStr, allocStr, availStr, ratioStr) } diff --git a/pkg/cmd/resourcecmd_test.go b/pkg/cmd/resourcecmd_test.go index 032a045..aa24e99 100644 --- a/pkg/cmd/resourcecmd_test.go +++ b/pkg/cmd/resourcecmd_test.go @@ -1,10 +1,14 @@ package cmd import ( + "io" + "os" + "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tidwall/gjson" ) func TestParseReclaimBytes(t *testing.T) { @@ -37,6 +41,29 @@ func TestParseReclaimBytes(t *testing.T) { } } +func TestNetworkCapacityRow(t *testing.T) { + for _, source := range []string{"unknown", "configured", "detected"} { + t.Run(source, func(t *testing.T) { + stdout := os.Stdout + reader, writer, err := os.Pipe() + require.NoError(t, err) + defer reader.Close() + os.Stdout = writer + defer func() { os.Stdout = stdout }() + res := gjson.Parse(`{"source":"` + source + `","capacity":0,"effective_limit":0,"allocated":50000000,"available":0,"oversub_ratio":1000}`) + printResourceRow("network", res, "bps") + require.NoError(t, writer.Close()) + output, err := io.ReadAll(reader) + require.NoError(t, err) + want := "network 0 bps 0 bps 400 Mbps 0 bps 1000.0x" + if source == "unknown" { + want = "network unknown unlimited 400 Mbps unlimited n/a" + } + assert.Equal(t, want, strings.Join(strings.Fields(string(output)), " ")) + }) + } +} + func TestFormatBytes(t *testing.T) { tests := []struct { bytes int64