Skip to content

Commit

Permalink
feat(pluginpreset) Add unit tests to delete validator for Plugin Pres…
Browse files Browse the repository at this point in the history
…et (#804)
  • Loading branch information
gciezkowski-acc committed Dec 18, 2024
1 parent add68c3 commit 5d98a6b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/admission/pluginpreset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (

// Webhook for the PluginPreset custom resource.

const preventDeletionAnnotation = "greenhouse.sap/prevent-deletion"

func SetupPluginPresetWebhookWithManager(mgr ctrl.Manager) error {
return setupWebhook(mgr,
&greenhousev1alpha1.PluginPreset{},
Expand Down Expand Up @@ -116,9 +118,9 @@ func ValidateDeletePluginPreset(_ context.Context, _ client.Client, obj runtime.
}

var allErrs field.ErrorList
if _, ok := pluginPreset.Annotations["greenhouse.sap/prevent-deletion"]; ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("annotation").Child("greenhouse.sap/prevent-deletion"), pluginPreset.Annotations,
"Plugin preset has prevent deletion annotation"))
if _, ok := pluginPreset.Annotations[preventDeletionAnnotation]; ok {
allErrs = append(allErrs, field.Invalid(field.NewPath("metadata").Child("annotation").Child(preventDeletionAnnotation),
pluginPreset.Annotations, "Plugin preset has prevent deletion annotation"))
}

if len(allErrs) > 0 {
Expand Down
31 changes: 31 additions & 0 deletions pkg/admission/pluginpreset_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,35 @@ var _ = Describe("PluginPreset Admission Tests", Ordered, func() {
Expect(test.K8sClient.Delete(test.Ctx, cut)).
To(Succeed(), "there must be no error deleting the PluginPreset")
})

It("should reject delete operation when PluginPreset has prevent deletion annotation", func() {
pluginPreset := &greenhousev1alpha1.PluginPreset{
ObjectMeta: metav1.ObjectMeta{
Name: pluginPresetUpdate,
Namespace: test.TestNamespace,
Annotations: map[string]string{
preventDeletionAnnotation: "true",
},
},
Spec: greenhousev1alpha1.PluginPresetSpec{
Plugin: greenhousev1alpha1.PluginSpec{
PluginDefinition: pluginPresetDefinition,
},
ClusterSelector: metav1.LabelSelector{MatchLabels: map[string]string{"foo": "bar"}},
},
}

err := test.K8sClient.Create(test.Ctx, pluginPreset)
Expect(err).ToNot(HaveOccurred())

err = test.K8sClient.Delete(test.Ctx, pluginPreset)
Expect(err).To(HaveOccurred())

pluginPreset.Annotations = map[string]string{}
err = test.K8sClient.Update(test.Ctx, pluginPreset)
Expect(err).ToNot(HaveOccurred())

err = test.K8sClient.Delete(test.Ctx, pluginPreset)
Expect(err).ToNot(HaveOccurred())
})
})

0 comments on commit 5d98a6b

Please sign in to comment.