Commit 3953f0e8 authored by adshmh's avatar adshmh Committed by Matthew Fisher
Browse files

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: default avatarArash Deshmeh <adeshmeh@ca.ibm.com>
Showing with 34 additions and 0 deletions
+34 -0
......@@ -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)
}
......
......@@ -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)
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment