Skip to content

Commit e52cd77

Browse files
authored
Merge pull request #3876 from alexandear/refactor/t-context
refactor: replace context.Background() with t.Context() in tests
2 parents 8a83226 + a4482bb commit e52cd77

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ linters:
126126
- -ST1022
127127
usetesting:
128128
os-temp-dir: true
129+
context-background: true
130+
context-todo: true
129131
exclusions:
130132
presets:
131133
- common-false-positives

pkg/downloader/downloader_test.go

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package downloader
55

66
import (
7-
"context"
87
"net/http"
98
"net/http/httptest"
109
"os"
@@ -43,63 +42,67 @@ func TestDownloadRemote(t *testing.T) {
4342

4443
t.Run("without cache", func(t *testing.T) {
4544
t.Run("without digest", func(t *testing.T) {
45+
ctx := t.Context()
4646
localPath := filepath.Join(t.TempDir(), t.Name())
47-
r, err := Download(context.Background(), localPath, dummyRemoteFileURL)
47+
r, err := Download(ctx, localPath, dummyRemoteFileURL)
4848
assert.NilError(t, err)
4949
assert.Equal(t, StatusDownloaded, r.Status)
5050

5151
// download again, make sure StatusSkippedIsReturned
52-
r, err = Download(context.Background(), localPath, dummyRemoteFileURL)
52+
r, err = Download(ctx, localPath, dummyRemoteFileURL)
5353
assert.NilError(t, err)
5454
assert.Equal(t, StatusSkipped, r.Status)
5555
})
5656
t.Run("with digest", func(t *testing.T) {
57+
ctx := t.Context()
5758
wrongDigest := digest.Digest("sha256:8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9")
5859
localPath := filepath.Join(t.TempDir(), t.Name())
59-
_, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))
60+
_, err := Download(ctx, localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest))
6061
assert.ErrorContains(t, err, "expected digest")
6162

6263
wrongDigest2 := digest.Digest("8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9")
63-
_, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest2))
64+
_, err = Download(ctx, localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest2))
6465
assert.ErrorContains(t, err, "invalid checksum digest format")
6566

