add ut for federatedresourcequota validating

Signed-off-by: changzhen <changzhen5@huawei.com>
This commit is contained in:
changzhen 2022-03-16 14:13:47 +08:00
parent 4f545e37c6
commit 85ab6c7699
3 changed files with 162 additions and 3 deletions

View File

@ -22,13 +22,14 @@ package lifted
import ( import (
"fmt" "fmt"
v1 "k8s.io/api/core/v1"
"testing" "testing"
corev1 "k8s.io/api/core/v1"
) )
func TestIsNativeResource(t *testing.T) { func TestIsNativeResource(t *testing.T) {
testCases := []struct { testCases := []struct {
resourceName v1.ResourceName resourceName corev1.ResourceName
expectVal bool expectVal bool
}{ }{
{ {

View File

@ -91,7 +91,7 @@ func validateOverallAndAssignments(quotaSpec *policyv1alpha1.FederatedResourceQu
for k, v := range quotaSpec.Overall { for k, v := range quotaSpec.Overall {
assignment := calculateAssignmentForResourceKey(k, quotaSpec.StaticAssignments) assignment := calculateAssignmentForResourceKey(k, quotaSpec.StaticAssignments)
if v.Cmp(assignment) < 0 { 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"))
} }
} }

View File

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