Skip to content

Commit cf20b24

Browse files
committed
Return contents and not path in result
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent e8841f6 commit cf20b24

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

pkg/downloader/downloader.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,20 @@ func WithExpectedDigest(expectedDigest digest.Digest) Opt {
120120
}
121121
}
122122

123+
func readFile(path string) string {
124+
if path == "" {
125+
return ""
126+
}
127+
if _, err := os.Stat(path); err != nil {
128+
return ""
129+
}
130+
b, err := os.ReadFile(path)
131+
if err != nil {
132+
return ""
133+
}
134+
return string(b)
135+
}
136+
123137
// Download downloads the remote resource into the local path.
124138
//
125139
// Download caches the remote resource if WithCache or WithCacheDir option is specified.
@@ -214,8 +228,8 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
214228
res := &Result{
215229
Status: StatusUsedCache,
216230
CachePath: shadData,
217-
LastModified: shadTime,
218-
ContentType: shadType,
231+
LastModified: readFile(shadTime),
232+
ContentType: readFile(shadType),
219233
ValidatedDigest: o.expectedDigest != "",
220234
}
221235
return res, nil
@@ -245,8 +259,8 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
245259
res := &Result{
246260
Status: StatusDownloaded,
247261
CachePath: shadData,
248-
LastModified: shadTime,
249-
ContentType: shadType,
262+
LastModified: readFile(shadTime),
263+
ContentType: readFile(shadType),
250264
ValidatedDigest: o.expectedDigest != "",
251265
}
252266
return res, nil
@@ -295,8 +309,8 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
295309
res := &Result{
296310
Status: StatusUsedCache,
297311
CachePath: shadData,
298-
LastModified: shadTime,
299-
ContentType: shadType,
312+
LastModified: readFile(shadTime),
313+
ContentType: readFile(shadType),
300314
ValidatedDigest: o.expectedDigest != "",
301315
}
302316
return res, nil

pkg/downloader/downloader_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"runtime"
1111
"strings"
1212
"testing"
13+
"time"
1314

1415
"github.com/opencontainers/go-digest"
1516
"gotest.tools/v3/assert"
@@ -25,6 +26,8 @@ func TestDownloadRemote(t *testing.T) {
2526
t.Cleanup(ts.Close)
2627
dummyRemoteFileURL := ts.URL + "/downloader.txt"
2728
const dummyRemoteFileDigest = "sha256:380481d26f897403368be7cb86ca03a4bc14b125bfaf2b93bff809a5a2ad717e"
29+
dummyRemoteFileStat, err := os.Stat(filepath.Join("testdata", "downloader.txt"))
30+
assert.NilError(t, err)
2831

2932
t.Run("without cache", func(t *testing.T) {
3033
t.Run("without digest", func(t *testing.T) {
@@ -105,6 +108,17 @@ func TestDownloadRemote(t *testing.T) {
105108
_, err = Cached(dummyRemoteFileURL, WithExpectedDigest(wrongDigest), WithCacheDir(cacheDir))
106109
assert.ErrorContains(t, err, "expected digest")
107110
})
111+
t.Run("metadata", func(t *testing.T) {
112+
_, err := Cached(dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest))
113+
assert.ErrorContains(t, err, "cache directory to be specified")
114+
115+
cacheDir := filepath.Join(t.TempDir(), "cache")
116+
r, err := Download(context.Background(), "", dummyRemoteFileURL, WithExpectedDigest(dummyRemoteFileDigest), WithCacheDir(cacheDir))
117+
assert.NilError(t, err)
118+
assert.Equal(t, StatusDownloaded, r.Status)
119+
assert.Equal(t, dummyRemoteFileStat.ModTime().UTC().Format(time.RFC1123), strings.Replace(r.LastModified, "GMT", "UTC", 1))
120+
assert.Equal(t, "text/plain; charset=utf-8", r.ContentType)
121+
})
108122
}
109123

110124
func TestDownloadLocal(t *testing.T) {

0 commit comments

Comments
 (0)