dnfjson: Cleanup old distro cache dirs

This adds a function, CleanupOldCacheDirs, that checks the dirs under
/var/cache/osbuild-composer/rpmmd/ and removes files and directories
that don't match the current list of supported distros.

This will clean up the cache from old releases as the are retired, and
will also cleanup the old top level cache directory structure after an
upgrade.

NOTE: This function does not return errors, any real problems it
encounters will also be caught by the cache initialization code and
handled there.
This commit is contained in:
Brian C. Lane 2023-03-02 16:42:16 -08:00 committed by Achilleas Koutsou
parent 8f20b550ea
commit f731ab53d0
3 changed files with 109 additions and 10 deletions

View file

@ -4,6 +4,7 @@ import (
"io/fs"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
@ -171,15 +172,6 @@ func TestCacheRead(t *testing.T) {
}
}
func strSliceContains(slc []string, elem string) bool {
for _, s := range slc {
if elem == s {
return true
}
}
return false
}
func sizeSum(cfg testCache, repoIDFilter ...string) uint64 {
var sum uint64
for path, info := range cfg {
@ -329,3 +321,64 @@ func TestDNFCacheCleanup(t *testing.T) {
_, ok := cache.Get("notreallyahash")
assert.False(t, ok)
}
func TestCleanupOldCacheDirs(t *testing.T) {
// Run the cleanup without the cache present and with dummy distro names
CleanupOldCacheDirs("/var/tmp/test-no-cache-rpmmd/", []string{"fedora-37", "fedora-38"})
testCacheRoot := t.TempDir()
// Make all the test caches under root, using their keys as a distro name.
var distros []string
for name, cfg := range testCfgs {
// Cache is now per-distro, use the name of the config as a distro name
createTestCache(filepath.Join(testCacheRoot, name), cfg)
distros = append(distros, name)
}
sort.Strings(distros)
// Add the content of the 'fake-real' cache to the top directory
// this will be used to simulate an old cache without distro subdirs
createTestCache(testCacheRoot, testCfgs["fake-real"])
CleanupOldCacheDirs(testCacheRoot, distros)
// The fake-real files under the root directory should all be gone.
for path := range testCfgs["fake-real"] {
_, err := os.Stat(filepath.Join(testCacheRoot, path))
assert.NotNil(t, err)
}
// The distro cache files should all still be present
for name, cfg := range testCfgs {
for path := range cfg {
_, err := os.Stat(filepath.Join(testCacheRoot, name, path))
assert.Nil(t, err)
}
}
// Remove the fake-real distro from the list
// This simulates retiring an older distribution and cleaning up its cache
distros = []string{}
for name := range testCfgs {
if name == "fake-real" {
continue
}
distros = append(distros, name)
}
// Cleanup should now remove the fake-real subdirectory and files
CleanupOldCacheDirs(testCacheRoot, distros)
// The remaining distro's cache files should all still be present
for _, name := range distros {
for path := range testCfgs[name] {
_, err := os.Stat(filepath.Join(testCacheRoot, name, path))
assert.Nil(t, err)
}
}
// But the fake-real ones should be gone
for path := range testCfgs["fake-real"] {
_, err := os.Stat(filepath.Join(testCacheRoot, "fake-real", path))
assert.NotNil(t, err)
}
}