Skip to content

Commit 2dbd6ea

Browse files
authored
chore: fully enable the usetesting Go linter (#4968)
1 parent 093b8ff commit 2dbd6ea

32 files changed

+117
-156
lines changed

.golangci.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ linters:
8080
- G306
8181
lll:
8282
line-length: 150
83-
usetesting:
84-
context-background: false
85-
context-todo: false
8683
exclusions:
8784
presets:
8885
- common-false-positives

core/clustersmngr/client_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestClientGet(t *testing.T) {
5353
},
5454
},
5555
}
56-
ctx := context.Background()
56+
ctx := t.Context()
5757
g.Expect(k8sEnv.Client.Create(ctx, kust)).To(Succeed())
5858

5959
k := &kustomizev1.Kustomization{}
@@ -89,7 +89,7 @@ func TestClientClusteredList(t *testing.T) {
8989
},
9090
},
9191
}
92-
ctx := context.Background()
92+
ctx := t.Context()
9393
g.Expect(k8sEnv.Client.Create(ctx, kust)).To(Succeed())
9494

9595
cklist := clustersmngr.NewClusteredList(func() client.ObjectList {
@@ -131,7 +131,7 @@ func TestClientClusteredList(t *testing.T) {
131131

132132
func TestClientClusteredListPagination(t *testing.T) {
133133
g := NewGomegaWithT(t)
134-
ctx := context.Background()
134+
ctx := t.Context()
135135
ns1 := createNamespace(g)
136136
ns2 := createNamespace(g)
137137
namespaced := true
@@ -150,7 +150,7 @@ func TestClientClusteredListPagination(t *testing.T) {
150150
},
151151
},
152152
}
153-
ctx := context.Background()
153+
ctx := t.Context()
154154
g.Expect(k8sEnv.Client.Create(ctx, kust)).To(Succeed())
155155
}
156156

@@ -240,7 +240,7 @@ func TestClientClusteredListClusterScoped(t *testing.T) {
240240
},
241241
}
242242

243-
ctx := context.Background()
243+
ctx := t.Context()
244244
g.Expect(k8sEnv.Client.Create(ctx, &clusterRole)).To(Succeed())
245245

246246
cklist := clustersmngr.NewClusteredList(func() client.ObjectList {
@@ -277,7 +277,7 @@ func TestClientCLusteredListErrors(t *testing.T) {
277277
"foo": "@invalid",
278278
}
279279

280-
cerr := clustersClient.ClusteredList(context.Background(), cklist, true, labels)
280+
cerr := clustersClient.ClusteredList(t.Context(), cklist, true, labels)
281281
g.Expect(cerr).ToNot(BeNil())
282282

283283
var errs clustersmngr.ClusteredListError
@@ -311,7 +311,7 @@ func TestClientList(t *testing.T) {
311311
},
312312
},
313313
}
314-
ctx := context.Background()
314+
ctx := t.Context()
315315
g.Expect(k8sEnv.Client.Create(ctx, kust)).To(Succeed())
316316

317317
list := &kustomizev1.KustomizationList{}
@@ -348,7 +348,7 @@ func TestClientCreate(t *testing.T) {
348348
},
349349
},
350350
}
351-
ctx := context.Background()
351+
ctx := t.Context()
352352

353353
g.Expect(clustersClient.Create(ctx, clusterName, kust)).To(Succeed())
354354

@@ -383,7 +383,7 @@ func TestClientDelete(t *testing.T) {
383383
},
384384
},
385385
}
386-
ctx := context.Background()
386+
ctx := t.Context()
387387

388388
g.Expect(k8sEnv.Client.Create(ctx, kust)).To(Succeed())
389389

@@ -417,7 +417,7 @@ func TestClientUpdate(t *testing.T) {
417417
},
418418
},
419419
}
420-
ctx := context.Background()
420+
ctx := t.Context()
421421
g.Expect(k8sEnv.Client.Create(ctx, kust)).To(Succeed())
422422

