chore(printers): add miss unit test for resourcequota

This commit is contained in:
googs1025 2024-12-28 19:44:14 +08:00
parent 7bfdda4696
commit b649c11366
2 changed files with 60 additions and 2 deletions

View File

@ -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
}

View File

@ -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