Files
gohoarder/pkg/scanner/leftovers_test.go
T
lukaszraczylo e4077066ef 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.
2026-12-01 03:11:51 +00:00

64 lines
2.5 KiB
Go

package scanner_test
import (
"context"
"os"
"path/filepath"
"testing"
"time"
"github.com/lukaszraczylo/gohoarder/pkg/config"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/govulncheck"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/grype"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/npmaudit"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/pipaudit"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/scannertest"
"github.com/lukaszraczylo/gohoarder/pkg/scanner/trivy"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type scanFunc func(ctx context.Context, registry, packageName, version, filePath string) (*metadata.ScanResult, error)
// A scanner binary killed by its timeout never removes its own temp files: a killed trivy
// leaves its database download (trivy-<n>, getter<n>, up to 279 MB) in TMPDIR.
func TestScanLeavesNothingInTheTempDir(t *testing.T) {
const leave = `mkdir -p "$TMPDIR/trivy-1/db" "$TMPDIR/getter1" && echo x > "$TMPDIR/trivy-1/db/trivy.db"`
cases := []struct {
scan scanFunc
name, bin, body string
registry, pkg, ver string
}{
{name: "trivy timeout", bin: "trivy", body: leave + "\nexec sleep 5", registry: "pypi", pkg: "p", ver: "1",
scan: trivy.New(config.TrivyConfig{Timeout: time.Second}).Scan},
{name: "grype failure", bin: "grype", body: leave + "\nexit 1", registry: "npm", pkg: "p", ver: "1.0.0",
scan: grype.New(config.GrypeConfig{}).Scan},
{name: "npm audit failure", bin: "npm", body: leave + "\nexit 1", registry: "npm", pkg: "p", ver: "1.0.0",
scan: npmaudit.New(config.NpmAuditConfig{}).Scan},
{name: "pip-audit failure", bin: "pip-audit", body: leave + "\nexit 1", registry: "pypi", pkg: "requests", ver: "2.0.0",
scan: pipaudit.New(config.PipAuditConfig{}).Scan},
{name: "govulncheck go list failure", bin: "go", body: leave + "\nexit 1", registry: "go", pkg: "golang.org/x/text", ver: "v0.3.5",
scan: govulncheck.New(config.GovulncheckConfig{}).Scan},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
scannertest.Script(t, tc.bin, tc.body)
archive := filepath.Join(t.TempDir(), "p.tgz")
require.NoError(t, os.WriteFile(archive, []byte("x"), 0o600))
tmp := t.TempDir()
t.Setenv("TMPDIR", tmp)
_, _ = tc.scan(context.Background(), tc.registry, tc.pkg, tc.ver, archive)
left, err := os.ReadDir(tmp)
require.NoError(t, err)
names := make([]string, 0, len(left))
for _, e := range left {
names = append(names, e.Name())
}
assert.Empty(t, names)
})
}
}