* perf(http): answer a command's repeated reads from one response (port of bd-enterprise e626072b8) Over an http workspace, bd show <id> --json made three identical GET /v0/beads/issues/<id> requests: the id resolver's exact probe, routed.go's GetIssue and show's detail read all build the same URL. Text show made six. Over a gateway each one is a sequential round trip, so the request count is the command's latency floor (mc-5dkzp). This ports bd-enterprise e626072b8 (bts-xpw.3.14.6, #217) verbatim, except cmd/bd/main.go, where OSS still opens the identity preview inline, and bd-enterprise's httpgw dialer test, which has no OSS counterpart: - httpclient.WithSharedConnections: the identity-gate preview and the real open share one transport, so a write makes one handshake. - httpclient.WithReadMemo and a wire.Client read memo, for one-shot commands only (not bd serve, --watch or --follow): identical getIssue, getSetting and listSettings reads are answered from the response already received. An entry ages from the server's answer (5 s), any write clears the memo, a read that overlapped a write is not stored, and expired entries are swept. - bd list's three vocabulary reads come from one memoized listSettings. - bd update --status / --if-status skips the custom-status read for a built-in status. - The hook decorator skips its post-write re-reads when no executable hook exists for the event. - bd dep add reads its confirmation titles before the write, from the memo. Against a loopback bd serve: show --json goes from 3 requests to 1, text show 6 to 3, update 5 to 3, close 6 to 4. Dolt and the other registered backends receive the same ctx and ignore it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * feat(http): BD_HTTP_READ_MEMO=0 turns the per-command read memo off An operator who suspects a stale read can rule the memo out without a rebuild. Any other value, or none, leaves the default. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(http): pin bd show to one fetch per issue (mc-5dkzp) TestE2E_PerCommandRequestBudget allowed show two requests, and a read that drifts to a URL of its own (a mutant whose reader spells brief_deps=false) fits that ceiling on both handshake shapes. These guards pin the single fetch instead: - TestE2E_ShowFetchesTheIssueOnce: the exact requests, path and query, of show --json, show --json --id, show --short, show of two ids and text show, against both handshake shapes, plus a BD_HTTP_READ_MEMO=0 step that must read the row more than once. - TestDefaultIssueReadsShareOneMemoKey: the resolver's exact probe, GetIssue, GetDependenciesWithMetadata, GetLabels and IssueReader().Get build one URL, checked without a server. - TestE2E_CloseRereadsItsOwnWrite: against the MC handshake, closing a lone molecule re-reads its own closed row from the server after the write, and prints no false "completed molecule" note. - Corpus shapes can pin `requests:`, counted on the served run alone (not setup, verify or {rev:ID} reads). The seven gc and mc show shapes pin 1, 4, 1, 1, 3, 1 and 3. Also: TestReadMemoOptInReadsTheRealPollingFlags holds the opt-in to the real show/list --watch and -w, events tail --follow and bd serve; the show budget drops from 2 to 1; requestCounter records each request's query; and ledger row L-read-memo records the memo's bounded difference from local reads. Mutation runs against this tree: brief_deps=true on GetIssue and brief_deps=false on the reader's Get each fail the e2e list and the unit test; show opting out of the memo fails the e2e list; writes that leave the memo standing fail the read-your-own-write e2e; a memo keyed by path alone fails the e2e list; dropping the kill switch fails its unit test and the e2e step. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(config): list BD_HTTP_READ_MEMO Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(http): memoize a read's answer only once it has decoded wire.Client.Do stored a memoable read's 2xx before it checked the body, so an empty or undecodable 200 would have answered every identical read in the command for the memo's whole TTL with the same failure, instead of asking the server again. The answer is now stored only after it decodes; a read that decodes nothing (out == nil) is not stored, and every memoable read in this package decodes its answer. TestReadMemoKeepsOnlyAnswersThatDecoded pins it (empty body, truncated body, undecoded answer). Ledger row L-read-memo says so. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(http): pin the read memo's 5xx, write-method, per-client, in-flight-write and hook-gate behavior Review follow-ups for the per-command read memo (mc-5dkzp): - TestReadMemoNeverKeepsA5xx: a 503, 500 or 502 is not memoized and the 2xx after it is. TestDispatchRetryOfABusyMemoizedReadAsksTheServer holds the same through dispatch's retry loop over a real wire.Client: the retry reaches the server instead of replaying the 503. - TestReadMemoIsDroppedByAnyWrite covers POST, PUT and DELETE, not just PATCH. - TestReadMemoIsPerClient: in either order, one client's read is never answered from another's memo, and opting one client in opts in no other. (A shared memo was otherwise caught only by test-order pollution.) - TestReadMemoDropsAnAnswerReadWhileAWriteWasInFlight pins the clear on the write's way back: a read that starts and ends while the write is on the wire is not answered again once the write has returned. - TestHookFiringStoreReReadsOnlyForAnEventWithAHook pins hookConfigured's probe: the decorator asks the runner about the event it fires, and re-reads the row to build a payload only when a hook for that event exists, through a probing double and through a real hooks.Runner. Each was run against a mutant of the code it pins and fails there: 5xx stored before the status check; only PATCH clearing; one memo shared by every client; no clear after the write returns; hookConfigured answering true, false, or about the wrong event. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(changelog): note the per-command http read memo Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
817 lines
30 KiB
Go
817 lines
30 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"slices"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/httpapi"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/testutil/bazeltest"
|
|
)
|
|
|
|
// TestServeFlags pins the flag surface. Every bound that is NOT here — the
|
|
// connection cap, the wait budget, the request deadline, the connection
|
|
// lifetimes — is still a constant precisely so that it becomes a flag later,
|
|
// deliberately, rather than arriving as one nobody designed.
|
|
//
|
|
// Every flag added here defaults to the behavior that existed without it, so
|
|
// a flag's default is never what changes `bd serve` with no arguments from
|
|
// one release to the next. For --large-apply-ceiling that behavior is
|
|
// httpapi.DefaultLargeApplyCeiling, the ceiling the server applies when none
|
|
// is configured.
|
|
func TestServeFlags(t *testing.T) {
|
|
var got []string
|
|
serveCmd.Flags().VisitAll(func(f *pflag.Flag) { got = append(got, f.Name) })
|
|
sort.Strings(got)
|
|
|
|
want := []string{
|
|
"addr", "allow-non-loopback", "allowed-host", "auth-token-file",
|
|
"insecure-no-auth", "large-apply-ceiling",
|
|
}
|
|
if strings.Join(got, ",") != strings.Join(want, ",") {
|
|
t.Errorf("bd serve flags = %v, want %v", got, want)
|
|
}
|
|
|
|
// The defaults ARE the compatibility promise: no token file, no extra
|
|
// hosts, no waiver, and the large-apply ceiling that SERVE_RUNBOOK.md
|
|
// sizes an orchestrator's stop grace against.
|
|
for _, tc := range []struct{ flag, want string }{
|
|
{"auth-token-file", ""},
|
|
{"insecure-no-auth", "false"},
|
|
{"allowed-host", "[]"},
|
|
{"large-apply-ceiling", "5m0s"},
|
|
} {
|
|
f := serveCmd.Flags().Lookup(tc.flag)
|
|
if f == nil {
|
|
t.Errorf("no --%s flag", tc.flag)
|
|
continue
|
|
}
|
|
if f.DefValue != tc.want {
|
|
t.Errorf("--%s default = %q, want %q: a non-neutral default would change an existing deployment", tc.flag, f.DefValue, tc.want)
|
|
}
|
|
}
|
|
|
|
// A raw --auth-token flag would put the credential in the process listing,
|
|
// where every local user reads it out of ps. There is no such flag and
|
|
// there must never be one.
|
|
if f := serveCmd.Flags().Lookup("auth-token"); f != nil {
|
|
t.Error("--auth-token takes the credential as an argument, which publishes it in ps output; the token belongs in a file")
|
|
}
|
|
|
|
addr := serveCmd.Flags().Lookup("addr")
|
|
if addr == nil {
|
|
t.Fatal("no --addr flag")
|
|
}
|
|
// Loopback, and ephemeral: a default port would be a port nobody chose,
|
|
// and picking one blesses a deployment shape the design deliberately does
|
|
// not bless.
|
|
if addr.DefValue != "127.0.0.1:0" {
|
|
t.Errorf("--addr default = %q, want 127.0.0.1:0", addr.DefValue)
|
|
}
|
|
if nonLoopback := serveCmd.Flags().Lookup("allow-non-loopback"); nonLoopback == nil {
|
|
t.Fatal("no --allow-non-loopback flag")
|
|
} else if nonLoopback.DefValue != "false" {
|
|
t.Errorf("--allow-non-loopback default = %q, want false", nonLoopback.DefValue)
|
|
}
|
|
}
|
|
|
|
// TestServeHelpTracksTheReadinessProbe. The help tells operators to probe
|
|
// readiness with GET /v0/beads/ready?limit=1, which is a 501 stub in this
|
|
// build — a probe wired from the help alone is permanently not-ready. The
|
|
// caveat that says so is transitional, and this keeps it honest in both
|
|
// directions: while ready.list is unimplemented the help must carry it, and
|
|
// the moment the handler lands the caveat must go. Nobody has to remember.
|
|
func TestServeHelpTracksTheReadinessProbe(t *testing.T) {
|
|
implemented := slices.Contains(httpapi.Capabilities(), "ready.list")
|
|
documented := strings.Contains(serveCmd.Long, "answers 501")
|
|
|
|
switch {
|
|
case implemented && documented:
|
|
t.Error("ready.list is implemented; delete the 501 caveat from the PROBES section of `bd serve --help`")
|
|
case !implemented && !documented:
|
|
t.Error("`bd serve --help` sends operators to a readiness endpoint this build answers 501 to, with no hint that it does")
|
|
}
|
|
}
|
|
|
|
func TestServeCommandIsRegistered(t *testing.T) {
|
|
for _, cmd := range rootCmd.Commands() {
|
|
if cmd.Name() == serveCmdName {
|
|
return
|
|
}
|
|
}
|
|
t.Fatal("serve is not registered under the root command")
|
|
}
|
|
|
|
// TestServeSkipsPostCommandMaintenance. In a server-mode workspace bd serve
|
|
// takes the non-proxied PersistentPostRunE branch, which is the one that runs
|
|
// the auto-commit / backup / export / push net — so on the way out of a SIGTERM
|
|
// a server would push and export, hours of requests attributed to the shutdown.
|
|
// The proxied branch never had that, and this is what keeps the two modes
|
|
// telling the operator the same story.
|
|
func TestServeSkipsPostCommandMaintenance(t *testing.T) {
|
|
if runsPostCommandMaintenance(serveCmdName, false) {
|
|
t.Error("bd serve runs post-command maintenance; the server would export and push at shutdown")
|
|
}
|
|
// The exclusion is serve's alone: a write command in the same workspace
|
|
// still gets the whole net.
|
|
if !runsPostCommandMaintenance("update", false) {
|
|
t.Error("bd update no longer runs post-command maintenance")
|
|
}
|
|
// And strict readonly still wins over everything, serve included.
|
|
if runsPostCommandMaintenance("update", true) {
|
|
t.Error("strict readonly no longer suppresses post-command maintenance")
|
|
}
|
|
}
|
|
|
|
// TestOnlyOneShotCommandsMemoizeRemoteReads (bts-xpw.3.14.6): an http
|
|
// transport's read memo answers a repeated row read from a response already
|
|
// received, which is only right for a command that exits within seconds. bd
|
|
// serve answering other clients, and a --watch or --follow poll, would show a
|
|
// snapshot instead of the changes they exist to show.
|
|
func TestOnlyOneShotCommandsMemoizeRemoteReads(t *testing.T) {
|
|
command := func(name string, args ...string) *cobra.Command {
|
|
t.Helper()
|
|
cmd := &cobra.Command{Use: name}
|
|
cmd.Flags().BoolP("watch", "w", false, "")
|
|
cmd.Flags().Bool("follow", false, "")
|
|
if err := cmd.ParseFlags(args); err != nil {
|
|
t.Fatalf("%s %v: %v", name, args, err)
|
|
}
|
|
return cmd
|
|
}
|
|
for _, tc := range []struct {
|
|
cmd *cobra.Command
|
|
args string
|
|
want bool
|
|
}{
|
|
{command("show"), "show", true},
|
|
{command("list"), "list", true},
|
|
{command("update"), "update", true},
|
|
{command("show", "--watch"), "show --watch", false},
|
|
{command("list", "-w"), "list -w", false},
|
|
{command("show", "--watch=false"), "show --watch=false", true},
|
|
{command("tail", "--follow"), "events tail --follow", false},
|
|
{command(serveCmdName), "serve", false},
|
|
{&cobra.Command{Use: "close"}, "close (no watch flag at all)", true},
|
|
} {
|
|
if got := commandMemoizesReads(tc.cmd); got != tc.want {
|
|
t.Errorf("bd %s: commandMemoizesReads = %v, want %v", tc.args, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestReadMemoOptInReadsTheRealPollingFlags (mc-5dkzp): the synthetic commands
|
|
// above pin the denylist's logic; this pins it against the commands that really
|
|
// poll. Renaming a polling flag, or giving it another spelling, must not quietly
|
|
// memoize a poll.
|
|
func TestReadMemoOptInReadsTheRealPollingFlags(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
cmd *cobra.Command
|
|
args []string
|
|
}{
|
|
{"show --watch", showCmd, []string{"--watch"}},
|
|
{"show -w", showCmd, []string{"-w"}},
|
|
{"list --watch", listCmd, []string{"--watch"}},
|
|
{"list -w", listCmd, []string{"-w"}},
|
|
{"events tail --follow", eventsTailCmd, []string{"--follow"}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
resetCommandFlags(tc.cmd)
|
|
t.Cleanup(func() { resetCommandFlags(tc.cmd) })
|
|
if !commandMemoizesReads(tc.cmd) {
|
|
t.Fatalf("bd %s without the flag: commandMemoizesReads = false, want true (a one-shot read)", tc.cmd.Name())
|
|
}
|
|
if err := tc.cmd.ParseFlags(tc.args); err != nil {
|
|
t.Fatalf("parse %v: %v", tc.args, err)
|
|
}
|
|
if commandMemoizesReads(tc.cmd) {
|
|
t.Errorf("bd %s: commandMemoizesReads = true, want false (a poll must see other writers)", tc.name)
|
|
}
|
|
})
|
|
}
|
|
if commandMemoizesReads(serveCmd) {
|
|
t.Errorf("bd serve: commandMemoizesReads = true, want false (a server answers other clients)")
|
|
}
|
|
}
|
|
|
|
// TestReadMemoKillSwitchTurnsTheMemoOff (mc-5dkzp): BD_HTTP_READ_MEMO=0 takes
|
|
// the memo away from every command, so an operator who suspects a stale read
|
|
// can rule it out without a rebuild. Any other value leaves the default.
|
|
func TestReadMemoKillSwitchTurnsTheMemoOff(t *testing.T) {
|
|
oneShot := &cobra.Command{Use: "show"}
|
|
for _, tc := range []struct {
|
|
value string
|
|
want bool
|
|
}{
|
|
{"", true},
|
|
{"1", true},
|
|
{"0", false},
|
|
} {
|
|
t.Setenv(readMemoEnvVar, tc.value)
|
|
if got := commandMemoizesReads(oneShot); got != tc.want {
|
|
t.Errorf("%s=%q: commandMemoizesReads(show) = %v, want %v", readMemoEnvVar, tc.value, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestServeRefusalsPromiseNothing is the honesty gate on the mode gate. The
|
|
// refusal is typed so a caller can dispatch on it, and it must promise nothing
|
|
// — claiming otherwise sends an operator to do the wrong work.
|
|
//
|
|
// The second case was, until bd-emv, the mirror image: it pinned the STAGED
|
|
// refusal for dolt server mode and required its text to read as "not yet". That
|
|
// wiring landed, so the case now asserts the reality it was staging for rather
|
|
// than being deleted with the refusal. Deleting it would have left nothing
|
|
// asserting that these workspaces are served: a later change could route them
|
|
// back into a refusal and no test would notice.
|
|
func TestServeRefusalsPromiseNothing(t *testing.T) {
|
|
t.Run("embedded is permanent", func(t *testing.T) {
|
|
err := errServeEmbedded()
|
|
|
|
var unsupported *storage.ErrUnsupported
|
|
if !errors.As(err, &unsupported) {
|
|
t.Fatalf("err = %v, want a typed storage.ErrUnsupported", err)
|
|
}
|
|
if unsupported.Op != "serve" {
|
|
t.Errorf("Op = %q, want serve", unsupported.Op)
|
|
}
|
|
// Backend names a BACKEND. The type documents it that way and it is the
|
|
// embryo of the pluggable-backend error taxonomy, so a topology string
|
|
// here would hand every downstream errors.As a mixed vocabulary.
|
|
if unsupported.Backend != "embedded-dolt" {
|
|
t.Errorf("Backend = %q, want embedded-dolt", unsupported.Backend)
|
|
}
|
|
|
|
msg := err.Error()
|
|
if !strings.Contains(msg, "embedded Dolt") {
|
|
t.Errorf("message does not name the workspace's actual backend: %q", msg)
|
|
}
|
|
for _, promise := range []string{"not yet", "coming", "tracked", "will be"} {
|
|
if strings.Contains(strings.ToLower(msg), promise) {
|
|
t.Errorf("permanent refusal hints at future support (%q): %q", promise, msg)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("the dolt server modes are served, not refused", func(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
apply func(t *testing.T)
|
|
}{
|
|
{
|
|
name: "server / external-server",
|
|
apply: func(t *testing.T) { serverMode = true },
|
|
},
|
|
{
|
|
name: "shared-server",
|
|
apply: func(t *testing.T) {
|
|
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
|
},
|
|
},
|
|
{
|
|
name: "proxied-server",
|
|
apply: func(t *testing.T) { proxiedServerMode = true },
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
useStorageModeGlobals(t)
|
|
beadsDir := writeContractBackendConfig(t, configfile.BackendDolt)
|
|
tc.apply(t)
|
|
db, err := serveDatabaseSource(beadsDir)
|
|
if err != nil {
|
|
t.Fatalf("serveDatabaseSource() = %v, want nil: this mode has a SQL server and bd serve builds a provider for it", err)
|
|
}
|
|
if db.source != serveSourceProvider {
|
|
t.Errorf("source = %v, want serveSourceProvider", db.source)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
|
|
t.Run("embedded is still gated", func(t *testing.T) {
|
|
useStorageModeGlobals(t)
|
|
if !isEmbeddedMode() {
|
|
// The !cgo build has no embedded backend to refuse: isEmbeddedMode
|
|
// is a constant false there, so there is no case to make.
|
|
t.Skip("this build cannot open an embedded workspace")
|
|
}
|
|
beadsDir := writeContractBackendConfig(t, configfile.BackendDolt)
|
|
_, err := serveDatabaseSource(beadsDir)
|
|
var unsupported *storage.ErrUnsupported
|
|
if !errors.As(err, &unsupported) {
|
|
t.Fatalf("serveDatabaseSource() = %v, want the typed embedded refusal", err)
|
|
}
|
|
if unsupported.Backend != "embedded-dolt" {
|
|
t.Errorf("Backend = %q, want embedded-dolt", unsupported.Backend)
|
|
}
|
|
})
|
|
}
|
|
|
|
// useStorageModeGlobals points the storage-mode accessors at the package
|
|
// globals for the duration of one test and restores them after. The mode gate
|
|
// reads them through cmdCtx otherwise, which no unit test builds.
|
|
func useStorageModeGlobals(t *testing.T) {
|
|
t.Helper()
|
|
oldServerMode, oldProxied := serverMode, proxiedServerMode
|
|
oldCmdCtx, oldUseGlobals := cmdCtx, testModeUseGlobals
|
|
t.Setenv("BEADS_DOLT_SHARED_SERVER", "0")
|
|
serverMode, proxiedServerMode = false, false
|
|
cmdCtx, testModeUseGlobals = nil, true
|
|
t.Cleanup(func() {
|
|
serverMode, proxiedServerMode = oldServerMode, oldProxied
|
|
cmdCtx, testModeUseGlobals = oldCmdCtx, oldUseGlobals
|
|
})
|
|
}
|
|
|
|
// TestServeNamesOneDatabaseSourcePerServerItBuilds is the source-level half of
|
|
// the embedded refusal. It replaces TestServeBuildsOnlyAProviderBackedServer,
|
|
// which pinned a world in which bd built provider-backed servers only.
|
|
//
|
|
// That world ended for a reason and not by accident: the registered-backend arm
|
|
// exists precisely so a store bd already opened can be served. But the property
|
|
// the old test protected did not end with it — internal/httpapi still cannot
|
|
// tell an embedded-backed role from any other, so the shortest edit from here to
|
|
// a server whose per-request atomicity claim is false is still handing Listen
|
|
// the roles off whatever store happens to be open.
|
|
//
|
|
// So this pins what is left, and it is narrow and checkable:
|
|
//
|
|
// - every httpapi.Config bd builds names exactly ONE COMPLETE database
|
|
// source — Provider alone, or Reader and Claimer together. A half-set pair
|
|
// binds, answers every read, and nil-dereferences on the first claim;
|
|
// Listen refuses it, and so does this, one layer earlier;
|
|
// - both arms exist, so the test cannot pass because one was deleted;
|
|
// - a roles-bearing Config is built only inside a function that consults
|
|
// serveDatabaseSource and names serveSourceStore. That is the gate holding
|
|
// the embedded refusal, so an edit that reaches for the roles anywhere else
|
|
// fails here and has to go read it.
|
|
func TestServeNamesOneDatabaseSourcePerServerItBuilds(t *testing.T) {
|
|
dir := packageDir(t)
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", dir, err)
|
|
}
|
|
|
|
fset := token.NewFileSet()
|
|
var providerBacked, rolesBacked int
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
|
|
continue
|
|
}
|
|
file, err := parser.ParseFile(fset, filepath.Join(dir, name), nil, 0)
|
|
if err != nil {
|
|
t.Fatalf("parse %s: %v", name, err)
|
|
}
|
|
|
|
// Attribute every literal to its enclosing function. A literal outside
|
|
// one is not reachable through the gate by construction, so the
|
|
// whole-file count below refuses to let it hide.
|
|
attributed := 0
|
|
for _, decl := range file.Decls {
|
|
fn, ok := decl.(*ast.FuncDecl)
|
|
if !ok {
|
|
continue
|
|
}
|
|
// NOTE: Reader+Claimer is the ROLES-SOURCE SIGNAL, not the whole
|
|
// role set — Config also carries Settings, Stats, CycleDetector,
|
|
// ReadyCounter, Sweeper, EdgeReader, BatchCreator and more. That is
|
|
// sound for what this test asks (which database source did this
|
|
// Config name), but the pair is hand-maintained: a future source
|
|
// signal that is not Reader+Claimer would read as "no database
|
|
// source". Deriving it is bd-lidlu's territory.
|
|
for _, lit := range httpapiConfigLiterals(fn) {
|
|
attributed++
|
|
keys := configLiteralKeys(lit)
|
|
provider := keys["Provider"]
|
|
reader, claimer := keys["Reader"], keys["Claimer"]
|
|
|
|
switch {
|
|
case provider && (reader || claimer):
|
|
t.Errorf("%s: this httpapi.Config names two database sources; pass exactly one",
|
|
fset.Position(lit.Pos()))
|
|
case reader != claimer:
|
|
t.Errorf("%s: this httpapi.Config sets one issue role without the other; a reader without a "+
|
|
"claimer binds, answers every read, and fails the first claim on a live server",
|
|
fset.Position(lit.Pos()))
|
|
case provider:
|
|
providerBacked++
|
|
case reader && claimer:
|
|
rolesBacked++
|
|
if !functionMentions(fn, "serveDatabaseSource") || !functionMentions(fn, "serveSourceStore") {
|
|
t.Errorf("%s: %s builds a roles-backed httpapi.Config without consulting serveDatabaseSource. "+
|
|
"That gate is where the permanent embedded-Dolt refusal lives, and internal/httpapi "+
|
|
"cannot tell an embedded-backed role from any other — read it before changing this",
|
|
fset.Position(lit.Pos()), fn.Name.Name)
|
|
}
|
|
default:
|
|
t.Errorf("%s: this httpapi.Config names no database source", fset.Position(lit.Pos()))
|
|
}
|
|
}
|
|
}
|
|
if total := len(httpapiConfigLiterals(file)); total != attributed {
|
|
t.Errorf("%s: %d of %d httpapi.Config literals are outside any function; such a literal cannot be "+
|
|
"reached through serveDatabaseSource", name, total-attributed, total)
|
|
}
|
|
}
|
|
|
|
// Both arms, so the test cannot pass because one was deleted, renamed, or
|
|
// stopped naming a source at all.
|
|
if providerBacked == 0 {
|
|
t.Error("no provider-backed httpapi.Config in cmd/bd: the dolt SQL-server workspaces are no longer served")
|
|
}
|
|
if rolesBacked == 0 {
|
|
t.Error("no roles-backed httpapi.Config in cmd/bd: a registered backend is no longer served from its store")
|
|
}
|
|
}
|
|
|
|
// httpapiConfigLiterals returns every `httpapi.Config{...}` composite literal
|
|
// under n.
|
|
func httpapiConfigLiterals(n ast.Node) []*ast.CompositeLit {
|
|
var out []*ast.CompositeLit
|
|
ast.Inspect(n, func(n ast.Node) bool {
|
|
lit, ok := n.(*ast.CompositeLit)
|
|
if !ok {
|
|
return true
|
|
}
|
|
sel, ok := lit.Type.(*ast.SelectorExpr)
|
|
if !ok || sel.Sel.Name != "Config" {
|
|
return true
|
|
}
|
|
if pkg, ok := sel.X.(*ast.Ident); ok && pkg.Name == "httpapi" {
|
|
out = append(out, lit)
|
|
}
|
|
return true
|
|
})
|
|
return out
|
|
}
|
|
|
|
// configLiteralKeys is the set of field names a keyed composite literal sets.
|
|
func configLiteralKeys(lit *ast.CompositeLit) map[string]bool {
|
|
keys := make(map[string]bool, len(lit.Elts))
|
|
for _, elt := range lit.Elts {
|
|
kv, ok := elt.(*ast.KeyValueExpr)
|
|
if !ok {
|
|
continue
|
|
}
|
|
if key, ok := kv.Key.(*ast.Ident); ok {
|
|
keys[key.Name] = true
|
|
}
|
|
}
|
|
return keys
|
|
}
|
|
|
|
// functionMentions reports whether fn's body names the given identifier.
|
|
func functionMentions(fn *ast.FuncDecl, name string) bool {
|
|
found := false
|
|
ast.Inspect(fn, func(n ast.Node) bool {
|
|
if ident, ok := n.(*ast.Ident); ok && ident.Name == name {
|
|
found = true
|
|
}
|
|
return !found
|
|
})
|
|
return found
|
|
}
|
|
|
|
// packageDir is this test file's own directory, resolved from the compiled-in
|
|
// source path rather than from the working directory: tests in this package
|
|
// chdir into temporary workspaces, and a relative path would read whichever one
|
|
// happened to be current.
|
|
func packageDir(t *testing.T) string {
|
|
t.Helper()
|
|
_, file, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("cannot resolve this test's source path")
|
|
}
|
|
return bazeltest.CallerDir(file, "cmd/bd")
|
|
}
|
|
|
|
// TestServeRefusesStrictReadonly.
|
|
//
|
|
// `bd --readonly serve` is a contradiction, and until it was refused it
|
|
// resolved differently on each of the two database sources — badly on both.
|
|
//
|
|
// On the STORE source the root command opens the workspace through
|
|
// backend.OpenReadOnly and serve takes its claimer off that store, so the
|
|
// server bound, GET /v0/beads/context went on advertising `issues.claim` (the
|
|
// capability set is derived from the route table and knows nothing about a CLI
|
|
// flag), and every claim came back 500 with the issue left open and unassigned.
|
|
// A server that advertises a write it will always fail is worse than no server.
|
|
//
|
|
// On the PROVIDER source it was the other silent answer: serve builds its own
|
|
// unit-of-work provider from the workspace's connection settings, which has no
|
|
// read-only posture at all, so `--readonly` bought the operator nothing and
|
|
// every claim landed. (Proxied mode never got that far — the root pre-run
|
|
// already refuses strict readonly for it.)
|
|
//
|
|
// Refusing is the same policy bd already applies one layer down, where a
|
|
// backend that cannot guarantee mutation-free access is turned away rather than
|
|
// opened anyway. It is also the only answer that is the same on both sources.
|
|
//
|
|
// The gate is ahead of the workspace, which this pins by refusing in a
|
|
// directory that has no workspace at all: no topology can reach past it.
|
|
func TestServeRefusesStrictReadonly(t *testing.T) {
|
|
// Refused in a directory with NO workspace, which is how the ordering is
|
|
// pinned: the gate cannot be sitting behind a topology if there is no
|
|
// topology to resolve. That is what makes one test cover both sources.
|
|
// TestServeRefusesStrictReadonlyOnARegisteredBackend drives the same
|
|
// refusal in a workspace that would otherwise have served.
|
|
t.Run("before any workspace is resolved", func(t *testing.T) {
|
|
stderr, err := runServeUnderReadonly(t, t.TempDir())
|
|
if err == nil {
|
|
t.Fatalf("bd --readonly serve bound a server\nstderr:\n%s", stderr)
|
|
}
|
|
if !strings.Contains(stderr, "--readonly") {
|
|
t.Errorf("the refusal does not name the flag that caused it: %q", stderr)
|
|
}
|
|
if strings.Contains(stderr, "cannot resolve workspace context") {
|
|
t.Errorf("the readonly refusal runs after the workspace is resolved: %q", stderr)
|
|
}
|
|
})
|
|
|
|
t.Run("the capability set stays honest", func(t *testing.T) {
|
|
// The other way to settle this would have been to drop issues.claim
|
|
// from what a read-only server advertises. That is a wire change —
|
|
// `capabilities` is the documented pre-flight a client checks — and it
|
|
// would make one operation's presence depend on a CLI flag, which no
|
|
// client can discover before connecting. Refusing the process instead
|
|
// leaves the published surface a property of the build.
|
|
if !slices.Contains(httpapi.Capabilities(), "issues.claim") {
|
|
t.Error("issues.claim left the advertised capability set; bd serve refuses --readonly " +
|
|
"precisely so that set never has to vary")
|
|
}
|
|
})
|
|
}
|
|
|
|
// runServeUnderReadonly runs bd serve with strict readonly set, in dir, and
|
|
// returns what it wrote to stderr. The refusal reaches the operator through
|
|
// HandleError, which writes the message to stderr and returns an opaque exit
|
|
// error, so the message is only observable here.
|
|
func runServeUnderReadonly(t *testing.T, dir string) (string, error) {
|
|
t.Helper()
|
|
useStorageModeGlobals(t)
|
|
restoreServeGlobals(t)
|
|
|
|
origReadonly := readonlyMode
|
|
readonlyMode = true
|
|
t.Cleanup(func() { readonlyMode = origReadonly })
|
|
|
|
store = nil
|
|
serveAddr, serveAllowNonLoopback = "127.0.0.1:0", false
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
setRootContext(ctx, cancel)
|
|
t.Chdir(dir)
|
|
// The workspace snapshot is resolved once per process and cached, so
|
|
// without this a directory with no workspace would still resolve to
|
|
// whichever one an earlier test in this binary left behind — and "no
|
|
// workspace" is the whole premise of the ordering assertion above.
|
|
resetRepoCachesForTest(t)
|
|
|
|
var err error
|
|
stderr := captureBootstrapStderr(t, func() { err = runServe() })
|
|
return stderr, err
|
|
}
|
|
|
|
// clearServeEnv removes the BEADS_SERVE_* fallback for one test. Without it a
|
|
// developer (or a CI runner) with it exported in their shell would silently
|
|
// change what these tests assert — env is read only in this package, which is
|
|
// what keeps the library and the dev binary immune to the same hazard.
|
|
func clearServeEnv(t *testing.T) {
|
|
t.Helper()
|
|
for _, key := range []string{serveTokenFileEnv} {
|
|
t.Setenv(key, "")
|
|
if err := os.Unsetenv(key); err != nil {
|
|
t.Fatalf("unset %s: %v", key, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// withServeFlags sets the serve globals for one test and restores them, so a
|
|
// table row cannot leak its posture into the next one.
|
|
func withServeFlags(t *testing.T) {
|
|
t.Helper()
|
|
addr, nonLoopback := serveAddr, serveAllowNonLoopback
|
|
token, insecure := serveAuthTokenFile, serveInsecureNoAuth
|
|
hosts := serveAllowedHosts
|
|
largeApplyCeiling := serveLargeApplyCeiling
|
|
t.Cleanup(func() {
|
|
serveAddr, serveAllowNonLoopback = addr, nonLoopback
|
|
serveAuthTokenFile, serveInsecureNoAuth = token, insecure
|
|
serveAllowedHosts = hosts
|
|
serveLargeApplyCeiling = largeApplyCeiling
|
|
})
|
|
serveAddr, serveAllowNonLoopback = "127.0.0.1:0", false
|
|
serveAuthTokenFile, serveInsecureNoAuth = "", false
|
|
serveAllowedHosts = nil
|
|
serveLargeApplyCeiling = httpapi.DefaultLargeApplyCeiling
|
|
}
|
|
|
|
func serveTokenFile(t *testing.T) string {
|
|
t.Helper()
|
|
path := filepath.Join(t.TempDir(), "tokens")
|
|
if err := os.WriteFile(path, []byte("a-token\n"), 0o600); err != nil {
|
|
t.Fatalf("write token file: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
// TestServeConfigRefusesAnUnservablePosture drives the flag resolution alone —
|
|
// no listener, no workspace — because the refusal must not depend on either.
|
|
// The row that matters most is the first: reaching the address used to BE the
|
|
// authorization, so a bind beyond loopback with no credential is now a refusal
|
|
// rather than a warning nobody reads.
|
|
func TestServeConfigRefusesAnUnservablePosture(t *testing.T) {
|
|
token := serveTokenFile(t)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
apply func(t *testing.T)
|
|
wantErr string
|
|
}{
|
|
{
|
|
name: "loopback with no token is unchanged",
|
|
apply: func(*testing.T) {},
|
|
},
|
|
{
|
|
name: "loopback with a token",
|
|
apply: func(*testing.T) { serveAuthTokenFile = token },
|
|
},
|
|
{
|
|
name: "non-loopback with a token",
|
|
apply: func(*testing.T) {
|
|
serveAddr, serveAllowNonLoopback = "0.0.0.0:0", true
|
|
serveAuthTokenFile = token
|
|
},
|
|
},
|
|
{
|
|
name: "non-loopback with no credential",
|
|
apply: func(*testing.T) {
|
|
serveAddr, serveAllowNonLoopback = "0.0.0.0:0", true
|
|
},
|
|
wantErr: "--auth-token-file",
|
|
},
|
|
{
|
|
name: "non-loopback waived explicitly",
|
|
apply: func(*testing.T) {
|
|
serveAddr, serveAllowNonLoopback = "0.0.0.0:0", true
|
|
serveInsecureNoAuth = true
|
|
},
|
|
},
|
|
{
|
|
name: "a token file that does not exist",
|
|
apply: func(*testing.T) {
|
|
serveAuthTokenFile = filepath.Join(t.TempDir(), "absent")
|
|
},
|
|
wantErr: "no such file",
|
|
},
|
|
{
|
|
name: "an allowed host with a port",
|
|
apply: func(*testing.T) { serveAllowedHosts = []string{"bd.beads.svc:8080"} },
|
|
wantErr: "--allowed-host",
|
|
},
|
|
{
|
|
name: "a zero large-apply ceiling",
|
|
apply: func(*testing.T) { serveLargeApplyCeiling = 0 },
|
|
wantErr: "--large-apply-ceiling",
|
|
},
|
|
{
|
|
name: "a negative large-apply ceiling",
|
|
apply: func(*testing.T) { serveLargeApplyCeiling = -time.Second },
|
|
wantErr: "--large-apply-ceiling",
|
|
},
|
|
{
|
|
name: "a non-default large-apply ceiling",
|
|
apply: func(*testing.T) { serveLargeApplyCeiling = 90 * time.Second },
|
|
},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
clearServeEnv(t)
|
|
withServeFlags(t)
|
|
tc.apply(t)
|
|
|
|
_, err := resolveServeConfig()
|
|
if tc.wantErr == "" {
|
|
if err != nil {
|
|
t.Fatalf("resolveServeConfig() = %v, want nil", err)
|
|
}
|
|
return
|
|
}
|
|
if err == nil {
|
|
t.Fatalf("resolveServeConfig() = nil, want a refusal naming %s", tc.wantErr)
|
|
}
|
|
if !strings.Contains(err.Error(), tc.wantErr) {
|
|
t.Errorf("refusal %q does not name %s, so it does not say how to proceed", err, tc.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestServeEnvFallbackAppliesOnlyWhenTheFlagIsUnset. The env alias exists for
|
|
// container deployments where the flag list is awkward; a flag the operator
|
|
// typed must still win.
|
|
func TestServeEnvFallbackAppliesOnlyWhenTheFlagIsUnset(t *testing.T) {
|
|
envToken := serveTokenFile(t)
|
|
|
|
t.Run("env supplies what the flag did not", func(t *testing.T) {
|
|
clearServeEnv(t)
|
|
withServeFlags(t)
|
|
t.Setenv(serveTokenFileEnv, envToken)
|
|
|
|
cfg, err := resolveServeConfig()
|
|
if err != nil {
|
|
t.Fatalf("resolveServeConfig: %v", err)
|
|
}
|
|
if cfg.Auth == nil {
|
|
t.Errorf("%s did not enable authentication", serveTokenFileEnv)
|
|
}
|
|
})
|
|
|
|
t.Run("an explicit flag wins", func(t *testing.T) {
|
|
clearServeEnv(t)
|
|
withServeFlags(t)
|
|
t.Setenv(serveTokenFileEnv, filepath.Join(t.TempDir(), "absent"))
|
|
serveAuthTokenFile = serveTokenFile(t)
|
|
|
|
if _, err := resolveServeConfig(); err != nil {
|
|
t.Fatalf("the flag lost to an unreadable environment fallback: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestServeConfigCarriesTheOperatorsChoicesThrough. resolveServeConfig is the
|
|
// only place the flags become a Config, so this is where a field that is
|
|
// parsed but never wired would show up.
|
|
func TestServeConfigCarriesTheOperatorsChoicesThrough(t *testing.T) {
|
|
clearServeEnv(t)
|
|
withServeFlags(t)
|
|
serveAuthTokenFile = serveTokenFile(t)
|
|
serveAllowedHosts = []string{"bd-proj.beads.svc.cluster.local", "bd-proj.beads.svc"}
|
|
serveLargeApplyCeiling = 90 * time.Second
|
|
|
|
cfg, err := resolveServeConfig()
|
|
if err != nil {
|
|
t.Fatalf("resolveServeConfig: %v", err)
|
|
}
|
|
if cfg.Auth == nil {
|
|
t.Error("--auth-token-file did not reach the server config")
|
|
}
|
|
if strings.Join(cfg.AllowedHosts, ",") != strings.Join(serveAllowedHosts, ",") {
|
|
t.Errorf("AllowedHosts = %v, want %v", cfg.AllowedHosts, serveAllowedHosts)
|
|
}
|
|
if cfg.InsecureNoAuth {
|
|
t.Error("InsecureNoAuth is set without the flag")
|
|
}
|
|
if cfg.LargeApplyCeiling != serveLargeApplyCeiling {
|
|
t.Errorf("LargeApplyCeiling = %s, want %s (--large-apply-ceiling did not reach the server config)", cfg.LargeApplyCeiling, serveLargeApplyCeiling)
|
|
}
|
|
|
|
var httpCfg httpapi.Config
|
|
cfg.applyTo(&httpCfg)
|
|
if httpCfg.LargeApplyCeiling != serveLargeApplyCeiling {
|
|
t.Errorf("serveOptions.applyTo did not carry LargeApplyCeiling through: got %s, want %s", httpCfg.LargeApplyCeiling, serveLargeApplyCeiling)
|
|
}
|
|
}
|
|
|
|
// TestServeHelpDescribesTheAuthPosture. The help text is where an operator
|
|
// learns what this server does and does not protect, and it used to say "No
|
|
// authentication and no TLS" flatly. Half of that is now false; leaving it
|
|
// would be a false security statement shipped in the binary.
|
|
func TestServeHelpDescribesTheAuthPosture(t *testing.T) {
|
|
long := serveCmd.Long
|
|
for _, want := range []string{"--auth-token-file", "--allowed-host", "rotat", "tls"} {
|
|
if !strings.Contains(strings.ToLower(long), want) {
|
|
t.Errorf("`bd serve --help` does not mention %q", want)
|
|
}
|
|
}
|
|
if strings.Contains(long, "No authentication and no TLS") {
|
|
t.Error("the help still claims this server has no authentication; it has optional bearer authentication and it is mandatory beyond loopback")
|
|
}
|
|
|
|
// The two refusal messages that make the same claim must not contradict
|
|
// the help either.
|
|
if _, err := httpapi.ValidateBindAddr("10.0.0.5:8080", false); err == nil {
|
|
t.Fatal("a non-loopback bind without the opt-in was accepted")
|
|
} else if strings.Contains(err.Error(), "no authentication") {
|
|
t.Errorf("the bind refusal still claims the server has no authentication: %q", err)
|
|
}
|
|
if usage := serveCmd.Flags().Lookup("allow-non-loopback").Usage; strings.Contains(usage, "no authentication") {
|
|
t.Errorf("--allow-non-loopback still advertises that the server has no authentication: %q", usage)
|
|
}
|
|
}
|