Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
add test case for RSP placement intersection
Browse files Browse the repository at this point in the history
  • Loading branch information
xunpan committed Apr 12, 2021
1 parent a1859f5 commit 25bd481
Showing 1 changed file with 58 additions and 5 deletions.
63 changes: 58 additions & 5 deletions test/e2e/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ var _ = Describe("Scheduling", func() {
cluster1 int32
cluster2 int32
noPreferences bool
intersection bool
}{
"replicas spread equally in clusters with no explicit per cluster preferences": {
total: int32(4),
Expand All @@ -113,6 +114,15 @@ var _ = Describe("Scheduling", func() {
cluster1: int32(3),
cluster2: int32(3),
},
"target clusters are the intersection of the RSP clusters and federated resource placement": {
intersection: true,
total: int32(6),
weight1: int64(2),
weight2: int64(1),
min1: int64(3),
min2: int64(3),
cluster1: int32(6),
},
}

for key := range schedulingTypes {
Expand Down Expand Up @@ -144,12 +154,27 @@ var _ = Describe("Scheduling", func() {
clusterNames[1]: tc.cluster2,
}

name, err := createTestObjs(tl, genericClient, typeConfig, kubeConfig, rspSpec, namespace)
if tc.intersection {
rspSpec.PlacementIntersection = true
fedNamespace := f.EnsureTestFederatedNamespace(true)

// Ensure the removal of the namespace placement to avoid affecting other tests.
defer func() {
fedNamespaceKey := util.NewQualifiedName(fedNamespace).String()

err := genericClient.Delete(context.Background(), fedNamespace, fedNamespace.GetNamespace(), fedNamespace.GetName())
if err != nil && !apierrors.IsNotFound(err) {
tl.Fatalf("Error deleting FederatedNamespace %q: %v", fedNamespaceKey, err)
}
}()
}

name, err := createTestObjs(tl, genericClient, typeConfig, kubeConfig, rspSpec, namespace, clusterNames)
if err != nil {
tl.Fatalf("Creation of test objects in the host cluster failed: %v", err)
}

err = waitForMatchingFederatedObject(tl, typeConfig, kubeConfig, name, namespace, expected)
err = waitForMatchingFederatedObject(tl, typeConfig, kubeConfig, name, namespace, expected, rspSpec.PlacementIntersection)
if err != nil {
tl.Fatalf("Failed waiting for matching federated object: %v", err)
}
Expand Down Expand Up @@ -190,7 +215,7 @@ func rspSpecWithClusterList(total int32, w1, w2, min1, min2 int64, clusters []st
return rspSpec
}

func createTestObjs(tl common.TestLogger, client genericclient.Client, typeConfig typeconfig.Interface, kubeConfig *restclient.Config, rspSpec fedschedulingv1a1.ReplicaSchedulingPreferenceSpec, namespace string) (string, error) {
func createTestObjs(tl common.TestLogger, client genericclient.Client, typeConfig typeconfig.Interface, kubeConfig *restclient.Config, rspSpec fedschedulingv1a1.ReplicaSchedulingPreferenceSpec, namespace string, fedClusters []string) (string, error) {
federatedTypeAPIResource := typeConfig.GetFederatedType()
federatedTypeClient, err := util.NewResourceClient(kubeConfig, &federatedTypeAPIResource)
if err != nil {
Expand All @@ -202,10 +227,21 @@ func createTestObjs(tl common.TestLogger, client genericclient.Client, typeConfi
if !ok {
return "", errors.Errorf("Unable to find fixture for %q", typeConfigName)
}
fedObject, err := common.NewTestObject(typeConfig, namespace, []string{}, fixture)

clusters := []string{}
fedObject, err := common.NewTestObject(typeConfig, namespace, clusters, fixture)
if err != nil {
return "", err
}

if rspSpec.PlacementIntersection {
clusters = append(clusters, fedClusters[0])
err = util.SetClusterNames(fedObject, clusters)
if err != nil {
return "", err
}
}

createdFedObject, err := federatedTypeClient.Resources(namespace).Create(context.Background(), fedObject, metav1.CreateOptions{})
if err != nil {
return "", err
Expand Down Expand Up @@ -242,7 +278,7 @@ func deleteTestObj(typeConfig typeconfig.Interface, kubeConfig *restclient.Confi
return nil
}

func waitForMatchingFederatedObject(tl common.TestLogger, typeConfig typeconfig.Interface, kubeConfig *restclient.Config, name, namespace string, expected32 map[string]int32) error {
func waitForMatchingFederatedObject(tl common.TestLogger, typeConfig typeconfig.Interface, kubeConfig *restclient.Config, name, namespace string, expected32 map[string]int32, isIntersection bool) error {
apiResource := typeConfig.GetFederatedType()
kind := apiResource.Kind
client, err := util.NewResourceClient(kubeConfig, &apiResource)
Expand Down Expand Up @@ -271,6 +307,23 @@ func waitForMatchingFederatedObject(tl common.TestLogger, typeConfig typeconfig.
tl.Errorf("An error occurred while retrieving cluster names for override %s %s/%s: %v", kind, namespace, name, err)
return false, nil
}

if isIntersection {
if schedulingtypes.PlacementUpdateNeeded(clusterNames, expectedClusterNames[:1]) {
return false, nil
}

overridesMap, err := util.GetOverrides(fedObject)
if err != nil {
tl.Errorf("Error reading cluster overrides for %s %s/%s: %v", kind, namespace, name, err)
return false, nil
}

expectedResult := make(map[string]int64)
expectedResult[expectedClusterNames[0]] = expected64[expectedClusterNames[0]]
return !schedulingtypes.OverrideUpdateNeeded(overridesMap, expectedResult), nil
}

if schedulingtypes.PlacementUpdateNeeded(clusterNames, expectedClusterNames) {
return false, nil
}
Expand Down

0 comments on commit 25bd481

Please sign in to comment.