diff --git a/.github/scripts/proxied-cmd-test-shards.txt b/.github/scripts/proxied-cmd-test-shards.txt index 7c2e916b9..fca167226 100644 --- a/.github/scripts/proxied-cmd-test-shards.txt +++ b/.github/scripts/proxied-cmd-test-shards.txt @@ -157,6 +157,7 @@ 34 17 TestProxiedServerPrime 34 17 TestProxiedServerSQLDatabaseFlag 34 17 TestProxiedServerServeModeGate +34 17 TestServerModeServeIgnoresGraphModeDependency 34 18 TestProxiedServerClose3 34 18 TestProxiedServerCount diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index 4a4e51b9d..8477c2d1a 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -1156,6 +1156,7 @@ go_test( "serve_delete_proxied_integration_test.go", "serve_dependencies_proxied_integration_test.go", "serve_edge_counts_proxied_integration_test.go", + "serve_graph_mode_env_test.go", "serve_inprocess_test.go", "serve_list_sort_proxied_integration_test.go", "serve_proxied_integration_test.go", diff --git a/cmd/bd/serve_graph_mode_env_test.go b/cmd/bd/serve_graph_mode_env_test.go new file mode 100644 index 000000000..fd61effd8 --- /dev/null +++ b/cmd/bd/serve_graph_mode_env_test.go @@ -0,0 +1,278 @@ +//go:build cgo + +package main + +import ( + "bytes" + "context" + "io" + "net/http" + "os" + "path/filepath" + "reflect" + "slices" + "strconv" + "strings" + "sync" + "testing" + + "github.com/steveyegge/beads/internal/configfile" + "github.com/steveyegge/beads/internal/storage" + "github.com/steveyegge/beads/internal/storage/backends" + "github.com/steveyegge/beads/internal/storage/embeddeddolt" +) + +// `bd serve` in an ordinary workspace is the same server whether or not +// BD_GRAPH_MODE=dependency is exported. +// +// bd reads no BD_GRAPH_MODE today. "dependency" is the value that names the +// format every existing workspace already has, so it is the one value a later +// reader of that variable (a graph-mode serve dispatch, say) must keep treating +// as "serve this workspace the way bd always has". These tests pin that before +// any such reader exists: with the variable exported, the run still has to +// reach runServe and build the same server. +// +// "The same server" is measured where an operator and a client measure it: the +// event=startup and event=limits lines, byte for byte apart from the ephemeral +// port, and the whole GET /v0/beads/context body. Each run is also checked on +// its own against what runServe builds (its mode label, its database source, +// the advertised capabilities), so two runs that were both routed somewhere +// else cannot pass by agreeing with each other. + +const serveGraphModeEnv = "BD_GRAPH_MODE" + +// TestServeOrdinaryWorkspaceIgnoresGraphModeDependency drives the whole root +// command in-process, PersistentPreRunE included, on a workspace served from +// the store source. In-process for the reason the registered-backend serve +// tests are: registration is Go wiring, so a spawned bd could not reach this +// arm. The server-mode twin below covers the provider source with a real bd. +func TestServeOrdinaryWorkspaceIgnoresGraphModeDependency(t *testing.T) { + const name = "serve-graph-mode-env" + + dir := t.TempDir() + initGitRepoAt(t, dir) + beadsDir := filepath.Join(dir, ".beads") + if err := os.MkdirAll(beadsDir, 0o755); err != nil { + t.Fatalf("mkdir .beads: %v", err) + } + seedRegisteredBackendWorkspace(t, beadsDir) + backends.Register(name, backends.Backend{ + Open: func(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { + return embeddeddolt.Open(ctx, beadsDir, serveRegisteredDatabase, "main") + }, + OpenReadOnly: func(ctx context.Context, beadsDir string) (storage.DoltStorage, error) { + return embeddeddolt.OpenReadOnly(ctx, beadsDir, serveRegisteredDatabase, "main") + }, + WorkspaceIsBeadsDir: true, + }) + t.Cleanup(func() { backends.Deregister(name) }) + if err := (&configfile.Config{Backend: name, DoltDatabase: serveRegisteredDatabase}).Save(beadsDir); err != nil { + t.Fatalf("save metadata.json: %v", err) + } + + // Unset rather than empty, so the baseline is a shell that never exported + // it. t.Setenv first so the original value comes back afterwards. + t.Setenv(serveGraphModeEnv, "") + if err := os.Unsetenv(serveGraphModeEnv); err != nil { + t.Fatalf("unset %s: %v", serveGraphModeEnv, err) + } + ordinary := serveInProcessSnapshot(t, dir, beadsDir) + + t.Setenv(serveGraphModeEnv, "dependency") + asserted := serveInProcessSnapshot(t, dir, beadsDir) + + mode := name + " (registered backend)" + assertRunServeBuiltIt(t, serveGraphModeEnv+" unset", ordinary, "roles", mode) + assertRunServeBuiltIt(t, serveGraphModeEnv+"=dependency", asserted, "roles", mode) + if asserted.context["backend"] != name { + t.Errorf("context backend = %v, want %q", asserted.context["backend"], name) + } + assertSameServer(t, ordinary, asserted) +} + +// TestServerModeServeIgnoresGraphModeDependency is the same pin for a +// `bd init --server` workspace, the topology an ordinary deployment serves +// from: the provider source, which serve builds itself. It runs a real bd, +// because that arm fronts the server through a proxy child, and a proxy child +// cannot be spawned from inside the test binary. +func TestServerModeServeIgnoresGraphModeDependency(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + p := newServerModeProject(t, bd, "srvgm") + + ordinaryEnv := envWithout(p.env, serveGraphModeEnv) + assertedEnv := append(slices.Clone(ordinaryEnv), serveGraphModeEnv+"=dependency") + + ordinary := serveProcessSnapshot(t, startServe(t, bd, p.dir, ordinaryEnv)) + asserted := serveProcessSnapshot(t, startServe(t, bd, p.dir, assertedEnv)) + + const mode = "server (external dolt)" + assertRunServeBuiltIt(t, serveGraphModeEnv+" unset", ordinary, "provider", mode) + assertRunServeBuiltIt(t, serveGraphModeEnv+"=dependency", asserted, "provider", mode) + if asserted.context["dolt_mode"] != "server" || asserted.context["database"] != p.database { + t.Errorf("context dolt_mode/database = %v/%v, want server/%s", + asserted.context["dolt_mode"], asserted.context["database"], p.database) + } + assertSameServer(t, ordinary, asserted) +} + +// serveSnapshot is what one bd serve run shows before any operation: its two +// startup events (from "event=" on, without the log prefix) and its handshake. +type serveSnapshot struct { + addr string + startup string + limits string + context map[string]any +} + +// serveInProcessSnapshot runs `bd serve` through rootCmd, reads the handshake, +// and shuts it down the way an operator does. +func serveInProcessSnapshot(t *testing.T, dir, beadsDir string) serveSnapshot { + t.Helper() + stopStderr := captureStderrText(t) + addr, done := startServeInProcess(t, dir, beadsDir) + // Stops a server that a failure below left running. Keyed on whether done + // has been read, not on rootCancel: a run that returns an error skips + // PersistentPostRunE and leaves rootCancel set, and waiting on done again + // after reading its only result would hang the test binary. + drained := false + t.Cleanup(func() { + if !drained && rootCancel != nil { + rootCancel() + <-done + } + }) + body := getJSON(t, "http://"+addr+"/v0/beads/context") + if rootCancel == nil { + t.Fatal("the root command published no cancel function; nothing here can stop the server") + } + rootCancel() + err := <-done + drained = true + if err != nil { + t.Fatalf("bd serve returned %v, want a clean shutdown", err) + } + log := stopStderr() + return serveSnapshot{ + addr: addr, + startup: serveEventLine(t, log, "startup"), + limits: serveEventLine(t, log, "limits"), + context: body, + } +} + +// serveProcessSnapshot reads a spawned serve's startup and handshake, then +// stops it with SIGTERM. +func serveProcessSnapshot(t *testing.T, sp *serveProcess) serveSnapshot { + t.Helper() + startup := sp.awaitLogLine(t, "event=startup") + limits := sp.awaitLogLine(t, "event=limits") + status, body, _ := sp.get(t, "/v0/beads/context") + if status != http.StatusOK { + t.Fatalf("GET /v0/beads/context status = %d, want 200: %v", status, body) + } + sp.shutdown(t) + return serveSnapshot{ + addr: sp.addr, + startup: serveEventLine(t, startup, "startup"), + limits: serveEventLine(t, limits, "limits"), + context: body, + } +} + +// serveEventLine returns the named event's line from "event=" on. +func serveEventLine(t *testing.T, log, name string) string { + t.Helper() + for _, line := range strings.Split(log, "\n") { + if i := strings.Index(line, "event="+name+" "); i >= 0 { + return line[i:] + } + } + t.Fatalf("no event=%s line in:\n%s", name, log) + return "" +} + +// assertRunServeBuiltIt checks one run against the server runServe builds: +// serveResolvedMode's label, the Issue API's database source, and a +// capabilities field equal to the handshake's. +func assertRunServeBuiltIt(t *testing.T, run string, s serveSnapshot, source, mode string) { + t.Helper() + assertServedCapabilities(t, s.context) + if s.context["api_version"] != "v0" { + t.Errorf("%s: context api_version = %v, want v0", run, s.context["api_version"]) + } + caps, _ := s.context["capabilities"].([]any) + tokens := make([]string, 0, len(caps)) + for _, c := range caps { + token, _ := c.(string) + tokens = append(tokens, token) + } + for _, field := range []string{ + "addr=" + s.addr, + "mode=" + strconv.Quote(mode), + "db=" + source, + "capabilities=" + strings.Join(tokens, ","), + } { + if !strings.Contains(s.startup+" ", " "+field+" ") { + t.Errorf("%s: startup line is missing %s:\n%s", run, field, s.startup) + } + } +} + +// assertSameServer compares two runs on everything but the ephemeral port. +func assertSameServer(t *testing.T, ordinary, asserted serveSnapshot) { + t.Helper() + normalize := func(s serveSnapshot) string { + return strings.Replace(s.startup, " addr="+s.addr+" ", " addr= ", 1) + } + if got, want := normalize(asserted), normalize(ordinary); got != want { + t.Errorf("%s=dependency changed the startup line:\n got: %s\nwant: %s", serveGraphModeEnv, got, want) + } + if asserted.limits != ordinary.limits { + t.Errorf("%s=dependency changed the limits line:\n got: %s\nwant: %s", serveGraphModeEnv, asserted.limits, ordinary.limits) + } + if !reflect.DeepEqual(asserted.context, ordinary.context) { + t.Errorf("%s=dependency changed GET /v0/beads/context:\n got: %v\nwant: %v", serveGraphModeEnv, asserted.context, ordinary.context) + } +} + +// captureStderrText redirects os.Stderr until the returned function is called, +// and returns everything written meanwhile. The restore also runs at cleanup, +// so a t.Fatal partway through cannot leak the redirect into later tests; a +// failed test logs what was captured. +func captureStderrText(t *testing.T) func() string { + t.Helper() + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("os.Pipe: %v", err) + } + orig := os.Stderr + os.Stderr = w + + var buf bytes.Buffer + copied := make(chan struct{}) + go func() { + defer close(copied) + _, _ = io.Copy(&buf, r) + }() + + var once sync.Once + var out string + stop := func() string { + once.Do(func() { + os.Stderr = orig + _ = w.Close() + <-copied + _ = r.Close() + out = buf.String() + }) + return out + } + t.Cleanup(func() { + if text := stop(); t.Failed() { + t.Logf("captured stderr:\n%s", text) + } + }) + return stop +} diff --git a/internal/httpapi/server_test.go b/internal/httpapi/server_test.go index ba2105508..54f43e91c 100644 --- a/internal/httpapi/server_test.go +++ b/internal/httpapi/server_test.go @@ -14,6 +14,7 @@ import ( "os" "path/filepath" "slices" + "strconv" "strings" "sync" "testing" @@ -1275,6 +1276,124 @@ func TestStartupLines(t *testing.T) { } } +// TestStartupEventPublishesTheAdvertisedCapabilitiesAndSource pins the whole +// event=startup line of a provider- or roles-backed server: its field sequence, +// its database-source label, and a capabilities field that is the handshake's +// list verbatim. +// +// TestStartupLines and TestStartupNamesTheDatabaseSource check fields by +// substring, and neither reads capabilities at all. So a server could print one +// list and advertise another, or a new kind of database source could relabel +// this line, and both would stay green. The line is how an operator learns +// what a server answers before any client connects, so it is pinned exactly: +// changing it should take a deliberate edit to this test, not happen as a side +// effect of adding a source. +func TestStartupEventPublishesTheAdvertisedCapabilitiesAndSource(t *testing.T) { + workspace := domain.ContextInfo{RepoRoot: "/w/repo", BeadsDir: "/w/repo/.beads", Database: "beads"} + for _, tc := range []struct { + name string + cfg Config + source string + }{ + {"provider", Config{Provider: &tunableProvider{}}, "provider"}, + {"provider with the external-dependency policy", Config{Provider: &tunableProvider{}, ExternalDependencyPolicy: true}, "provider"}, + {"roles", rolesConfig(Config{}), "roles"}, + {"roles with the external-dependency policy", rolesConfig(Config{ExternalDependencyPolicy: true}), "roles"}, + } { + t.Run(tc.name, func(t *testing.T) { + cfg := tc.cfg + cfg.Mode = "server (external dolt)" + cfg.Workspace = workspace + ts := newTestServer(t, cfg) + + var advertised []string + caps, _ := decodeBody(t, ts.get(t, "/v0/beads/context"))["capabilities"].([]any) + for _, c := range caps { + advertised = append(advertised, c.(string)) + } + if len(advertised) == 0 { + t.Fatal("the handshake advertises no capabilities; comparing the startup line against it would pass for an empty one") + } + // The conditional token is in the list exactly when the case asks + // for it, so the policy rows really cover a list that differs. + if got := slices.Contains(advertised, CapExternalDependencies); got != cfg.ExternalDependencyPolicy { + t.Fatalf("handshake advertises %s = %v, want %v", CapExternalDependencies, got, cfg.ExternalDependencyPolicy) + } + + line := findLogLine(t, ts.stderr.String(), "event=startup") + keys, fields := eventFields(t, line) + want := []struct{ key, value string }{ + {"event", "startup"}, + {"addr", ts.Addr()}, + {"mode", cfg.Mode}, + {"db", tc.source}, + {"workspace", workspace.RepoRoot}, + {"beads_dir", workspace.BeadsDir}, + {"database", workspace.Database}, + {"host_allowlist", "127.0.0.1,::1,localhost"}, + {"capabilities", strings.Join(advertised, ",")}, + {"auth", "none"}, + } + var wantKeys []string + for _, w := range want { + wantKeys = append(wantKeys, w.key) + if got := fields[w.key]; got != w.value { + t.Errorf("startup %s = %q, want %q", w.key, got, w.value) + } + } + if !slices.Equal(keys, wantKeys) { + t.Errorf("startup fields = %v, want %v\n%s", keys, wantKeys, line) + } + }) + } +} + +// eventFields splits one event line back into its fields, in order: the +// inverse of Server.event, where every value is a bare token or a Go-quoted +// string (logValue). The log prefix before "event=" is dropped. +func eventFields(t *testing.T, line string) ([]string, map[string]string) { + t.Helper() + start := strings.Index(line, "event=") + if start < 0 { + t.Fatalf("not an event line: %q", line) + } + rest := line[start:] + var keys []string + fields := map[string]string{} + for rest != "" { + key, value, ok := strings.Cut(rest, "=") + if !ok || key == "" || strings.ContainsAny(key, ` "`) { + t.Fatalf("malformed field at %q in %q", rest, line) + } + if strings.HasPrefix(value, `"`) { + quoted, err := strconv.QuotedPrefix(value) + if err != nil { + t.Fatalf("malformed quoted value at %q in %q: %v", value, line, err) + } + unquoted, err := strconv.Unquote(quoted) + if err != nil { + t.Fatalf("unquote %s: %v", quoted, err) + } + rest = value[len(quoted):] + value = unquoted + if rest != "" { + if rest[0] != ' ' { + t.Fatalf("field %s is not followed by a space in %q", key, line) + } + rest = rest[1:] + } + } else { + value, rest, _ = strings.Cut(value, " ") + } + if _, dup := fields[key]; dup { + t.Fatalf("field %s appears twice in %q", key, line) + } + keys = append(keys, key) + fields[key] = value + } + return keys, fields +} + // TestPoolLimitsUnavailableIsAnnounced: running unbounded is a decision, so it // must not be a silent one. func TestPoolLimitsUnavailableIsAnnounced(t *testing.T) {