From 3432ff7774d19eafb089a919e01bd33d4326c786 Mon Sep 17 00:00:00 2001 From: Youn Jae Kim Date: Wed, 12 Oct 2022 18:12:27 +0900 Subject: [PATCH] test: Adding E2E for deletion (#331) * Adding E2E for deletion * Fixes based on comments * Fixes based on comments * Fixes based on comments * Fixes based on comments * Fixes based on comments * Fixes based on comments Co-authored-by: Youn Jae Kim --- test/e2e/utils/helper.go | 13 ++++++++ test/e2e/work_api_e2e_test.go | 57 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/test/e2e/utils/helper.go b/test/e2e/utils/helper.go index 2912c914e..0ff01d8c6 100644 --- a/test/e2e/utils/helper.go +++ b/test/e2e/utils/helper.go @@ -178,6 +178,19 @@ func UpdateWork(ctx context.Context, hubCluster *framework.Cluster, work *workap return work } +// DeleteWork deletes the given Work object and waits until work becomes not found. +func DeleteWork(ctx context.Context, hubCluster framework.Cluster, work workapi.Work) { + // Deleting Work + gomega.Expect(hubCluster.KubeClient.Delete(ctx, &work)).Should(gomega.Succeed(), "Deletion of work %s failed", work.Name) + + // Waiting for the Work to be deleted and not found. + gomega.Eventually(func() error { + namespaceType := types.NamespacedName{Name: work.Name, Namespace: work.Namespace} + return hubCluster.KubeClient.Get(ctx, namespaceType, &work) + }).Should(&utils.NotFoundMatcher{}, + "The Work resource %s was not deleted", work.Name, hubCluster.ClusterName) +} + // AddManifests adds manifests to be included within a Work. func AddManifests(objects []runtime.Object, manifests []workapi.Manifest) []workapi.Manifest { for _, obj := range objects { diff --git a/test/e2e/work_api_e2e_test.go b/test/e2e/work_api_e2e_test.go index 258ae7137..ef11354c3 100644 --- a/test/e2e/work_api_e2e_test.go +++ b/test/e2e/work_api_e2e_test.go @@ -672,6 +672,63 @@ var _ = Describe("Work API Controller test", func() { "Work Condition for Work Object %s should still be applied / mismatch (-want, +got): ") }) }) + + Context("Work-api Deletion tests", func() { + It("Deleting a Work Object on Hub Cluster should also delete the corresponding resource on Member Cluster.", func() { + // creating work resource to be deleted + workName := testutils.RandomWorkName(5) + manifestConfigMapName := "work-update-configmap" + configMapBeforeDelete := corev1.ConfigMap{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "v1", + Kind: "ConfigMap", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: manifestConfigMapName, + Namespace: resourceNamespace.Name, + }, + Data: map[string]string{ + "before-delete-key": "before-delete-data", + }, + } + + // Creating types.NamespacedName to use in retrieving objects. + namespaceType := types.NamespacedName{Name: workName, Namespace: workNamespace.Name} + manifests := testutils.AddManifests([]runtime.Object{&configMapBeforeDelete}, []workapi.Manifest{}) + + By(fmt.Sprintf("creating work %s of %s", namespaceType, manifestConfigMapName)) + workBeforeDelete := testutils.CreateWork(ctx, *HubCluster, workName, workNamespace.Name, manifests) + + Eventually(func() string { + if err := HubCluster.KubeClient.Get(ctx, namespaceType, &workBeforeDelete); err != nil { + return err.Error() + } + + want := []metav1.Condition{ + { + Type: conditionTypeApplied, + Status: metav1.ConditionTrue, + Reason: "appliedWorkComplete", + }, + } + + return cmp.Diff(want, workBeforeDelete.Status.Conditions, cmpOptions...) + }, testutils.PollTimeout, testutils.PollInterval).Should(BeEmpty(), "Validate WorkStatus mismatch (-want, +got):") + + testutils.DeleteWork(ctx, *HubCluster, workBeforeDelete) + + By("Deleting the Work Object should also delete the AppliedWork in the member cluster") + appliedWork := workapi.AppliedWork{} + Expect(MemberCluster.KubeClient.Get(ctx, namespaceType, &appliedWork)).Should(&utils.NotFoundMatcher{}, + "AppliedWork %s was either not deleted or encountered an error in cluster %s", workName, MemberCluster.ClusterName) + + By("Deleting the Work Object should also delete the resources in the member cluster") + configMapDeleted := corev1.ConfigMap{} + resourceNamespaceType := types.NamespacedName{Name: configMapBeforeDelete.Name, Namespace: resourceNamespace.Name} + Expect(MemberCluster.KubeClient.Get(ctx, resourceNamespaceType, &configMapDeleted)).Should(&utils.NotFoundMatcher{}, + "resource %s was either not deleted or encountered an error in cluster %s", configMapBeforeDelete.Name, MemberCluster.ClusterName) + }) + }) }) func isKeyMetadata(p cmp.Path) bool {