Skip to content

Commit

Permalink
test: add more server-side apply tests (Azure#836)
Browse files Browse the repository at this point in the history
Co-authored-by: Zhiying Lin <[email protected]>
  • Loading branch information
zhiying-lin and Zhiying Lin authored May 29, 2024
1 parent 1f83b62 commit baeea07
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 10 deletions.
176 changes: 176 additions & 0 deletions test/e2e/placement_apply_strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,182 @@ var _ = Describe("validating CRP when resources exists", Ordered, func() {
})
})

Context("Test a CRP place objects successfully (server-side-apply and disallow co-own) and existing resource has no owner reference", Ordered, func() {
BeforeAll(func() {
ns := appNamespace()
ns.Annotations = map[string]string{
annotationKey: annotationValue,
}
By(fmt.Sprintf("creating namespace %s on member cluster", ns.Name))
Expect(allMemberClusters[0].KubeClient.Create(ctx, &ns)).Should(Succeed(), "Failed to create namespace %s", ns.Name)

// Create the CRP.
strategy := &placementv1beta1.ApplyStrategy{
Type: placementv1beta1.ApplyStrategyTypeServerSideApply,
AllowCoOwnership: false,
}
createCRP(crpName, strategy)
})

AfterAll(func() {
By(fmt.Sprintf("deleting placement %s", crpName))
cleanupCRP(crpName)
})

It("should update CRP status as expected", func() {
crpStatusUpdatedActual := crpStatusUpdatedActual(workResourceIdentifiers(), allMemberClusterNames, nil, "0")
Eventually(crpStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update CRP %s status as expected", crpName)
})

// This check will ignore the annotation of resources.
It("should place the selected resources on member clusters", checkIfPlacedWorkResourcesOnAllMemberClusters)

It("should have annotations on the namespace", func() {
want := map[string]string{annotationKey: annotationValue}
Expect(validateAnnotationOfWorkNamespaceOnCluster(memberCluster1EastProd, want)).Should(Succeed(), "Failed to override the annotation of work namespace on %s", memberCluster1EastProdName)
})

It("can delete the CRP", func() {
// Delete the CRP.
crp := &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: crpName,
},
}
Expect(hubClient.Delete(ctx, crp)).To(Succeed(), "Failed to delete CRP %s", crpName)
})

It("should remove the selected resources on member clusters", checkIfRemovedWorkResourcesFromAllMemberClusters)

It("should remove controller finalizers from CRP", func() {
finalizerRemovedActual := allFinalizersExceptForCustomDeletionBlockerRemovedFromCRPActual(crpName)
Eventually(finalizerRemovedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to remove controller finalizers from CRP %s", crpName)
})
})

Context("Test a CRP fail to apply namespace (server-side-apply and disallow co-own) and existing resource is owned by others", Ordered, func() {
BeforeAll(func() {
ns := appNamespace()
ns.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: "another-api-version",
Kind: "another-kind",
Name: "another-owner",
UID: "another-uid",
},
})
By(fmt.Sprintf("creating namespace %s on member cluster", ns.Name))
Expect(allMemberClusters[0].KubeClient.Create(ctx, &ns)).Should(Succeed(), "Failed to create namespace %s", ns.Name)

// Create the CRP.
strategy := &placementv1beta1.ApplyStrategy{
Type: placementv1beta1.ApplyStrategyTypeServerSideApply,
AllowCoOwnership: false,
}
createCRP(crpName, strategy)
})

AfterAll(func() {
By(fmt.Sprintf("deleting placement %s", crpName))
cleanupCRP(crpName)

By("deleting created work resources on member cluster")
cleanWorkResourcesOnCluster(allMemberClusters[0])
})

