Files
dibs/cmd/dibs/fixtures.go
T
Overlord 2175c0e5aa Add scheduler, rate limiting and headless mode.
Hard-coded 60s URL and 10s host floors with jitter and per-host
backoff, shared between dibs processes through a locked state file.
The scheduler shuffles due URLs each round and runs one worker per
host; 'dibs --headless' watches and logs JSON.
2026-10-14 09:04:15 +02:00

110 lines
3.0 KiB
Go

package main
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"os/signal"
"git.security-command.org/Overlord/dibs/internal/fixtures"
"git.security-command.org/Overlord/dibs/internal/plugin"
)
// runFixtures implements `dibs fixtures [--dir DIR] [--only NAME]`: it fetches
// the pages listed in DIR/<shop>/cases.toml for the plugin tests.
func runFixtures(args []string, stdout, stderr io.Writer) int {
flags, configPath := newFlagSet("dibs fixtures", stderr)
dir := flags.String("dir", "testdata", "directory with <shop>/cases.toml files")
only := flags.String("only", "", "fetch only the case with this name")
pos, ok := parseArgs(flags, args)
if !ok { return 2 }
if len(pos) > 0 {
_, _ = fmt.Fprintf(stderr, "dibs fixtures: unexpected argument %q\n\n%s", pos[0], usage)
return 2
}
suites, err := fixtures.Load(*dir)
if err != nil { return fail(stderr, "fixtures", err) }
if len(suites) == 0 { return fail(stderr, "fixtures", fmt.Errorf("no */cases.toml in %s", *dir)) }
cfg, ok := loadOptionalConfig(*configPath, stderr)
if !ok { return 1 }
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
logger := slog.New(slog.NewTextHandler(stderr, nil))
host, err := newHost(cfg, logger)
if err != nil { return fail(stderr, "fixtures", err) }
defer host.Close()
e, err := openEnv(logger)
if err != nil { return fail(stderr, "fixtures", err) }
failed := 0
for _, suite := range suites {
p, ok := host.Plugin(suite.Shop)
if !ok {
_, _ = fmt.Fprintf(stderr, "dibs fixtures: %s: no plugin named %q, skipping\n", suite.Dir, suite.Shop)
continue
}
for _, c := range suite.Cases {
if *only != "" && c.Name != *only { continue }
if err := fetchFixture(ctx, e, p, suite, c, logger, stdout); err != nil {
if ctx.Err() != nil { return fail(stderr, "fixtures", ctx.Err()) }
_, _ = fmt.Fprintf(stderr, "dibs fixtures: %s: %v\n", c.Name, err)
failed++
}
}
}
if failed > 0 { return 1 }
return 0
}
func fetchFixture(ctx context.Context, e *env, p *plugin.Plugin, suite fixtures.Suite, c fixtures.Case, logger *slog.Logger, stdout io.Writer) error {
fetchURL, err := p.Normalize(ctx, c.URL)
if err != nil { return err }
resp, err := e.fetch(ctx, fetchURL, logger)
if err != nil { return err }
report, err := plugin.Evaluate(ctx, p, c.URL, resp)
if err != nil { return err }
// Saved reports must not depend on how fast the shop answered.
report.ElapsedMS = 0
if err := suite.Save(c, resp.Body, report); err != nil { return err }
_, _ = fmt.Fprintf(stdout, "%-20s %s\n", c.Name, summary(report))
_, _ = fmt.Fprintf(stdout, "%-20s %s\n\n", "", c.Note)
return nil
}
func summary(r *plugin.Report) string {
s := r.Verdict
if r.Challenge != "" { s += " (" + r.Challenge + ")" }
if r.Result == nil { return s }
res := r.Result
avail := "unavailable"
if res.Available { avail = "AVAILABLE" }
price := "no price"
if res.Price != nil { price = fmt.Sprintf("%.2f %s", *res.Price, res.Currency) }
return fmt.Sprintf("%s, %s, %s, seller %s", s, avail, price, res.Seller)
}