Skip to content

Commit 0e0240e

Browse files
committed
Fix file URLs with digest
Fixes #1217 Test skipped on windows for now. Signed-off-by: Pris Nasrat <[email protected]>
1 parent 25b137d commit 0e0240e

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

pkg/downloader/downloader.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,15 @@ func canonicalLocalPath(s string) (string, error) {
243243
}
244244

245245
func copyLocal(dst, src string, expectedDigest digest.Digest) error {
246-
if err := validateLocalFileDigest(src, expectedDigest); err != nil {
247-
return err
248-
}
249246
srcPath, err := canonicalLocalPath(src)
250247
if err != nil {
251248
return err
252249
}
250+
251+
if err := validateLocalFileDigest(srcPath, expectedDigest); err != nil {
252+
return err
253+
}
254+
253255
if dst == "" {
254256
// empty dst means caching-only mode
255257
return nil

pkg/downloader/downloader_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package downloader
22

33
import (
4+
"io/ioutil"
5+
"os"
46
"path/filepath"
7+
"runtime"
58
"testing"
69

710
"github.com/opencontainers/go-digest"
@@ -80,3 +83,45 @@ func TestDownloadRemote(t *testing.T) {
8083
assert.Equal(t, StatusUsedCache, r.Status)
8184
})
8285
}
86+
87+
func TestDownloadLocal(t *testing.T) {
88+
89+
if runtime.GOOS == "windows" {
90+
// FIXME: `TempDir RemoveAll cleanup: remove C:\users\runner\Temp\TestDownloadLocalwithout_digest2738386858\002\test-file: Sharing violation.`
91+
t.Skip("Skipping on windows")
92+
}
93+
94+
const emptyFileDigest = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
95+
const testDownloadLocalDigest = "sha256:0c1e0fba69e8919b306d030bf491e3e0c46cf0a8140ff5d7516ba3a83cbea5b3"
96+
97+
t.Run("without digest", func(t *testing.T) {
98+
localPath := filepath.Join(t.TempDir(), t.Name())
99+
localFile := filepath.Join(t.TempDir(), "test-file")
100+
os.Create(localFile)
101+
testLocalFileURL := "file://" + localFile
102+
103+
r, err := Download(localPath, testLocalFileURL)
104+
assert.NilError(t, err)
105+
assert.Equal(t, StatusDownloaded, r.Status)
106+
})
107+
108+
t.Run("with file digest", func(t *testing.T) {
109+
localPath := filepath.Join(t.TempDir(), t.Name())
110+
localTestFile := filepath.Join(t.TempDir(), "some-file")
111+
testDownloadFileContents := []byte("TestDownloadLocal")
112+
113+
ioutil.WriteFile(localTestFile, testDownloadFileContents, 0644)
114+
testLocalFileURL := "file://" + localTestFile
115+
wrongDigest := digest.Digest(emptyFileDigest)
116+
117+
_, err := Download(localPath, testLocalFileURL, WithExpectedDigest(wrongDigest))
118+
assert.ErrorContains(t, err, "expected digest")
119+
120+
r, err := Download(localPath, testLocalFileURL, WithExpectedDigest(testDownloadLocalDigest))
121+
assert.NilError(t, err)
122+
assert.Equal(t, StatusDownloaded, r.Status)
123+
124+
os.Remove(localTestFile)
125+
})
126+
127+
}

0 commit comments

Comments
 (0)