Skip to content

Commit bf5bd42

Browse files
committed
fix(lint): resolve golangci-lint errors in testing context
Since Go 1.24, the golangci-lint linter 'usetesing' reports errors when "context.Background()" or "context.TODO()" are used in tests. This commit updates the codebase to use "t.Context()" as recommended by Go 1.24 release documentation. Refactor some 'TestRealWaiter*' functions to send errors through a 'chan error' instead of calling 't.Errorf' inside goroutines. This ensures that all logging and test assertions occur in the main test thread to avoid panics caused by logging after test has completed. Signed-off-by: Alexander Cobas Rodríguez <[email protected]>
1 parent 2804278 commit bf5bd42

File tree

157 files changed

+693
-762
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

157 files changed

+693
-762
lines changed

cmd/entrypoint/runner_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestRealRunnerSignalForwarding(t *testing.T) {
4141
rr := realRunner{}
4242
rr.signals = make(chan os.Signal, 1)
4343
rr.signal(syscall.SIGINT)
44-
if err := rr.Run(context.Background(), "sleep", "3600"); err.Error() == "signal: interrupt" {
44+
if err := rr.Run(t.Context(), "sleep", "3600"); err.Error() == "signal: interrupt" {
4545
t.Logf("SIGINT forwarded to Entrypoint")
4646
} else {
4747
t.Fatalf("Unexpected error received: %v", err)
@@ -66,7 +66,7 @@ func TestRealRunnerStdoutAndStderrPaths(t *testing.T) {
6666
errReader, errWriter, _ := os.Pipe()
6767
os.Stderr = errWriter
6868

69-
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
69+
if err := rr.Run(t.Context(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
7070
t.Fatalf("Unexpected error: %v", err)
7171
}
7272

@@ -116,7 +116,7 @@ func TestRealRunnerStdoutAndStderrSamePath(t *testing.T) {
116116
stdoutPath: path,
117117
stderrPath: path,
118118
}
119-
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
119+
if err := rr.Run(t.Context(), "sh", "-c", fmt.Sprintf("echo %s && echo %s >&2", expectedString, expectedString)); err != nil {
120120
t.Fatalf("Unexpected error: %v", err)
121121
}
122122

@@ -156,7 +156,7 @@ func TestRealRunnerStdoutPathWithSignal(t *testing.T) {
156156
rr.signal(syscall.SIGINT)
157157
}()
158158

159-
if err := rr.Run(context.Background(), "sh", "-c", fmt.Sprintf("echo %s && sleep 20", expectedString)); err == nil || err.Error() != expectedError {
159+
if err := rr.Run(t.Context(), "sh", "-c", fmt.Sprintf("echo %s && sleep 20", expectedString)); err == nil || err.Error() != expectedError {
160160
t.Fatalf("Expected error %v but got %v", expectedError, err)
161161
}
162162
if got, err := os.ReadFile(path); err != nil {
@@ -170,7 +170,7 @@ func TestRealRunnerStdoutPathWithSignal(t *testing.T) {
170170
func TestRealRunnerTimeout(t *testing.T) {
171171
rr := realRunner{}
172172
timeout := time.Millisecond
173-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
173+
ctx, cancel := context.WithTimeout(t.Context(), timeout)
174174
defer cancel()
175175

176176
if err := rr.Run(ctx, "sleep", "0.01"); err != nil {
@@ -206,7 +206,7 @@ func TestRealRunnerCancel(t *testing.T) {
206206
}
207207
for _, tc := range testCases {
208208
rr := realRunner{}
209-
ctx, cancel := context.WithCancel(context.Background())
209+
ctx, cancel := context.WithCancel(t.Context())
210210
go func() {
211211
time.Sleep(tc.timeout)
212212
cancel()

cmd/entrypoint/waiter_test.go

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,18 @@ func TestRealWaiterWaitMissingFile(t *testing.T) {
3939
}
4040
os.Remove(tmp.Name())
4141
rw := realWaiter{}
42-
doneCh := make(chan struct{})
42+
errCh := make(chan error)
4343
go func() {
44-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmp.Name(), false, false)
45-
if err != nil {
46-
t.Errorf("error waiting on tmp file %q", tmp.Name())
47-
}
48-
close(doneCh)
44+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmp.Name(), false, false)
45+
errCh <- err
4946
}()
5047

5148
delay := time.NewTimer(2 * testWaitPollingInterval)
5249
select {
5350
case <-delay.C:
5451
// Success
55-
case <-doneCh:
56-
t.Errorf("did not expect Wait() to have detected a file at path %q", tmp.Name())
52+
case err := <-errCh:
53+
t.Errorf("did not expect Wait() to have detected a file at path %q, err: %v", tmp.Name(), err)
5754
if !delay.Stop() {
5855
<-delay.C
5956
}
@@ -69,7 +66,7 @@ func TestRealWaiterWaitWithFile(t *testing.T) {
6966
rw := realWaiter{}
7067
doneCh := make(chan struct{})
7168
go func() {
72-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmp.Name(), false, false)
69+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmp.Name(), false, false)
7370
if err != nil {
7471
t.Errorf("error waiting on tmp file %q", tmp.Name())
7572
}
@@ -91,20 +88,19 @@ func TestRealWaiterWaitMissingContent(t *testing.T) {
9188
}
9289
defer os.Remove(tmp.Name())
9390
rw := realWaiter{}
94-
doneCh := make(chan struct{})
91+
errCh := make(chan error)
9592
go func() {
96-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmp.Name(), true, false)
97-
if err != nil {
98-
t.Errorf("error waiting on tmp file %q", tmp.Name())
99-
}
100-
close(doneCh)
93+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmp.Name(), true, false)
94+
errCh <- err
10195
}()
10296
delay := time.NewTimer(2 * testWaitPollingInterval)
10397
select {
10498
case <-delay.C:
10599
// Success
106-
case <-doneCh:
107-
t.Errorf("no data was written to tmp file, did not expect Wait() to have detected a non-zero file size and returned")
100+
case err := <-errCh:
101+
if err == nil {
102+
t.Errorf("no data was written to tmp file, did not expect Wait() to have detected a non-zero file size and returned")
103+
}
108104
if !delay.Stop() {
109105
<-delay.C
110106
}
@@ -120,7 +116,7 @@ func TestRealWaiterWaitWithContent(t *testing.T) {
120116
rw := realWaiter{}
121117
doneCh := make(chan struct{})
122118
go func() {
123-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmp.Name(), true, false)
119+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmp.Name(), true, false)
124120
if err != nil {
125121
t.Errorf("error waiting on tmp file %q", tmp.Name())
126122
}
@@ -149,7 +145,7 @@ func TestRealWaiterWaitWithErrorWaitfile(t *testing.T) {
149145
doneCh := make(chan struct{})
150146
go func() {
151147
// error of type skipError is returned after encountering a error waitfile
152-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmpFileName, false, false)
148+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmpFileName, false, false)
153149
if err == nil {
154150
t.Errorf("expected skipError upon encounter error waitfile")
155151
}
@@ -180,7 +176,7 @@ func TestRealWaiterWaitWithBreakpointOnFailure(t *testing.T) {
180176
doneCh := make(chan struct{})
181177
go func() {
182178
// When breakpoint on failure is enabled skipError shouldn't be returned for a error waitfile
183-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmpFileName, false, true)
179+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmpFileName, false, true)
184180
if err != nil {
185181
t.Errorf("error waiting on tmp file %q", tmp.Name())
186182
}
@@ -201,7 +197,7 @@ func TestRealWaiterWaitWithContextCanceled(t *testing.T) {
201197
t.Errorf("error creating temp file: %v", err)
202198
}
203199
defer os.Remove(tmp.Name())
204-
ctx, cancel := context.WithCancel(context.Background())
200+
ctx, cancel := context.WithCancel(t.Context())
205201
rw := realWaiter{}
206202
errCh := make(chan error)
207203
go func() {
@@ -229,7 +225,7 @@ func TestRealWaiterWaitWithTimeout(t *testing.T) {
229225
t.Errorf("error creating temp file: %v", err)
230226
}
231227
defer os.Remove(tmp.Name())
232-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
228+
ctx, cancel := context.WithTimeout(t.Context(), 1*time.Millisecond)
233229
defer cancel()
234230
rw := realWaiter{}
235231
errCh := make(chan error)
@@ -262,7 +258,7 @@ func TestRealWaiterWaitContextWithBreakpointOnFailure(t *testing.T) {
262258
doneCh := make(chan struct{})
263259
go func() {
264260
// When breakpoint on failure is enabled skipError shouldn't be returned for a error waitfile
265-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmpFileName, false, true)
261+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmpFileName, false, true)
266262
if err != nil {
267263
t.Errorf("error waiting on tmp file %q", tmp.Name())
268264
}
@@ -288,7 +284,7 @@ func TestRealWaiterWaitContextWithErrorWaitfile(t *testing.T) {
288284
doneCh := make(chan struct{})
289285
go func() {
290286
// error of type skipError is returned after encountering a error waitfile
291-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmpFileName, false, false)
287+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmpFileName, false, false)
292288
if err == nil {
293289
t.Errorf("expected skipError upon encounter error waitfile")
294290
}
@@ -317,7 +313,7 @@ func TestRealWaiterWaitContextWithContent(t *testing.T) {
317313
rw := realWaiter{}
318314
doneCh := make(chan struct{})
319315
go func() {
320-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmp.Name(), true, false)
316+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmp.Name(), true, false)
321317
if err != nil {
322318
t.Errorf("error waiting on tmp file %q", tmp.Name())
323319
}
@@ -345,21 +341,18 @@ func TestRealWaiterWaitContextMissingFile(t *testing.T) {
345341
}
346342
os.Remove(tmp.Name())
347343
rw := realWaiter{}
348-
doneCh := make(chan struct{})
344+
errCh := make(chan error)
349345
go func() {
350-
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(context.Background(), tmp.Name(), false, false)
351-
if err != nil {
352-
t.Errorf("error waiting on tmp file %q", tmp.Name())
353-
}
354-
close(doneCh)
346+
err := rw.setWaitPollingInterval(testWaitPollingInterval).Wait(t.Context(), tmp.Name(), false, false)
347+
errCh <- err
355348
}()
356349

357350
delay := time.NewTimer(2 * testWaitPollingInterval)
358351
select {
359352
case <-delay.C:
360353
// Success
361-
case <-doneCh:
362-
t.Errorf("did not expect Wait() to have detected a file at path %q", tmp.Name())
354+
case err := <-errCh:
355+
t.Errorf("did not expect Wait() to have detected a file at path %q, err: %v", tmp.Name(), err)
363356
if !delay.Stop() {
364357
<-delay.C
365358
}

internal/sidecarlogresults/sidecarlogresults_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package sidecarlogresults
1818

1919
import (
2020
"bytes"
21-
"context"
2221
"encoding/json"
2322
"errors"
2423
"fmt"
@@ -544,7 +543,7 @@ func TestGetResultsFromSidecarLogs(t *testing.T) {
544543
wantError: true,
545544
}} {
546545
t.Run(c.desc, func(t *testing.T) {
547-
ctx := context.Background()
546+
ctx := t.Context()
548547
clientset := fakekubeclientset.NewSimpleClientset()
549548
pod := &corev1.Pod{
550549
TypeMeta: metav1.TypeMeta{
@@ -567,7 +566,7 @@ func TestGetResultsFromSidecarLogs(t *testing.T) {
567566
Phase: c.podPhase,
568567
},
569568
}
570-
pod, err := clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
569+
pod, err := clientset.CoreV1().Pods(pod.Namespace).Create(t.Context(), pod, metav1.CreateOptions{})
571570
if err != nil {
572571
t.Errorf("Error occurred while creating pod %s: %s", pod.Name, err.Error())
573572
}

pkg/apis/config/feature_flags_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config_test
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -333,7 +332,7 @@ func TestGetVerificationNoMatchPolicy(t *testing.T) {
333332

334333
for _, tc := range tcs {
335334
t.Run(tc.name, func(t *testing.T) {
336-
ctx := context.Background()
335+
ctx := t.Context()
337336
store := config.NewStore(logging.FromContext(ctx).Named("config-store"))
338337
featureflags := &corev1.ConfigMap{
339338
ObjectMeta: metav1.ObjectMeta{
@@ -387,7 +386,7 @@ func TestIsSpireEnabled(t *testing.T) {
387386
},
388387
want: true,
389388
}}
390-
ctx := context.Background()
389+
ctx := t.Context()
391390
store := config.NewStore(logging.FromContext(ctx).Named("config-store"))
392391
for _, tc := range testCases {
393392
featureflags := &corev1.ConfigMap{

pkg/apis/config/featureflags_validation_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config_test
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
"github.com/tektoncd/pipeline/pkg/apis/config"
@@ -64,7 +63,7 @@ func TestValidateEnabledAPIFields(t *testing.T) {
6463
cfg := &config.Config{
6564
FeatureFlags: flags,
6665
}
67-
ctx := config.ToContext(context.Background(), cfg)
66+
ctx := config.ToContext(t.Context(), cfg)
6867
if err := config.ValidateEnabledAPIFields(ctx, "test feature", tc.wantVersion); err != nil {
6968
t.Errorf("unexpected error for compatible feature gates: %q", err)
7069
}
@@ -105,7 +104,7 @@ func TestValidateEnabledAPIFieldsError(t *testing.T) {
105104
cfg := &config.Config{
106105
FeatureFlags: flags,
107106
}
108-
ctx := config.ToContext(context.Background(), cfg)
107+
ctx := config.ToContext(t.Context(), cfg)
109108
fieldErr := config.ValidateEnabledAPIFields(ctx, "test feature", tc.wantVersion)
110109

111110
if fieldErr == nil {

pkg/apis/config/resolver/store_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package resolver_test
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -39,7 +38,7 @@ func TestStoreLoadWithContext(t *testing.T) {
3938
store := resolver.NewStore(logtesting.TestLogger(t))
4039
store.OnConfigChanged(featuresConfig)
4140

42-
cfg := resolver.FromContext(store.ToContext(context.Background()))
41+
cfg := resolver.FromContext(store.ToContext(t.Context()))
4342

4443
if d := cmp.Diff(expected, cfg); d != "" {
4544
t.Errorf("Unexpected config %s", diff.PrintWantGot(d))

pkg/apis/config/store_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package config_test
1818

1919
import (
20-
"context"
2120
"testing"
2221

2322
"github.com/google/go-cmp/cmp"
@@ -59,7 +58,7 @@ func TestStoreLoadWithContext(t *testing.T) {
5958
store.OnConfigChanged(eventsConfig)
6059
store.OnConfigChanged(tracingConfig)
6160

62-
cfg := config.FromContext(store.ToContext(context.Background()))
61+
cfg := config.FromContext(store.ToContext(t.Context()))
6362

6463
if d := cmp.Diff(expected, cfg); d != "" {
6564
t.Errorf("Unexpected config %s", diff.PrintWantGot(d))
@@ -78,7 +77,7 @@ func TestStoreLoadWithContext_Empty(t *testing.T) {
7877

7978
store := config.NewStore(logtesting.TestLogger(t))
8079

81-
got := config.FromContext(store.ToContext(context.Background()))
80+
got := config.FromContext(store.ToContext(t.Context()))
8281

8382
if d := cmp.Diff(want, got); d != "" {
8483
t.Errorf("Unexpected config %s", diff.PrintWantGot(d))

pkg/apis/pipeline/v1/container_validation_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestRef_Valid(t *testing.T) {
8383
}}
8484
for _, ts := range tests {
8585
t.Run(ts.name, func(t *testing.T) {
86-
ctx := context.Background()
86+
ctx := t.Context()
8787
if ts.wc != nil {
8888
ctx = ts.wc(ctx)
8989
}
@@ -162,7 +162,7 @@ func TestRef_Invalid(t *testing.T) {
162162
}}
163163
for _, ts := range tests {
164164
t.Run(ts.name, func(t *testing.T) {
165-
ctx := context.Background()
165+
ctx := t.Context()
166166
if ts.wc != nil {
167167
ctx = ts.wc(ctx)
168168
}
@@ -212,7 +212,7 @@ func TestStepValidate(t *testing.T) {
212212
}}
213213
for _, st := range tests {
214214
t.Run(st.name, func(t *testing.T) {
215-
ctx := cfgtesting.EnableAlphaAPIFields(context.Background())
215+
ctx := cfgtesting.EnableAlphaAPIFields(t.Context())
216216
if err := st.Step.Validate(ctx); err != nil {
217217
t.Errorf("Step.Validate() = %v", err)
218218
}
@@ -292,7 +292,7 @@ func TestStepValidateError(t *testing.T) {
292292
}}
293293
for _, st := range tests {
294294
t.Run(st.name, func(t *testing.T) {
295-
ctx := cfgtesting.EnableAlphaAPIFields(context.Background())
295+
ctx := cfgtesting.EnableAlphaAPIFields(t.Context())
296296
err := st.Step.Validate(ctx)
297297
if err == nil {
298298
t.Fatalf("Expected an error, got nothing for %v", st.Step)
@@ -318,7 +318,7 @@ func TestSidecarValidate(t *testing.T) {
318318

319319
for _, sct := range tests {
320320
t.Run(sct.name, func(t *testing.T) {
321-
err := sct.sidecar.Validate(context.Background())
321+
err := sct.sidecar.Validate(t.Context())
322322
if err != nil {
323323
t.Errorf("Sidecar.Validate() returned error for valid Sidecar: %v", err)
324324
}
@@ -368,7 +368,7 @@ func TestSidecarValidateError(t *testing.T) {
368368

369369
for _, sct := range tests {
370370
t.Run(sct.name, func(t *testing.T) {
371-
err := sct.sidecar.Validate(context.Background())
371+
err := sct.sidecar.Validate(t.Context())
372372
if err == nil {
373373
t.Fatalf("Expected an error, got nothing for %v", sct.sidecar)
374374
}

0 commit comments

Comments
 (0)