44package downloader
55
66import (
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
0 commit comments