From 3953f0e884afd85a68ab4d8b4488cec735e2bc58 Mon Sep 17 00:00:00 2001 From: adshmh <23505281+adshmh@users.noreply.github.com> Date: Fri, 1 Feb 2019 12:12:03 -0500 Subject: [PATCH] fix(helm): add test for repo update strict flag (#5183) While adding the test, noticed a race in the repo update code, due to multiple go routines potentially incrementing the error counter. Included the required mutex in the repo update code in the same commit, since the new test uncovered the race condition. Signed-off-by: Arash Deshmeh --- cmd/helm/repo_update.go | 7 +++++++ cmd/helm/repo_update_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/cmd/helm/repo_update.go b/cmd/helm/repo_update.go index 526300343..1a239b407 100644 --- a/cmd/helm/repo_update.go +++ b/cmd/helm/repo_update.go @@ -93,21 +93,28 @@ func updateCharts(repos []*repo.ChartRepository, out io.Writer, home helmpath.Ho var ( errorCounter int wg sync.WaitGroup + mu sync.Mutex ) for _, re := range repos { wg.Add(1) go func(re *repo.ChartRepository) { defer wg.Done() if re.Config.Name == localRepository { + mu.Lock() fmt.Fprintf(out, "...Skip %s chart repository\n", re.Config.Name) + mu.Unlock() return } err := re.DownloadIndexFile(home.Cache()) if err != nil { + mu.Lock() errorCounter++ fmt.Fprintf(out, "...Unable to get an update from the %q chart repository (%s):\n\t%s\n", re.Config.Name, re.Config.URL, err) + mu.Unlock() } else { + mu.Lock() fmt.Fprintf(out, "...Successfully got an update from the %q chart repository\n", re.Config.Name) + mu.Unlock() } }(re) } diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 86af437c5..5b1058008 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -105,3 +105,30 @@ func TestUpdateCharts(t *testing.T) { t.Error("Update was not successful") } } + +func TestUpdateCmdStrictFlag(t *testing.T) { + thome, err := tempHelmHome(t) + if err != nil { + t.Fatal(err) + } + + cleanup := resetEnv() + defer func() { + os.RemoveAll(thome.String()) + cleanup() + }() + + settings.Home = thome + + out := bytes.NewBuffer(nil) + cmd := newRepoUpdateCmd(out) + cmd.ParseFlags([]string{"--strict"}) + + if err := cmd.RunE(cmd, []string{}); err == nil { + t.Fatal("expected error due to strict flag") + } + + if got := out.String(); !strings.Contains(got, "Unable to get an update") { + t.Errorf("Expected 'Unable to get an update', got %q", got) + } +}