Files
gohoarder/pkg/scanner/leftovers_test.go
T
lukaszraczylo f367ce618e fix(scanner): stop trivy scans from updating the database
Each trivy scan updated the vulnerability database when the database was
stale. Six concurrent rescans then downloaded it at the same time. A
download took about 11 seconds alone and 38 seconds under contention.
Five of six trivy processes crashed with SIGBUS, because they read a
database that another process replaced. The other scans hit their timeout.

Scans now run with --skip-db-update. A refresh loop updates the database
every 6 hours (trivy.db_update_interval). Scans hold a read lock and an
update holds the write lock, so an update never replaces the database
under a running scan. A scan that finds no database downloads it once
before it scans.

The grype refresh loop moves to the dbrefresh package, which both scanners
use.
2026-12-01 03:20:49 +00:00

73 lines
2.9 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"
)
// trivyCache returns a trivy cache dir that already holds a database.
func trivyCache(t *testing.T) string {
t.Helper()
dir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(dir, "db"), 0o750))
require.NoError(t, os.WriteFile(filepath.Join(dir, "db", "trivy.db"), nil, 0o600))
return dir
}
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, CacheDB: trivyCache(t)}).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)
})
}
}