423423
kust.Spec.Path = "/bar"
@@ -460,7 +460,7 @@ func TestClientPatch(t *testing.T) {
460460
},
461461
}
462462

463-
ctx := context.Background()
463+
ctx := t.Context()
464464
opt := []client.PatchOption{
465465
client.ForceOwnership,
466466
client.FieldOwner("test"),

core/clustersmngr/cluster/delegating_cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestDelegatingCacheGet(t *testing.T) {
2121
cache, err := cache.New(k8sEnv.Rest, cache.Options{})
2222
g.Expect(err).To(BeNil())
2323

24-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
24+
ctx, cancel := context.WithTimeout(t.Context(), 1*time.Second)
2525
defer cancel()
2626

2727
go cache.Start(ctx)
@@ -56,7 +56,7 @@ func TestDelegatingCacheList(t *testing.T) {
5656
cache, err := cache.New(k8sEnv.Rest, cache.Options{})
5757
g.Expect(err).To(BeNil())
5858

59-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
59+
ctx, cancel := context.WithTimeout(t.Context(), 1*time.Second)
6060
defer cancel()
6161

6262
go cache.Start(ctx)

core/clustersmngr/factory_test.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/go-logr/logr"
77
. "github.com/onsi/gomega"
8-
"golang.org/x/net/context"
98
v1 "k8s.io/api/core/v1"
109
"k8s.io/client-go/kubernetes"
1110

@@ -25,8 +24,7 @@ func TestGetImpersonatedClient(t *testing.T) {
2524
g := NewGomegaWithT(t)
2625
logger := logr.Discard()
2726

28-
ctx, cancel := context.WithCancel(context.Background())
29-
defer cancel()
27+
ctx := t.Context()
3028

3129
ns1 := createNamespace(g)
3230
ns2 := createNamespace(g)
@@ -74,8 +72,7 @@ func TestUseUserClientForNamespaces(t *testing.T) {
7472
g := NewGomegaWithT(t)
7573
logger := logr.Discard()
7674

77-
ctx, cancel := context.WithCancel(context.Background())
78-
defer cancel()
75+
ctx := t.Context()
7976

8077
ns1 := createNamespace(g)
8178
ns2 := createNamespace(g)
@@ -126,8 +123,7 @@ func TestGetImpersonatedDiscoveryClient(t *testing.T) {
126123
g := NewGomegaWithT(t)
127124
logger := logr.Discard()
128125

129-
ctx, cancel := context.WithCancel(context.Background())
130-
defer cancel()
126+
ctx := t.Context()
131127

132128
ns1 := createNamespace(g)
133129

@@ -157,8 +153,7 @@ func TestUpdateNamespaces(t *testing.T) {
157153
g := NewGomegaWithT(t)
158154
logger := logr.Discard()
159155

160-
ctx, cancel := context.WithCancel(context.Background())
161-
defer cancel()
156+
ctx := t.Context()
162157

163158
nsChecker := &nsaccessfakes.FakeChecker{}
164159
clustersFetcher := new(clustersmngrfakes.FakeClusterFetcher)
@@ -219,8 +214,7 @@ func TestUpdateUserNamespaces(t *testing.T) {
219214
g := NewGomegaWithT(t)
220215
logger := logr.Discard()
221216

222-
ctx, cancel := context.WithCancel(context.Background())
223-
defer cancel()
217+
ctx := t.Context()
224218

225219
nsChecker := &nsaccessfakes.FakeChecker{}
226220
clustersFetcher := new(clustersmngrfakes.FakeClusterFetcher)
@@ -265,8 +259,7 @@ func TestUpdateUserNamespacesFailsToConnect(t *testing.T) {
265259
g := NewGomegaWithT(t)
266260
logger := logr.Discard()
267261

268-
ctx, cancel := context.WithCancel(context.Background())
269-
defer cancel()
262+
ctx := t.Context()
270263

271264
nsChecker := nsaccess.NewChecker(nil)
272265
clustersFetcher := new(clustersmngrfakes.FakeClusterFetcher)
@@ -299,8 +292,7 @@ func TestGetClusters(t *testing.T) {
299292
g := NewGomegaWithT(t)
300293
logger := logr.Discard()
301294

302-
ctx, cancel := context.WithCancel(context.Background())
303-
defer cancel()
295+
ctx := t.Context()
304296

305297
nsChecker := nsaccess.NewChecker(nil)
306298
clustersFetcher := new(clustersmngrfakes.FakeClusterFetcher)
@@ -335,8 +327,7 @@ func TestUpdateClusters(t *testing.T) {
335327
g := NewGomegaWithT(t)
336328
logger := logr.Discard()
337329

338-
ctx, cancel := context.WithCancel(context.Background())
339-
defer cancel()
330+
ctx := t.Context()
340331

341332
nsChecker := &nsaccessfakes.FakeChecker{}
342333

@@ -451,8 +442,7 @@ func TestClientCaching(t *testing.T) {
451442
g := NewGomegaWithT(t)
452443
logger := logr.Discard()
453444

454-
ctx, cancel := context.WithCancel(context.Background())
455-
defer cancel()
445+
ctx := t.Context()
456446

457447
ns1 := createNamespace(g)
458448

core/clustersmngr/fetcher/single_test.go

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

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/gomega"
@@ -25,7 +24,7 @@ func TestSingleFetcher(t *testing.T) {
2524

2625
fetcher := fetcher.NewSingleClusterFetcher(cluster)
2726

28-
clusters, err := fetcher.Fetch(context.TODO())
27+
clusters, err := fetcher.Fetch(t.Context())
2928
g.Expect(err).To(BeNil())
3029

3130
g.Expect(clusters[0].GetName()).To(Equal("Default"))

core/fluxsync/adapters_test.go

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

33
import (
4-
"context"
54
"testing"
65

76
. "github.com/onsi/gomega"
@@ -105,7 +104,7 @@ func TestAsClientObjectCompatibilityWithTestClient(t *testing.T) {
105104
},
106105
}
107106

108-
err := cl.Create(context.TODO(), obj.AsClientObject())
107+
err := cl.Create(t.Context(), obj.AsClientObject())
109108
g.Expect(err).NotTo(HaveOccurred())
110109

111110
retrieved := &UnstructuredAdapter{
@@ -116,7 +115,7 @@ func TestAsClientObjectCompatibilityWithTestClient(t *testing.T) {
116115
},
117116
},
118117
}
119-
err = cl.Get(context.TODO(), client.ObjectKey{Namespace: "default", Name: "test-cm"}, retrieved.AsClientObject())
118+
err = cl.Get(t.Context(), client.ObjectKey{Namespace: "default", Name: "test-cm"}, retrieved.AsClientObject())
120119
g.Expect(err).NotTo(HaveOccurred())
121120

122121
// check the data key

core/nsaccess/nsaccess_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var userName = "test-user"
2525

2626
func TestFilterAccessibleNamespaces(t *testing.T) {
2727
g := NewGomegaWithT(t)
28-
ctx := context.Background()
28+
ctx := t.Context()
2929

3030
testEnv := &envtest.Environment{}
3131
testEnv.ControlPlane.GetAPIServer().Configure().Append("--authorization-mode=RBAC")
@@ -104,7 +104,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
104104
})
105105
t.Run("filters out namespaces that do not have the right resources", func(t *testing.T) {
106106
g := NewGomegaWithT(t)
107-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
107+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
108108
defer removeNs(t, adminClient, ns)
109109

110110
roleName := makeRole(ns)
@@ -139,7 +139,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
139139
})
140140
t.Run("filters out namespaces that do not have the right verbs", func(t *testing.T) {
141141
g := NewGomegaWithT(t)
142-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
142+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
143143
defer removeNs(t, adminClient, ns)
144144

145145
roleName := makeRole(ns)
@@ -174,7 +174,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
174174
})
175175
t.Run("filters out namespaces that do not have the right resources (multiple required rules)", func(t *testing.T) {
176176
g := NewGomegaWithT(t)
177-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
177+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
178178
defer removeNs(t, adminClient, ns)
179179

180180
roleName := makeRole(ns)
@@ -213,7 +213,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
213213
})
214214
t.Run("filters out namespaces that do not have the right verbs (multiple required rules)", func(t *testing.T) {
215215
g := NewGomegaWithT(t)
216-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
216+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
217217
defer removeNs(t, adminClient, ns)
218218

219219
roleName := makeRole(ns)
@@ -252,7 +252,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
252252
})
253253
t.Run("works when api groups are defined in multiple roles", func(t *testing.T) {
254254
g := NewGomegaWithT(t)
255-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
255+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
256256
defer removeNs(t, adminClient, ns)
257257

258258
roleName := makeRole(ns)
@@ -291,7 +291,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
291291
})
292292
t.Run("works when api groups are defined in multiple roles (multiple required rules)", func(t *testing.T) {
293293
g := NewGomegaWithT(t)
294-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
294+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
295295
defer removeNs(t, adminClient, ns)
296296

297297
roleName := makeRole(ns)
@@ -375,7 +375,7 @@ func TestFilterAccessibleNamespaces(t *testing.T) {
375375
for name, roleRules := range testCases {
376376
t.Run(name, func(t *testing.T) {
377377
g := NewGomegaWithT(t)
378-
ns := newNamespace(context.Background(), adminClient, NewGomegaWithT(t))
378+
ns := newNamespace(t.Context(), adminClient, NewGomegaWithT(t))
379379
defer removeNs(t, adminClient, ns)
380380

381381
userName = userName + "-" + rand.String(5)
@@ -424,7 +424,7 @@ func createRole(t *testing.T, cl client.Client, key types.NamespacedName, rules
424424
ObjectMeta: metav1.ObjectMeta{Name: "test-role", Namespace: key.Namespace},
425425
Rules: rules,
426426
}
427-
if err := cl.Create(context.TODO(), role); err != nil {
427+
if err := cl.Create(t.Context(), role); err != nil {
428428
t.Fatalf("failed to write role: %s", err)
429429
}
430430

@@ -446,7 +446,7 @@ func createRole(t *testing.T, cl client.Client, key types.NamespacedName, rules
446446
},
447447
}
448448

449-
if err := cl.Create(context.TODO(), binding); err != nil {
449+
if err := cl.Create(t.Context(), binding); err != nil {
450450
t.Fatalf("failed to write role-binding: %s", err)
451451
}
452452
}
@@ -490,7 +490,7 @@ func newRestConfigWithRole(t *testing.T, testCfg *rest.Config, roleName types.Na
490490
func removeNs(t *testing.T, k client.Client, ns *corev1.Namespace) {
491491
t.Helper()
492492

493-
if err := k.Delete(context.Background(), ns); err != nil {
493+
if err := k.Delete(t.Context(), ns); err != nil {
494494
t.Error(err)
495495
}
496496
}

core/server/crd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestIsAvailable(t *testing.T) {
1818
g := NewGomegaWithT(t)
1919

20-
ctx := context.Background()
20+
ctx := t.Context()
2121

2222
c := makeGRPCServer(ctx, t, k8sEnv.Rest)
2323

core/server/events_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
func TestListEvents(t *testing.T) {
2222
g := NewGomegaWithT(t)
2323

24-
ctx := context.Background()
24+
ctx := t.Context()
2525

2626
c := makeGRPCServer(ctx, t, k8sEnv.Rest)
2727

0 commit comments

Comments
 (0)