From 85ab6c7699c4a9e1d3ed973e3fd4b84cbd7f349b Mon Sep 17 00:00:00 2001 From: changzhen Date: Wed, 16 Mar 2022 14:13:47 +0800 Subject: [PATCH] add ut for federatedresourcequota validating Signed-off-by: changzhen --- pkg/util/lifted/resourcehelpers_test.go | 5 +- .../federatedresourcequota/validating.go | 2 +- .../federatedresourcequota/validating_test.go | 158 ++++++++++++++++++ 3 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 pkg/webhook/federatedresourcequota/validating_test.go diff --git a/pkg/util/lifted/resourcehelpers_test.go b/pkg/util/lifted/resourcehelpers_test.go index 65acbda12..157212eda 100644 --- a/pkg/util/lifted/resourcehelpers_test.go +++ b/pkg/util/lifted/resourcehelpers_test.go @@ -22,13 +22,14 @@ package lifted import ( "fmt" - v1 "k8s.io/api/core/v1" "testing" + + corev1 "k8s.io/api/core/v1" ) func TestIsNativeResource(t *testing.T) { testCases := []struct { - resourceName v1.ResourceName + resourceName corev1.ResourceName expectVal bool }{ { diff --git a/pkg/webhook/federatedresourcequota/validating.go b/pkg/webhook/federatedresourcequota/validating.go index c3702a199..5732bfc6e 100644 --- a/pkg/webhook/federatedresourcequota/validating.go +++ b/pkg/webhook/federatedresourcequota/validating.go @@ -91,7 +91,7 @@ func validateOverallAndAssignments(quotaSpec *policyv1alpha1.FederatedResourceQu for k, v := range quotaSpec.Overall { assignment := calculateAssignmentForResourceKey(k, quotaSpec.StaticAssignments) if v.Cmp(assignment) < 0 { - errs = append(errs, field.Invalid(overallPath.Key(string(k)), v, "overall is less than assignments")) + errs = append(errs, field.Invalid(overallPath.Key(string(k)), v.String(), "overall is less than assignments")) } } diff --git a/pkg/webhook/federatedresourcequota/validating_test.go b/pkg/webhook/federatedresourcequota/validating_test.go new file mode 100644 index 000000000..e634a9544 --- /dev/null +++ b/pkg/webhook/federatedresourcequota/validating_test.go @@ -0,0 +1,158 @@ +package federatedresourcequota + +import ( + "reflect" + "testing" + + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/util/validation/field" + + policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1" +) + +func Test_validateOverallAndAssignments(t *testing.T) { + specFld := field.NewPath("spec") + cpuParse := resource.MustParse("10") + memoryParse := resource.MustParse("10Gi") + + type args struct { + quotaSpec *policyv1alpha1.FederatedResourceQuotaSpec + fld *field.Path + } + tests := []struct { + name string + args args + want field.ErrorList + }{ + { + "normal", + args{ + quotaSpec: &policyv1alpha1.FederatedResourceQuotaSpec{ + Overall: corev1.ResourceList{ + "cpu": cpuParse, + "memory": memoryParse, + }, + StaticAssignments: []policyv1alpha1.StaticClusterAssignment{ + { + ClusterName: "m1", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("1"), + "memory": resource.MustParse("1Gi"), + }, + }, + { + ClusterName: "m2", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("1.5"), + "memory": resource.MustParse("2Gi"), + }, + }, + }, + }, + fld: specFld, + }, + field.ErrorList{}, + }, + { + "overall[cpu] is less than assignments", + args{ + quotaSpec: &policyv1alpha1.FederatedResourceQuotaSpec{ + Overall: corev1.ResourceList{ + "cpu": cpuParse, + "memory": memoryParse, + }, + StaticAssignments: []policyv1alpha1.StaticClusterAssignment{ + { + ClusterName: "m1", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("1"), + "memory": resource.MustParse("1Gi"), + }, + }, + { + ClusterName: "m2", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("10"), + "memory": resource.MustParse("2Gi"), + }, + }, + }, + }, + fld: specFld, + }, + field.ErrorList{ + field.Invalid(specFld.Child("overall").Key("cpu"), cpuParse.String(), "overall is less than assignments"), + }, + }, + { + "overall[memory] is less than assignments", + args{ + quotaSpec: &policyv1alpha1.FederatedResourceQuotaSpec{ + Overall: corev1.ResourceList{ + "cpu": cpuParse, + "memory": memoryParse, + }, + StaticAssignments: []policyv1alpha1.StaticClusterAssignment{ + { + ClusterName: "m1", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("1"), + "memory": resource.MustParse("1Gi"), + }, + }, + { + ClusterName: "m2", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("1.5"), + "memory": resource.MustParse("10Gi"), + }, + }, + }, + }, + fld: specFld, + }, + field.ErrorList{ + field.Invalid(specFld.Child("overall").Key("memory"), memoryParse.String(), "overall is less than assignments"), + }, + }, + { + "assignment resourceName is not exist in overall", + args{ + quotaSpec: &policyv1alpha1.FederatedResourceQuotaSpec{ + Overall: corev1.ResourceList{ + "cpu": cpuParse, + "memory": memoryParse, + }, + StaticAssignments: []policyv1alpha1.StaticClusterAssignment{ + { + ClusterName: "m1", + Hard: corev1.ResourceList{ + "cpux": resource.MustParse("1"), + "memory": resource.MustParse("1Gi"), + }, + }, + { + ClusterName: "m2", + Hard: corev1.ResourceList{ + "cpu": resource.MustParse("1.5"), + "memory": resource.MustParse("2Gi"), + }, + }, + }, + }, + fld: specFld, + }, + field.ErrorList{ + field.Invalid(specFld.Child("staticAssignments").Index(0).Child("hard").Key("cpux"), corev1.ResourceName("cpux"), "assignment resourceName is not exist in overall"), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := validateOverallAndAssignments(tt.args.quotaSpec, tt.args.fld); !reflect.DeepEqual(got, tt.want) { + t.Errorf("validateOverallAndAssignments() = %v, want %v", got, tt.want) + } + }) + } +}