release / go (push) Successful in 2m7s
release / web (push) Successful in 1m25s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m30s
release / android (push) Successful in 4m39s
release / Build signed APK (push) Successful in 5m10s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 29s
release / Verify release artifacts (tag releases only) (push) Skipped
- Module path: github.com/Fabled-Sword/minstrel, in go.mod, every import and the Dockerfile's version ldflags. - buildinfo.ProjectURL, the contact outside services see in Minstrel's User-Agent: https://github.com/Fabled-Sword/minstrel. - README's compose example pulls ghcr.io/fabled-sword/minstrel:latest. - docs/security.md sends problem reports to the GitHub issues. - The demo-library stack builds Minstrel from the checkout, since :dev is not published to GHCR. - hygiene.go's same-site example uses example.com names. The workbench CI (.gitea/, ci/, export-ignored) still builds and publishes :dev to the private registry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
3.8 KiB
Go
152 lines
3.8 KiB
Go
package library
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
|
)
|
|
|
|
// triggerTestPool returns a pool against MINSTREL_TEST_DATABASE_URL or skips.
|
|
func triggerTestPool(t *testing.T) *pgxpool.Pool {
|
|
t.Helper()
|
|
pool := dbtest.Open(t)
|
|
// Reset scan_runs so each test starts clean.
|
|
if _, err := pool.Exec(context.Background(), `DELETE FROM scan_runs`); err != nil {
|
|
t.Fatalf("reset: %v", err)
|
|
}
|
|
return pool
|
|
}
|
|
|
|
// quietLogger returns a logger that drops all output.
|
|
func quietLogger() *slog.Logger {
|
|
return slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
}
|
|
|
|
func TestTryStartScan_NoInFlight_Starts(t *testing.T) {
|
|
pool := triggerTestPool(t)
|
|
|
|
started, existing, err := TryStartScan(
|
|
context.Background(), pool,
|
|
nil, nil, // scanner/enricher: nil OK because the goroutine that uses them is detached and we don't await it.
|
|
quietLogger(), RunScanConfig{},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("err = %v, want nil", err)
|
|
}
|
|
if !started {
|
|
t.Errorf("started = false, want true")
|
|
}
|
|
if existing != nil {
|
|
t.Errorf("existing = %v, want nil", existing)
|
|
}
|
|
// Give the detached goroutine a moment.
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
|
|
func TestTryStartScan_FreshInFlight_Skips(t *testing.T) {
|
|
pool := triggerTestPool(t)
|
|
|
|
// Seed a fresh in-flight row.
|
|
var rowID pgtype.UUID
|
|
if err := pool.QueryRow(context.Background(),
|
|
`INSERT INTO scan_runs (started_at) VALUES (now()) RETURNING id`,
|
|
).Scan(&rowID); err != nil {
|
|
t.Fatalf("seed: %v", err)
|
|
}
|
|
|
|
started, existing, err := TryStartScan(
|
|
context.Background(), pool,
|
|
nil, nil,
|
|
quietLogger(), RunScanConfig{},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("err = %v, want nil", err)
|
|
}
|
|
if started {
|
|
t.Errorf("started = true, want false")
|
|
}
|
|
if existing == nil {
|
|
t.Fatal("existing = nil, want non-nil")
|
|
}
|
|
if existing.ID != rowID {
|
|
t.Errorf("existing.ID = %v, want %v", existing.ID, rowID)
|
|
}
|
|
}
|
|
|
|
func TestTryStartScan_StaleInFlight_Reaps(t *testing.T) {
|
|
pool := triggerTestPool(t)
|
|
|
|
// Seed a stale in-flight row (started 2 hours ago).
|
|
var staleID pgtype.UUID
|
|
if err := pool.QueryRow(context.Background(),
|
|
`INSERT INTO scan_runs (started_at) VALUES (now() - interval '2 hours') RETURNING id`,
|
|
).Scan(&staleID); err != nil {
|
|
t.Fatalf("seed: %v", err)
|
|
}
|
|
|
|
started, existing, err := TryStartScan(
|
|
context.Background(), pool,
|
|
nil, nil,
|
|
quietLogger(), RunScanConfig{},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("err = %v, want nil", err)
|
|
}
|
|
if !started {
|
|
t.Errorf("started = false, want true (stale row should reap and proceed)")
|
|
}
|
|
if existing != nil {
|
|
t.Errorf("existing = %v, want nil", existing)
|
|
}
|
|
|
|
// Verify the stale row was reaped: finished_at non-NULL, error_message
|
|
// contains "reaped".
|
|
var finishedAt pgtype.Timestamptz
|
|
var errMsg *string
|
|
if err := pool.QueryRow(context.Background(),
|
|
`SELECT finished_at, error_message FROM scan_runs WHERE id = $1`, staleID,
|
|
).Scan(&finishedAt, &errMsg); err != nil {
|
|
t.Fatalf("verify reap: %v", err)
|
|
}
|
|
if !finishedAt.Valid {
|
|
t.Errorf("finished_at = NULL, want non-NULL after reap")
|
|
}
|
|
if errMsg == nil || *errMsg != "reaped (stale)" {
|
|
t.Errorf("error_message = %v, want 'reaped (stale)'", errMsg)
|
|
}
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
|
|
func TestTryStartScan_DBError_Propagates(t *testing.T) {
|
|
pool := dbtest.Open(t)
|
|
pool.Close() // close immediately to force errors on subsequent queries
|
|
|
|
started, existing, err := TryStartScan(
|
|
context.Background(), pool,
|
|
nil, nil,
|
|
quietLogger(), RunScanConfig{},
|
|
)
|
|
if err == nil {
|
|
t.Errorf("err = nil, want non-nil for closed pool")
|
|
}
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
t.Errorf("err is ErrNoRows; want a different DB error type")
|
|
}
|
|
if started {
|
|
t.Errorf("started = true, want false on DB error")
|
|
}
|
|
if existing != nil {
|
|
t.Errorf("existing = %v, want nil on DB error", existing)
|
|
}
|
|
}
|