Skip to content

Commit ef0b3a0

Browse files
committed
downloader: add re-download tests
Signed-off-by: Norio Nomura <[email protected]>
1 parent 32fa635 commit ef0b3a0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

pkg/downloader/downloader_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,69 @@ func TestDownloadRemote(t *testing.T) {
121121
})
122122
}
123123

124+
func TestRedownloadRemote(t *testing.T) {
125+
remoteDir, err := os.MkdirTemp("", "redownloadRemote")
126+
assert.NilError(t, err)
127+
t.Cleanup(func() { _ = os.RemoveAll(remoteDir) })
128+
ts := httptest.NewServer(http.FileServer(http.Dir(remoteDir)))
129+
t.Cleanup(ts.Close)
130+
131+
cacheDir, err := os.MkdirTemp("", "redownloadCache")
132+
assert.NilError(t, err)
133+
t.Cleanup(func() { _ = os.RemoveAll(cacheDir) })
134+
135+
downloadDir, err := os.MkdirTemp("", "redownloadLocal")
136+
assert.NilError(t, err)
137+
t.Cleanup(func() { _ = os.RemoveAll(downloadDir) })
138+
139+
cacheOpt := WithCacheDir(cacheDir)
140+
141+
t.Run("digest-less", func(t *testing.T) {
142+
remoteFile := filepath.Join(remoteDir, "digest-less.txt")
143+
assert.NilError(t, os.WriteFile(remoteFile, []byte("digest-less"), 0o644))
144+
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now().Add(-time.Hour)))
145+
opt := []Opt{cacheOpt}
146+
147+
r, err := Download(context.Background(), filepath.Join(downloadDir, "digest-less1.txt"), ts.URL+"/digest-less.txt", opt...)
148+
assert.NilError(t, err)
149+
assert.Equal(t, StatusDownloaded, r.Status)
150+
r, err = Download(context.Background(), filepath.Join(downloadDir, "digest-less2.txt"), ts.URL+"/digest-less.txt", opt...)
151+
assert.NilError(t, err)
152+
assert.Equal(t, StatusUsedCache, r.Status)
153+
154+
// modifying remote file will cause redownload
155+
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now()))
156+
r, err = Download(context.Background(), filepath.Join(downloadDir, "digest-less3.txt"), ts.URL+"/digest-less.txt", opt...)
157+
assert.NilError(t, err)
158+
assert.Equal(t, StatusDownloaded, r.Status)
159+
})
160+
161+
t.Run("has-digest", func(t *testing.T) {
162+
remoteFile := filepath.Join(remoteDir, "has-digest.txt")
163+
bytes := []byte("has-digest")
164+
assert.NilError(t, os.WriteFile(remoteFile, bytes, 0o644))
165+
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now().Add(-time.Hour)))
166+
167+
digester := digest.SHA256.Digester()
168+
_, err := digester.Hash().Write(bytes)
169+
assert.NilError(t, err)
170+
opt := []Opt{cacheOpt, WithExpectedDigest(digester.Digest())}
171+
172+
r, err := Download(context.Background(), filepath.Join(downloadDir, "has-digest1.txt"), ts.URL+"/has-digest.txt", opt...)
173+
assert.NilError(t, err)
174+
assert.Equal(t, StatusDownloaded, r.Status)
175+
r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest2.txt"), ts.URL+"/has-digest.txt", opt...)
176+
assert.NilError(t, err)
177+
assert.Equal(t, StatusUsedCache, r.Status)
178+
179+
// modifying remote file won't cause redownload because expected digest is provided
180+
assert.NilError(t, os.Chtimes(remoteFile, time.Now(), time.Now()))
181+
r, err = Download(context.Background(), filepath.Join(downloadDir, "has-digest3.txt"), ts.URL+"/has-digest.txt", opt...)
182+
assert.NilError(t, err)
183+
assert.Equal(t, StatusUsedCache, r.Status)
184+
})
185+
}
186+
124187
func TestDownloadLocal(t *testing.T) {
125188
const emptyFileDigest = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
126189
const testDownloadLocalDigest = "sha256:0c1e0fba69e8919b306d030bf491e3e0c46cf0a8140ff5d7516ba3a83cbea5b3"

0 commit comments

Comments
 (0)