Skip to content

Commit f5b59a6

Browse files
committed
Ensure cleanup in all webhook tests
1 parent b5c7e20 commit f5b59a6

File tree

5 files changed

+35
-26
lines changed

5 files changed

+35
-26
lines changed

internal/webhook/v1alpha1/biosversion_webhook_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,5 @@ var _ = Describe("BIOSVersion Webhook", func() {
191191
biosVersionV1.Status.State = metalv1alpha1.BIOSVersionStateCompleted
192192
})).Should(Succeed())
193193
})
194+
194195
})

internal/webhook/v1alpha1/bmcsettings_webhook_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package v1alpha1
66
import (
77
"fmt"
88

9+
"github.com/ironcore-dev/metal-operator/internal/controller"
910
. "github.com/onsi/ginkgo/v2"
1011
. "github.com/onsi/gomega"
1112
v1 "k8s.io/api/core/v1"
@@ -36,12 +37,19 @@ var _ = Describe("BMCSettings Webhook", func() {
3637
}
3738
By("Creating an BMCSettings")
3839
Expect(k8sClient.Create(ctx, BMCSettingsV1)).To(Succeed())
39-
DeferCleanup(k8sClient.Delete, BMCSettingsV1)
4040
validator = BMCSettingsCustomValidator{Client: k8sClient}
4141
SetClient(k8sClient)
4242

4343
})
4444

45+
AfterEach(func() {
46+
By("Deleting BMCSettings")
47+
Expect(k8sClient.DeleteAllOf(ctx, &metalv1alpha1.BMCSettings{})).To(Succeed())
48+
49+
By("Ensuring clean state")
50+
controller.EnsureCleanState()
51+
})
52+
4553
Context("When creating or updating BMCSettings under Validating Webhook", func() {
4654

4755
It("Should deny creation if a BMC referred is already referred by another", func(ctx SpecContext) {
@@ -76,7 +84,6 @@ var _ = Describe("BMCSettings Webhook", func() {
7684
},
7785
}
7886
Expect(k8sClient.Create(ctx, BMCSettingsV2)).To(Succeed())
79-
DeferCleanup(k8sClient.Delete, BMCSettingsV2)
8087
})
8188

8289
It("Should deny Update if a BMC referred is already referred by another", func() {
@@ -94,7 +101,6 @@ var _ = Describe("BMCSettings Webhook", func() {
94101
},
95102
}
96103
Expect(k8sClient.Create(ctx, BMCSettingsV2)).To(Succeed())
97-
DeferCleanup(k8sClient.Delete, BMCSettingsV2)
98104

99105
By("Updating an BMCSettingsV2 to refer to existing BMC")
100106
BMCSettingsV2Updated := BMCSettingsV2.DeepCopy()
@@ -117,7 +123,6 @@ var _ = Describe("BMCSettings Webhook", func() {
117123
},
118124
}
119125
Expect(k8sClient.Create(ctx, BMCSettingsV2)).To(Succeed())
120-
DeferCleanup(k8sClient.Delete, BMCSettingsV2)
121126

122127
By("Updating an BMCSettingsV2 to refer to new BMC")
123128
BMCSettingsV2Updated := BMCSettingsV2.DeepCopy()
@@ -161,8 +166,6 @@ var _ = Describe("BMCSettings Webhook", func() {
161166
Eventually(UpdateStatus(BMCSettingsV1, func() {
162167
BMCSettingsV1.Status.State = metalv1alpha1.BMCSettingsStateApplied
163168
})).Should(Succeed())
164-
165-
By("Deleting the BMCSettings should pass: by DeferCleanup")
166169
})
167170
})
168171
})

