-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdelete.go
More file actions
213 lines (178 loc) · 9.99 KB
/
Copy pathdelete.go
File metadata and controls
213 lines (178 loc) · 9.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cluster
import (
"context"
"errors"
"net/http"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega" //nolint:staticcheck // dot import for test readability
"github.com/openshift-hyperfleet/hyperfleet-e2e/pkg/api/openapi"
"github.com/openshift-hyperfleet/hyperfleet-e2e/pkg/client"
"github.com/openshift-hyperfleet/hyperfleet-e2e/pkg/helper"
"github.com/openshift-hyperfleet/hyperfleet-e2e/pkg/labels"
)
var _ = ginkgo.Describe("[Suite: cluster][delete] Cluster Deletion Lifecycle",
ginkgo.Label(labels.Tier0),
func() {
var h *helper.Helper
var clusterID string
ginkgo.BeforeEach(func(ctx context.Context) {
h = helper.New()
ginkgo.By("creating cluster and waiting for Reconciled")
var err error
clusterID, err = h.GetTestCluster(ctx, h.TestDataPath("payloads/clusters/cluster-request.json"))
Expect(err).NotTo(HaveOccurred(), "failed to create cluster")
ginkgo.DeferCleanup(func(ctx context.Context) {
if cluster, err := h.Client.GetCluster(ctx, clusterID); err == nil && cluster.DeletedTime == nil {
if _, err := h.Client.DeleteCluster(ctx, clusterID); err != nil {
ginkgo.GinkgoWriter.Printf("Warning: API delete failed for cluster %s: %v\n", clusterID, err)
}
}
if err := h.CleanupTestCluster(ctx, clusterID); err != nil {
ginkgo.GinkgoWriter.Printf("Warning: cleanup failed for cluster %s: %v\n", clusterID, err)
}
})
Eventually(h.PollCluster(ctx, clusterID), h.Cfg.Timeouts.Cluster.Reconciled, h.Cfg.Polling.Interval).
Should(helper.HaveResourceCondition(client.ConditionTypeReconciled, openapi.ResourceConditionStatusTrue))
})
ginkgo.It("should complete full deletion lifecycle from soft-delete through hard-delete", func(ctx context.Context) {
clusterBefore, err := h.Client.GetCluster(ctx, clusterID)
Expect(err).NotTo(HaveOccurred())
ginkgo.By("soft-deleting the cluster")
deletedCluster, err := h.Client.DeleteCluster(ctx, clusterID)
Expect(err).NotTo(HaveOccurred(), "DELETE request should succeed with 202")
Expect(deletedCluster.DeletedTime).NotTo(BeNil(), "soft-deleted cluster should have deleted_time set")
Expect(deletedCluster.Generation).To(Equal(clusterBefore.Generation+1), "generation should increment after soft-delete")
if expected := h.ExpectedIdentity(); expected != "" {
Expect(deletedCluster.DeletedBy).NotTo(BeNil(), "deleted_by should be set on soft-delete")
Expect(string(*deletedCluster.DeletedBy)).To(Equal(expected), "deleted_by should match configured identity")
}
ginkgo.By("waiting for cluster adapters to finalize and cluster to be hard-deleted")
// Hard-delete executes atomically within the POST /adapter_statuses request that
// computes Reconciled=True, so there is no observable window to see Finalized=True
// on the statuses endpoint. Accept either Finalized=True OR 404 (already hard-deleted).
Eventually(func(g Gomega) {
var httpErr *client.HTTPError
_, err := h.Client.GetCluster(ctx, clusterID)
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound {
return
}
g.Expect(err).NotTo(HaveOccurred())
statuses, err := h.Client.GetClusterStatuses(ctx, clusterID)
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound {
return
}
g.Expect(err).NotTo(HaveOccurred())
g.Expect(statuses).To(helper.HaveAllAdaptersWithCondition(
h.Cfg.Adapters.Cluster, client.ConditionTypeFinalized, openapi.AdapterConditionStatusTrue))
}, h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).Should(Succeed())
ginkgo.By("confirming cluster is hard-deleted")
Eventually(h.PollClusterHTTPStatus(ctx, clusterID), h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).
Should(Equal(http.StatusNotFound))
ginkgo.By("verifying downstream K8s namespace is cleaned up")
Eventually(h.PollNamespacesByPrefix(ctx, clusterID), h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).
Should(BeEmpty())
})
ginkgo.It("should return 409 Conflict when PATCHing a soft-deleted cluster", ginkgo.Label(labels.Negative), func(ctx context.Context) {
ginkgo.By("soft-deleting the cluster")
deletedCluster, err := h.Client.DeleteCluster(ctx, clusterID)
Expect(err).NotTo(HaveOccurred(), "DELETE request should succeed with 202")
Expect(deletedCluster.DeletedTime).NotTo(BeNil(), "soft-deleted cluster should have deleted_time set")
deletedGeneration := deletedCluster.Generation
ginkgo.By("attempting PATCH on the soft-deleted cluster")
patchReq := openapi.ClusterPatchRequest{
Spec: &openapi.ClusterSpec{"updated-key": "should-not-work"},
}
_, patchErr := h.Client.PatchCluster(ctx, clusterID, patchReq)
Expect(patchErr).To(HaveOccurred(), "PATCH on soft-deleted cluster should be rejected")
var httpErr *client.HTTPError
Expect(errors.As(patchErr, &httpErr)).To(BeTrue(), "error should be an HTTP error")
Expect(httpErr.StatusCode).To(Or(Equal(http.StatusConflict), Equal(http.StatusNotFound)),
"PATCH should be rejected once cluster deletion has started")
if httpErr.StatusCode == http.StatusConflict {
ginkgo.By("verifying cluster state is unchanged after rejected PATCH")
cluster, err := h.Client.GetCluster(ctx, clusterID)
Expect(err).NotTo(HaveOccurred())
Expect(cluster.Generation).To(Equal(deletedGeneration), "generation should not change after rejected PATCH")
Expect(cluster.DeletedTime).NotTo(BeNil(), "cluster should still be marked as deleted")
}
})
},
)
var _ = ginkgo.Describe("[Suite: cluster][delete] Cluster Cascade Deletion",
ginkgo.Label(labels.Tier0),
func() {
var h *helper.Helper
var clusterID string
var nodepoolID1 string
var nodepoolID2 string
ginkgo.BeforeEach(func(ctx context.Context) {
h = helper.New()
ginkgo.By("creating cluster and waiting for Reconciled")
var err error
clusterID, err = h.GetTestCluster(ctx, h.TestDataPath("payloads/clusters/cluster-request.json"))
Expect(err).NotTo(HaveOccurred(), "failed to create cluster")
ginkgo.DeferCleanup(func(ctx context.Context) {
if cluster, err := h.Client.GetCluster(ctx, clusterID); err == nil && cluster.DeletedTime == nil {
if _, err := h.Client.DeleteCluster(ctx, clusterID); err != nil {
ginkgo.GinkgoWriter.Printf("Warning: API delete failed for cluster %s: %v\n", clusterID, err)
}
}
if err := h.CleanupTestCluster(ctx, clusterID); err != nil {
ginkgo.GinkgoWriter.Printf("Warning: cleanup failed for cluster %s: %v\n", clusterID, err)
}
})
Eventually(h.PollCluster(ctx, clusterID), h.Cfg.Timeouts.Cluster.Reconciled, h.Cfg.Polling.Interval).
Should(helper.HaveResourceCondition(client.ConditionTypeReconciled, openapi.ResourceConditionStatusTrue))
ginkgo.By("creating two nodepools")
np1, err := h.Client.CreateNodePoolFromPayload(ctx, clusterID, h.TestDataPath("payloads/nodepools/nodepool-request.json"))
Expect(err).NotTo(HaveOccurred(), "failed to create first nodepool")
Expect(np1.Id).NotTo(BeNil())
nodepoolID1 = *np1.Id
np2, err := h.Client.CreateNodePoolFromPayload(ctx, clusterID, h.TestDataPath("payloads/nodepools/nodepool-request.json"))
Expect(err).NotTo(HaveOccurred(), "failed to create second nodepool")
Expect(np2.Id).NotTo(BeNil())
nodepoolID2 = *np2.Id
ginkgo.By("waiting for both nodepools to reach Reconciled")
Eventually(h.PollNodePool(ctx, clusterID, nodepoolID1), h.Cfg.Timeouts.NodePool.Reconciled, h.Cfg.Polling.Interval).
Should(helper.HaveResourceCondition(client.ConditionTypeReconciled, openapi.ResourceConditionStatusTrue))
Eventually(h.PollNodePool(ctx, clusterID, nodepoolID2), h.Cfg.Timeouts.NodePool.Reconciled, h.Cfg.Polling.Interval).
Should(helper.HaveResourceCondition(client.ConditionTypeReconciled, openapi.ResourceConditionStatusTrue))
})
ginkgo.It("should cascade deletion to child nodepools and hard-delete all resources", func(ctx context.Context) {
ginkgo.By("soft-deleting the cluster")
deletedCluster, err := h.Client.DeleteCluster(ctx, clusterID)
Expect(err).NotTo(HaveOccurred(), "DELETE request should succeed with 202")
Expect(deletedCluster.DeletedTime).NotTo(BeNil(), "cluster should have deleted_time set")
ginkgo.By("verifying cascade: both child nodepools are soft-deleted or already hard-deleted")
Eventually(func(g Gomega) {
np1, err := h.Client.GetNodePool(ctx, clusterID, nodepoolID1)
var httpErr *client.HTTPError
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound {
return
}
g.Expect(err).NotTo(HaveOccurred(), "first nodepool should be accessible or 404")
g.Expect(np1.DeletedTime).NotTo(BeNil(), "first nodepool should have deleted_time set via cascade")
}, h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).Should(Succeed())
Eventually(func(g Gomega) {
np2, err := h.Client.GetNodePool(ctx, clusterID, nodepoolID2)
var httpErr *client.HTTPError
if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusNotFound {
return
}
g.Expect(err).NotTo(HaveOccurred(), "second nodepool should be accessible or 404")
g.Expect(np2.DeletedTime).NotTo(BeNil(), "second nodepool should have deleted_time set via cascade")
}, h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).Should(Succeed())
ginkgo.By("waiting for both nodepools to be hard-deleted")
Eventually(h.PollNodePoolHTTPStatus(ctx, clusterID, nodepoolID1), h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).
Should(Equal(http.StatusNotFound))
Eventually(h.PollNodePoolHTTPStatus(ctx, clusterID, nodepoolID2), h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).
Should(Equal(http.StatusNotFound))
ginkgo.By("waiting for cluster to be hard-deleted after all nodepools removed")
Eventually(h.PollClusterHTTPStatus(ctx, clusterID), h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).
Should(Equal(http.StatusNotFound))
ginkgo.By("verifying downstream K8s namespace is cleaned up")
Eventually(h.PollNamespacesByPrefix(ctx, clusterID), h.Cfg.Timeouts.Adapter.Processing, h.Cfg.Polling.Interval).
Should(BeEmpty())
})
},
)