Skip to content

Commit c843b25

Browse files
authored
replace context.Background() with global context in notebook controller tests (#649)
1 parent af2d995 commit c843b25

File tree

4 files changed

+7
-29
lines changed

4 files changed

+7
-29
lines changed

components/odh-notebook-controller/controllers/notebook_controller_test.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
9393
route := &routev1.Route{}
9494

9595
It("Should create a Route to expose the traffic externally", func() {
96-
ctx := context.Background()
97-
9896
By("By creating a new Notebook")
9997
Expect(cli.Create(ctx, notebook)).Should(Succeed())
10098

@@ -185,8 +183,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
185183
})
186184

187185
It("Should create a RoleBinding when the referenced Role exists", func() {
188-
ctx := context.Background()
189-
190186
By("Creating a Notebook and ensuring the Role exists")
191187
Expect(cli.Create(ctx, notebook)).Should(Succeed())
192188
time.Sleep(interval)
@@ -217,8 +213,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
217213
})
218214

219215
It("Should delete the RoleBinding when the Notebook is deleted", func() {
220-
ctx := context.Background()
221-
222216
By("Ensuring the RoleBinding exists")
223217
roleBinding := &rbacv1.RoleBinding{}
224218
Eventually(func() error {
@@ -244,7 +238,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
244238
)
245239

246240
It("Should mount a trusted-ca when it exists on the given namespace", func() {
247-
ctx := context.Background()
248241
logger := logr.Discard()
249242

250243
By("By simulating the existence of odh-trusted-ca-bundle ConfigMap")
@@ -381,8 +374,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
381374
route := &routev1.Route{}
382375

383376
It("Should create a Route to expose the traffic externally", func() {
384-
ctx := context.Background()
385-
386377
By("By creating a new Notebook")
387378
Expect(cli.Create(ctx, notebook)).Should(Succeed())
388379
time.Sleep(interval)
@@ -471,8 +462,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
471462
notebook := createNotebook(Name, Namespace)
472463

473464
It("Should update the Notebook specification", func() {
474-
ctx := context.Background()
475-
476465
By("By creating a new Notebook")
477466
Expect(cli.Create(ctx, notebook)).Should(Succeed())
478467

@@ -492,7 +481,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
492481
})
493482

494483
It("When notebook CR is updated, should mount a trusted-ca if it exists on the given namespace", func() {
495-
ctx := context.Background()
496484
logger := logr.Discard()
497485

498486
By("By simulating the existence of odh-trusted-ca-bundle ConfigMap")
@@ -622,8 +610,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
622610
notebookOAuthNetworkPolicy := &netv1.NetworkPolicy{}
623611

624612
It("Should create network policies to restrict undesired traffic", func() {
625-
ctx := context.Background()
626-
627613
By("By creating a new Notebook")
628614
Expect(cli.Create(ctx, notebook)).Should(Succeed())
629615

@@ -803,8 +789,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
803789
}
804790

805791
It("Should inject the OAuth proxy as a sidecar container", func() {
806-
ctx := context.Background()
807-
808792
By("By creating a new Notebook")
809793
Expect(cli.Create(ctx, notebook)).Should(Succeed())
810794

@@ -1045,7 +1029,6 @@ var _ = Describe("The Openshift Notebook controller", func() {
10451029
It("Should not add OAuth sidecar", func() {
10461030
notebook := createNotebook(name, namespace)
10471031
notebook.SetAnnotations(map[string]string{AnnotationServiceMesh: "true"})
1048-
ctx := context.Background()
10491032
Expect(cli.Create(ctx, notebook)).Should(Succeed())
10501033

10511034
actualNotebook := &nbv1.Notebook{}
@@ -1060,7 +1043,7 @@ var _ = Describe("The Openshift Notebook controller", func() {
10601043
It("Should not define OAuth network policy", func() {
10611044
policies := &netv1.NetworkPolicyList{}
10621045
Eventually(func() error {
1063-
return cli.List(context.Background(), policies, client.InNamespace(namespace))
1046+
return cli.List(ctx, policies, client.InNamespace(namespace))
10641047
}, duration, interval).Should(Succeed())
10651048

10661049
Expect(policies.Items).To(Not(ContainElement(notebookOAuthNetworkPolicy)))
@@ -1069,7 +1052,7 @@ var _ = Describe("The Openshift Notebook controller", func() {
10691052
It("Should not create routes", func() {
10701053
routes := &routev1.RouteList{}
10711054
Eventually(func() error {
1072-
return cli.List(context.Background(), routes, client.InNamespace(namespace))
1055+
return cli.List(ctx, routes, client.InNamespace(namespace))
10731056
}, duration, interval).Should(Succeed())
10741057

10751058
Expect(routes.Items).To(BeEmpty())
@@ -1080,7 +1063,7 @@ var _ = Describe("The Openshift Notebook controller", func() {
10801063

10811064
serviceAccounts := &corev1.ServiceAccountList{}
10821065
Eventually(func() error {
1083-
return cli.List(context.Background(), serviceAccounts, client.InNamespace(namespace))
1066+
return cli.List(ctx, serviceAccounts, client.InNamespace(namespace))
10841067
}, duration, interval).Should(Succeed())
10851068

10861069
Expect(serviceAccounts.Items).ToNot(ContainElement(oauthServiceAccount))
@@ -1094,7 +1077,7 @@ var _ = Describe("The Openshift Notebook controller", func() {
10941077
},
10951078
}
10961079
Eventually(func() error {
1097-
return cli.List(context.Background(), secrets, client.InNamespace(namespace))
1080+
return cli.List(ctx, secrets, client.InNamespace(namespace))
10981081
}, duration, interval).Should(Succeed())
10991082

11001083
Expect(secrets.Items).To(BeEmpty())

components/odh-notebook-controller/controllers/notebook_runtime_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package controllers
22

33
import (
4-
"context"
54
"fmt"
65
"time"
76

@@ -27,8 +26,6 @@ const (
2726
)
2827

2928
var _ = Describe("Runtime images ConfigMap should be mounted", func() {
30-
ctx := context.Background()
31-
3229
When("Empty ConfigMap for runtime images", func() {
3330

3431
BeforeEach(func() {

components/odh-notebook-controller/controllers/notebook_webhook_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ limitations under the License.
1616
package controllers
1717

1818
import (
19-
"context"
2019
"fmt"
2120
"time"
2221

@@ -33,8 +32,6 @@ import (
3332
)
3433

3534
var _ = Describe("The Openshift Notebook webhook", func() {
36-
ctx := context.Background()
37-
3835
When("Creating a Notebook with internal registry disabled", func() {
3936
const (
4037
Name = "test-notebook-with-last-image-selection"
@@ -290,7 +287,7 @@ var _ = Describe("The Openshift Notebook webhook", func() {
290287
}
291288

292289
BeforeEach(func() {
293-
Expect(tracings.TraceProvider.ForceFlush(context.Background())).To(Succeed())
290+
Expect(tracings.TraceProvider.ForceFlush(ctx)).To(Succeed())
294291
tracings.SpanExporter.Reset()
295292
})
296293

@@ -311,7 +308,7 @@ var _ = Describe("The Openshift Notebook webhook", func() {
311308
Expect(testCase.notebook.Spec.Template.Spec.Containers[0].Image).To(Equal(testCase.expectedImage))
312309

313310
By("Checking telemetry events")
314-
Expect(tracings.TraceProvider.ForceFlush(context.Background())).To(Succeed())
311+
Expect(tracings.TraceProvider.ForceFlush(ctx)).To(Succeed())
315312
spans := tracings.SpanExporter.GetSpans()
316313
events := make([]string, 0)
317314
for _, span := range spans {

components/odh-notebook-controller/controllers/notebook_webhook_utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestGetStructDiff(t *testing.T) {
5757

5858
for _, v := range tests {
5959
t.Run(v.name, func(t *testing.T) {
60+
// global context isn´t used here (tests were failing)
6061
diff := getStructDiff(context.Background(), v.a, v.b)
6162
assert.Equal(t, diff, v.expected)
6263
})

0 commit comments

Comments
 (0)