Skip to content

Commit a02917f

Browse files
authored
Remove context.background and variable copy from test case (#4875)
1 parent e6f4d73 commit a02917f

39 files changed

+129
-143
lines changed

internal/pkg/action/dispatcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func Test_Dispatcher_Run(t *testing.T) {
259259
}
260260

261261
now := time.Now()
262-
ctx, cancel := context.WithCancel(context.Background())
262+
ctx, cancel := context.WithCancel(t.Context())
263263
defer cancel()
264264
go func() {
265265
err := d.Run(ctx)
@@ -366,7 +366,7 @@ func Test_offsetStartTime(t *testing.T) {
366366
}}
367367
for _, tt := range tests {
368368
t.Run(tt.name, func(t *testing.T) {
369-
r := offsetStartTime(context.Background(), tt.start, tt.dur, tt.i, tt.total)
369+
r := offsetStartTime(t.Context(), tt.start, tt.dur, tt.i, tt.total)
370370
assert.Equal(t, tt.result, r)
371371
})
372372
}

internal/pkg/api/error_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func Test_ErrorResp(t *testing.T) {
4848
tracer.ResetPayloads()
4949

5050
tx := tracer.StartTransaction("test", "test")
51-
ctx := apm.ContextWithTransaction(context.Background(), tx)
51+
ctx := apm.ContextWithTransaction(t.Context(), tx)
5252
ctx = logger.WithContext(ctx)
5353

5454
wr := httptest.NewRecorder()
@@ -78,7 +78,7 @@ func Test_ErrorResp(t *testing.T) {
7878

7979
func Test_ErrorResp_NoTransaction(t *testing.T) {
8080
tracer := apmtest.NewRecordingTracer()
81-
ctx := testlog.SetLogger(t).WithContext(context.Background())
81+
ctx := testlog.SetLogger(t).WithContext(t.Context())
8282

8383
wr := httptest.NewRecorder()
8484
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost", nil)

internal/pkg/api/handleAudit_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package api
66

77
import (
8-
"context"
98
"io"
109
"net/http"
1110
"net/http/httptest"
@@ -98,7 +97,7 @@ func Test_Audit_markUnenroll(t *testing.T) {
9897
bulker.On("Update", mock.Anything, dl.FleetAgents, agent.Id, mock.Anything, mock.Anything, mock.Anything).Return(nil)
9998
audit := AuditT{bulk: bulker}
10099
logger := testlog.SetLogger(t)
101-
err := audit.markUnenroll(context.Background(), logger, &AuditUnenrollRequest{Reason: Uninstall, Timestamp: time.Now().UTC()}, agent)
100+
err := audit.markUnenroll(t.Context(), logger, &AuditUnenrollRequest{Reason: Uninstall, Timestamp: time.Now().UTC()}, agent)
102101
require.NoError(t, err)
103102
bulker.AssertExpectations(t)
104103
}

internal/pkg/api/handlePGPRequest_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package api
66

77
import (
8-
"context"
98
"net/http"
109
"net/http/httptest"
1110
"os"
@@ -119,7 +118,7 @@ func Test_PGPRetrieverT_getPGPKey(t *testing.T) {
119118
},
120119
}
121120

122-
content, err := pt.getPGPKey(context.Background(), testlog.SetLogger(t))
121+
content, err := pt.getPGPKey(t.Context(), testlog.SetLogger(t))
123122
require.ErrorIs(t, err, tc.err)
124123
require.Equal(t, tc.content, content)
125124
mockCache.AssertExpectations(t)

internal/pkg/api/metrics_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestMetricsEndpoints(t *testing.T) {
2929
Port: 8080,
3030
},
3131
}
32-
ctx, cancel := context.WithCancel(context.Background())
32+
ctx, cancel := context.WithCancel(t.Context())
3333
defer cancel()
3434
ctx = testlog.SetLogger(t).WithContext(ctx)
3535

internal/pkg/api/openapi_spec_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ func Test_UpgradeDetailsMetadata_Downloading(t *testing.T) {
228228
}}
229229

230230
for _, tc := range tests {
231-
tc := tc
232231
t.Run(tc.name, func(t *testing.T) {
233232
meta, err := tc.md.AsUpgradeMetadataDownloading()
234233
if tc.err == nil {
@@ -286,7 +285,6 @@ func Test_UpgradeDetailsMetadata_Failed(t *testing.T) {
286285
}}
287286

288287
for _, tc := range tests {
289-
tc := tc
290288
t.Run(tc.name, func(t *testing.T) {
291289
meta, err := tc.md.AsUpgradeMetadataFailed()
292290
if tc.err == nil {
@@ -342,7 +340,6 @@ func Test_UpgradeDetailsMetadata_Scheduled(t *testing.T) {
342340
}}
343341

344342
for _, tc := range tests {
345-
tc := tc
346343
t.Run(tc.name, func(t *testing.T) {
347344
meta, err := tc.md.AsUpgradeMetadataScheduled()
348345
if tc.err == nil {
@@ -385,7 +382,6 @@ func TestUpgradeDetailsSerialization(t *testing.T) {
385382
TargetVersion: "1.2.3",
386383
}}
387384
for _, d := range details {
388-
d := d
389385
t.Run(string(d.State), func(t *testing.T) {
390386
p, err := json.Marshal(d)
391387
require.NoError(t, err)

internal/pkg/apikey/apikey_integration_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const testFleetRoles = `
3232
`
3333

3434
func TestRead_existingKey(t *testing.T) {
35-
ctx, cn := context.WithCancel(context.Background())
35+
ctx, cn := context.WithCancel(t.Context())
3636
defer cn()
3737
ctx = testlog.SetLogger(t).WithContext(ctx)
3838

@@ -84,7 +84,7 @@ func TestRead_existingKey(t *testing.T) {
8484
}
8585

8686
func TestRead_noKey(t *testing.T) {
87-
ctx, cn := context.WithCancel(context.Background())
87+
ctx, cn := context.WithCancel(t.Context())
8888
defer cn()
8989
ctx = testlog.SetLogger(t).WithContext(ctx)
9090

@@ -116,7 +116,7 @@ func TestCreateAPIKeyWithMetadata(t *testing.T) {
116116

117117
for _, tt := range tts {
118118
t.Run(tt.name, func(t *testing.T) {
119-
ctx, cn := context.WithCancel(context.Background())
119+
ctx, cn := context.WithCancel(t.Context())
120120
defer cn()
121121
ctx = testlog.SetLogger(t).WithContext(ctx)
122122

internal/pkg/apikey/auth_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func setup(t *testing.T, statusCode int) (context.Context, *APIKey, *elasticsear
2424
token := base64.StdEncoding.EncodeToString([]byte(rawToken))
2525
apiKey, err := NewAPIKeyFromToken(token)
2626
assert.NoError(t, err)
27-
ctx := context.Background()
27+
ctx := t.Context()
2828

2929
mockES, mockTransport := esutil.MockESClient(t)
3030
mockTransport.RoundTripFn = func(req *http.Request) (*http.Response, error) { return &http.Response{StatusCode: statusCode}, nil }

internal/pkg/bulk/bulk_integration_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
func TestBulkCreate(t *testing.T) {
25-
ctx, cn := context.WithCancel(context.Background())
25+
ctx, cn := context.WithCancel(t.Context())
2626
defer cn()
2727

2828
index, bulker := SetupIndexWithBulk(ctx, t, testPolicy, WithFlushThresholdCount(1))
@@ -124,7 +124,7 @@ func TestBulkCreate(t *testing.T) {
124124
}
125125

126126
func TestBulkCreateBody(t *testing.T) {
127-
ctx, cn := context.WithCancel(context.Background())
127+
ctx, cn := context.WithCancel(t.Context())
128128
defer cn()
129129

130130
index, bulker := SetupIndexWithBulk(ctx, t, testPolicy, WithFlushThresholdCount(1))
@@ -177,7 +177,7 @@ func TestBulkCreateBody(t *testing.T) {
177177
}
178178

179179
func TestBulkIndex(t *testing.T) {
180-
ctx, cn := context.WithCancel(context.Background())
180+
ctx, cn := context.WithCancel(t.Context())
181181
defer cn()
182182
ctx = testlog.SetLogger(t).WithContext(ctx)
183183

@@ -201,7 +201,7 @@ func TestBulkIndex(t *testing.T) {
201201
}
202202

203203
func TestBulkUpdate(t *testing.T) {
204-
ctx, cn := context.WithCancel(context.Background())
204+
ctx, cn := context.WithCancel(t.Context())
205205
defer cn()
206206
ctx = testlog.SetLogger(t).WithContext(ctx)
207207

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

242242
func TestBulkSearch(t *testing.T) {
243-
ctx, cn := context.WithCancel(context.Background())
243+
ctx, cn := context.WithCancel(t.Context())
244244
defer cn()
245245
ctx = testlog.SetLogger(t).WithContext(ctx)
246246

@@ -283,7 +283,7 @@ func TestBulkSearch(t *testing.T) {
283283
}
284284

285285
func TestBulkSearchWithIgnoreUnavailable(t *testing.T) {
286-
ctx, cn := context.WithCancel(context.Background())
286+
ctx, cn := context.WithCancel(t.Context())
287287
defer cn()
288288
ctx = testlog.SetLogger(t).WithContext(ctx)
289289

@@ -306,7 +306,7 @@ func TestBulkSearchWithIgnoreUnavailable(t *testing.T) {
306306
}
307307

308308
func TestBulkDelete(t *testing.T) {
309-
ctx, cn := context.WithCancel(context.Background())
309+
ctx, cn := context.WithCancel(t.Context())
310310
defer cn()
311311
ctx = testlog.SetLogger(t).WithContext(ctx)
312312

@@ -344,7 +344,7 @@ func TestBulkDelete(t *testing.T) {
344344
func benchmarkCreate(n int, b *testing.B) {
345345
b.ReportAllocs()
346346

347-
ctx, cn := context.WithCancel(context.Background())
347+
ctx, cn := context.WithCancel(b.Context())
348348
defer cn()
349349
ctx = testlog.SetLogger(b).WithContext(ctx)
350350

@@ -396,7 +396,7 @@ func BenchmarkCreate(b *testing.B) {
396396
// Not a particularly useful benchmark, but gives some idea of memory overhead.
397397

398398
func benchmarkCRUD(n int, b *testing.B) {
399-
ctx, cn := context.WithCancel(context.Background())
399+
ctx, cn := context.WithCancel(b.Context())
400400
defer cn()
401401
ctx = testlog.SetLogger(b).WithContext(ctx)
402402

internal/pkg/bulk/bulk_remote_output_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func Test_hasChangedAndUpdateRemoteOutputConfig(t *testing.T) {
9191
}
9292

9393
func Test_CreateAndGetBulkerNew(t *testing.T) {
94-
ctx, cn := context.WithCancel(context.Background())
94+
ctx, cn := context.WithCancel(t.Context())
9595
defer cn()
9696
bulker := NewBulker(nil, nil)
9797
outputMap := make(map[string]map[string]interface{})
@@ -107,7 +107,7 @@ func Test_CreateAndGetBulkerNew(t *testing.T) {
107107
}
108108

109109
func Test_CreateAndGetBulkerExisting(t *testing.T) {
110-
ctx, cn := context.WithCancel(context.Background())
110+
ctx, cn := context.WithCancel(t.Context())
111111
defer cn()
112112
bulker := NewBulker(nil, nil)
113113
outputBulker := NewBulker(nil, nil)
@@ -127,7 +127,7 @@ func Test_CreateAndGetBulkerExisting(t *testing.T) {
127127
}
128128

129129
func Test_CreateAndGetBulkerChanged(t *testing.T) {
130-
ctx, cn := context.WithCancel(context.Background())
130+
ctx, cn := context.WithCancel(t.Context())
131131
defer cn()
132132
bulker := NewBulker(nil, nil)
133133
outputBulker := NewBulker(nil, nil)
@@ -154,7 +154,7 @@ func Test_CreateAndGetBulkerChanged(t *testing.T) {
154154

155155
func Benchmark_CreateAndGetBulker(b *testing.B) {
156156
b.Skip("Crashes on remote runner")
157-
ctx, cancel := context.WithCancel(context.Background())
157+
ctx, cancel := context.WithCancel(b.Context())
158158
defer cancel()
159159
log := zerolog.Nop()
160160
outputMap := map[string]map[string]any{

0 commit comments

Comments
 (0)