Skip to content

Commit a53aee2

Browse files
Use testing.Context (a Go 1.24 feature)
Signed-off-by: Richard Wall <[email protected]>
1 parent 2bbd0a6 commit a53aee2

File tree

10 files changed

+15
-22
lines changed

10 files changed

+15
-22
lines changed

cmd/agent_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestAgentRunOneShot(t *testing.T) {
3939
return
4040
}
4141
t.Log("Running child process")
42-
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
42+
ctx, cancel := context.WithTimeout(t.Context(), time.Second*3)
4343
defer cancel()
4444
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestAgentRunOneShot$")
4545
var (

pkg/agent/config_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
659659
t.Run("server, uploader_id, and cluster name are correctly passed", func(t *testing.T) {
660660
t.Setenv("POD_NAMESPACE", "venafi")
661661

662-
ctx, cancel := context.WithCancel(context.Background())
662+
ctx, cancel := context.WithCancel(t.Context())
663663
defer cancel()
664664
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(10)))
665665
ctx = klog.NewContext(ctx, log)
@@ -752,7 +752,7 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
752752
name: venafi-connection
753753
namespace: venafi
754754
`))) {
755-
require.NoError(t, kcl.Create(context.Background(), obj))
755+
require.NoError(t, kcl.Create(t.Context(), obj))
756756
}
757757

758758
t.Run("err when cluster_id field is empty", func(t *testing.T) {
@@ -768,7 +768,7 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
768768
})
769769

770770
t.Run("the server field is ignored when VenafiConnection is used", func(t *testing.T) {
771-
ctx, cancel := context.WithCancel(context.Background())
771+
ctx, cancel := context.WithCancel(t.Context())
772772
defer cancel()
773773
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(10)))
774774
ctx = klog.NewContext(ctx, log)

pkg/client/client_venconn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
//
3737
// [1] https://github.com/kubernetes-sigs/controller-runtime/issues/2341
3838
func TestVenConnClient_PostDataReadingsWithOptions(t *testing.T) {
39-
ctx, cancel := context.WithCancel(context.Background())
39+
ctx, cancel := context.WithCancel(t.Context())
4040
defer cancel()
4141
log := ktesting.NewLogger(t, ktesting.NewConfig(ktesting.Verbosity(10)))
4242
ctx = klog.NewContext(ctx, log)

pkg/datagatherer/k8s/dynamic_test.go

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

33
import (
4-
"context"
54
"encoding/json"
65
"fmt"
76
"reflect"
@@ -117,7 +116,7 @@ func sortGatheredResources(list []*api.GatheredResource) {
117116
}
118117

119118
func TestNewDataGathererWithClientAndDynamicInformer(t *testing.T) {
120-
ctx := context.Background()
119+
ctx := t.Context()
121120
config := ConfigDynamic{
122121
ExcludeNamespaces: []string{"kube-system"},
123122
GroupVersionResource: schema.GroupVersionResource{Group: "foobar", Version: "v1", Resource: "foos"},
@@ -164,7 +163,7 @@ func TestNewDataGathererWithClientAndDynamicInformer(t *testing.T) {
164163
}
165164

166165
func TestNewDataGathererWithClientAndSharedIndexInformer(t *testing.T) {
167-
ctx := context.Background()
166+
ctx := t.Context()
168167
config := ConfigDynamic{
169168
IncludeNamespaces: []string{"a"},
170169
GroupVersionResource: schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"},
@@ -638,7 +637,7 @@ func TestDynamicGatherer_Fetch(t *testing.T) {
638637
for name, tc := range tests {
639638
t.Run(name, func(t *testing.T) {
640639
var wg sync.WaitGroup
641-
ctx := context.Background()
640+
ctx := t.Context()
642641
gvrToListKind := map[schema.GroupVersionResource]string{
643642
{Group: "foobar", Version: "v1", Resource: "foos"}: "UnstructuredList",
644643
{Group: "apps", Version: "v1", Resource: "deployments"}: "UnstructuredList",
@@ -958,7 +957,7 @@ func TestDynamicGathererNativeResources_Fetch(t *testing.T) {
958957
for name, tc := range tests {
959958
t.Run(name, func(t *testing.T) {
960959
var wg sync.WaitGroup
961-
ctx := context.Background()
960+
ctx := t.Context()
962961

963962
clientset := fakeclientset.NewSimpleClientset(tc.addObjects...)
964963

pkg/echo/echo_test.go

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

33
import (
44
"bytes"
5-
"context"
65
"encoding/json"
76
"net/http"
87
"net/http/httptest"
@@ -61,7 +60,7 @@ func TestEchoServerRequestResponse(t *testing.T) {
6160
}
6261

6362
// generate a request to test the handler containing the JSON data as a body
64-
req, err := http.NewRequestWithContext(context.TODO(), sampleUpload.method, "http://example.com/api/v1/datareadings", bytes.NewBuffer(requestBodyJSON))
63+
req, err := http.NewRequestWithContext(t.Context(), sampleUpload.method, "http://example.com/api/v1/datareadings", bytes.NewBuffer(requestBodyJSON))
6564
if err != nil {
6665
t.Fatalf("[%s]\nfailed to generate request to test echo server: %s", sampleUpload.description, err)
6766
}

pkg/internal/cyberark/dataupload/dataupload_test.go

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

33
import (
4-
"context"
54
"crypto/x509"
65
"encoding/pem"
76
"fmt"
@@ -113,7 +112,7 @@ func TestCyberArkClient_PostDataReadingsWithOptions(t *testing.T) {
113112
cyberArkClient, err := dataupload.NewCyberArkClient(certPool, server.Server.URL, tc.authenticate)
114113
require.NoError(t, err)
115114

116-
err = cyberArkClient.PostDataReadingsWithOptions(context.TODO(), tc.payload, tc.opts)
115+
err = cyberArkClient.PostDataReadingsWithOptions(t.Context(), tc.payload, tc.opts)
117116
tc.requireFn(t, err)
118117
})
119118
}

pkg/internal/cyberark/identity/advance_authentication_test.go

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

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

@@ -98,7 +97,7 @@ func Test_IdentityAdvanceAuthentication(t *testing.T) {
9897

9998
for name, testSpec := range tests {
10099
t.Run(name, func(t *testing.T) {
101-
ctx := context.Background()
100+
ctx := t.Context()
102101

103102
identityServer := MockIdentityServer()
104103
defer identityServer.Close()

pkg/internal/cyberark/identity/start_authentication_test.go

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

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

@@ -39,7 +38,7 @@ func Test_IdentityStartAuthentication(t *testing.T) {
3938

4039
for name, testSpec := range tests {
4140
t.Run(name, func(t *testing.T) {
42-
ctx := context.Background()
41+
ctx := t.Context()
4342

4443
identityServer := MockIdentityServer()
4544
defer identityServer.Close()

pkg/internal/cyberark/servicediscovery/discovery_test.go

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

33
import (
4-
"context"
54
"fmt"
65
"testing"
76
)
@@ -46,7 +45,7 @@ func Test_DiscoverIdentityAPIURL(t *testing.T) {
4645

4746
for name, testSpec := range tests {
4847
t.Run(name, func(t *testing.T) {
49-
ctx := context.Background()
48+
ctx := t.Context()
5049

5150
ts := MockDiscoveryServer()
5251
defer ts.Close()

pkg/logs/logs_test.go

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

33
import (
44
"bytes"
5-
"context"
65
"errors"
76
"fmt"
87
"io"
@@ -65,7 +64,7 @@ func TestLogs(t *testing.T) {
6564
klog.Warning("klog Warning")
6665
klog.ErrorS(errors.New("fake-error"), "klog Error")
6766
klog.InfoS("klog InfoS", "key", "value")
68-
logger := klog.FromContext(context.Background()).WithName("foo")
67+
logger := klog.FromContext(t.Context()).WithName("foo")
6968
logger.V(3).Info("Contextual Info Level 3", "key", "value")
7069
logger.Error(errors.New("fake-error"), "Contextual error", "key", "value")
7170

0 commit comments

Comments
 (0)