Skip to content

Commit 5f1e0fe

Browse files
committed
test: Avoid using a shared context variable
Signed-off-by: Andrea Panattoni <[email protected]>
1 parent ac646af commit 5f1e0fe

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

pkg/daemon/daemon_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import (
3232
)
3333

3434
var (
35-
cancel context.CancelFunc
36-
ctx context.Context
3735
k8sManager manager.Manager
3836
kubeclient *kubernetes.Clientset
3937
eventRecorder *daemon.EventRecorder
@@ -53,8 +51,12 @@ const (
5351

5452
var _ = Describe("Daemon Controller", Ordered, func() {
5553
BeforeAll(func() {
56-
ctx, cancel = context.WithCancel(context.Background())
5754
wg = sync.WaitGroup{}
55+
DeferCleanup(wg.Wait)
56+
57+
ctx, cancel := context.WithCancel(context.Background())
58+
DeferCleanup(cancel)
59+
5860
startDaemon = func(dc *daemon.NodeReconciler) {
5961
By("start controller manager")
6062
wg.Add(1)
@@ -116,14 +118,9 @@ var _ = Describe("Daemon Controller", Ordered, func() {
116118
})
117119

118120
AfterEach(func() {
119-
Expect(k8sClient.DeleteAllOf(context.Background(), &sriovnetworkv1.SriovNetworkNodeState{}, client.InNamespace(testNamespace))).ToNot(HaveOccurred())
121+
Expect(k8sClient.DeleteAllOf(context.Background(), &corev1.Node{})).ToNot(HaveOccurred())
120122

121-
By("Shutdown controller manager")
122-
cancel()
123-
wg.Wait()
124-
})
125-
126-
AfterAll(func() {
123+
Expect(k8sClient.DeleteAllOf(context.Background(), &sriovnetworkv1.SriovNetworkNodeState{}, client.InNamespace(testNamespace))).ToNot(HaveOccurred())
127124
Expect(k8sClient.DeleteAllOf(context.Background(), &sriovnetworkv1.SriovOperatorConfig{}, client.InNamespace(testNamespace))).ToNot(HaveOccurred())
128125
})
129126

@@ -136,7 +133,7 @@ var _ = Describe("Daemon Controller", Ordered, func() {
136133
}
137134
})
138135

139-
It("Should expose nodeState Status section", func() {
136+
It("Should expose nodeState Status section", func(ctx context.Context) {
140137
By("Init mock functions")
141138
afterConfig := false
142139
hostHelper.EXPECT().DiscoverSriovDevices(hostHelper).DoAndReturn(func(helpersInterface helper.HostHelpersInterface) ([]sriovnetworkv1.InterfaceExt, error) {
@@ -263,7 +260,7 @@ var _ = Describe("Daemon Controller", Ordered, func() {
263260
Expect(nodeState.Status.LastSyncError).To(Equal(""))
264261
})
265262

266-
It("Should apply the reset configuration when disableDrain is true", func() {
263+
It("Should apply the reset configuration when disableDrain is true", func(ctx context.Context) {
267264
DeferCleanup(func(x bool) { vars.DisableDrain = x }, vars.DisableDrain)
268265
vars.DisableDrain = true
269266

@@ -352,7 +349,7 @@ var _ = Describe("Daemon Controller", Ordered, func() {
352349
func patchAnnotation(nodeState *sriovnetworkv1.SriovNetworkNodeState, key, value string) {
353350
originalNodeState := nodeState.DeepCopy()
354351
nodeState.Annotations[key] = value
355-
err := k8sClient.Patch(ctx, nodeState, client.MergeFrom(originalNodeState))
352+
err := k8sClient.Patch(context.Background(), nodeState, client.MergeFrom(originalNodeState))
356353
Expect(err).ToNot(HaveOccurred())
357354
}
358355

@@ -381,8 +378,8 @@ func createNode(nodeName string) (*corev1.Node, *sriovnetworkv1.SriovNetworkNode
381378
},
382379
}
383380

384-
Expect(k8sClient.Create(ctx, &node)).ToNot(HaveOccurred())
385-
Expect(k8sClient.Create(ctx, &nodeState)).ToNot(HaveOccurred())
381+
Expect(k8sClient.Create(context.Background(), &node)).ToNot(HaveOccurred())
382+
Expect(k8sClient.Create(context.Background(), &nodeState)).ToNot(HaveOccurred())
386383
vars.NodeName = nodeName
387384

388385
return &node, &nodeState

0 commit comments

Comments
 (0)