|
1 | 1 | package downloader
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "io/ioutil" |
| 5 | + "os" |
4 | 6 | "path/filepath"
|
| 7 | + "runtime" |
5 | 8 | "testing"
|
6 | 9 |
|
7 | 10 | "github.com/opencontainers/go-digest"
|
@@ -80,3 +83,45 @@ func TestDownloadRemote(t *testing.T) {
|
80 | 83 | assert.Equal(t, StatusUsedCache, r.Status)
|
81 | 84 | })
|
82 | 85 | }
|
| 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