From 2ae83f276b60085c550fc81fb6e21b38146e2d9f Mon Sep 17 00:00:00 2001 From: Martin Hickey Date: Mon, 18 May 2020 18:06:09 +0000 Subject: [PATCH 1/2] Fix repo cache setting Fix `repo add` and `repo update` to use a repository cache set using `--repository-cache` flag Signed-off-by: Martin Hickey Signed-off-by: Trond Hindenes --- cmd/helm/repo_add.go | 3 +++ cmd/helm/repo_update.go | 9 +++++++-- cmd/helm/repo_update_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/cmd/helm/repo_add.go b/cmd/helm/repo_add.go index 3d36fd0ed..9c67641e5 100644 --- a/cmd/helm/repo_add.go +++ b/cmd/helm/repo_add.go @@ -130,6 +130,9 @@ func (o *repoAddOptions) run(out io.Writer) error { return err } + if o.repoCache != "" { + r.CachePath = o.repoCache + } if _, err := r.DownloadIndexFile(); err != nil { return errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", o.url) } diff --git a/cmd/helm/repo_update.go b/cmd/helm/repo_update.go index 027f18518..5c1086e81 100644 --- a/cmd/helm/repo_update.go +++ b/cmd/helm/repo_update.go @@ -37,8 +37,9 @@ Information is cached locally, where it is used by commands like 'helm search'. var errNoRepositories = errors.New("no repositories found. You must add one before updating") type repoUpdateOptions struct { - update func([]*repo.ChartRepository, io.Writer) - repoFile string + update func([]*repo.ChartRepository, io.Writer) + repoFile string + repoCache string } func newRepoUpdateCmd(out io.Writer) *cobra.Command { @@ -52,6 +53,7 @@ func newRepoUpdateCmd(out io.Writer) *cobra.Command { Args: require.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { o.repoFile = settings.RepositoryConfig + o.repoCache = settings.RepositoryCache return o.run(out) }, } @@ -69,6 +71,9 @@ func (o *repoUpdateOptions) run(out io.Writer) error { if err != nil { return err } + if o.repoCache != "" { + r.CachePath = o.repoCache + } repos = append(repos, r) } diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 6ddce0637..b13d575e2 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -19,6 +19,8 @@ import ( "bytes" "fmt" "io" + "io/ioutil" + "path/filepath" "strings" "testing" @@ -79,3 +81,34 @@ func TestUpdateCharts(t *testing.T) { t.Error("Update was not successful") } } + +func TestUpdateChartsCustomCache(t *testing.T) { + defer resetEnv()() + defer ensure.HelmHome(t)() + + ts, err := repotest.NewTempServer("testdata/testserver/*.*") + if err != nil { + t.Fatal(err) + } + defer ts.Stop() + + r, err := repo.NewChartRepository(&repo.Entry{ + Name: "charts", + URL: ts.URL(), + }, getter.All(settings)) + if err != nil { + t.Error(err) + } + + cachePath := ensure.TempDir(t) + cacheFile := filepath.Join(cachePath, "charts-index.yaml") + r.CachePath = cachePath + + b := bytes.NewBuffer(nil) + updateCharts([]*repo.ChartRepository{r}, b) + + _, err = ioutil.ReadFile(cacheFile) + if err != nil { + t.Error(err) + } +} From 5600a2c82d236422e54b6089d5384dea44349900 Mon Sep 17 00:00:00 2001 From: Martin Hickey Date: Tue, 19 May 2020 15:45:14 +0000 Subject: [PATCH 2/2] Fix unit test Signed-off-by: Martin Hickey --- cmd/helm/repo_update_test.go | 52 ++++++++++++++---------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index b13d575e2..981f0bba3 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -19,7 +19,7 @@ import ( "bytes" "fmt" "io" - "io/ioutil" + "os" "path/filepath" "strings" "testing" @@ -52,6 +52,25 @@ func TestUpdateCmd(t *testing.T) { } } +func TestUpdateCustomCacheCmd(t *testing.T) { + var out bytes.Buffer + rootDir := ensure.TempDir(t) + cachePath := filepath.Join(rootDir, "updcustomcache") + _ = os.Mkdir(cachePath, os.ModePerm) + defer os.RemoveAll(cachePath) + o := &repoUpdateOptions{ + update: updateCharts, + repoFile: "testdata/repositories.yaml", + repoCache: cachePath, + } + if err := o.run(&out); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(filepath.Join(cachePath, "charts-index.yaml")); err != nil { + t.Fatalf("error finding created index file in custom cache: %#v", err) + } +} + func TestUpdateCharts(t *testing.T) { defer resetEnv()() defer ensure.HelmHome(t)() @@ -81,34 +100,3 @@ func TestUpdateCharts(t *testing.T) { t.Error("Update was not successful") } } - -func TestUpdateChartsCustomCache(t *testing.T) { - defer resetEnv()() - defer ensure.HelmHome(t)() - - ts, err := repotest.NewTempServer("testdata/testserver/*.*") - if err != nil { - t.Fatal(err) - } - defer ts.Stop() - - r, err := repo.NewChartRepository(&repo.Entry{ - Name: "charts", - URL: ts.URL(), - }, getter.All(settings)) - if err != nil { - t.Error(err) - } - - cachePath := ensure.TempDir(t) - cacheFile := filepath.Join(cachePath, "charts-index.yaml") - r.CachePath = cachePath - - b := bytes.NewBuffer(nil) - updateCharts([]*repo.ChartRepository{r}, b) - - _, err = ioutil.ReadFile(cacheFile) - if err != nil { - t.Error(err) - } -}