feat(chart_downloader): Use cached chart if possible in chart_downloader.go

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
pull/13213/head
Suleiman Dibirov 1 year ago
parent 717529a7e5
commit a3a756eced

@ -99,21 +99,26 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
c.Options = append(c.Options, getter.WithAcceptHeader("application/gzip,application/octet-stream")) c.Options = append(c.Options, getter.WithAcceptHeader("application/gzip,application/octet-stream"))
data, err := g.Get(u.String(), c.Options...)
if err != nil {
return "", nil, err
}
name := filepath.Base(u.Path) name := filepath.Base(u.Path)
if u.Scheme == registry.OCIScheme { if u.Scheme == registry.OCIScheme {
idx := strings.LastIndexByte(name, ':') idx := strings.LastIndexByte(name, ':')
name = fmt.Sprintf("%s-%s.tgz", name[:idx], name[idx+1:]) name = fmt.Sprintf("%s-%s.tgz", name[:idx], name[idx+1:])
} }
destfile := filepath.Join(dest, name) destfile := filepath.Join(dest, name)
// If the file does not exist locally, fetch it from the remote source.
if _, err := os.Stat(destfile); err != nil {
data, err := g.Get(u.String(), c.Options...)
if err != nil {
return "", nil, err
}
if err := fileutil.AtomicWriteFile(destfile, data, 0644); err != nil { if err := fileutil.AtomicWriteFile(destfile, data, 0644); err != nil {
return destfile, nil, err return destfile, nil, err
} }
}
// If provenance is requested, verify it. // If provenance is requested, verify it.
ver := &provenance.Verification{} ver := &provenance.Verification{}

@ -197,7 +197,8 @@ func TestDownloadTo(t *testing.T) {
}, },
} }
cname := "/signtest-0.1.0.tgz" cname := "/signtest-0.1.0.tgz"
dest := srv.Root() dest := filepath.Join(srv.Root(), "signtest")
os.MkdirAll(dest, 0755)
where, v, err := c.DownloadTo(srv.URL()+cname, "", dest) where, v, err := c.DownloadTo(srv.URL()+cname, "", dest)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -216,6 +217,35 @@ func TestDownloadTo(t *testing.T) {
} }
} }
func TestDownloadToWithCachedChart(t *testing.T) {
c := ChartDownloader{
Out: os.Stderr,
Verify: VerifyNever,
Keyring: "testdata/helm-test-key.pub",
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
Getters: getter.All(&cli.EnvSettings{
RepositoryConfig: repoConfig,
RepositoryCache: repoCache,
}),
}
dest := t.TempDir()
cname := "signtest-0.1.0.tgz"
os.Create(filepath.Join(dest, cname))
where, _, err := c.DownloadTo("https://localhost/"+cname, "", dest)
if err != nil {
t.Fatal(err)
}
if expect := filepath.Join(dest, cname); where != expect {
t.Errorf("Expected download to %s, got %s", expect, where)
}
if _, err := os.Stat(filepath.Join(dest, cname)); err != nil {
t.Error(err)
}
}
func TestDownloadTo_TLS(t *testing.T) { func TestDownloadTo_TLS(t *testing.T) {
// Set up mock server w/ tls enabled // Set up mock server w/ tls enabled
srv, err := repotest.NewTempServerWithCleanup(t, "testdata/*.tgz*") srv, err := repotest.NewTempServerWithCleanup(t, "testdata/*.tgz*")
@ -248,7 +278,8 @@ func TestDownloadTo_TLS(t *testing.T) {
Options: []getter.Option{}, Options: []getter.Option{},
} }
cname := "test/signtest" cname := "test/signtest"
dest := srv.Root() dest := filepath.Join(srv.Root(), "signtest")
os.MkdirAll(dest, 0755)
where, v, err := c.DownloadTo(cname, "", dest) where, v, err := c.DownloadTo(cname, "", dest)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

Loading…
Cancel
Save