mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-12-06 07:34:00 +00:00
fix(scanner): remove scanner temp files after every scan
A scanner binary that its timeout kills cannot remove its own temp files. A killed trivy left its database download in the temp dir as trivy-<n> and getter<n>, up to 279 MB for each scan. Concurrent rescans filled the scanner temp dir, and the disk guard then refused all new scans. Each scanner run now gets a private dir in the temp dir, and the scanner binary gets this dir as TMPDIR. The scan removes the dir and its contents when the scan ends, also after a timeout or an error. Each new scan also removes abandoned dirs that are older than one hour. These dirs come from a process that stopped during a scan, or from a removal that failed.
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package workdir
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func entries(t *testing.T, dir string) []string {
|
||||
t.Helper()
|
||||
list, err := os.ReadDir(dir)
|
||||
require.NoError(t, err)
|
||||
names := make([]string, 0, len(list))
|
||||
for _, e := range list {
|
||||
names = append(names, e.Name())
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func TestRemoveDeletesWhatTheScannerLeft(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
t.Setenv("TMPDIR", tmp)
|
||||
|
||||
wd, err := New("trivy")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tmp, filepath.Dir(wd.Path()))
|
||||
assert.Equal(t, "TMPDIR="+wd.Path(), wd.Env([]string{"TMPDIR=/tmp"})[1])
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(wd.Path(), "trivy-1", "db"), 0o700))
|
||||
require.NoError(t, os.WriteFile(filepath.Join(wd.Path(), "trivy-1", "db", "trivy.db"), []byte("x"), 0o600))
|
||||
|
||||
wd.Remove()
|
||||
|
||||
assert.Empty(t, entries(t, tmp))
|
||||
}
|
||||
|
||||
func TestSweepRemovesOnlyAbandonedDirs(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
t.Setenv("TMPDIR", tmp)
|
||||
old := time.Now().Add(-2 * abandonedAfter)
|
||||
mk := func(name string, mtime time.Time) {
|
||||
path := filepath.Join(tmp, name)
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(path, "x"), 0o700))
|
||||
require.NoError(t, os.Chtimes(path, mtime, mtime))
|
||||
}
|
||||
mk(prefix+"trivy-1", old)
|
||||
mk(prefix+"npm-audit-2", time.Now())
|
||||
mk("trivy-3", old)
|
||||
live, err := New("grype")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Chtimes(live.Path(), old, old))
|
||||
|
||||
sweep(time.Now().Add(-abandonedAfter))
|
||||
|
||||
assert.ElementsMatch(t, []string{prefix + "npm-audit-2", "trivy-3", filepath.Base(live.Path())}, entries(t, tmp))
|
||||
live.Remove()
|
||||
}
|
||||
|
||||
func TestSweepRetriesADirWhoseRemoveFailed(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
t.Setenv("TMPDIR", tmp)
|
||||
wd, err := New("trivy")
|
||||
require.NoError(t, err)
|
||||
locked := filepath.Join(wd.Path(), "locked")
|
||||
require.NoError(t, os.MkdirAll(filepath.Join(locked, "f"), 0o700))
|
||||
require.NoError(t, os.Chmod(locked, 0o500)) // #nosec G302 -- read-only on purpose
|
||||
t.Cleanup(func() { _ = os.Chmod(locked, 0o700) }) // #nosec G302 -- test dir
|
||||
|
||||
wd.Remove()
|
||||
require.NoError(t, os.Chmod(locked, 0o700)) // #nosec G302 -- test dir
|
||||
old := time.Now().Add(-2 * abandonedAfter)
|
||||
require.NoError(t, os.Chtimes(wd.Path(), old, old))
|
||||
sweep(time.Now().Add(-abandonedAfter))
|
||||
|
||||
assert.Empty(t, entries(t, tmp))
|
||||
}
|
||||
|
||||
func TestConcurrentScansLeaveNothing(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
t.Setenv("TMPDIR", tmp)
|
||||
var wg sync.WaitGroup
|
||||
for range 16 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
wd, err := New("npm-audit")
|
||||
if !assert.NoError(t, err) {
|
||||
return
|
||||
}
|
||||
assert.NoError(t, os.WriteFile(filepath.Join(wd.Path(), "f"), []byte("x"), 0o600))
|
||||
wd.Remove()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
assert.Empty(t, entries(t, tmp))
|
||||
}
|
||||
Reference in New Issue
Block a user