Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
clean chart cache dir on error processing (#281)
Browse files Browse the repository at this point in the history
* clean chart cache dir on error processing

* improve coverage

* address review

* use real function for clearChartDataDir

* fix defer approach
  • Loading branch information
prydonius authored Jun 21, 2017
1 parent 3f5bf20 commit 5cf88bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/api/data/cache/charthelper/chart_package_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ const defaultTimeout time.Duration = 10 * time.Second

// DownloadAndExtractChartTarball the chart tar file linked by metadata.Urls and store
// the wanted files (i.e README.md) under chartDataDir
var DownloadAndExtractChartTarball = func(chart *models.ChartPackage) error {
var DownloadAndExtractChartTarball = func(chart *models.ChartPackage) (err error) {
if err := ensureChartDataDir(chart); err != nil {
return err
}

defer func() {
if err != nil {
cleanChartDataDir(chart)
}
}()

if !tarballExists(chart) {
if err := downloadTarball(chart); err != nil {
return err
Expand Down Expand Up @@ -134,6 +140,10 @@ var ensureChartDataDir = func(chart *models.ChartPackage) error {
return nil
}

func cleanChartDataDir(chart *models.ChartPackage) error {
return os.RemoveAll(chartDataDir(chart))
}

// TarballPath returns the location of the chart package in the local cache
var TarballPath = func(chart *models.ChartPackage) string {
return filepath.Join(chartDataDir(chart), "chart.tgz")
Expand Down
25 changes: 25 additions & 0 deletions src/api/data/cache/charthelper/chart_package_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestDownloadAndExtractChartTarballErrorDownload(t *testing.T) {
assert.NoErr(t, err)
err = DownloadAndExtractChartTarball(chart)
assert.ExistsErr(t, err, "Error downloading the tar file")
_, err = os.Stat(chartDataDir(chart))
assert.ExistsErr(t, err, "chart data dir has been removed")
}

func TestDownloadAndExtractChartTarballErrorExtract(t *testing.T) {
Expand All @@ -54,6 +56,8 @@ func TestDownloadAndExtractChartTarballErrorExtract(t *testing.T) {
assert.NoErr(t, err)
err = DownloadAndExtractChartTarball(chart)
assert.ExistsErr(t, err, "Error extracting tar file content")
_, err = os.Stat(chartDataDir(chart))
assert.ExistsErr(t, err, "chart data dir has been removed")
}

// It creates the tar file in local filesystem
Expand Down Expand Up @@ -200,6 +204,27 @@ func TestEnsureChartDataDir(t *testing.T) {
assert.NoErr(t, err)
}

func TestCleanChartDataDir(t *testing.T) {
chart, err := getTestChart()
assert.NoErr(t, err)
randomPath, _ := ioutil.TempDir(os.TempDir(), "chart")
DataDirBaseOrig := DataDirBase
defer func() { DataDirBase = DataDirBaseOrig }()
DataDirBase = func() string {
return randomPath
}
chartPath := chartDataDir(chart)
ensureChartDataDir(chart)
_, err = os.Stat(chartPath)
assert.NoErr(t, err)

err = cleanChartDataDir(chart)
assert.NoErr(t, err)

_, err = os.Stat(chartPath)
assert.ExistsErr(t, err, "chart dir removed")
}

// Required because DataDirBase has been overriden
var origDataDirBase = DataDirBase()

Expand Down

0 comments on commit 5cf88bd

Please sign in to comment.