internal/webhook/v1alpha1/bmcversion_webhook_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package v1alpha1
66
import (
77
"fmt"
88

9+
"github.com/ironcore-dev/metal-operator/internal/controller"
910
. "github.com/onsi/ginkgo/v2"
1011
. "github.com/onsi/gomega"
1112
v1 "k8s.io/api/core/v1"
@@ -39,13 +40,16 @@ var _ = Describe("BMCVersion Webhook", func() {
3940
}
4041
By("Creating an BMCVersion")
4142
Expect(k8sClient.Create(ctx, BMCVersionV1)).To(Succeed())
42-
DeferCleanup(k8sClient.Delete, BMCVersionV1)
4343
SetClient(k8sClient)
4444

4545
})
4646

4747
AfterEach(func() {
48-
// TODO (user): Add any teardown logic common to all tests
48+
By("Deleting BMCVersion")
49+
Expect(k8sClient.DeleteAllOf(ctx, &metalv1alpha1.BMCVersion{})).To(Succeed())
50+
51+
By("Ensuring clean state")
52+
controller.EnsureCleanState()
4953
})
5054

5155
Context("When creating or updating BMCVersion under Validating Webhook", func() {
@@ -85,7 +89,6 @@ var _ = Describe("BMCVersion Webhook", func() {
8589
},
8690
}
8791
Expect(k8sClient.Create(ctx, BMCVersionV2)).To(Succeed())
88-
DeferCleanup(k8sClient.Delete, BMCVersionV2)
8992
})
9093

9194
It("Should deny Update if a BMC referred is already referred by another", func() {
@@ -105,7 +108,6 @@ var _ = Describe("BMCVersion Webhook", func() {
105108
},
106109
}
107110
Expect(k8sClient.Create(ctx, BMCVersionV2)).To(Succeed())
108-
DeferCleanup(k8sClient.Delete, BMCVersionV2)
109111

110112
By("Updating an BMCVersionV2 to refer to existing BMC")
111113
BMCVersionV2Updated := BMCVersionV2.DeepCopy()
@@ -130,7 +132,6 @@ var _ = Describe("BMCVersion Webhook", func() {
130132
},
131133
}
132134
Expect(k8sClient.Create(ctx, BMCVersionV2)).To(Succeed())
133-
DeferCleanup(k8sClient.Delete, BMCVersionV2)
134135

135136
By("Updating an BMCVersionV2 to refer to new BMC")
136137
BMCVersionV2Updated := BMCVersionV2.DeepCopy()
@@ -172,9 +173,6 @@ var _ = Describe("BMCVersion Webhook", func() {
172173
Eventually(UpdateStatus(BMCVersionV1, func() {
173174
BMCVersionV1.Status.State = metalv1alpha1.BMCVersionStateCompleted
174175
})).Should(Succeed())
175-
176-
By("Deleting the BMCVersionV1 should pass: by DeferCleanup")
177176
})
178177
})
179-
180178
})

internal/webhook/v1alpha1/endpoint_webhook_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1alpha1
55

