Skip to content

Commit

Permalink
MIC should clean-up cache and any system files after run (#10598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adub17030MS authored Oct 17, 2024
1 parent e57bb7d commit d8a5bb0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
25 changes: 25 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/customizepackages.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ func addRemoveAndUpdatePackages(buildDir string, baseConfigPath string, config *
}
}

if needRpmsSources {
err = cleanTdnfCache(imageChroot)
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -224,3 +231,21 @@ func isPackageInstalled(imageChroot *safechroot.Chroot, packageName string) bool
}
return true
}

func cleanTdnfCache(imageChroot *safechroot.Chroot) error {
logger.Log.Infof("Cleaning up RPM cache")
// Run all cleanup tasks inside the chroot environment
return imageChroot.UnsafeRun(func() error {
tdnfArgs := []string{
"-v", "clean", "all",
}
err := shell.NewExecBuilder("tdnf", tdnfArgs...).
LogLevel(logrus.TraceLevel, logrus.DebugLevel).
ErrorStderrLines(1).
Execute()
if err != nil {
return fmt.Errorf("Failed to clean tdnf cache: %w", err)
}
return nil
})
}
34 changes: 34 additions & 0 deletions toolkit/tools/pkg/imagecustomizerlib/customizepackages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package imagecustomizerlib

import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -185,6 +186,9 @@ func TestCustomizeImagePackagesUpdate(t *testing.T) {
}
defer imageConnection.Close()

// Ensure tdnf cache was cleaned.
ensureTdnfCacheCleanup(t, imageConnection, "/var/cache/tdnf")

// Ensure packages were installed.
ensureFilesExist(t, imageConnection,
"/usr/bin/jq",
Expand All @@ -211,3 +215,33 @@ func TestCustomizeImagePackagesDiskSpace(t *testing.T) {
assert.ErrorContains(t, err, "failed to customize raw image")
assert.ErrorContains(t, err, "failed to install package (gcc)")
}

func ensureTdnfCacheCleanup(t *testing.T, imageConnection *ImageConnection, dirPath string) {
// Array to capture all the files of the provided root directory
var existingFiles []string

// Start the directory walk from the initial dirPath and collect all existing files
fullPath := filepath.Join(imageConnection.Chroot().RootDir(), dirPath)
err := filepath.WalkDir(fullPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("Failed to access path (%s): %w", path, err)
}
// Ignore files in the local-repo folder if the base image version is 2.0
if !(strings.Contains(path, "local-repo") && baseImageVersionDefault == "2.0") {
fileInfo, err := os.Stat(path)
if err != nil {
return fmt.Errorf("failed to get file info for %s: %w", path, err)
}
if !fileInfo.IsDir() {
// Append the file to the existingFiles array
existingFiles = append(existingFiles, path)
}
}
return nil
})

assert.NoError(t, err)

// Ensure the cache has been cleaned up
assert.Equal(t, 0, len(existingFiles), "Expected no file data in cache, but got %d files", len(existingFiles))
}

0 comments on commit d8a5bb0

Please sign in to comment.