diff --git a/pkg/downloader/downloader_test.go b/pkg/downloader/downloader_test.go index 1173df50a63..d2f28d02c5f 100644 --- a/pkg/downloader/downloader_test.go +++ b/pkg/downloader/downloader_test.go @@ -4,7 +4,6 @@ package downloader import ( - "context" "net/http" "net/http/httptest" "os" @@ -44,26 +43,26 @@ func TestDownloadRemote(t *testing.T) { t.Run("without cache", func(t *testing.T) { t.Run("without digest", func(t *testing.T) { localPath := filepath.Join(t.TempDir(), t.Name()) - r, err := Download(context.Background(), localPath, dummyRemoteFileURL) + r, err := Download(t.Context(), localPath, dummyRemoteFileURL) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) // download again, make sure StatusSkippedIsReturned - r, err = Download(context.Background(), localPath, dummyRemoteFileURL) + r, err = Download(t.Context(), localPath, dummyRemoteFileURL) assert.NilError(t, err) assert.Equal(t, StatusSkipped, r.Status) }) t.Run("with digest", func(t *testing.T) { wrongDigest := digest.Digest("sha256:8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9") localPath := filepath.Join(t.TempDir(), t.Name()) - _, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest)) + _, err := Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(wrongDigest)) assert.ErrorContains(t, err, "expected digest") - r, err := Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) + r, err := Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) - r, err = Download(context.Background(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) + r, err = Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) assert.NilError(t, err) assert.Equal(t, StatusSkipped, r.Status) }) @@ -72,18 +71,18 @@ func TestDownloadRemote(t *testing.T) { t.Run("serial", func(t *testing.T) { cacheDir := filepath.Join(t.TempDir(), "cache") localPath := filepath.Join(t.TempDir(), t.Name()) - r, err := Download(context.Background(), localPath, dummyRemoteFileURL, + r, err := Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) - r, err = Download(context.Background(), localPath, dummyRemoteFileURL, + r, err = Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusSkipped, r.Status) localPath2 := localPath + "-2" - r, err = Download(context.Background(), localPath2, dummyRemoteFileURL, + r, err = Download(t.Context(), localPath2, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) @@ -95,7 +94,7 @@ func TestDownloadRemote(t *testing.T) { go func() { // Parallel download is supported only for different instances with unique localPath. localPath := filepath.Join(t.TempDir(), t.Name()) - r, err := Download(context.Background(), localPath, dummyRemoteFileURL, + r, err := Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) results <- downloadResult{r, err} }() @@ -108,22 +107,22 @@ func TestDownloadRemote(t *testing.T) { }) t.Run("caching-only mode", func(t *testing.T) { t.Run("serial", func(t *testing.T) { - _, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) + _, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) assert.ErrorContains(t, err, "cache directory to be specified") cacheDir := filepath.Join(t.TempDir(), "cache") - r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), + r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) - r, err = Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), + r, err = Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) localPath := filepath.Join(t.TempDir(), t.Name()) - r, err = Download(context.Background(), localPath, dummyRemoteFileURL, + r, err = Download(t.Context(), localPath, dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) @@ -133,7 +132,7 @@ func TestDownloadRemote(t *testing.T) { results := make(chan downloadResult, parallelDownloads) for range parallelDownloads { go func() { - r, err := Download(context.Background(), "", dummyRemoteFileURL, + r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) results <- downloadResult{r, err} }() @@ -149,7 +148,7 @@ func TestDownloadRemote(t *testing.T) { assert.ErrorContains(t, err, "cache directory to be specified") cacheDir := filepath.Join(t.TempDir(), "cache") - r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) + r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) @@ -167,7 +166,7 @@ func TestDownloadRemote(t *testing.T) { assert.ErrorContains(t, err, "cache directory to be specified") cacheDir := filepath.Join(t.TempDir(), "cache") - r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) + r, err := Download(t.Context(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) assert.Equal(t, dummyRemoteFileStat.ModTime().Truncate(time.Second).UTC(), r.LastModified) @@ -211,23 +210,23 @@ func TestRedownloadRemote(t *testing.T) { opt := []Opt{cacheOpt} // Download on the first call - r, err := Download(context.Background(), filepath.Join(downloadDir, "1"), ts.URL+"/digest-less.txt", opt...) + r, err := Download(t.Context(), filepath.Join(downloadDir, "1"), ts.URL+"/digest-less.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) // Next download will use the cached download - r, err = Download(context.Background(), filepath.Join(downloadDir, "2"), ts.URL+"/digest-less.txt", opt...) + r, err = Download(t.Context(), filepath.Join(downloadDir, "2"), ts.URL+"/digest-less.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) // Modifying remote file will cause redownload assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now())) - r, err = Download(context.Background(), filepath.Join(downloadDir, "3"), ts.URL+"/digest-less.txt", opt...) + r, err = Download(t.Context(), filepath.Join(downloadDir, "3"), ts.URL+"/digest-less.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) // Next download will use the cached download - r, err = Download(context.Background(), filepath.Join(downloadDir, "4"), ts.URL+"/digest-less.txt", opt...) + r, err = Download(t.Context(), filepath.Join(downloadDir, "4"), ts.URL+"/digest-less.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) }) @@ -243,16 +242,16 @@ func TestRedownloadRemote(t *testing.T) { assert.NilError(t, err) opt := []Opt{cacheOpt, WithExpectedDigest(digester.Digest())} - r, err := Download(context.Background(), filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...) + r, err := Download(t.Context(), filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) - r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...) + r, err = Download(t.Context(), filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) // modifying remote file won't cause redownload because expected digest is provided assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now())) - r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...) + r, err = Download(t.Context(), filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...) assert.NilError(t, err) assert.Equal(t, StatusUsedCache, r.Status) }) @@ -270,7 +269,7 @@ func TestDownloadLocal(t *testing.T) { t.Cleanup(func() { _ = f.Close() }) testLocalFileURL := "file://" + localFile - r, err := Download(context.Background(), localPath, testLocalFileURL) + r, err := Download(t.Context(), localPath, testLocalFileURL) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) }) @@ -284,10 +283,10 @@ func TestDownloadLocal(t *testing.T) { testLocalFileURL := "file://" + localTestFile wrongDigest := digest.Digest(emptyFileDigest) - _, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(wrongDigest)) + _, err := Download(t.Context(), localPath, testLocalFileURL, WithExpectedDigest(wrongDigest)) assert.ErrorContains(t, err, "expected digest") - r, err := Download(context.Background(), localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest)) + r, err := Download(t.Context(), localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) }) @@ -320,7 +319,7 @@ func TestDownloadCompressed(t *testing.T) { localFile += ".gz" testLocalFileURL := "file://" + localFile - r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true)) + r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) @@ -338,7 +337,7 @@ func TestDownloadCompressed(t *testing.T) { localFile += ".bz2" testLocalFileURL := "file://" + localFile - r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true)) + r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status) @@ -354,7 +353,7 @@ func TestDownloadCompressed(t *testing.T) { assert.NilError(t, os.WriteFile(localFile, testDownloadCompressedContents, 0o644)) testLocalFileURL := "file://" + localFile - r, err := Download(context.Background(), localPath, testLocalFileURL, WithDecompress(true)) + r, err := Download(t.Context(), localPath, testLocalFileURL, WithDecompress(true)) assert.NilError(t, err) assert.Equal(t, StatusDownloaded, r.Status)