Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG-6.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### 2024-02-01

### Changed
- The expandWithDuration in boltdb_manager to have a assetSHA dir cleanup.

### Added
- Added the cleanup in expander to clean the assetSHA in case of asset cache getting deleted

Comment thread
SudhanshuBawane marked this conversation as resolved.
Outdated
### Changed
- Upgraded CI Go version to 1.21.3
- Upgraded jwt version to 4.4.3
Expand Down
11 changes: 11 additions & 0 deletions asset/boltdb_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/viper"
"os"
"path/filepath"

Expand All @@ -23,6 +24,7 @@ const (
// ExpandDuration is the name of the prometheus summary vec used to track
// average latencies of asset expansion.
ExpandDuration = "sensu_go_asset_expand_duration"
FlagCacheDir = "cache-dir"
)

var (
Expand Down Expand Up @@ -241,6 +243,15 @@ func (b *boltDBAssetManager) expandWithDuration(tmpFile *os.File, asset *corev2.
}))
defer timer.ObserveDuration()

assetSHA := asset.Sha512
CacheDir := viper.GetString(FlagCacheDir)
fullPath := filepath.Join(CacheDir, assetSHA)

if err := CleanUp(fullPath); err != nil {
logger.WithField("assetSHA path", fullPath).WithError(err).
Error("error cleaning up the assetSHA")
}

assetPath = filepath.Join(b.localStorage, asset.Sha512)
return assetPath, b.expander.Expand(tmpFile, assetPath)
}
11 changes: 11 additions & 0 deletions asset/expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"os"

archiver "github.com/mholt/archiver/v3"

Expand Down Expand Up @@ -90,3 +91,13 @@ func sniffType(f io.ReadSeeker) (filetype_types.Type, error) {

return ft, nil
}

// cleanup of the assetSHA when cache dir gets force deleted
func CleanUp(fullPath string) error {
errorSHA := os.RemoveAll(fullPath)
if errorSHA != nil {
return errorSHA
}
return nil

}
Comment thread
SudhanshuBawane marked this conversation as resolved.
Outdated
30 changes: 30 additions & 0 deletions asset/expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,33 @@ func TestExpandInvalidArchive(t *testing.T) {
t.Fail()
}
}

// ---Test to check CleanUp
func TestCleanUp(t *testing.T) {
t.Parallel()

// Create a temporary directory for testing
tmpDir := t.TempDir()

// Define the SHA and file name
SHAName := "shaAsset.tar"
SHAFilePath := filepath.Join(tmpDir, SHAName)

// Create a dummy file inside the temporary directory
SHAFile, err := os.Create(SHAFilePath)
if err != nil {
t.Fatalf("Failed to create dummy file: %v", err)
}
SHAFile.Close()

// Call CleanUp with the SHA of the dummy file and the temporary directory
err = CleanUp(SHAFilePath)
if err != nil {
t.Errorf("CleanUp returned an error: %v", err)
}

_, err = os.Stat(SHAFilePath)
if !os.IsNotExist(err) {
t.Errorf("CleanUp did not remove the dummy file as expected")
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only tests os.RemoveAll and is therefore unnecessary