Merge pull request #1628 from AllenZMC/improve_test

add ut for filter
This commit is contained in:
karmada-bot 2022-04-21 10:04:07 +08:00 committed by GitHub
commit 53d24092b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,150 @@
package core
import (
"encoding/json"
"testing"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
policyv1alpha1 "github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
"github.com/karmada-io/karmada/pkg/util"
)
func TestValidateGVK(t *testing.T) {
tests := []struct {
name string
reference *workv1alpha2.ObjectReference
expected bool
}{
{
name: "supportedGVKs",
reference: &workv1alpha2.ObjectReference{
APIVersion: "apps/v1",
Kind: "Deployment",
},
expected: true,
},
{
name: "unsupportedGVKs",
reference: &workv1alpha2.ObjectReference{
APIVersion: "v1",
Kind: "Pod",
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := validateGVK(tt.reference)
if res != tt.expected {
t.Errorf("validateGVK() = %v, want %v", res, tt.expected)
}
})
}
}
func TestValidatePlacement(t *testing.T) {
fakePlacement1 := policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDuplicated,
},
}
marshaledBytes1, _ := json.Marshal(fakePlacement1)
fakePlacement2 := policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
ReplicaDivisionPreference: policyv1alpha1.ReplicaDivisionPreferenceAggregated,
},
}
marshaledBytes2, _ := json.Marshal(fakePlacement2)
fakePlacement3 := policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
ReplicaDivisionPreference: policyv1alpha1.ReplicaDivisionPreferenceWeighted,
WeightPreference: &policyv1alpha1.ClusterPreferences{},
},
}
marshaledBytes3, _ := json.Marshal(fakePlacement3)
fakePlacement4 := policyv1alpha1.Placement{
ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{
ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided,
ReplicaDivisionPreference: policyv1alpha1.ReplicaDivisionPreferenceWeighted,
WeightPreference: &policyv1alpha1.ClusterPreferences{
DynamicWeight: policyv1alpha1.DynamicWeightByAvailableReplicas,
},
},
}
marshaledBytes4, _ := json.Marshal(fakePlacement4)
tests := []struct {
name string
binding *workv1alpha2.ResourceBinding
expected bool
}{
{
name: "no policyPlacementAnnotation",
binding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
},
expected: false,
},
{
name: "propagationPolicy schedules replicas as non-dynamic",
binding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Annotations: map[string]string{util.PolicyPlacementAnnotation: string(marshaledBytes1)},
},
},
expected: false,
},
{
name: "propagationPolicy schedules replicas as dynamic: ReplicaDivisionPreference is Aggregated",
binding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Annotations: map[string]string{util.PolicyPlacementAnnotation: string(marshaledBytes2)},
},
},
expected: true,
},
{
name: "propagationPolicy schedules replicas as dynamic: DynamicWeight is null",
binding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Annotations: map[string]string{util.PolicyPlacementAnnotation: string(marshaledBytes3)},
},
},
expected: false,
},
{
name: "propagationPolicy schedules replicas as dynamic: DynamicWeight is not null",
binding: &workv1alpha2.ResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
Annotations: map[string]string{util.PolicyPlacementAnnotation: string(marshaledBytes4)},
},
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
res := validatePlacement(tt.binding)
if res != tt.expected {
t.Errorf("validatePlacement() = %v, want %v", res, tt.expected)
}
})
}
}