66
import (
7+
"github.com/ironcore-dev/metal-operator/internal/controller"
78
. "github.com/onsi/ginkgo/v2"
89
. "github.com/onsi/gomega"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -29,6 +30,14 @@ var _ = Describe("Endpoint Webhook", func() {
2930
Expect(obj).NotTo(BeNil(), "Expected obj to be initialized")
3031
})
3132

33+
AfterEach(func() {
34+
By("Deleting the Endpoint")
35+
Expect(k8sClient.DeleteAllOf(ctx, &metalv1alpha1.Endpoint{})).To(Succeed())
36+
37+
By("Ensuring clean state")
38+
controller.EnsureCleanState()
39+
})
40+
3241
Context("When creating or updating an Endpoint under Validating Webhook", func() {
3342
It("Should deny creation if an Endpoint has a duplicate MAC address", func(ctx SpecContext) {
3443
By("Creating an Endpoint")
@@ -42,7 +51,6 @@ var _ = Describe("Endpoint Webhook", func() {
4251
},
4352
}
4453
Expect(k8sClient.Create(ctx, endpoint)).To(Succeed())
45-
DeferCleanup(k8sClient.Delete, endpoint)
4654

4755
By("Creating an Endpoint with existing MAC address")
4856
existingEndpoint := &metalv1alpha1.Endpoint{
@@ -69,7 +77,6 @@ var _ = Describe("Endpoint Webhook", func() {
6977
},
7078
}
7179
Expect(k8sClient.Create(ctx, endpoint)).ToNot(HaveOccurred())
72-
DeferCleanup(k8sClient.Delete, endpoint)
7380

7481
By("Creating an Endpoint with non-existing MAC address")
7582
existingEndpoint := &metalv1alpha1.Endpoint{
@@ -96,7 +103,6 @@ var _ = Describe("Endpoint Webhook", func() {
96103
},
97104
}
98105
Expect(k8sClient.Create(ctx, endpoint)).To(Succeed())
99-
DeferCleanup(k8sClient.Delete, endpoint)
100106

101107
By("Creating an Endpoint with different MAC address")
102108
existingEndpoint := &metalv1alpha1.Endpoint{
@@ -109,7 +115,6 @@ var _ = Describe("Endpoint Webhook", func() {
109115
},
110116
}
111117
Expect(k8sClient.Create(ctx, existingEndpoint)).To(Succeed())
112-
DeferCleanup(k8sClient.Delete, existingEndpoint)
113118

114119
By("Updating an Endpoint to conflicting MAC address")
115120
updatedEndpoint := endpoint.DeepCopy()
@@ -129,7 +134,6 @@ var _ = Describe("Endpoint Webhook", func() {
129134
},
130135
}
131136
Expect(k8sClient.Create(ctx, existingEndpoint)).To(Succeed())
132-
DeferCleanup(k8sClient.Delete, existingEndpoint)
133137

134138
By("Updating an Endpoint IP address")
135139
updatedEndpoint := existingEndpoint.DeepCopy()

internal/webhook/v1alpha1/server_webhook_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package v1alpha1
55

66
import (
7+
"context"
8+
79
. "github.com/onsi/ginkgo/v2"
810
. "github.com/onsi/gomega"
911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -36,21 +38,25 @@ var _ = Describe("Server Webhook", func() {
3638
},
3739
}
3840
Expect(k8sClient.Create(ctx, server)).To(Succeed())
39-
DeferCleanup(k8sClient.Delete, server)
4041
validator = ServerCustomValidator{Client: k8sClient}
4142
SetClient(k8sClient)
4243
By("Creating an server")
4344
})
4445

46+
AfterEach(func(ctx context.Context) {
47+
By("Deleting the server")
48+
Expect(k8sClient.DeleteAllOf(ctx, server)).To(Succeed())
49+
50+
By("Ensuring clean state")
51+
controller.EnsureCleanState()
52+
})
53+
4554
Context("When deleting Server under Validating Webhook", func() {
4655
It("Should refuse to delete if in Maintenance", func() {
4756
By("Patching the server to a maintenance state and adding finalizer")
4857
Eventually(UpdateStatus(server, func() {
4958
server.Status.State = metalv1alpha1.ServerStateMaintenance
5059
})).Should(Succeed())
51-
Eventually(Update(server, func() {
52-
server.Finalizers = append(server.Finalizers, controller.ServerFinalizer)
53-
})).Should(Succeed())
5460

5561
By("Deleting the Server should not pass")
5662
Expect(validator.ValidateDelete(ctx, server)).Error().To(HaveOccurred())
@@ -61,9 +67,6 @@ var _ = Describe("Server Webhook", func() {
6167
metalv1alpha1.OperationAnnotation: metalv1alpha1.OperationAnnotationForceUpdateOrDeleteInProgress,
6268
}
6369
})).Should(Succeed())
64-
65-
By("Deleting the server should pass: by DeferCleanup")
6670
})
6771
})
68-
6972
})

0 commit comments

Comments
 (0)