|
5 | 5 | "os/exec"
|
6 | 6 | "path/filepath"
|
7 | 7 | "runtime"
|
| 8 | + "strings" |
8 | 9 | "testing"
|
9 | 10 |
|
10 | 11 | "github.com/opencontainers/go-digest"
|
@@ -87,6 +88,24 @@ func TestDownloadRemote(t *testing.T) {
|
87 | 88 | assert.NilError(t, err)
|
88 | 89 | assert.Equal(t, StatusUsedCache, r.Status)
|
89 | 90 | })
|
| 91 | + t.Run("cached", func(t *testing.T) { |
| 92 | + _, err := Cached(dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest)) |
| 93 | + assert.ErrorContains(t, err, "cache directory to be specified") |
| 94 | + |
| 95 | + cacheDir := filepath.Join(t.TempDir(), "cache") |
| 96 | + r, err := Download("", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) |
| 97 | + assert.NilError(t, err) |
| 98 | + assert.Equal(t, StatusDownloaded, r.Status) |
| 99 | + |
| 100 | + r, err = Cached(dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir)) |
| 101 | + assert.NilError(t, err) |
| 102 | + assert.Equal(t, StatusUsedCache, r.Status) |
| 103 | + assert.Assert(t, strings.HasPrefix(r.CachePath, cacheDir), "expected %s to be in %s", r.CachePath, cacheDir) |
| 104 | + |
| 105 | + wrongDigest := digest.Digest("sha256:8313944efb4f38570c689813f288058b674ea6c487017a5a4738dc674b65f9d9") |
| 106 | + _, err = Cached(dummyRemoteFileURL, WithExpectedDigest(wrongDigest), WithCacheDir(cacheDir)) |
| 107 | + assert.ErrorContains(t, err, "expected digest") |
| 108 | + }) |
90 | 109 | }
|
91 | 110 |
|
92 | 111 | func TestDownloadLocal(t *testing.T) {
|
@@ -129,6 +148,15 @@ func TestDownloadLocal(t *testing.T) {
|
129 | 148 | os.Remove(localTestFile)
|
130 | 149 | })
|
131 | 150 |
|
| 151 | + t.Run("cached", func(t *testing.T) { |
| 152 | + localFile := filepath.Join(t.TempDir(), "test-file") |
| 153 | + os.Create(localFile) |
| 154 | + testLocalFileURL := "file://" + localFile |
| 155 | + |
| 156 | + cacheDir := filepath.Join(t.TempDir(), "cache") |
| 157 | + _, err := Cached(testLocalFileURL, WithCacheDir(cacheDir)) |
| 158 | + assert.ErrorContains(t, err, "not cached") |
| 159 | + }) |
132 | 160 | }
|
133 | 161 |
|
134 | 162 | func TestDownloadCompressed(t *testing.T) {
|
|
0 commit comments