66-
r, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
67+
r, err := Download(ctx, localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
6768
assert.NilError(t, err)
6869
assert.Equal(t, StatusDownloaded, r.Status)
6970

70-
r, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
71+
r, err = Download(ctx, localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
7172
assert.NilError(t, err)
7273
assert.Equal(t, StatusSkipped, r.Status)
7374
})
7475
})
7576
t.Run("with cache", func(t *testing.T) {
7677
t.Run("serial", func(t *testing.T) {
78+
ctx := t.Context()
7779
cacheDir := filepath.Join(t.TempDir(), "cache")
7880
localPath := filepath.Join(t.TempDir(), t.Name())
79-
r, err := Download(context.Background(), localPath, dummyRemoteFileURL,
81+
r, err := Download(ctx, localPath, dummyRemoteFileURL,
8082
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
8183
assert.NilError(t, err)
8284
assert.Equal(t, StatusDownloaded, r.Status)
8385

84-
r, err = Download(context.Background(), localPath, dummyRemoteFileURL,
86+
r, err = Download(ctx, localPath, dummyRemoteFileURL,
8587
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
8688
assert.NilError(t, err)
8789
assert.Equal(t, StatusSkipped, r.Status)
8890

8991
localPath2 := localPath + "-2"
90-
r, err = Download(context.Background(), localPath2, dummyRemoteFileURL,
92+
r, err = Download(ctx, localPath2, dummyRemoteFileURL,
9193
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
9294
assert.NilError(t, err)
9395
assert.Equal(t, StatusUsedCache, r.Status)
9496
})
9597
t.Run("parallel", func(t *testing.T) {
98+
ctx := t.Context()
9699
cacheDir := filepath.Join(t.TempDir(), "cache")
97100
results := make(chan downloadResult, parallelDownloads)
98101
for range parallelDownloads {
99102
go func() {
100103
// Parallel download is supported only for different instances with unique localPath.
101104
localPath := filepath.Join(t.TempDir(), t.Name())
102-
r, err := Download(context.Background(), localPath, dummyRemoteFileURL,
105+
r, err := Download(ctx, localPath, dummyRemoteFileURL,
103106
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
104107
results <- downloadResult{r, err}
105108
}()
@@ -112,32 +115,34 @@ func TestDownloadRemote(t *testing.T) {
112115
})
113116
t.Run("caching-only mode", func(t *testing.T) {
114117
t.Run("serial", func(t *testing.T) {
115-
_, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
118+
ctx := t.Context()
119+
_, err := Download(ctx, "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
116120
assert.ErrorContains(t, err, "cache directory to be specified")
117121

118122
cacheDir := filepath.Join(t.TempDir(), "cache")
119-
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
123+
r, err := Download(ctx, "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
120124
WithCacheDir(cacheDir))
121125
assert.NilError(t, err)
122126
assert.Equal(t, StatusDownloaded, r.Status)
123127

124-
r, err = Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
128+
r, err = Download(ctx, "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest),
125129
WithCacheDir(cacheDir))
126130
assert.NilError(t, err)
127131
assert.Equal(t, StatusUsedCache, r.Status)
128132

129133
localPath := filepath.Join(t.TempDir(), t.Name())
130-
r, err = Download(context.Background(), localPath, dummyRemoteFileURL,
134+
r, err = Download(ctx, localPath, dummyRemoteFileURL,
131135
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
132136
assert.NilError(t, err)
133137
assert.Equal(t, StatusUsedCache, r.Status)
134138
})
135139
t.Run("parallel", func(t *testing.T) {
140+
ctx := t.Context()
136141
cacheDir := filepath.Join(t.TempDir(), "cache")
137142
results := make(chan downloadResult, parallelDownloads)
138143
for range parallelDownloads {
139144
go func() {
140-
r, err := Download(context.Background(), "", dummyRemoteFileURL,
145+
r, err := Download(ctx, "", dummyRemoteFileURL,
141146
WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
142147
results <- downloadResult{r, err}
143148
}()
@@ -149,11 +154,12 @@ func TestDownloadRemote(t *testing.T) {
149154
})
150155
})
151156
t.Run("cached", func(t *testing.T) {
157+
ctx := t.Context()
152158
_, err := Cached(dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
153159
assert.ErrorContains(t, err, "cache directory to be specified")
154160

155161
cacheDir := filepath.Join(t.TempDir(), "cache")
156-
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
162+
r, err := Download(ctx, "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
157163
assert.NilError(t, err)
158164
assert.Equal(t, StatusDownloaded, r.Status)
159165

@@ -167,11 +173,12 @@ func TestDownloadRemote(t *testing.T) {
167173
assert.ErrorContains(t, err, "expected digest")
168174
})
169175
t.Run("metadata", func(t *testing.T) {
176+
ctx := t.Context()
170177
_, err := Cached(dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
171178
assert.ErrorContains(t, err, "cache directory to be specified")
172179

173180
cacheDir := filepath.Join(t.TempDir(), "cache")
174-
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
181+
r, err := Download(ctx, "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
175182
assert.NilError(t, err)
176183
assert.Equal(t, StatusDownloaded, r.Status)
177184
assert.Equal(t, dummyRemoteFileStat.ModTime().Truncate(time.Second).UTC(), r.LastModified)
@@ -209,34 +216,36 @@ func TestRedownloadRemote(t *testing.T) {
209216
cacheOpt := WithCacheDir(t.TempDir())
210217

211218
t.Run("digest-less", func(t *testing.T) {
219+
ctx := t.Context()
212220
remoteFile := filepath.Join(remoteDir, "digest-less.txt")
213221
assert.NilError(t, os.WriteFile(remoteFile, []byte("digest-less"), 0o644))
214222
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now().Add(-time.Hour)))
215223
opt := []Opt{cacheOpt}
216224

217225
// Download on the first call
218-
r, err := Download(context.Background(), filepath.Join(downloadDir, "1"), ts.URL+"/digest-less.txt", opt...)
226+
r, err := Download(ctx, filepath.Join(downloadDir, "1"), ts.URL+"/digest-less.txt", opt...)
219227
assert.NilError(t, err)
220228
assert.Equal(t, StatusDownloaded, r.Status)
221229

222230
// Next download will use the cached download
223-
r, err = Download(context.Background(), filepath.Join(downloadDir, "2"), ts.URL+"/digest-less.txt", opt...)
231+
r, err = Download(ctx, filepath.Join(downloadDir, "2"), ts.URL+"/digest-less.txt", opt...)
224232
assert.NilError(t, err)
225233
assert.Equal(t, StatusUsedCache, r.Status)
226234

227235
// Modifying remote file will cause redownload
228236
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now()))
229-
r, err = Download(context.Background(), filepath.Join(downloadDir, "3"), ts.URL+"/digest-less.txt", opt...)
237+
r, err = Download(ctx, filepath.Join(downloadDir, "3"), ts.URL+"/digest-less.txt", opt...)
230238
assert.NilError(t, err)
231239
assert.Equal(t, StatusDownloaded, r.Status)
232240

233241
// Next download will use the cached download
234-
r, err = Download(context.Background(), filepath.Join(downloadDir, "4"), ts.URL+"/digest-less.txt", opt...)
242+
r, err = Download(ctx, filepath.Join(downloadDir, "4"), ts.URL+"/digest-less.txt", opt...)
235243
assert.NilError(t, err)
236244
assert.Equal(t, StatusUsedCache, r.Status)
237245
})
238246

239247
t.Run("has-digest", func(t *testing.T) {
248+
ctx := t.Context()
240249
remoteFile := filepath.Join(remoteDir, "has-digest.txt")
241250
bytes := []byte("has-digest")
242251
assert.NilError(t, os.WriteFile(remoteFile, bytes, 0o644))
@@ -247,16 +256,16 @@ func TestRedownloadRemote(t *testing.T) {
247256
assert.NilError(t, err)
248257
opt := []Opt{cacheOpt, WithExpectedDigest(digester.Digest())}
249258

250-
r, err := Download(context.Background(), filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...)
259+
r, err := Download(ctx, filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...)
251260
assert.NilError(t, err)
252261
assert.Equal(t, StatusDownloaded, r.Status)
253-
r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...)
262+
r, err = Download(ctx, filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...)
254263
assert.NilError(t, err)
255264
assert.Equal(t, StatusUsedCache, r.Status)
256265

257266
// modifying remote file won't cause redownload because expected digest is provided
258267
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now()))
259-
r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...)
268+
r, err = Download(ctx, filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...)
260269
assert.NilError(t, err)
261270
assert.Equal(t, StatusUsedCache, r.Status)
262271
})
@@ -274,12 +283,13 @@ func TestDownloadLocal(t *testing.T) {
274283
t.Cleanup(func() { _ = f.Close() })
275284
testLocalFileURL := "file://" + localFile
276285

277-
r, err := Download(context.Background(), localPath, testLocalFileURL)
286+
r, err := Download(t.Context(), localPath, testLocalFileURL)
278287
assert.NilError(t, err)
279288
assert.Equal(t, StatusDownloaded, r.Status)
280289
})
281290

282291
t.Run("with file digest", func(t *testing.T) {
292+
ctx := t.Context()
283293
localPath := filepath.Join(t.TempDir(), t.Name())
284294
localTestFile := filepath.Join(t.TempDir(), "some-file")
285295
testDownloadFileContents := []byte("TestDownloadLocal")
@@ -288,10 +298,10 @@ func TestDownloadLocal(t *testing.T) {
288298
testLocalFileURL := "file://" + localTestFile
289299
wrongDigest := digest.Digest(emptyFileDigest)
290300

291-
_, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
301+
_, err := Download(ctx, localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
292302
assert.ErrorContains(t, err, "expected digest")
293303

294-
r, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
304+
r, err := Download(ctx, localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
295305
assert.NilError(t, err)
296306
assert.Equal(t, StatusDownloaded, r.Status)
297307
})
@@ -325,7 +335,7 @@ func TestDownloadCompressed(t *testing.T) {
325335
localFile += ".gz"
326336
testLocalFileURL := "file://" + localFile
327337

328-
r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
338+
r, err := Download(ctx, localPath, testLocalFileURL, WithDecompress(true))
329339
assert.NilError(t, err)
330340
assert.Equal(t, StatusDownloaded, r.Status)
331341

@@ -344,7 +354,7 @@ func TestDownloadCompressed(t *testing.T) {
344354
localFile += ".bz2"
345355
testLocalFileURL := "file://" + localFile
346356

347-
r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
357+
r, err := Download(ctx, localPath, testLocalFileURL, WithDecompress(true))
348358
assert.NilError(t, err)
349359
assert.Equal(t, StatusDownloaded, r.Status)
350360

@@ -360,7 +370,7 @@ func TestDownloadCompressed(t *testing.T) {
360370
assert.NilError(t, os.WriteFile(localFile, testDownloadCompressedContents, 0o644))
361371
testLocalFileURL := "file://" + localFile
362372

363-
r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true))
373+
r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true))
364374
assert.NilError(t, err)
365375
assert.Equal(t, StatusDownloaded, r.Status)
366376

pkg/downloader/fuzz_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package downloader
55

66
import (
7-
"context"
87
"os"
98
"path/filepath"
109
"testing"
@@ -24,9 +23,9 @@ func FuzzDownload(f *testing.F) {
2423
testLocalFileURL := "file://" + remoteFile
2524
if checkDigest {
2625
d := algorithm.FromBytes(fileContents)
27-
_, _ = Download(context.Background(), localFile, testLocalFileURL, WithExpectedDigest(d))
26+
_, _ = Download(t.Context(), localFile, testLocalFileURL, WithExpectedDigest(d))
2827
} else {
29-
_, _ = Download(context.Background(), localFile, testLocalFileURL)
28+
_, _ = Download(t.Context(), localFile, testLocalFileURL)
3029
}
3130
})
3231
}

pkg/guestagent/kubernetesservice/kubernetesservice_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func newFakeKubeClient() (clientSet.Interface, informers.SharedInformerFactory)
2525
}
2626

2727
func TestGetPorts(t *testing.T) {
28-
ctx, cancel := context.WithCancel(context.Background())
28+
ctx, cancel := context.WithCancel(t.Context())
2929
defer cancel()
3030
serviceCreatedCh := make(chan struct{}, 1)
3131
kubeClient, informerFactory := newFakeKubeClient()

pkg/imgutil/nativeimgutil/nativeimgutil_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func TestRoundUp(t *testing.T) {
3232
}
3333
}
3434

35-
func createImg(name, format, size string) error {
36-
ctx := context.TODO()
35+
func createImg(ctx context.Context, name, format, size string) error {
3736
return exec.CommandContext(ctx, "qemu-img", "create", name, "-f", format, size).Run()
3837
}
3938

@@ -43,23 +42,24 @@ func TestConvertToRaw(t *testing.T) {
4342
t.Skipf("qemu-img does not seem installed: %v", err)
4443
}
4544
tmpDir := t.TempDir()
45+
ctx := t.Context()
4646

4747
qcowImage, err := os.Create(filepath.Join(tmpDir, "qcow.img"))
4848
assert.NilError(t, err)
4949
defer qcowImage.Close()
50-
err = createImg(qcowImage.Name(), "qcow2", "1M")
50+
err = createImg(ctx, qcowImage.Name(), "qcow2", "1M")
5151
assert.NilError(t, err)
5252

5353
rawImage, err := os.Create(filepath.Join(tmpDir, "raw.img"))
5454
assert.NilError(t, err)
5555
defer rawImage.Close()
56-
err = createImg(rawImage.Name(), "raw", "1M")
56+
err = createImg(ctx, rawImage.Name(), "raw", "1M")
5757
assert.NilError(t, err)
5858

5959
rawImageExtended, err := os.Create(filepath.Join(tmpDir, "raw_extended.img"))
6060
assert.NilError(t, err)
6161
defer rawImageExtended.Close()
62-
err = createImg(rawImageExtended.Name(), "raw", "2M")
62+
err = createImg(ctx, rawImageExtended.Name(), "raw", "2M")
6363
assert.NilError(t, err)
6464

6565
t.Run("qcow without backing file", func(t *testing.T) {

pkg/limatmpl/embed_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package limatmpl
55

66
import (
7-
"context"
87
"fmt"
98
"os"
109
"reflect"
@@ -453,7 +452,7 @@ func RunEmbedTest(t *testing.T, tc embedTestCase) {
453452
if strings.HasPrefix(tc.base, "#!") {
454453
tmpl.Bytes = []byte(tc.template)
455454
}
456-
err := tmpl.Embed(context.TODO(), false, false)
455+
err := tmpl.Embed(t.Context(), false, false)
457456
if expectError {
458457
assert.ErrorContains(t, err, tc.expected, tc.description)
459458
return

0 commit comments

Comments
 (0)