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

Commit

Permalink
e2e test for DeleteOptioins
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandMa1986 committed Feb 3, 2021
1 parent 6931011 commit 967ce5b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/common/crudtester.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"

appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,6 +36,8 @@ import (
kubeclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"

"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/kubefed/pkg/apis/core/common"
"sigs.k8s.io/kubefed/pkg/apis/core/typeconfig"
fedv1a1 "sigs.k8s.io/kubefed/pkg/apis/core/v1alpha1"
Expand Down Expand Up @@ -362,6 +365,63 @@ func (c *FederatedTypeCrudTester) CheckDelete(fedObject *unstructured.Unstructur
}
}

func (c *FederatedTypeCrudTester) SetDeleteOption(fedObject *unstructured.Unstructured, opts ...client.DeleteOption) {
apiResource := c.typeConfig.GetFederatedType()
qualifiedName := util.NewQualifiedName(fedObject)
kind := apiResource.Kind
_, err := c.updateObject(apiResource, fedObject, func(obj *unstructured.Unstructured) {
util.ApplyDeleteOptions(obj, opts...)
})
if err != nil {
c.tl.Fatalf("Error updating %s %q: %v", kind, qualifiedName, err)
}
}

func (c *FederatedTypeCrudTester) CheckReplicaSet(fedObject *unstructured.Unstructured) {
lb, ok, _ := unstructured.NestedStringMap(fedObject.Object, "spec", "selector", "matchLabels")
if !ok {
c.tl.Fatal("Failed to get MatchLabes on the target deployment")
}

matchingLabels := (client.MatchingLabels)(lb)

for clusterName := range c.testClusters {
clusterConfig := c.testClusters[clusterName].Config

kubeClient := kubeclientset.NewForConfigOrDie(clusterConfig)
WaitForNamespaceOrDie(c.tl, kubeClient, clusterName, fedObject.GetNamespace(),
c.waitInterval, 30*time.Second)

clusterClient := genericclient.NewForConfigOrDie(clusterConfig)

c.tl.Log("Checking that the ReplicaSet still exists in every cluster")

err := wait.PollImmediate(c.waitInterval, wait.ForeverTestTimeout, func() (bool, error) {
objList := &appsv1.ReplicaSetList{}
err := clusterClient.List(context.TODO(), objList, fedObject.GetNamespace(), matchingLabels)
if err != nil {
return false, errors.Errorf("Error retrieving ReplicatSet: %v", err)
}

if len(objList.Items) == 0 {
return false, errors.Errorf("ReplicatSet was unexpectedly deleted from cluster %q", clusterName)
}

c.tl.Log("Checking that OwnerReferences has been removed from the ReplicaSet")
hasOwner := false
for _, rs := range objList.Items {
if len(rs.OwnerReferences) > 0 {
hasOwner = true
}
}
return !hasOwner, nil
})
if err != nil {
c.tl.Fatalf("Failed to confirm whether ReplicatSet is in cluster %q: %v", clusterName, err)
}
}
}

// CheckPropagation checks propagation for the crud tester's clients
func (c *FederatedTypeCrudTester) CheckPropagation(fedObject *unstructured.Unstructured) {
federatedKind := c.typeConfig.GetFederatedType().Kind
Expand Down
56 changes: 56 additions & 0 deletions test/e2e/deleteoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package e2e

import (
. "github.com/onsi/ginkgo" //nolint:stylecheck
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

"sigs.k8s.io/kubefed/test/common"
"sigs.k8s.io/kubefed/test/e2e/framework"
)

var typeConfigName = "deployments.apps"

var _ = Describe("DeleteOptions", func() {
f := framework.NewKubeFedFramework("delete-options")

tl := framework.NewE2ELogger()

typeConfigFixtures := common.TypeConfigFixturesOrDie(tl)

fixture := typeConfigFixtures[typeConfigName]

It(`Deployment should be created and deleted successfully, but ReplicaSet that created by Deployment won't be deleted`, func() {

typeConfig, testObjectsFunc := getCrudTestInput(f, tl, typeConfigName, fixture)
crudTester, targetObject, overrides := initCrudTest(f, tl, typeConfig, testObjectsFunc)
fedObject := crudTester.CheckCreate(targetObject, overrides)

By("Set PropagationPolicy property as 'Orphan' on the DeleteOptions for Federated Deployment")
orphan := metav1.DeletePropagationOrphan
prop := client.PropagationPolicy(orphan)

crudTester.SetDeleteOption(fedObject, prop)

crudTester.CheckDelete(fedObject, false)

By("Checking ReplicatSet stutus for every cluster")
crudTester.CheckReplicaSet(targetObject)
})
})

0 comments on commit 967ce5b

Please sign in to comment.