Files
MrAlders0n 94b4b40f9b planguard: plan and result regression checks, fixture tier in CI, read-only mode (#240)
* feat(planguard): read sqlc statement text and load a strict plan corpus

* fix(planguard): fail on unextracted statements, keep fragments mode-neutral

* feat(planguard): plan assertions, scoped sort exemptions and narrow retired-index verdicts

* fix(planguard): an errored run always fails

* fix(planguard): keep subplan conds off their parent scan; fail on unanalyzed budgets

* feat(planguard): read-only plan runner, reports and the run command

* fix(planguard): validate identifiers in emitted scripts; prove read-only runs

* feat(planguard): end-state corpus, planted regressions and the generated cached-plan policy

* fix(planguard): derive observer cases from data; mark legacy generic plans

* fix(planguard): bound the dense-region route resolvers; test policy refusals

* fix(planguard): re-measure the dense-region route cases; cover every policy refusal

* feat(planguard): synthetic fixture built through the migrations, checked in CI

* ci(planguard): build the fixture once; guard fixture data for identifiers

* fix(planguard): hash every fixture generator input; harden the fixture guard

* feat(planguard): snapshot diff over every column, named result classes and cursor walks

* fix(planguard): bound the relaxed diff classes; derive the cursor tie from data

* fix(planguard): refuse multi-statement text in the corpus runner

* feat: read-only server mode and an HTTP page diff between two builds

* fix(planguard): refuse escape strings and psql meta-commands in corpus text

* fix: storediff follows each build's own cursor; prove read-only gates via the log

* fix(planguard): match the PostgreSQL lexer on high-bit bytes; check every emitted line

* feat(admin): daily read-only plan check on the maintenance pool, its metric and report

* feat(planguard): daily prod check behind planguard.daily_check

* fix(planguard): cancel fresh runs at shutdown; expose a never-ran check

* ci(planguard): pin the fixture server and collation; refresh baselines

* fix(planguard): calibrate route budgets on one dataset; skip entries whose index is absent
2026-10-19 13:45:39 -04:00

108 lines
4.4 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestCLIUsage(t *testing.T) {
cases := []struct {
name string
args []string
want string // in stderr
}{
{"no args", nil, "usage"},
{"unknown subcommand", []string{"frobnicate"}, "frobnicate"},
{"run without tier", []string{"run"}, "--tier"},
{"run with unknown tier", []string{"run", "--tier", "staging"}, "staging"},
{"bad baseline", []string{"run", "--tier", "local", "--baseline", "maybe"}, "--baseline"},
{"bad only", []string{"run", "--tier", "local", "--only", "("}, "--only"},
{"emit plans without params", []string{"run", "--tier", "prod", "--emit", "plans"}, "--params"},
{"emit on local", []string{"run", "--tier", "local", "--emit", "resolvers"}, "--tier prod"},
{"plans on fixture", []string{"run", "--tier", "fixture", "--plans", "p.out", "--params", "r.out"}, "--tier prod"},
{"unknown flag", []string{"run", "--tier", "local", "--nope"}, "nope"},
{"gen-policy unknown flag", []string{"gen-policy", "--nope"}, "nope"},
{"gen-policy argument", []string{"gen-policy", "extra"}, "extra"},
{"diff without cases", []string{"diff", "--dsn", "x"}, "--cases"},
{"diff bad walk", []string{"diff", "--cases", "c.yaml", "--walk", "1,0"}, "--walk"},
{"diff bad walk word", []string{"diff", "--cases", "c.yaml", "--walk", "web,all"}, "all"},
{"diff unknown flag", []string{"diff", "--nope"}, "nope"},
{"storediff without new", []string{"storediff", "--old", "http://a", "--cases", "c.yaml"}, "--new"},
{"storediff bad url", []string{"storediff", "--old", "a:8481", "--new", "http://b", "--cases", "c.yaml"}, "a:8481"},
{"storediff bad walk", []string{"storediff", "--old", "http://a", "--new", "http://b", "--cases", "c.yaml", "--walk", "0"}, "--walk"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var out, errb bytes.Buffer
if code := run(tc.args, &out, &errb); code != 2 {
t.Errorf("exit %d, want 2 (stderr %q)", code, errb.String())
}
if !strings.Contains(errb.String(), tc.want) {
t.Errorf("stderr %q, want it to name %q", errb.String(), tc.want)
}
})
}
}
// gen-policy finds the module root from a subdirectory. It writes to a temporary file, which must equal the
// committed cached_gen.go; the committed file is never written.
func TestGenPolicyFromSubdirectory(t *testing.T) {
want, err := os.ReadFile("../../db/planpolicy/cached_gen.go")
if err != nil {
t.Fatal(err)
}
out := filepath.Join(t.TempDir(), "cached_gen.go")
t.Chdir("../../internal")
var stdout, errb bytes.Buffer
if code := run([]string{"gen-policy", "--out", out}, &stdout, &errb); code != 0 {
t.Fatalf("exit %d: %s", code, errb.String())
}
got, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(got, want) {
t.Errorf("generated policy differs from the committed db/planpolicy/cached_gen.go:\n%s", got)
}
}
// storediff exits 0 when every page matches and 1 when one differs, and writes its report under --out.
func TestStoreDiffCLI(t *testing.T) {
serve := func(name string) *httptest.Server {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"items":[{"id":1,"name":%q}],"hasMore":false}`, name)
}))
t.Cleanup(s.Close)
return s
}
dir := t.TempDir()
cases := filepath.Join(dir, "cases.yaml")
if err := os.WriteFile(cases, []byte("cases:\n - id: things\n path: /api/v1/things\n"), 0o600); err != nil {
t.Fatal(err)
}
a, b, c := serve("x"), serve("x"), serve("y")
var out, errb bytes.Buffer
if code := run([]string{"storediff", "--old", a.URL, "--new", b.URL, "--cases", cases, "--walk", "1,web", "--out", dir}, &out, &errb); code != 0 {
t.Fatalf("same servers: exit %d, stderr %s", code, errb.String())
}
report, err := os.ReadFile(filepath.Join(dir, "storediff_report.md"))
if err != nil || !strings.Contains(string(report), "| things | 50 | 1 | identical |") {
t.Errorf("report %q (%v)", report, err)
}
errb.Reset()
if code := run([]string{"storediff", "--old", a.URL, "--new", c.URL, "--cases", cases}, &out, &errb); code != 1 ||
!strings.Contains(errb.String(), `$.items[0].name: old "x", new "y"`) {
t.Errorf("different servers: exit %d, stderr %s", code, errb.String())
}
}