|
| 1 | +package functional |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports |
| 7 | + . "github.com/onsi/gomega" //revive:disable:dot-imports |
| 8 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 9 | + "k8s.io/apimachinery/pkg/types" |
| 10 | + |
| 11 | + "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 12 | + dataplanev1 "github.com/openstack-k8s-operators/openstack-operator/api/dataplane/v1beta1" |
| 13 | +) |
| 14 | + |
| 15 | +var _ = Describe("DataplaneDeployment Webhook", func() { |
| 16 | + var dataplaneDeploymentName types.NamespacedName |
| 17 | + |
| 18 | + createDeployment := func(name types.NamespacedName) { |
| 19 | + CreateDataplaneDeploymentWithoutConfirm(name, DefaultDataPlaneDeploymentSpec()) |
| 20 | + DeferCleanup(func() { |
| 21 | + instance := &dataplanev1.OpenStackDataPlaneDeployment{} |
| 22 | + err := th.K8sClient.Get(th.Ctx, name, instance) |
| 23 | + if apierrors.IsNotFound(err) { |
| 24 | + return |
| 25 | + } |
| 26 | + Expect(err).NotTo(HaveOccurred()) |
| 27 | + |
| 28 | + if instance.Annotations == nil { |
| 29 | + instance.Annotations = map[string]string{} |
| 30 | + } |
| 31 | + instance.Annotations[dataplanev1.ConfirmDeleteAnnotation] = "true" |
| 32 | + Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed()) |
| 33 | + Expect(th.K8sClient.Delete(th.Ctx, instance)).To(Succeed()) |
| 34 | + }) |
| 35 | + } |
| 36 | + |
| 37 | + setDeploymentRunning := func(name types.NamespacedName) { |
| 38 | + Eventually(func(g Gomega) error { |
| 39 | + instance := &dataplanev1.OpenStackDataPlaneDeployment{} |
| 40 | + g.Expect(th.K8sClient.Get(th.Ctx, name, instance)).To(Succeed()) |
| 41 | + instance.Status.Conditions = condition.Conditions{} |
| 42 | + instance.Status.Conditions.MarkFalse( |
| 43 | + condition.DeploymentReadyCondition, |
| 44 | + condition.RequestedReason, |
| 45 | + condition.SeverityInfo, |
| 46 | + condition.DeploymentReadyRunningMessage, |
| 47 | + ) |
| 48 | + return th.K8sClient.Status().Update(th.Ctx, instance) |
| 49 | + }).Should(Succeed()) |
| 50 | + } |
| 51 | + |
| 52 | + setDeploymentCompleted := func(name types.NamespacedName) { |
| 53 | + Eventually(func(g Gomega) error { |
| 54 | + instance := &dataplanev1.OpenStackDataPlaneDeployment{} |
| 55 | + g.Expect(th.K8sClient.Get(th.Ctx, name, instance)).To(Succeed()) |
| 56 | + instance.Status.Conditions = condition.Conditions{} |
| 57 | + instance.Status.Conditions.MarkTrue( |
| 58 | + condition.DeploymentReadyCondition, |
| 59 | + condition.DeploymentReadyMessage, |
| 60 | + ) |
| 61 | + return th.K8sClient.Status().Update(th.Ctx, instance) |
| 62 | + }).Should(Succeed()) |
| 63 | + } |
| 64 | + |
| 65 | + When("a running deployment is deleted without the confirm annotation", func() { |
| 66 | + BeforeEach(func() { |
| 67 | + dataplaneDeploymentName = types.NamespacedName{ |
| 68 | + Name: "edpm-running-delete-blocked", |
| 69 | + Namespace: namespace, |
| 70 | + } |
| 71 | + createDeployment(dataplaneDeploymentName) |
| 72 | + setDeploymentRunning(dataplaneDeploymentName) |
| 73 | + }) |
| 74 | + |
| 75 | + It("should reject the delete request", func() { |
| 76 | + Eventually(func() string { |
| 77 | + instance := GetDataplaneDeployment(dataplaneDeploymentName) |
| 78 | + err := th.K8sClient.Delete(th.Ctx, instance) |
| 79 | + return fmt.Sprintf("%s", err) |
| 80 | + }).Should(ContainSubstring("deletion of a running deployment not allowed")) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + When("a running deployment is deleted with the confirm annotation", func() { |
| 85 | + BeforeEach(func() { |
| 86 | + dataplaneDeploymentName = types.NamespacedName{ |
| 87 | + Name: "edpm-running-delete-confirmed", |
| 88 | + Namespace: namespace, |
| 89 | + } |
| 90 | + createDeployment(dataplaneDeploymentName) |
| 91 | + setDeploymentRunning(dataplaneDeploymentName) |
| 92 | + }) |
| 93 | + |
| 94 | + It("should allow the delete request", func() { |
| 95 | + Eventually(func(g Gomega) error { |
| 96 | + instance := GetDataplaneDeployment(dataplaneDeploymentName) |
| 97 | + if instance.Annotations == nil { |
| 98 | + instance.Annotations = map[string]string{} |
| 99 | + } |
| 100 | + instance.Annotations[dataplanev1.ConfirmDeleteAnnotation] = "true" |
| 101 | + g.Expect(th.K8sClient.Update(th.Ctx, instance)).To(Succeed()) |
| 102 | + return th.K8sClient.Delete(th.Ctx, instance) |
| 103 | + }).Should(Succeed()) |
| 104 | + |
| 105 | + Eventually(func() bool { |
| 106 | + instance := &dataplanev1.OpenStackDataPlaneDeployment{} |
| 107 | + err := th.K8sClient.Get(th.Ctx, dataplaneDeploymentName, instance) |
| 108 | + return apierrors.IsNotFound(err) |
| 109 | + }).Should(BeTrue()) |
| 110 | + }) |
| 111 | + }) |
| 112 | + |
| 113 | + When("a completed deployment is deleted without the confirm annotation", func() { |
| 114 | + BeforeEach(func() { |
| 115 | + dataplaneDeploymentName = types.NamespacedName{ |
| 116 | + Name: "edpm-completed-delete-allowed", |
| 117 | + Namespace: namespace, |
| 118 | + } |
| 119 | + createDeployment(dataplaneDeploymentName) |
| 120 | + setDeploymentCompleted(dataplaneDeploymentName) |
| 121 | + }) |
| 122 | + |
| 123 | + It("should allow the delete request", func() { |
| 124 | + Eventually(func() error { |
| 125 | + instance := GetDataplaneDeployment(dataplaneDeploymentName) |
| 126 | + return th.K8sClient.Delete(th.Ctx, instance) |
| 127 | + }).Should(Succeed()) |
| 128 | + |
| 129 | + Eventually(func() bool { |
| 130 | + instance := &dataplanev1.OpenStackDataPlaneDeployment{} |
| 131 | + err := th.K8sClient.Get(th.Ctx, dataplaneDeploymentName, instance) |
| 132 | + return apierrors.IsNotFound(err) |
| 133 | + }).Should(BeTrue()) |
| 134 | + }) |
| 135 | + }) |
| 136 | +}) |
0 commit comments