Skip to content

Commit

Permalink
test: Adding E2E for deletion (Azure#331)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
aagusuab and aagusuab authored Oct 12, 2022
1 parent fb307c1 commit 3432ff7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions test/e2e/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
57 changes: 57 additions & 0 deletions test/e2e/work_api_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 3432ff7

Please sign in to comment.