beads/cmd/bd/bootstrap_embedded_test.go
Julian Knutsen 0d865460b1
ci(bazel): split the slow embedded cmd tests and run the cmd block as 2x50 shards (#7438)
//cmd/bd:bd_embedded_test was the embedded lane's floor: at Bazel's 50-shard
cap per rule, each shard's wall time was its slowest top-level test, and
~40 tests ran 65-130s on rbe-west.

- 34 slow TestEmbedded* functions are split into 89 smaller top-level
  functions. Subtests and table cases are moved verbatim: no assertion,
  flag or data changes, the same t.Run set per file, the same skip guard and
  t.Parallel. Subtests that read state earlier ones left stay together, in
  order; each new function repeats the shared setup it needs.
- The manifest's cmd/bd block is now 100 shards, run by two targets:
  bd_embedded_test (shards 1-50) and the new bd_embedded_part2_test
  (51-100), through go_test_manifest_shard.sh --shard-offset/--shard-total.
  check_shard_coverage.py takes SHARDS@OFFSET/TOTAL and fails unless the
  targets' ranges tile the block exactly once, so every test runs in exactly
  one shard of one target; the policy tests read both rules' ranges from
  cmd/bd/BUILD.bazel.
- Durations re-measured on rbe-west with the split tests and repacked. The
  frozen legacy 20-shard block drops the renamed names (no workflow reads
  it; new names hash-distribute as before).

Not split: the *Concurrent stress tests (their bd subprocesses queue on the
workspace's single writer lock: TestEmbeddedUpdateConcurrent is ~100
serialized bd commands), TestEmbeddedPurgeWispsPlaneRetention (one stateful
sequence), and the MaxRowsList* tests (setup is 21 sequential creates every
subtest reads).

rbe-west, both targets, --nocache_test_results: slowest shard 138-163s ->
109s (TestEmbeddedUpdateConcurrent alone; every other shard <=86s), median
80-84s -> 40s, shard-seconds 4.1-4.4k -> 4.2k, critical path 213-306s ->
113s.

Bead: ga-vnycm2.17

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-09 20:30:52 +00:00

297 lines
9.6 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// bdBootstrap runs "bd bootstrap" with the given args and returns stdout.
func bdBootstrap(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd bootstrap %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
func bdBootstrapAllowError(t *testing.T, bd, dir string, args ...string) (string, error) {
t.Helper()
fullArgs := append([]string{"bootstrap"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
return string(out), err
}
func TestBootstrapNoWorkspace(t *testing.T) {
t.Parallel()
bd := buildEmbeddedBD(t)
dir := t.TempDir()
initGitRepoAt(t, dir)
t.Run("default_output", func(t *testing.T) {
out, err := bdBootstrapAllowError(t, bd, dir)
if err == nil {
t.Fatal("expected bd bootstrap to exit non-zero without a workspace")
}
if !strings.Contains(out, activeWorkspaceNotFoundMessage()) {
t.Fatalf("expected no-workspace message, got: %s", out)
}
if !strings.Contains(out, "bd where") {
t.Fatalf("expected bootstrap hint to mention bd where, got: %s", out)
}
})
t.Run("json_output", func(t *testing.T) {
cmd := exec.Command(bd, "bootstrap", "--json")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err == nil {
t.Fatal("expected bd bootstrap --json to exit non-zero without a workspace")
}
s := strings.TrimSpace(stdout.String())
start := strings.Index(s, "{")
if start < 0 {
t.Fatalf("expected JSON object in output:\nstdout: %s\nstderr: %s", s, stderr.String())
}
var payload map[string]interface{}
if err := json.Unmarshal([]byte(s[start:]), &payload); err != nil {
t.Fatalf("parse bootstrap JSON: %v\nstdout: %s", err, s)
}
if action, _ := payload["action"].(string); action != "none" {
t.Fatalf("action = %q, want %q", action, "none")
}
if reason, _ := payload["reason"].(string); reason != activeWorkspaceNotFoundError() {
t.Fatalf("reason = %q, want %q", reason, activeWorkspaceNotFoundError())
}
if suggestion, _ := payload["suggestion"].(string); !strings.Contains(suggestion, "bd where") {
t.Fatalf("suggestion should mention bd where, got: %q", suggestion)
}
})
}
func TestEmbeddedBootstrapExistingDryRunInit(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== Already Exists =====
t.Run("bootstrap_existing_db", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "tb")
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "already exists") && !strings.Contains(out, "Nothing to do") {
t.Errorf("expected 'already exists' for initialized db: %s", out)
}
})
// ===== Dry Run (fresh .beads with no db) =====
t.Run("bootstrap_dry_run_fresh", func(t *testing.T) {
dir := t.TempDir()
cmd := gitCommand("init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
// Create .beads with metadata.json so FindBeadsDir detects it
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
out := bdBootstrap(t, bd, dir, "--dry-run")
if !strings.Contains(out, "create fresh") && !strings.Contains(out, "init") {
t.Errorf("expected fresh init plan: %s", out)
}
})
// ===== Full Bootstrap (init action) =====
t.Run("bootstrap_init", func(t *testing.T) {
dir := t.TempDir()
cmd := gitCommand("init", "-q")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.name", "Test")
cmd.Dir = dir
cmd.CombinedOutput()
cmd = gitCommand("config", "user.email", "test@test.com")
cmd.Dir = dir
cmd.CombinedOutput()
beadsDir := filepath.Join(dir, ".beads")
os.MkdirAll(beadsDir, 0o750)
os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{}"), 0o644)
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
stdout, stderr, err := runCommandBuffers(t, bcmd)
if err != nil {
t.Fatalf("bootstrap init failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
combined := stdout.String() + stderr.String()
if !strings.Contains(combined, "Created fresh database") {
t.Errorf("expected 'Created fresh database':\nstdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
}
})
}
func TestEmbeddedBootstrapJSONLAndGitOrigin(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// ===== JSONL Import =====
t.Run("bootstrap_jsonl_import", func(t *testing.T) {
// First create a db and export
srcDir, _, _ := bdInit(t, bd, "--prefix", "bs")
bdCreate(t, bd, srcDir, "Export for bootstrap", "--type", "task")
cmd := exec.Command(bd, "export", "-o", filepath.Join(srcDir, ".beads", "issues.jsonl"))
cmd.Dir = srcDir
cmd.Env = bdEnv(srcDir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("export failed: %v\n%s", err, out)
}
// Create new dir with .beads + issues.jsonl but no database
dir := t.TempDir()
gitCmd := gitCommand("init", "-q")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = gitCommand("config", "user.name", "Test")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
gitCmd = gitCommand("config", "user.email", "test@test.com")
gitCmd.Dir = dir
gitCmd.CombinedOutput()
destBeads := filepath.Join(dir, ".beads")
os.MkdirAll(destBeads, 0o750)
os.WriteFile(filepath.Join(destBeads, "metadata.json"), []byte("{}"), 0o644)
// Copy the JSONL file
data, _ := os.ReadFile(filepath.Join(srcDir, ".beads", "issues.jsonl"))
os.WriteFile(filepath.Join(dir, ".beads", "issues.jsonl"), data, 0o644)
// Bootstrap should detect and import JSONL
bcmd := exec.Command(bd, "bootstrap")
bcmd.Dir = dir
bcmd.Env = bdEnv(dir)
bcmd.Stdin = strings.NewReader("y\n")
stdout, stderr, err := runCommandBuffers(t, bcmd)
if err != nil {
t.Fatalf("bootstrap jsonl-import failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
combined := stdout.String() + stderr.String()
if !strings.Contains(combined, "Imported") {
t.Errorf("expected 'Imported' in output:\nstdout:\n%s\nstderr:\n%s", stdout.String(), stderr.String())
}
})
t.Run("bootstrap_from_git_origin_wires_remote", func(t *testing.T) {
bareDir := filepath.Join(t.TempDir(), "origin.git")
runGitForBootstrapTest(t, "", "init", "--bare", "--initial-branch=main", bareDir)
remoteURL := "file://" + bareDir
sourceDir := t.TempDir()
initGitRepoAt(t, sourceDir)
runGitForBootstrapTest(t, sourceDir, "branch", "-M", "main")
runGitForBootstrapTest(t, sourceDir, "remote", "add", "origin", remoteURL)
runGitForBootstrapTest(t, sourceDir, "commit", "--allow-empty", "-m", "init")
runGitForBootstrapTest(t, sourceDir, "push", "-u", "origin", "main")
runBDInit(t, bd, sourceDir, "--prefix", "beads", "--skip-hooks", "--skip-agents")
bdCreate(t, bd, sourceDir, "Seed remote data", "--type", "task")
bdDolt(t, bd, sourceDir, "push")
cloneDir := t.TempDir()
runGitForBootstrapTest(t, cloneDir, "init", "-b", "main")
runGitForBootstrapTest(t, cloneDir, "remote", "add", "origin", remoteURL)
out := bdBootstrap(t, bd, cloneDir, "--yes")
if !strings.Contains(out, "clone from remote") {
t.Fatalf("expected bootstrap sync plan, got:\n%s", out)
}
remotes := bdDolt(t, bd, cloneDir, "remote", "list")
if !strings.Contains(remotes, "origin") || !strings.Contains(remotes, remoteURL) {
t.Fatalf("bootstrap should leave origin configured as a Dolt remote %q; remote list:\n%s", remoteURL, remotes)
}
configYAML, err := os.ReadFile(filepath.Join(cloneDir, ".beads", "config.yaml"))
if err != nil {
t.Fatalf("read config.yaml: %v", err)
}
if !strings.Contains(string(configYAML), remoteURL) {
t.Fatalf("bootstrap should persist sync.remote; config.yaml:\n%s", configYAML)
}
})
}
// TestEmbeddedBootstrapConcurrent exercises bootstrap --dry-run concurrently.
func TestEmbeddedBootstrapConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "bx")
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "bootstrap", "--dry-run")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("bootstrap --dry-run (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}