Skip to content

Commit e2dbfb5

Browse files
committed
Normalize tests on SpecContext in leaf
The tests before where partly relying one a global context, which means the cancelation from ginkgo wasn't really passed down. The leaf functions can take a context.Context or SpecContext argument. The latter is already imported, so lets go with that one.
1 parent 184e075 commit e2dbfb5

10 files changed

+77
-96
lines changed

internal/controller/aggregates_controller_test.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ limitations under the License.
1818
package controller
1919

2020
import (
21-
"context"
2221
"fmt"
2322
"net/http"
2423

@@ -105,7 +104,7 @@ var _ = Describe("AggregatesController", func() {
105104
)
106105
// Setup and teardown
107106

108-
BeforeEach(func(ctx context.Context) {
107+
BeforeEach(func(ctx SpecContext) {
109108
By("Setting up the OpenStack http mock server")
110109
fakeServer = testhelper.SetupHTTP()
111110
DeferCleanup(fakeServer.Teardown)
@@ -135,19 +134,19 @@ var _ = Describe("AggregatesController", func() {
135134
computeClient: client.ServiceClient(fakeServer),
136135
}
137136

138-
DeferCleanup(func(ctx context.Context) {
137+
DeferCleanup(func(ctx SpecContext) {
139138
Expect(tc.Client.Delete(ctx, hypervisor)).To(Succeed())
140139
})
141140
})
142141

143-
JustBeforeEach(func(ctx context.Context) {
142+
JustBeforeEach(func(ctx SpecContext) {
144143
_, err := tc.Reconcile(ctx, ctrl.Request{NamespacedName: hypervisorName})
145144
Expect(err).NotTo(HaveOccurred())
146145
})
147146

148147
// Tests
149148
Context("Adding new Aggregate", func() {
150-
BeforeEach(func() {
149+
BeforeEach(func(ctx SpecContext) {
151150
By("Setting a missing aggregate")
152151
hypervisor := &kvmv1.Hypervisor{}
153152
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
@@ -189,7 +188,7 @@ var _ = Describe("AggregatesController", func() {
189188
})
190189
})
191190

192-
It("should update Aggregates and set status condition as Aggregates differ", func() {
191+
It("should update Aggregates and set status condition as Aggregates differ", func(ctx SpecContext) {
193192
updated := &kvmv1.Hypervisor{}
194193
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
195194
Expect(updated.Status.Aggregates).To(ContainElements("test-aggregate1"))
@@ -198,7 +197,7 @@ var _ = Describe("AggregatesController", func() {
198197
})
199198

200199
Context("Removing Aggregate", func() {
201-
BeforeEach(func() {
200+
BeforeEach(func(ctx SpecContext) {
202201
hypervisor := &kvmv1.Hypervisor{}
203202
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
204203
// update status to have existing aggregate
@@ -233,7 +232,7 @@ var _ = Describe("AggregatesController", func() {
233232
fakeServer.Mux.HandleFunc("POST /os-aggregates/99/action", expectRemoveHostFromAggregate)
234233
})
235234

236-
It("should update Aggregates and set status condition when Aggregates differ", func() {
235+
It("should update Aggregates and set status condition when Aggregates differ", func(ctx SpecContext) {
237236
updated := &kvmv1.Hypervisor{}
238237
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
239238
Expect(updated.Status.Aggregates).To(BeEmpty())
@@ -242,15 +241,15 @@ var _ = Describe("AggregatesController", func() {
242241
})
243242

244243
Context("before onboarding", func() {
245-
BeforeEach(func() {
244+
BeforeEach(func(ctx SpecContext) {
246245
hypervisor := &kvmv1.Hypervisor{}
247246
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
248247
// Remove the onboarding condition
249248
hypervisor.Status.Conditions = []v1.Condition{}
250249
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
251250
})
252251

253-
It("should neither update Aggregates and nor set status condition", func() {
252+
It("should neither update Aggregates and nor set status condition", func(ctx SpecContext) {
254253
updated := &kvmv1.Hypervisor{}
255254
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
256255
Expect(updated.Status.Aggregates).To(BeEmpty())
@@ -259,7 +258,7 @@ var _ = Describe("AggregatesController", func() {
259258
})
260259

261260
Context("when terminating", func() {
262-
BeforeEach(func() {
261+
BeforeEach(func(ctx SpecContext) {
263262
hypervisor := &kvmv1.Hypervisor{}
264263
Expect(k8sClient.Get(ctx, hypervisorName, hypervisor)).To(Succeed())
265264
// Remove the onboarding condition
@@ -272,7 +271,7 @@ var _ = Describe("AggregatesController", func() {
272271
Expect(k8sClient.Status().Update(ctx, hypervisor)).To(Succeed())
273272
})
274273

275-
It("should neither update Aggregates and nor set status condition", func() {
274+
It("should neither update Aggregates and nor set status condition", func(ctx SpecContext) {
276275
updated := &kvmv1.Hypervisor{}
277276
Expect(tc.Client.Get(ctx, hypervisorName, updated)).To(Succeed())
278277
Expect(updated.Status.Aggregates).To(BeEmpty())

internal/controller/decomission_controller_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ limitations under the License.
1818
package controller
1919

2020
import (
21-
"context"
22-
2321
. "github.com/onsi/ginkgo/v2"
2422
. "github.com/onsi/gomega"
2523
corev1 "k8s.io/api/core/v1"
@@ -84,7 +82,7 @@ var _ = Describe("Decommission Controller", func() {
8482
})
8583
})
8684

87-
AfterEach(func(ctx context.Context) {
85+
AfterEach(func(ctx SpecContext) {
8886
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName.Name}}
8987
By("Cleanup the specific node and hypervisor resource")
9088
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, node))).To(Succeed())
@@ -102,7 +100,7 @@ var _ = Describe("Decommission Controller", func() {
102100
})
103101

104102
Context("When reconciling a node", func() {
105-
It("should set the finalizer", func(ctx context.Context) {
103+
It("should set the finalizer", func(ctx SpecContext) {
106104
By("reconciling the created resource")
107105
_, err := r.Reconcile(ctx, reconcileReq)
108106
Expect(err).NotTo(HaveOccurred())

internal/controller/eviction_controller_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ limitations under the License.
1818
package controller
1919

2020
import (
21-
"context"
2221
"fmt"
2322
"net/http"
2423

@@ -97,14 +96,12 @@ var _ = Describe("Eviction Controller", func() {
9796
fakeServer testhelper.FakeServer
9897
)
9998

100-
ctx := context.Background() //nolint:govet
101-
10299
BeforeEach(func() {
103100
By("Setting up the OpenStack http mock server")
104101
fakeServer = testhelper.SetupHTTP()
105102
})
106103

107-
AfterEach(func() {
104+
AfterEach(func(ctx SpecContext) {
108105
resource := &kvmv1.Eviction{}
109106
err := k8sClient.Get(ctx, typeNamespacedName, resource)
110107
if err != nil {
@@ -133,7 +130,7 @@ var _ = Describe("Eviction Controller", func() {
133130

134131
Describe("API validation", func() {
135132
When("creating an eviction without hypervisor", func() {
136-
It("it should fail creating the resource", func() {
133+
It("it should fail creating the resource", func(ctx SpecContext) {
137134
resource := &kvmv1.Eviction{
138135
ObjectMeta: metav1.ObjectMeta{
139136
Name: resourceName,
@@ -149,7 +146,7 @@ var _ = Describe("Eviction Controller", func() {
149146
})
150147

151148
When("creating an eviction without reason", func() {
152-
It("it should fail creating the resource", func() {
149+
It("it should fail creating the resource", func(ctx SpecContext) {
153150
resource := &kvmv1.Eviction{
154151
ObjectMeta: metav1.ObjectMeta{
155152
Name: resourceName,
@@ -165,7 +162,7 @@ var _ = Describe("Eviction Controller", func() {
165162
})
166163

167164
When("creating an eviction with reason and hypervisor", func() {
168-
BeforeEach(func() {
165+
BeforeEach(func(ctx SpecContext) {
169166
By("creating the hypervisor resource")
170167
hypervisor := &kvmv1.Hypervisor{
171168
ObjectMeta: metav1.ObjectMeta{
@@ -174,7 +171,7 @@ var _ = Describe("Eviction Controller", func() {
174171
}
175172
Expect(ctrlRuntimeClient.IgnoreAlreadyExists(k8sClient.Create(ctx, hypervisor))).To(Succeed())
176173
})
177-
It("should successfully create the resource", func() {
174+
It("should successfully create the resource", func(ctx SpecContext) {
178175
resource := &kvmv1.Eviction{
179176
ObjectMeta: metav1.ObjectMeta{
180177
Name: resourceName,
@@ -193,7 +190,7 @@ var _ = Describe("Eviction Controller", func() {
193190

194191
Describe("Reconciliation", func() {
195192
Describe("an eviction for 'test-hypervisor'", func() {
196-
BeforeEach(func() {
193+
BeforeEach(func(ctx SpecContext) {
197194
By("Creating the resource")
198195
resource := &kvmv1.Eviction{
199196
ObjectMeta: metav1.ObjectMeta{
@@ -224,7 +221,7 @@ var _ = Describe("Eviction Controller", func() {
224221
})
225222
})
226223

227-
It("should fail reconciliation", func() {
224+
It("should fail reconciliation", func(ctx SpecContext) {
228225
for range 3 {
229226
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
230227
Expect(err).NotTo(HaveOccurred())
@@ -247,7 +244,7 @@ var _ = Describe("Eviction Controller", func() {
247244

248245
})
249246
When("enabled hypervisor has no servers", func() {
250-
BeforeEach(func() {
247+
BeforeEach(func(ctx SpecContext) {
251248
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
252249
w.Header().Add("Content-Type", "application/json")
253250
w.WriteHeader(http.StatusOK)
@@ -266,7 +263,7 @@ var _ = Describe("Eviction Controller", func() {
266263
}
267264
Expect(ctrlRuntimeClient.IgnoreAlreadyExists(k8sClient.Create(ctx, hypervisor))).To(Succeed())
268265
})
269-
It("should succeed the reconciliation", func() {
266+
It("should succeed the reconciliation", func(ctx SpecContext) {
270267
runningCond := &metav1.Condition{
271268
Type: kvmv1.ConditionTypeEvicting,
272269
Status: metav1.ConditionTrue,
@@ -344,7 +341,7 @@ var _ = Describe("Eviction Controller", func() {
344341
})
345342
})
346343
When("disabled hypervisor has no servers", func() {
347-
BeforeEach(func() {
344+
BeforeEach(func(ctx SpecContext) {
348345
fakeServer.Mux.HandleFunc("GET /os-hypervisors/detail", func(w http.ResponseWriter, r *http.Request) {
349346
w.Header().Add("Content-Type", "application/json")
350347
w.WriteHeader(http.StatusOK)
@@ -362,7 +359,7 @@ var _ = Describe("Eviction Controller", func() {
362359
}
363360
Expect(ctrlRuntimeClient.IgnoreAlreadyExists(k8sClient.Create(ctx, hypervisor))).To(Succeed())
364361
})
365-
It("should succeed the reconciliation", func() {
362+
It("should succeed the reconciliation", func(ctx SpecContext) {
366363
for range 3 {
367364
_, err := controllerReconciler.Reconcile(ctx, reconcileRequest)
368365
Expect(err).NotTo(HaveOccurred())

internal/controller/gardener_node_lifecycle_controller_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var _ = Describe("Gardener Maintenance Controller", func() {
3131
const nodeName = "node-test"
3232
var controller *GardenerNodeLifecycleController
3333

34-
BeforeEach(func() {
34+
BeforeEach(func(ctx SpecContext) {
3535
controller = &GardenerNodeLifecycleController{
3636
Client: k8sClient,
3737
Scheme: k8sClient.Scheme(),
@@ -49,16 +49,13 @@ var _ = Describe("Gardener Maintenance Controller", func() {
4949
},
5050
}
5151
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
52-
})
53-
54-
AfterEach(func() {
55-
node := &corev1.Node{ObjectMeta: metav1.ObjectMeta{Name: nodeName}}
56-
By("Cleanup the specific node")
57-
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, node))).To(Succeed())
52+
DeferCleanup(func(ctx SpecContext) {
53+
Expect(client.IgnoreNotFound(k8sClient.Delete(ctx, resource))).To(Succeed())
54+
})
5855
})
5956

6057
Context("When reconciling a node", func() {
61-
It("should successfully reconcile the resource", func() {
58+
It("should successfully reconcile the resource", func(ctx SpecContext) {
6259
req := ctrl.Request{
6360
NamespacedName: types.NamespacedName{Name: nodeName},
6461
}

0 commit comments

Comments
 (0)