Skip to content

Commit 1cde94c

Browse files
committed
Switch to t.Context() in tests.
1 parent 8688985 commit 1cde94c

15 files changed

Lines changed: 87 additions & 102 deletions

File tree

‎controllers/gitopsset_controller_test.go‎

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

33
import (
4-
"context"
54
"encoding/json"
65
"path/filepath"
76
"sort"
@@ -100,10 +99,10 @@ func TestReconciliation(t *testing.T) {
10099
}
101100

102101
test.AssertNoError(t, reconciler.SetupWithManager(mgr))
103-
test.AssertNoError(t, k8sClient.Create(context.TODO(), test.NewNamespace("test-ns")))
102+
test.AssertNoError(t, k8sClient.Create(t.Context(), test.NewNamespace("test-ns")))
104103

105104
t.Run("reconciling creation of new resources", func(t *testing.T) {
106-
ctx := context.TODO()
105+
ctx := t.Context()
107106
gs := createAndReconcileToFinalizedState(t, k8sClient, reconciler, makeTestGitOpsSet(t))
108107
defer deleteGitOpsSetAndFinalize(t, k8sClient, reconciler, gs)
109108

@@ -124,7 +123,7 @@ func TestReconciliation(t *testing.T) {
124123
})
125124

126125
t.Run("reconciling creation of resources in different namespaces", func(t *testing.T) {
127-
ctx := context.TODO()
126+
ctx := t.Context()
128127

129128
// https://book.kubebuilder.io/reference/envtest.html#namespace-usage-limitation
130129
test.AssertNoError(t, k8sClient.Create(ctx, test.NewNamespace("engineering-dev-ns")))
@@ -156,7 +155,7 @@ func TestReconciliation(t *testing.T) {
156155
})
157156

158157
t.Run("reconciling cleanup when deleted", func(t *testing.T) {
159-
ctx := context.TODO()
158+
ctx := t.Context()
160159
gs := createAndReconcileToFinalizedState(t, k8sClient, reconciler, makeTestGitOpsSet(t))
161160
// manually deletes below
162161

@@ -179,7 +178,7 @@ func TestReconciliation(t *testing.T) {
179178
})
180179

181180
t.Run("error conditions", func(t *testing.T) {
182-
ctx := context.TODO()
181+
ctx := t.Context()
183182
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
184183
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{
185184
{
@@ -218,7 +217,7 @@ func TestReconciliation(t *testing.T) {
218217
})
219218

220219
t.Run("reconciling removal of resources", func(t *testing.T) {
221-
ctx := context.TODO()
220+
ctx := t.Context()
222221
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
223222
gs.Spec.Generators = []templatesv1.GitOpsSetGenerator{
224223
{
@@ -271,7 +270,7 @@ func TestReconciliation(t *testing.T) {
271270
})
272271

273272
t.Run("reconciling update of resources", func(t *testing.T) {
274-
ctx := context.TODO()
273+
ctx := t.Context()
275274
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
276275
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{
277276
{
@@ -358,7 +357,7 @@ func TestReconciliation(t *testing.T) {
358357
})
359358

360359
t.Run("reconciling update of configmaps", func(t *testing.T) {
361-
ctx := context.TODO()
360+
ctx := t.Context()
362361
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
363362
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{
364363
{
@@ -450,7 +449,7 @@ func TestReconciliation(t *testing.T) {
450449
})
451450

452451
t.Run("reconciling with no generated resources", func(t *testing.T) {
453-
ctx := context.TODO()
452+
ctx := t.Context()
454453
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
455454
// No templates to generate resources from
456455
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{}
@@ -475,7 +474,7 @@ func TestReconciliation(t *testing.T) {
475474
})
476475

477476
t.Run("reconciling update of deleted resource", func(t *testing.T) {
478-
ctx := context.TODO()
477+
ctx := t.Context()
479478
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
480479
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{
481480
{
@@ -533,7 +532,7 @@ func TestReconciliation(t *testing.T) {
533532
})
534533

535534
t.Run("service account impersonation", func(t *testing.T) {
536-
ctx := context.TODO()
535+
ctx := t.Context()
537536
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
538537
gs.Spec.ServiceAccountName = "test-sa"
539538
})
@@ -564,7 +563,7 @@ func TestReconciliation(t *testing.T) {
564563
})
565564

566565
t.Run("default service account impersonation", func(t *testing.T) {
567-
ctx := context.TODO()
566+
ctx := t.Context()
568567
gs := makeTestGitOpsSet(t)
569568
gs = createAndReconcileToFinalizedState(t, k8sClient, reconciler, gs)
570569
defer deleteGitOpsSetAndFinalize(t, k8sClient, reconciler, gs)
@@ -598,7 +597,7 @@ func TestReconciliation(t *testing.T) {
598597
})
599598

600599
t.Run("reconciling update of resources with service account", func(t *testing.T) {
601-
ctx := context.TODO()
600+
ctx := t.Context()
602601
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
603602
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{
604603
{
@@ -701,7 +700,7 @@ func TestReconciliation(t *testing.T) {
701700
})
702701

703702
t.Run("reconciling with annotation-triggered reconciliation", func(t *testing.T) {
704-
ctx := context.TODO()
703+
ctx := t.Context()
705704
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
706705
gs.ObjectMeta.Annotations = map[string]string{
707706
fluxMeta.ReconcileRequestAnnotation: time.Now().Format(time.RFC3339Nano),
@@ -722,7 +721,7 @@ func TestReconciliation(t *testing.T) {
722721
})
723722

724723
t.Run("reconciling creation when suspended", func(t *testing.T) {
725-
ctx := context.TODO()
724+
ctx := t.Context()
726725
gs := createAndReconcileToFinalizedState(t, k8sClient, reconciler, makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
727726
gs.Spec.Suspend = true
728727
}))
@@ -737,7 +736,7 @@ func TestReconciliation(t *testing.T) {
737736
})
738737

739738
t.Run("reconciling when gitrepository has no artifact", func(t *testing.T) {
740-
ctx := context.TODO()
739+
ctx := t.Context()
741740
emptyGR := test.NewGitRepository()
742741
test.AssertNoError(t, k8sClient.Create(ctx, test.ToUnstructured(t, emptyGR)))
743742
defer deleteObject(t, k8sClient, emptyGR)
@@ -768,7 +767,7 @@ func TestReconciliation(t *testing.T) {
768767
})
769768

770769
t.Run("error conditions - existing resource", func(t *testing.T) {
771-
ctx := context.TODO()
770+
ctx := t.Context()
772771
gs := makeTestGitOpsSet(t, func(gs *templatesv1.GitOpsSet) {
773772
gs.Spec.Templates = []templatesv1.GitOpsSetTemplate{
774773
{
@@ -1064,7 +1063,7 @@ func deleteAllKustomizations(t *testing.T, cl client.Client) {
10641063
u := &unstructured.Unstructured{}
10651064
u.SetGroupVersionKind(kustomizationGVK)
10661065

1067-
err := cl.DeleteAllOf(context.TODO(), u, client.InNamespace("default"))
1066+
err := cl.DeleteAllOf(t.Context(), u, client.InNamespace("default"))
10681067
if client.IgnoreNotFound(err) != nil {
10691068
t.Fatal(err)
10701069
}
@@ -1075,7 +1074,7 @@ func assertResourceDoesNotExist(t *testing.T, cl client.Client, gs *kustomizev1.
10751074
check := &unstructured.Unstructured{}
10761075
check.SetGroupVersionKind(kustomizationGVK)
10771076

1078-
if err := cl.Get(context.TODO(), client.ObjectKeyFromObject(gs), check); !apierrors.IsNotFound(err) {
1077+
if err := cl.Get(t.Context(), client.ObjectKeyFromObject(gs), check); !apierrors.IsNotFound(err) {
10791078
t.Fatalf("object %v still exists", gs)
10801079
}
10811080
}
@@ -1084,7 +1083,7 @@ func assertKustomizationsExist(t *testing.T, cl client.Client, ns string, want .
10841083
t.Helper()
10851084
gss := &unstructured.UnstructuredList{}
10861085
gss.SetGroupVersionKind(kustomizationGVK)
1087-
test.AssertNoError(t, cl.List(context.TODO(), gss, client.InNamespace(ns)))
1086+
test.AssertNoError(t, cl.List(t.Context(), gss, client.InNamespace(ns)))
10881087

10891088
existingNames := func(l []unstructured.Unstructured) []string {
10901089
names := []string{}
@@ -1105,7 +1104,7 @@ func assertNoKustomizationsExistInNamespace(t *testing.T, cl client.Client, ns s
11051104
t.Helper()
11061105
gss := &unstructured.UnstructuredList{}
11071106
gss.SetGroupVersionKind(kustomizationGVK)
1108-
test.AssertNoError(t, cl.List(context.TODO(), gss, client.InNamespace(ns)))
1107+
test.AssertNoError(t, cl.List(t.Context(), gss, client.InNamespace(ns)))
11091108

11101109
if len(gss.Items) != 0 {
11111110
t.Fatalf("want no Kustomizations to exist, got %v", len(gss.Items))
@@ -1136,15 +1135,15 @@ func assertInventoryHasNoItems(t *testing.T, gs *templatesv1.GitOpsSet) {
11361135

11371136
func deleteObject(t *testing.T, cl client.Client, obj client.Object) {
11381137
t.Helper()
1139-
if err := cl.Delete(context.TODO(), obj); err != nil {
1138+
if err := cl.Delete(t.Context(), obj); err != nil {
11401139
t.Fatal(err)
11411140
}
11421141
}
11431142

11441143
// Create the provided GitOpsSet and ensure that it has been setup for
11451144
// finalization.
11461145
func createAndReconcileToFinalizedState(t *testing.T, k8sClient client.Client, r *GitOpsSetReconciler, gs *templatesv1.GitOpsSet) *templatesv1.GitOpsSet {
1147-
test.AssertNoError(t, k8sClient.Create(context.TODO(), gs))
1146+
test.AssertNoError(t, k8sClient.Create(t.Context(), gs))
11481147
reconcileAndAssertFinalizerExists(t, k8sClient, r, gs)
11491148

11501149
return gs
@@ -1155,7 +1154,7 @@ func createAndReconcileToFinalizedState(t *testing.T, k8sClient client.Client, r
11551154
// This is needed because the reconciler returns after applying the finalizer to
11561155
// avoid race conditions.
11571156
func reconcileAndAssertFinalizerExists(t *testing.T, cl client.Client, reconciler *GitOpsSetReconciler, gs *templatesv1.GitOpsSet) {
1158-
ctx := context.TODO()
1157+
ctx := t.Context()
11591158
_, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(gs)})
11601159
test.AssertNoError(t, err)
11611160

@@ -1258,7 +1257,7 @@ func createRBACForServiceAccount(t *testing.T, cl client.Client, serviceAccountN
12581257
ObjectMeta: metav1.ObjectMeta{Name: "test-role", Namespace: namespace},
12591258
Rules: rules,
12601259
}
1261-
if err := cl.Create(context.TODO(), role); err != nil {
1260+
if err := cl.Create(t.Context(), role); err != nil {
12621261
t.Fatalf("failed to write role: %s", err)
12631262
}
12641263
t.Cleanup(func() {
@@ -1280,7 +1279,7 @@ func createRBACForServiceAccount(t *testing.T, cl client.Client, serviceAccountN
12801279
APIGroup: "rbac.authorization.k8s.io",
12811280
},
12821281
}
1283-
if err := cl.Create(context.TODO(), binding); err != nil {
1282+
if err := cl.Create(t.Context(), binding); err != nil {
12841283
t.Fatalf("failed to write role-binding: %s", err)
12851284
}
12861285
t.Cleanup(func() {
@@ -1289,7 +1288,7 @@ func createRBACForServiceAccount(t *testing.T, cl client.Client, serviceAccountN
12891288
}
12901289
func deleteGitOpsSetAndFinalize(t *testing.T, cl client.Client, reconciler *GitOpsSetReconciler, gs *templatesv1.GitOpsSet) {
12911290
t.Helper()
1292-
ctx := context.TODO()
1291+
ctx := t.Context()
12931292
if gs.Spec.Suspend {
12941293
gs.Spec.Suspend = false
12951294
test.AssertNoError(t, cl.Update(ctx, gs))

‎controllers/templates/renderer_test.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package templates
22

33
import (
44
"bytes"
5-
"context"
65
"encoding/json"
76
"os"
87
"testing"
@@ -501,7 +500,7 @@ func TestRender(t *testing.T) {
501500
for _, tt := range generatorTests {
502501
t.Run(tt.name, func(t *testing.T) {
503502
gset := makeTestGitOpsSet(t, append(tt.setOptions, listElements(tt.elements))...)
504-
objs, err := Render(context.TODO(), gset, testGenerators)
503+
objs, err := Render(t.Context(), gset, testGenerators)
505504
test.AssertNoError(t, err)
506505

507506
if diff := cmp.Diff(tt.want, objs); diff != "" {
@@ -537,7 +536,7 @@ func TestRender_files(t *testing.T) {
537536
for _, tt := range generatorTests {
538537
t.Run(tt.filename, func(t *testing.T) {
539538
gset := readFixtureAsGitOpsSet(t, tt.filename)
540-
objs, err := Render(context.TODO(), gset, testGenerators)
539+
objs, err := Render(t.Context(), gset, testGenerators)
541540
test.AssertNoError(t, err)
542541

543542
assertFixturesMatch(t, tt.want, objs)
@@ -608,7 +607,7 @@ func TestRender_errors(t *testing.T) {
608607
for _, tt := range templateTests {
609608
t.Run(tt.name, func(t *testing.T) {
610609
gset := makeTestGitOpsSet(t, tt.setOptions...)
611-
_, err := Render(context.TODO(), gset, testGenerators)
610+
_, err := Render(t.Context(), gset, testGenerators)
612611

613612
test.AssertErrorMatch(t, tt.wantErr, err)
614613
})
@@ -619,7 +618,7 @@ func TestRender_disabled(t *testing.T) {
619618
gset := makeTestGitOpsSet(t)
620619
// no generators available
621620
testGenerators := map[string]generators.Generator{}
622-
res, err := Render(context.TODO(), gset, testGenerators)
621+
res, err := Render(t.Context(), gset, testGenerators)
623622
test.AssertNoError(t, err)
624623
if cmp.Diff([]*unstructured.Unstructured{}, res) != "" {
625624
t.Fatalf("expected no resources to be rendered")

‎pkg/cmd/fetcher.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"fmt"
65
"net/url"
76
"os"
@@ -38,7 +37,7 @@ func (p *ProxyArchiveFetcher) Fetch(archiveURL, checksum, dir string) error {
3837
}
3938

4039
responseWrapper := p.Client.Services(parsed.namespace).ProxyGet(parsed.scheme, parsed.name, parsed.port, parsed.path, nil)
41-
b, err := responseWrapper.DoRaw(context.TODO())
40+
b, err := responseWrapper.DoRaw(t.Context())
4241
if err != nil {
4342
return err
4443
}

‎pkg/cmd/local_client_test.go‎

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

33
import (
4-
"context"
54
"path/filepath"
65
"testing"
76

@@ -18,7 +17,7 @@ func TestLocalObjectReader_Get_v1GitRepository(t *testing.T) {
1817
v := localObjectReader{logger: logr.Discard(), repositoryRoot: "testdata"}
1918

2019
gr := sourcev1.GitRepository{}
21-
test.AssertNoError(t, v.Get(context.TODO(), client.ObjectKey{Name: "testing", Namespace: "testing"}, &gr))
20+
test.AssertNoError(t, v.Get(t.Context(), client.ObjectKey{Name: "testing", Namespace: "testing"}, &gr))
2221

2322
rootURL, err := filepath.Abs("testdata")
2423
test.AssertNoError(t, err)
@@ -32,7 +31,7 @@ func TestLocalObjectReader_Get_v1beta2GitRepository(t *testing.T) {
3231
v := localObjectReader{logger: logr.Discard(), repositoryRoot: "testdata"}
3332

3433
gr := v1beta2.GitRepository{}
35-
test.AssertNoError(t, v.Get(context.TODO(), client.ObjectKey{Name: "demo-gr", Namespace: "testing"}, &gr))
34+
test.AssertNoError(t, v.Get(t.Context(), client.ObjectKey{Name: "demo-gr", Namespace: "testing"}, &gr))
3635

3736
rootURL, err := filepath.Abs("testdata")
3837
test.AssertNoError(t, err)
@@ -46,7 +45,7 @@ func TestLocalObjectReader_Get_v1beta2OCIRepository(t *testing.T) {
4645
v := localObjectReader{logger: logr.Discard(), repositoryRoot: "testdata"}
4746

4847
gr := v1beta2.OCIRepository{}
49-
test.AssertNoError(t, v.Get(context.TODO(), client.ObjectKey{Name: "demo-or", Namespace: "testing"}, &gr))
48+
test.AssertNoError(t, v.Get(t.Context(), client.ObjectKey{Name: "demo-or", Namespace: "testing"}, &gr))
5049

5150
rootURL, err := filepath.Abs("testdata")
5251
test.AssertNoError(t, err)

0 commit comments

Comments
 (0)