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 <adeshmeh@ca.ibm.com>
pull/5252/head
adshmh 7 years ago committed by Matthew Fisher
parent f5df47b1c8
commit 3953f0e884

@ -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)
}
}

Loading…
Cancel
Save