From b649c1136614d147e56ffef217ac714810e6a9b2 Mon Sep 17 00:00:00 2001 From: googs1025 Date: Sat, 28 Dec 2024 19:44:14 +0800 Subject: [PATCH] chore(printers): add miss unit test for resourcequota --- pkg/printers/internalversion/printers.go | 4 +- pkg/printers/internalversion/printers_test.go | 58 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/pkg/printers/internalversion/printers.go b/pkg/printers/internalversion/printers.go index 3ed1c4917ea..196bc40ec08 100644 --- a/pkg/printers/internalversion/printers.go +++ b/pkg/printers/internalversion/printers.go @@ -483,9 +483,9 @@ func AddHandlers(h printers.PrintHandler) { resourceQuotaColumnDefinitions := []metav1.TableColumnDefinition{ {Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]}, - {Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]}, {Name: "Request", Type: "string", Description: "Request represents a minimum amount of cpu/memory that a container may consume."}, {Name: "Limit", Type: "string", Description: "Limits control the maximum amount of cpu/memory that a container may use independent of contention on the node."}, + {Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]}, } _ = h.TableHandler(resourceQuotaColumnDefinitions, printResourceQuota) _ = h.TableHandler(resourceQuotaColumnDefinitions, printResourceQuotaList) @@ -2792,7 +2792,7 @@ func printResourceQuota(resourceQuota *api.ResourceQuota, options printers.Gener } age := translateTimestampSince(resourceQuota.CreationTimestamp) - row.Cells = append(row.Cells, resourceQuota.Name, age, strings.TrimSuffix(requestColumn.String(), ", "), strings.TrimSuffix(limitColumn.String(), ", ")) + row.Cells = append(row.Cells, resourceQuota.Name, strings.TrimSuffix(requestColumn.String(), ", "), strings.TrimSuffix(limitColumn.String(), ", "), age) return []metav1.TableRow{row}, nil } diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index 57d78f80ebc..e72cab35696 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -6894,6 +6894,64 @@ func TestPrintLeaseCandidate(t *testing.T) { } } +func TestPrintResourceQuota(t *testing.T) { + tests := []struct { + resourceQuota api.ResourceQuota + options printers.GenerateOptions + expected []metav1.TableRow + }{ + { + resourceQuota: api.ResourceQuota{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-resourcequota", + CreationTimestamp: metav1.Time{Time: time.Now().Add(-3e11)}, + }, + Spec: api.ResourceQuotaSpec{}, + Status: api.ResourceQuotaStatus{ + Used: api.ResourceList{ + api.ResourceCPU: resource.MustParse("1"), + api.ResourceMemory: resource.MustParse("1Gi"), + api.ResourcePods: resource.MustParse("1"), + api.ResourceServices: resource.MustParse("1"), + api.ResourceReplicationControllers: resource.MustParse("1"), + api.ResourceQuotas: resource.MustParse("1"), + api.ResourceLimitsCPU: resource.MustParse("2"), + api.ResourceLimitsMemory: resource.MustParse("2Gi"), + }, + Hard: api.ResourceList{ + api.ResourceCPU: resource.MustParse("100"), + api.ResourceMemory: resource.MustParse("4Gi"), + api.ResourcePods: resource.MustParse("10"), + api.ResourceServices: resource.MustParse("10"), + api.ResourceReplicationControllers: resource.MustParse("10"), + api.ResourceQuotas: resource.MustParse("1"), + api.ResourceLimitsCPU: resource.MustParse("200"), + api.ResourceLimitsMemory: resource.MustParse("8Gi"), + }, + }, + }, + expected: []metav1.TableRow{ + { + Cells: []interface{}{"test-resourcequota", "cpu: 1/100, memory: 1Gi/4Gi, pods: 1/10, replicationcontrollers: 1/10, resourcequotas: 1/1, services: 1/10", "limits.cpu: 2/200, limits.memory: 2Gi/8Gi", "5m"}, + }, + }, + }, + } + + for i, test := range tests { + rows, err := printResourceQuota(&test.resourceQuota, test.options) + if err != nil { + t.Fatal(err) + } + for i := range rows { + rows[i].Object.Object = nil + } + if !reflect.DeepEqual(test.expected, rows) { + t.Errorf("%d mismatch: %s", i, cmp.Diff(test.expected, rows)) + } + } +} + func TestTableRowDeepCopyShouldNotPanic(t *testing.T) { tests := []struct { name string