Skip to content

Commit

Permalink
Remove old cached bundle on refresh
Browse files Browse the repository at this point in the history
When we refresh the cached files for a bundle,
delete the old directory so that any files that would not be
overwritten, for example the updated bundle doesn't have a relocation
mapping file or manifest, are removed.
  • Loading branch information
carolynvs-msft committed Apr 16, 2020
1 parent affe1e1 commit 91c8adc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func (c *Cache) StoreBundle(bundleTag string, bun bundle.Bundle, reloMap *reloca
}
cb.SetCacheDir(cacheDir)

// Remove any previously cached bundle files
err = c.FileSystem.RemoveAll(cb.cacheDir)
if err != nil {
return CachedBundle{}, errors.Wrapf(err, "cannot remove existing cache directory %s", cb.BundlePath)
}

cb.BundlePath = cb.BuildBundlePath()
err = c.FileSystem.MkdirAll(filepath.Dir(cb.BundlePath), os.ModePerm)
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,36 @@ func TestStoreManifest(t *testing.T) {
})
}
}

func TestCache_StoreBundle_Overwrite(t *testing.T) {
cfg := config.NewTestConfig(t)
home, _ := cfg.Config.GetHomeDir()
cacheDir := filepath.Join(home, "cache")
cfg.TestContext.AddTestDirectory("testdata", cacheDir)
c := New(cfg.Config)

// Setup an existing bundle with some extraneous junk that would not
// be overwritten
cb := CachedBundle{Tag: kahn1dot01}
cb.SetCacheDir(cacheDir)
cfg.FileSystem.Create(cb.BuildManifestPath())
cfg.FileSystem.Create(cb.BuildRelocationFilePath())
junkPath := filepath.Join(cb.cacheDir, "junk.txt")
cfg.FileSystem.Create(junkPath)

// Refresh the cache
cb, err := c.StoreBundle(cb.Tag, bundle.Bundle{}, nil)
require.NoError(t, err, "StoreBundle failed")

exists, _ := cfg.FileSystem.Exists(cb.BuildBundlePath())
assert.True(t, exists, "bundle.json should have been written in the refreshed cache")

exists, _ = cfg.FileSystem.Exists(cb.BuildManifestPath())
assert.False(t, exists, "porter.yaml should have been deleted from the bundle cache")

exists, _ = cfg.FileSystem.Exists(cb.BuildRelocationFilePath())
assert.False(t, exists, "relocation-mapping.json should have been deleted from the bundle cache")

exists, _ = cfg.FileSystem.Exists(junkPath)
assert.False(t, exists, "the random file should have been deleted from the bundle cache")
}

0 comments on commit 91c8adc

Please sign in to comment.