4
4
package downloader
5
5
6
6
import (
7
- "context"
8
7
"net/http"
9
8
"net/http/httptest"
10
9
"os"
@@ -43,63 +42,67 @@ func TestDownloadRemote(t *testing.T) {
43
42
44
43
t .Run ("without cache" , func (t * testing.T ) {
45
44
t .Run ("without digest" , func (t * testing.T ) {
45
+ ctx := t .Context ()
46
46
localPath := filepath .Join (t .TempDir (), t .Name ())
47
- r , err := Download (context . Background () , localPath , dummyRemoteFileURL )
47
+ r , err := Download (ctx , localPath , dummyRemoteFileURL )
48
48
assert .NilError (t , err )
49
49
assert .Equal (t , StatusDownloaded , r .Status )
50
50
51
51
// download again, make sure StatusSkippedIsReturned
52
- r , err = Download (context . Background () , localPath , dummyRemoteFileURL )
52
+ r , err = Download (ctx , localPath , dummyRemoteFileURL )
53
53
assert .NilError (t , err )
54
54
assert .Equal (t , StatusSkipped , r .Status )
55
55
})
56
56
t .Run ("with digest" , func (t * testing.T ) {
57
+ ctx := t .Context ()
57
58
wrongDigest := digest .Digest ("sha256:8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9" )
58
59
localPath := filepath .Join (t .TempDir (), t .Name ())
59
- _ , err := Download (context . Background () , localPath , dummyRemoteFileURL , WithExpectedDigest (wrongDigest ))
60
+ _ , err := Download (ctx , localPath , dummyRemoteFileURL , WithExpectedDigest (wrongDigest ))
60
61
assert .ErrorContains (t , err , "expected digest" )
61
62
62
63
wrongDigest2 := digest .Digest ("8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9" )
63
- _ , err = Download (context . Background () , localPath , dummyRemoteFileURL , WithExpectedDigest (wrongDigest2 ))
64
+ _ , err = Download (ctx , localPath , dummyRemoteFileURL , WithExpectedDigest (wrongDigest2 ))
64
65
assert .ErrorContains (t , err , "invalid checksum digest format" )
65
66
66
- r , err := Download (context . Background () , localPath , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ))
67
+ r , err := Download (ctx , localPath , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ))
67
68
assert .NilError (t , err )
68
69
assert .Equal (t , StatusDownloaded , r .Status )
69
70
70
- r , err = Download (context . Background () , localPath , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ))
71
+ r , err = Download (ctx , localPath , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ))
71
72
assert .NilError (t , err )
72
73
assert .Equal (t , StatusSkipped , r .Status )
73
74
})
74
75
})
75
76
t .Run ("with cache" , func (t * testing.T ) {
76
77
t .Run ("serial" , func (t * testing.T ) {
78
+ ctx := t .Context ()
77
79
cacheDir := filepath .Join (t .TempDir (), "cache" )
78
80
localPath := filepath .Join (t .TempDir (), t .Name ())
79
- r , err := Download (context . Background () , localPath , dummyRemoteFileURL ,
81
+ r , err := Download (ctx , localPath , dummyRemoteFileURL ,
80
82
WithExpectedDigest (dummyRemoteFileDigest ), WithCacheDir (cacheDir ))
81
83
assert .NilError (t , err )
82
84
assert .Equal (t , StatusDownloaded , r .Status )
83
85
84
- r , err = Download (context . Background () , localPath , dummyRemoteFileURL ,
86
+ r , err = Download (ctx , localPath , dummyRemoteFileURL ,
85
87
WithExpectedDigest (dummyRemoteFileDigest ), WithCacheDir (cacheDir ))
86
88
assert .NilError (t , err )
87
89
assert .Equal (t , StatusSkipped , r .Status )
88
90
89
91
localPath2 := localPath + "-2"
90
- r , err = Download (context . Background () , localPath2 , dummyRemoteFileURL ,
92
+ r , err = Download (ctx , localPath2 , dummyRemoteFileURL ,
91
93
WithExpectedDigest (dummyRemoteFileDigest ), WithCacheDir (cacheDir ))
92
94
assert .NilError (t , err )
93
95
assert .Equal (t , StatusUsedCache , r .Status )
94
96
})
95
97
t .Run ("parallel" , func (t * testing.T ) {
98
+ ctx := t .Context ()
96
99
cacheDir := filepath .Join (t .TempDir (), "cache" )
97
100
results := make (chan downloadResult , parallelDownloads )
98
101
for range parallelDownloads {
99
102
go func () {
100
103
// Parallel download is supported only for different instances with unique localPath.
101
104
localPath := filepath .Join (t .TempDir (), t .Name ())
102
- r , err := Download (context . Background () , localPath , dummyRemoteFileURL ,
105
+ r , err := Download (ctx , localPath , dummyRemoteFileURL ,
103
106
WithExpectedDigest (dummyRemoteFileDigest ), WithCacheDir (cacheDir ))
104
107
results <- downloadResult {r , err }
105
108
}()
@@ -112,32 +115,34 @@ func TestDownloadRemote(t *testing.T) {
112
115
})
113
116
t .Run ("caching-only mode" , func (t * testing.T ) {
114
117
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 ))
116
120
assert .ErrorContains (t , err , "cache directory to be specified" )
117
121
118
122
cacheDir := filepath .Join (t .TempDir (), "cache" )
119
- r , err := Download (context . Background () , "" , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ),
123
+ r , err := Download (ctx , "" , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ),
120
124
WithCacheDir (cacheDir ))
121
125
assert .NilError (t , err )
122
126
assert .Equal (t , StatusDownloaded , r .Status )
123
127
124
- r , err = Download (context . Background () , "" , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ),
128
+ r , err = Download (ctx , "" , dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ),
125
129
WithCacheDir (cacheDir ))
126
130
assert .NilError (t , err )
127
131
assert .Equal (t , StatusUsedCache , r .Status )
128
132
129
133
localPath := filepath .Join (t .TempDir (), t .Name ())
130
- r , err = Download (context . Background () , localPath , dummyRemoteFileURL ,
134
+ r , err = Download (ctx , localPath , dummyRemoteFileURL ,
131
135
WithExpectedDigest (dummyRemoteFileDigest ), WithCacheDir (cacheDir ))
132
136
assert .NilError (t , err )
133
137
assert .Equal (t , StatusUsedCache , r .Status )
134
138
})
135
139
t .Run ("parallel" , func (t * testing.T ) {
140
+ ctx := t .Context ()
136
141
cacheDir := filepath .Join (t .TempDir (), "cache" )
137
142
results := make (chan downloadResult , parallelDownloads )
138
143
for range parallelDownloads {
139
144
go func () {
140
- r , err := Download (context . Background () , "" , dummyRemoteFileURL ,
145
+ r , err := Download (ctx , "" , dummyRemoteFileURL ,
141
146
WithExpectedDigest (dummyRemoteFileDigest ), WithCacheDir (cacheDir ))
142
147
results <- downloadResult {r , err }
143
148
}()
@@ -149,11 +154,12 @@ func TestDownloadRemote(t *testing.T) {
149
154
})
150
155
})
151
156
t .Run ("cached" , func (t * testing.T ) {
157
+ ctx := t .Context ()
152
158
_ , err := Cached (dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ))
153
159
assert .ErrorContains (t , err , "cache directory to be specified" )
154
160
155
161
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 ))
157
163
assert .NilError (t , err )
158
164
assert .Equal (t , StatusDownloaded , r .Status )
159
165
@@ -167,11 +173,12 @@ func TestDownloadRemote(t *testing.T) {
167
173
assert .ErrorContains (t , err , "expected digest" )
168
174
})
169
175
t .Run ("metadata" , func (t * testing.T ) {
176
+ ctx := t .Context ()
170
177
_ , err := Cached (dummyRemoteFileURL , WithExpectedDigest (dummyRemoteFileDigest ))
171
178
assert .ErrorContains (t , err , "cache directory to be specified" )
172
179
173
180
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 ))
175
182
assert .NilError (t , err )
176
183
assert .Equal (t , StatusDownloaded , r .Status )
177
184
assert .Equal (t , dummyRemoteFileStat .ModTime ().Truncate (time .Second ).UTC (), r .LastModified )
@@ -209,34 +216,36 @@ func TestRedownloadRemote(t *testing.T) {
209
216
cacheOpt := WithCacheDir (t .TempDir ())
210
217
211
218
t .Run ("digest-less" , func (t * testing.T ) {
219
+ ctx := t .Context ()
212
220
remoteFile := filepath .Join (remoteDir , "digest-less.txt" )
213
221
assert .NilError (t , os .WriteFile (remoteFile , []byte ("digest-less" ), 0o644 ))
214
222
assert .NilError (t , os .Chtimes (remoteFile , time .Now (), time .Now ().Add (- time .Hour )))
215
223
opt := []Opt {cacheOpt }
216
224
217
225
// 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 ... )
219
227
assert .NilError (t , err )
220
228
assert .Equal (t , StatusDownloaded , r .Status )
221
229
222
230
// 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 ... )
224
232
assert .NilError (t , err )
225
233
assert .Equal (t , StatusUsedCache , r .Status )
226
234
227
235
// Modifying remote file will cause redownload
228
236
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 ... )
230
238
assert .NilError (t , err )
231
239
assert .Equal (t , StatusDownloaded , r .Status )
232
240
233
241
// 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 ... )
235
243
assert .NilError (t , err )
236
244
assert .Equal (t , StatusUsedCache , r .Status )
237
245
})
238
246
239
247
t .Run ("has-digest" , func (t * testing.T ) {
248
+ ctx := t .Context ()
240
249
remoteFile := filepath .Join (remoteDir , "has-digest.txt" )
241
250
bytes := []byte ("has-digest" )
242
251
assert .NilError (t , os .WriteFile (remoteFile , bytes , 0o644 ))
@@ -247,16 +256,16 @@ func TestRedownloadRemote(t *testing.T) {
247
256
assert .NilError (t , err )
248
257
opt := []Opt {cacheOpt , WithExpectedDigest (digester .Digest ())}
249
258
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 ... )
251
260
assert .NilError (t , err )
252
261
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 ... )
254
263
assert .NilError (t , err )
255
264
assert .Equal (t , StatusUsedCache , r .Status )
256
265
257
266
// modifying remote file won't cause redownload because expected digest is provided
258
267
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 ... )
260
269
assert .NilError (t , err )
261
270
assert .Equal (t , StatusUsedCache , r .Status )
262
271
})
@@ -274,12 +283,13 @@ func TestDownloadLocal(t *testing.T) {
274
283
t .Cleanup (func () { _ = f .Close () })
275
284
testLocalFileURL := "file://" + localFile
276
285
277
- r , err := Download (context . Background (), localPath , testLocalFileURL )
286
+ r , err := Download (t . Context (), localPath , testLocalFileURL )
278
287
assert .NilError (t , err )
279
288
assert .Equal (t , StatusDownloaded , r .Status )
280
289
})
281
290
282
291
t .Run ("with file digest" , func (t * testing.T ) {
292
+ ctx := t .Context ()
283
293
localPath := filepath .Join (t .TempDir (), t .Name ())
284
294
localTestFile := filepath .Join (t .TempDir (), "some-file" )
285
295
testDownloadFileContents := []byte ("TestDownloadLocal" )
@@ -288,10 +298,10 @@ func TestDownloadLocal(t *testing.T) {
288
298
testLocalFileURL := "file://" + localTestFile
289
299
wrongDigest := digest .Digest (emptyFileDigest )
290
300
291
- _ , err := Download (context . Background () , localPath , testLocalFileURL , WithExpectedDigest (wrongDigest ))
301
+ _ , err := Download (ctx , localPath , testLocalFileURL , WithExpectedDigest (wrongDigest ))
292
302
assert .ErrorContains (t , err , "expected digest" )
293
303
294
- r , err := Download (context . Background () , localPath , testLocalFileURL , WithExpectedDigest (testDownloadLocalDigest ))
304
+ r , err := Download (ctx , localPath , testLocalFileURL , WithExpectedDigest (testDownloadLocalDigest ))
295
305
assert .NilError (t , err )
296
306
assert .Equal (t , StatusDownloaded , r .Status )
297
307
})
@@ -325,7 +335,7 @@ func TestDownloadCompressed(t *testing.T) {
325
335
localFile += ".gz"
326
336
testLocalFileURL := "file://" + localFile
327
337
328
- r , err := Download (context . Background () , localPath , testLocalFileURL , WithDecompress (true ))
338
+ r , err := Download (ctx , localPath , testLocalFileURL , WithDecompress (true ))
329
339
assert .NilError (t , err )
330
340
assert .Equal (t , StatusDownloaded , r .Status )
331
341
@@ -344,7 +354,7 @@ func TestDownloadCompressed(t *testing.T) {
344
354
localFile += ".bz2"
345
355
testLocalFileURL := "file://" + localFile
346
356
347
- r , err := Download (context . Background () , localPath , testLocalFileURL , WithDecompress (true ))
357
+ r , err := Download (ctx , localPath , testLocalFileURL , WithDecompress (true ))
348
358
assert .NilError (t , err )
349
359
assert .Equal (t , StatusDownloaded , r .Status )
350
360
@@ -360,7 +370,7 @@ func TestDownloadCompressed(t *testing.T) {
360
370
assert .NilError (t , os .WriteFile (localFile , testDownloadCompressedContents , 0o644 ))
361
371
testLocalFileURL := "file://" + localFile
362
372
363
- r , err := Download (context . Background (), localPath , testLocalFileURL , WithDecompress (true ))
373
+ r , err := Download (t . Context (), localPath , testLocalFileURL , WithDecompress (true ))
364
374
assert .NilError (t , err )
365
375
assert .Equal (t , StatusDownloaded , r .Status )
366
376
0 commit comments