It("should update CRP status as expected", func() {
crpStatusUpdatedActual := func() error {
crp := &placementv1beta1.ClusterResourcePlacement{}
if err := hubClient.Get(ctx, types.NamespacedName{Name: crpName}, crp); err != nil {
return err
}

workNamespaceName := fmt.Sprintf(workNamespaceNameTemplate, GinkgoParallelProcess())
appConfigMapName := fmt.Sprintf(appConfigMapNameTemplate, GinkgoParallelProcess())
wantStatus := placementv1beta1.ClusterResourcePlacementStatus{
Conditions: crpAppliedFailedConditions(crp.Generation),
PlacementStatuses: []placementv1beta1.ResourcePlacementStatus{
{
ClusterName: memberCluster1EastProdName,
FailedPlacements: []placementv1beta1.FailedResourcePlacement{
{
ResourceIdentifier: placementv1beta1.ResourceIdentifier{
Kind: "Namespace",
Name: workNamespaceName,
Version: "v1",
},
Condition: metav1.Condition{
Type: placementv1beta1.WorkConditionTypeApplied,
Status: metav1.ConditionFalse,
Reason: work.ManifestsAlreadyOwnedByOthersReason,
ObservedGeneration: 0,
},
},
},
Conditions: resourcePlacementApplyFailedConditions(crp.Generation),
},
{
ClusterName: memberCluster2EastCanaryName,
Conditions: resourcePlacementRolloutCompletedConditions(crp.Generation, true, false),
},
{
ClusterName: memberCluster3WestProdName,
Conditions: resourcePlacementRolloutCompletedConditions(crp.Generation, true, false),
},
},
SelectedResources: []placementv1beta1.ResourceIdentifier{
{
Kind: "Namespace",
Name: workNamespaceName,
Version: "v1",
},
{
Kind: "ConfigMap",
Name: appConfigMapName,
Version: "v1",
Namespace: workNamespaceName,
},
},
ObservedResourceIndex: "0",
}
if diff := cmp.Diff(crp.Status, wantStatus, crpStatusCmpOptions...); diff != "" {
return fmt.Errorf("CRP status diff (-got, +want): %s", diff)
}
return nil
}
Eventually(crpStatusUpdatedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to update CRP %s status as expected", crpName)
})

It("should place the selected resources on member clusters", checkIfPlacedWorkResourcesOnAllMemberClusters)

It("can delete the CRP", func() {
// Delete the CRP.
crp := &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: crpName,
},
}
Expect(hubClient.Delete(ctx, crp)).To(Succeed(), "Failed to delete CRP %s", crpName)
})

It("should remove placed resources from member clusters excluding the first one", func() {
checkIfRemovedWorkResourcesFromMemberClusters(allMemberClusters[1:])
})

It("should remove controller finalizers from CRP", func() {
finalizerRemovedActual := allFinalizersExceptForCustomDeletionBlockerRemovedFromCRPActual(crpName)
Eventually(finalizerRemovedActual, eventuallyDuration, eventuallyInterval).Should(Succeed(), "Failed to remove controller finalizers from CRP %s", crpName)
})

It("namespace should be kept on member cluster", func() {
Consistently(func() error {
workNamespaceName := fmt.Sprintf(workNamespaceNameTemplate, GinkgoParallelProcess())
ns := &corev1.Namespace{}
return allMemberClusters[0].KubeClient.Get(ctx, types.NamespacedName{Name: workNamespaceName}, ns)
}, consistentlyDuration, consistentlyInterval).Should(Succeed(), "Namespace which is not owned by the CRP should not be deleted")
})
})

Context("Test a CRP fail to apply namespace when the conflicted annotation is managed by others (server-side-apply and allow co-own)", Ordered, func() {
BeforeAll(func() {
ns := appNamespace()
Expand Down
14 changes: 4 additions & 10 deletions test/e2e/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,8 @@ var _ = Describe("webhook tests for CRP UPDATE operations", Ordered, func() {
})

AfterAll(func() {
By(fmt.Sprintf("deleting placement %s", crpName))
cleanupCRP(crpName)

By("deleting created work resources")
cleanupWorkResources()
By(fmt.Sprintf("deleting placement %s and related resources", crpName))
ensureCRPAndRelatedResourcesDeletion(crpName, allMemberClusters)
})

It("should deny update on CRP with invalid label selector", func() {
Expand Down Expand Up @@ -321,11 +318,8 @@ var _ = Describe("webhook tests for CRP tolerations", Ordered, func() {
})

AfterAll(func() {
By(fmt.Sprintf("deleting placement %s", crpName))
cleanupCRP(crpName)

By("deleting created work resources")
cleanupWorkResources()
By(fmt.Sprintf("deleting placement %s and related resources", crpName))
ensureCRPAndRelatedResourcesDeletion(crpName, allMemberClusters)
})

It("should deny update on CRP with invalid toleration", func() {
Expand Down

0 comments on commit baeea07

Please sign in to comment.