beads/cmd/bd/dolt_embedded_test.go
Julian Knutsen 1137aeb70d
ci(bazel): embedded-Dolt tier 20/5 → 50/15 duration-balanced Bazel shards (F1) (#7156)
* ci(bazel): embedded-Dolt tier 20/5 -> 40/14 duration-balanced shards (F1)

Give the Bazel-only bazel-embedded job its own duration-balanced manifest
blocks for //cmd/bd:bd_embedded_test (20 -> 40 shards) and
//internal/storage/embeddeddolt:embeddeddolt_embedded_test (5 -> 14
shards), decoupled from PR Risk's/main.yml's legacy frozen 20/5-shard
fork/push jobs, mirroring F2's bd_proxied_test precedent.

- Measured every TestEmbedded* / embeddeddolt test's wall time in
  isolation; split 21 cmd-tier and 1 storage-tier single top-level test
  over 180s into disjoint sub-tests (3 irreducible exceptions left:
  TestEmbeddedUpdateConcurrent 475s, TestEmbeddedCloseConcurrent 308s,
  TestLargeBatchApplyStatementCounts712_Embedded 261s).
- New scripts/ci/gen_embedded_{cmd,storage}_shard_manifest.py +
  _embedded_shard_manifest_lib.py generate/check duration-balanced
  manifest blocks (LPT packing, incremental --write/--repack, exact-once
  --check), chosen at N=40/M=14 where the heaviest shard reaches the
  irreducible-test floor.
- Fixed 18 stale legacy 20-shard cmd manifest entries (and 1 storage
  entry) left over from earlier test splits, which would have hard-failed
  every shard of PR Risk's real test-embedded-cmd jobs on their next run.
- New scripts/embedded_shard_count_test.go policy tests tie the
  Bazel-only block's shard count to bazel.yml's check_shard_coverage.py
  argument and catch generator staleness; generalized
  TestBazelRetiredLanesCheckListedTestsRan's live-shard-count dispatch
  from bd_proxied_test-only to a map covering both embedded targets;
  removed TestBazelEmbeddedJobMirrorsEmbeddedTier's now-invalid
  BUILD.bazel-shard-count-equals-PR-Risk-matrix assertion.
- Verified: go test ./scripts/..., bazel test //scripts:scripts_test,
  actionlint, make ci-pr-policy, make bazel-sync-check all pass; full
  local bazel test of both targets at 40/14 shards passes 2/2; mutation
  testing confirms each new/changed policy test fails exactly when it
  should.

See ~/beads-bazel-plan/f1/impl-report.md for full detail.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* fix(embedded-dolt): skip 712-item statement-count assertion under -race (F1)

TestLargeBatchApplyStatementCounts712_Embedded genuinely costs ~260-300s
under -race (confirmed by solo re-measurement: 298.60s) -- 14014 real SQL
statement round-trips through the race-instrumented in-process Dolt engine,
not an artifact and not splittable (one indivisible ApplyBatchInTx
transaction). It was the storage tier's single-test floor for CI shard
packing.

Apply the exact precedent TestLargeBatchApplyWallClock_Embedded already set
for its 1000-item shape: skip this assertion under raceEnabled (the cost is
race-instrumentation overhead on the Dolt engine's own internal
goroutine/lock machinery, not this test's own logic) and extend nightly.yml's
"Embedded Dolt batch-apply suite (non-race)" step's -run regex so the full
pinned 14014-statement baseline still runs, non-race, every night. Verified
directly: under -race the assertion now skips in 0.00s; without -race it
still runs and asserts Total=14015 (within the existing +/-2 tolerance) in
36.76s.

Updates scripts/ci_workflow_test.go's
TestNightlyFullTestRunsEmbeddedDoltBatchApplySuite to match the extended
regex.

Storage tier's new single-test ceiling under the per-PR -race lane drops
from ~260.7s to 152.38s (TestBatchApplyContract).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* ci(bazel): correct cmd-tier long-pole durations, re-pack embedded manifolds (F1)

Investigated coordinator item 2 (single-test long poles): TestEmbeddedUpdateConcurrent
(recorded 475.39s) and TestEmbeddedCloseConcurrent (recorded 308.06s), plus
the two other >180s cmd-tier entries from the same capture
(TestEmbeddedInitA 196.94s, TestEmbeddedEpicCloseEligibleReason 192.47s).

Root cause: NOT host contention, NOT backoff/retry sleeps (bdRunWithFlockRetry
contributes negligibly), NOT race-instrumented-subprocess overhead alone.
These numbers were captured via `bazel --nohome_rc test --config=embedded`
with the then-current 20-shard legacy manifest (~10-11 tests/shard) and
`-test.parallel=4`. Go's t.Parallel() starts each parallel test's elapsed
clock when its function is invoked, before the Parallel() call blocks it
waiting for a free one-of-4 slot -- so a test queued behind several other
heavy shard-mates has that queueing wait baked into its own recorded
`--- PASS: TestX (Ys)` time. This is shard-composition-dependent scheduling
delay, not a cost intrinsic to the test.

Confirmed two ways in an isolated clone (not this worktree, per the
no-plain-go-test-from-a-beads-checkout rule):
- Solo (no rivals at all): 151.83s / 106.48s / 63.58s / 66.08s.
- All four together with the real -race -test.parallel=4 flags and exactly
  4 tests requested (so nobody queues -- parallel slots == test count):
  141.34s / 113.80s / 69.79s / 74.63s.
Both agree closely and both are far below the capture-time numbers.

F1's LPT packer already isolates the heaviest tests into shards with few or
no other heavy rivals (TestEmbeddedUpdateConcurrent lands alone in its own
40-shard-block shard), so under the new manifest these tests see little to
no queueing delay in real CI. None of the four needed splitting, parallel
subtests, or raceEnabled-scaled N: once correctly measured, none exceeds the
180s single-test guideline (new corrected weights: 155.0 / 115.0 / 72.0 /
77.0s). Corrected scripts/ci/embedded_cmd_test_durations.json accordingly
(with a doc-comment explaining the methodology and both confirmations) and
re-packed the 40-shard Bazel-only block (--repack --write); --check passes
(exact-once coverage of all 241 discovered tests). The frozen legacy 20-shard
block is untouched and independently re-verified exact-once by simulating
embedded-test-shard.sh across all 20 shards (241/241 tests, no
duplicates/misses/errors).

New cmd-tier single-test ceiling: 179.47s (TestEmbeddedCloseAlreadyClosed,
likely itself somewhat inflated by the same mechanism but already under the
180s line as recorded, so no action needed).

Also updates scripts/ci/embedded_storage_test_durations.json: the prior
commit's raceEnabled-skip for TestLargeBatchApplyStatementCounts712_Embedded
means it now costs ~0s in the per-PR -race lane bazel-embedded actually
packs for (not its real ~260-300s non-race cost, which nightly.yml still
exercises) -- weight set to 1.0 with a doc comment tying it to the skip in
large_batch_apply_measure_test.go. Re-packed the 14-shard Bazel-only block
accordingly; --check passes (exact-once coverage of all 387 discovered
tests, excluding TestMain/TestConformance per that file's existing
convention). The frozen legacy 5-shard block is untouched and independently
re-verified exact-once via embedded-storage-test-shard.sh simulation
(388/388 tests, no duplicates/misses/errors).

Verification: go test ./scripts/... (includes the nightly.yml policy test),
make ci-pr-policy, make bazel-sync-check, actionlint all pass. Real local
`bazel --nohome_rc test --config=embedded //cmd/bd:bd_embedded_test
//internal/storage/embeddeddolt:embeddeddolt_embedded_test`
(--local_test_jobs=8, both targets together, on a shared/noisy sandbox host)
passes both targets; per-shard wall time from each shard's test.xml: cmd/bd
max 218.9s (shard 23/40, min 20.6s), storage max 251.1s (shard 5/14, min
0.98s) -- down from the prior oversubscribed sweep's 783.41s / 351.94s and
well under the single-test-floor-based ~475s projection this slice started
from. Projected tier lane time (max of the two targets, real dedicated CI
runners should do at least this well): ~251s, under the 5-minute target.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* test(embedded): un-vacuous TestEmbeddedDepB edges, split epic-close test, trim concurrency stress N (F1 review)

Coordinator review follow-ups on the F1 embedded-Dolt resharding slice:

- TestEmbeddedDepB (one of TestEmbeddedDep's two CI-speed split halves) was
  asserting its tree_* subtests against an edgeless graph, passing
  vacuously; restores the same parent-child/blocks edges TestEmbeddedDepA
  builds so those subtests exercise real multi-node traversal (review S2).
- TestEmbeddedEpicCloseEligibleReason split into
  TestEmbeddedEpicCloseEligibleReasonFlag/...Output for CI shard balance,
  matching the project's established single-concern split pattern.
- TestEmbeddedUpdateConcurrent/TestEmbeddedCloseConcurrent's
  issuesPerWorker reduced 5->2: under real concurrent-shard-load
  measurement these were genuine (not queueing-artifact) serialized-write
  costs; 2 preserves every invariant the test checks (duplicate-ID
  detection across all workers, one non-decreasing-list-count check per
  worker) at a fraction of the serialized work.
- large712ShapeName: the 712-shape's race skip and its zero-match guard now
  share one named constant instead of two independent literal shape-name
  strings, so a future rename of the shape can't silently skip the pinned
  statement-count assertion under race (review N2).
- Comment-only fixes (review N5) in files referencing test names since
  renamed/split by this slice's earlier work: close_proxied_integration_test.go,
  graph_apply_test.go, storage_class_proxied_integration_test.go,
  tests/regression/discovery_test.go, plus doc-comment path cleanups (N1:
  drop unverified ~/beads-bazel-plan paths/bead ids in favor of the
  committed duration JSON files and engdocs/TESTING.md).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* ci(bazel): re-derive embedded-Dolt durations under concurrent load; cap cmd shards at Bazel's 50-shard limit (F1 round 2)

Coordinator review required re-deriving every per-test duration weight from
a single, consistent, CI-realistic measurement (all shards running
concurrently, -test.parallel=4, race-enabled) instead of the prior pass's
mix of crowded-shard captures and solo/4-together spot-corrections, and
required measuring per-shard wall time via BEP testAttemptDuration rather
than a shard's outer test.xml <testsuites time> (which can include a
different, already-finished shard-mate's setup/teardown).

- scripts/ci/embedded_cmd_test_durations.json (242 tests) and
  scripts/ci/embedded_storage_test_durations.json (387 tests) rewritten in
  full from one consistent sweep. Every entry is now a fresh measurement;
  none is a carried-over capture-time value or an unverified estimate. Both
  files' _comment blocks retract the earlier "queueing delay baked into a
  test's own reported elapsed time" theory (testing.go's (*T).Parallel does
  not add wait time to a blocked test's own clock) and document the real
  methodology.
- Only one cmd-tier test remains over 200s (TestEmbeddedListFiltering,
  239.70s), already under the ~240s ceiling; no further top-level splits
  were needed this round.
- New finding: Bazel hard-caps shard_count at 50 per sh_test-like rule
  ("Having more than 50 shards is indicative of poor test organization";
  a native analysis-time limit, not configurable). The previous 88-shard
  plan for //cmd/bd:bd_embedded_test (chosen as the LPT-packed floor before
  this limit was discovered) is reverted to 50, Bazel's real ceiling.
  Repacking the fresh weights at 50 shards gives a heaviest shard of
  409.6s -- above the single-test floor of 239.7s, because 50 bins is not
  enough headroom over the suite's ~20145 measured test-seconds to flatten
  every bin to that floor. This makes the <=5 min lane target unreachable
  for the cmd tier via resharding alone within Bazel's native per-rule
  limit; see ~/beads-bazel-plan/f1/impl-report.md section 3c for the full
  writeup and the two remaining levers (suite-wide speedups or a
  multi-Bazel-rule split), neither attempted in this slice.
- internal/storage/embeddeddolt gets 15 Bazel-only shards (unaffected by
  the 50-shard cap): LPT-packed heaviest shard 225.3s, within target.
- cmd/bd/BUILD.bazel, internal/storage/embeddeddolt/BUILD.bazel,
  .github/workflows/bazel.yml, engdocs/CI_REQUIRED_CHECK_TOPOLOGY.md,
  scripts/embedded_shard_count_test.go, scripts/pr_risk_bazel_coverage_test.go,
  scripts/ci/gen_embedded_cmd_shard_manifest.py updated to the corrected
  50/15 shard counts (and, for the generator, N3: --write without an
  explicit total_shards now errors instead of silently regenerating the
  frozen legacy 20-shard block).
- .github/scripts/embedded-{cmd,storage}-test-shards.txt: re-packed
  Bazel-only blocks at 50/15; frozen legacy 20/5 blocks untouched except for
  N5 fixes to stale entries left by this slice's earlier test splits
  (TestShardScriptsListOnlyRealTests catches this class of bug; both
  legacy blocks reconfirmed exact-once).
- .github/workflows/nightly.yml / scripts/ci_workflow_test.go (N4): the
  embedded-dolt batch-apply nightly step now runs with `if: ${{
  !cancelled() }}` and its own explicit `-timeout 20m`, independent of the
  job-level timeout, with both halves now asserted by
  TestNightlyFullTestRunsEmbeddedDoltBatchApplySuite.

Verified: gofmt -l clean, go vet ./scripts/... clean, go test ./scripts/...
green, both generators' --check pass at 50/15 (and legacy 20/5 remain
exact-once via the frozen-block invariant).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* test(ci): rename embedded manifest staleness tests out of the ^TestEmbedded skip

The allowlisted go-test leg runs with -skip ^TestEmbedded, so tests named
TestEmbedded*ShardManifestGeneratorNotStale never ran there and the leg
failed with "matched no test that ran".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-03 21:30:03 -07:00

412 lines
13 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdDolt runs "bd dolt" with the given args and returns stdout.
func bdDolt(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"dolt"}, 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 dolt %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdDoltFail runs "bd dolt" expecting failure and returns stderr+stdout.
func bdDoltFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"dolt"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd dolt %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
// TestEmbeddedDoltBlockedAndCommit was split from TestEmbeddedDolt
// (originally ~183s, measured under --config=embedded) into 2 top-level
// tests over disjoint subtest groups, for CI shard balance (see
// scripts/ci/embedded_cmd_test_durations.json and engdocs/TESTING.md). Every
// original subtest is preserved exactly once. This group keeps the
// server-only-command-blocked, status/show, and commit subtests; the
// remote/push/pull subtests moved to TestEmbeddedDoltRemoteAndPush below.
// Every subtest in both groups only ever reads or mutates its own fresh
// `dir` (or its own ppDir/pfDir/cleanDir), so neither group depends on state
// the other group's subtests produce.
func TestEmbeddedDoltBlockedAndCommit(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", "td")
// ===== Server-only commands must fail in embedded mode =====
// status and show are intentionally NOT in this list: they report
// embedded-mode state rather than erroring out. See the
// embedded_status/embedded_show subtests below.
serverOnlyCmds := []struct {
name string
args []string
}{
{"start", []string{"start"}},
{"stop", []string{"stop"}},
{"test", []string{"test"}},
{"set", []string{"set", "host", "127.0.0.1"}},
{"killall", []string{"killall"}},
{"clean-databases", []string{"clean-databases"}},
}
for _, tc := range serverOnlyCmds {
t.Run("blocked_"+tc.name, func(t *testing.T) {
out := bdDoltFail(t, bd, dir, tc.args...)
if !strings.Contains(out, "not supported in embedded mode") {
t.Errorf("expected 'not supported in embedded mode' in output for dolt %s: %s", tc.name, out)
}
})
}
// ===== Embedded-mode inspection commands succeed with embedded-mode output =====
t.Run("embedded_status", func(t *testing.T) {
out := bdDolt(t, bd, dir, "status")
if !strings.Contains(out, "embedded") {
t.Errorf("expected embedded-mode status output to mention 'embedded': %s", out)
}
})
t.Run("embedded_status_json", func(t *testing.T) {
out := bdDolt(t, bd, dir, "status", "--json")
var result map[string]interface{}
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("status --json returned invalid JSON: %v\n%s", err, out)
}
if result["mode"] != "embedded" {
t.Errorf("mode = %v, want embedded", result["mode"])
}
if result["server_running"] != false {
t.Errorf("server_running = %v, want false", result["server_running"])
}
if _, ok := result["running"]; ok {
t.Errorf("running should be omitted for embedded mode; use server_running instead: %v", result["running"])
}
})
t.Run("embedded_show", func(t *testing.T) {
out := bdDolt(t, bd, dir, "show")
if !strings.Contains(out, "Mode:") || !strings.Contains(out, "embedded") {
t.Errorf("expected embedded-mode show output to contain 'Mode:' and 'embedded': %s", out)
}
})
// ===== Working commands =====
// Create an issue so there are pending changes to commit
bdCreate(t, bd, dir, "Dolt commit test issue", "--type", "task")
t.Run("commit", func(t *testing.T) {
out := bdDolt(t, bd, dir, "commit")
// Should succeed — either commits or reports nothing to commit
_ = out
})
t.Run("commit_with_message", func(t *testing.T) {
bdCreate(t, bd, dir, "Another issue for commit", "--type", "task")
out := bdDolt(t, bd, dir, "commit", "-m", "test commit message")
_ = out
})
t.Run("commit_clean_reports_nothing", func(t *testing.T) {
// bd init leaves the working set clean (bootstrap's own commit
// tolerates nothing-to-commit, GH#3886). A second, truly no-op
// "bd dolt commit" here must print "Nothing to commit.", not
// "Committed." — this is the case EmbeddedDoltStore.Commit's new
// nothing-to-commit tolerance made possible to reach with a nil
// error.
cleanDir, _, _ := bdInit(t, bd, "--prefix", "tdcln")
out := bdDolt(t, bd, cleanDir, "commit")
if !strings.Contains(out, "Nothing to commit.") {
t.Errorf("expected 'Nothing to commit.' on a clean working set, got: %s", out)
}
})
}
// TestEmbeddedDoltRemoteAndPush is the second half of the TestEmbeddedDolt
// split described on TestEmbeddedDoltBlockedAndCommit above: the
// remote-list/add/remove and push/pull subtests, none of which depend on
// state produced by the first group's subtests.
func TestEmbeddedDoltRemoteAndPush(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", "td")
// ===== Remote management =====
t.Run("remote_list_empty", func(t *testing.T) {
out := bdDolt(t, bd, dir, "remote", "list")
_ = out // Should succeed even with no remotes
})
t.Run("remote_add_and_list", func(t *testing.T) {
remoteDir := t.TempDir()
bdDolt(t, bd, dir, "remote", "add", "test-remote", "file://"+remoteDir)
out := bdDolt(t, bd, dir, "remote", "list")
if !strings.Contains(out, "test-remote") {
t.Errorf("expected 'test-remote' in remote list: %s", out)
}
})
t.Run("remote_remove", func(t *testing.T) {
bdDolt(t, bd, dir, "remote", "remove", "test-remote")
out := bdDolt(t, bd, dir, "remote", "list")
if strings.Contains(out, "test-remote") {
t.Errorf("expected 'test-remote' to be removed: %s", out)
}
})
// ===== Push/Pull with file remote =====
t.Run("push_to_file_remote", func(t *testing.T) {
ppDir, _, _ := bdInit(t, bd, "--prefix", "pp")
remoteDir := t.TempDir()
bdDolt(t, bd, ppDir, "remote", "add", "origin", "file://"+remoteDir)
// Create an issue and commit
bdCreate(t, bd, ppDir, "Push roundtrip issue", "--type", "task")
bdDolt(t, bd, ppDir, "commit", "-m", "roundtrip commit")
// Push to file remote
bdDolt(t, bd, ppDir, "push")
})
t.Run("push_force", func(t *testing.T) {
pfDir, _, _ := bdInit(t, bd, "--prefix", "pf")
remoteDir := t.TempDir()
bdDolt(t, bd, pfDir, "remote", "add", "origin", "file://"+remoteDir)
bdCreate(t, bd, pfDir, "Force push issue", "--type", "task")
bdDolt(t, bd, pfDir, "commit", "-m", "force push commit")
bdDolt(t, bd, pfDir, "push", "--force")
})
t.Run("push_no_remote", func(t *testing.T) {
// Push without a remote should fail gracefully, not panic
cmd := exec.Command(bd, "dolt", "push")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Log("push without remote succeeded unexpectedly")
}
_ = out
})
t.Run("pull_no_remote", func(t *testing.T) {
cmd := exec.Command(bd, "dolt", "pull")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Log("pull without remote succeeded unexpectedly")
}
_ = out
})
// ===== Push/Pull between two repos via shared file remote =====
t.Run("push_then_push_more", func(t *testing.T) {
// Push twice to verify incremental pushes work
ppDir, _, _ := bdInit(t, bd, "--prefix", "pm")
remoteDir := t.TempDir()
bdDolt(t, bd, ppDir, "remote", "add", "origin", "file://"+remoteDir)
// First push
bdCreate(t, bd, ppDir, "First push issue", "--type", "task")
bdDolt(t, bd, ppDir, "commit", "-m", "first commit")
bdDolt(t, bd, ppDir, "push")
// Second push
bdCreate(t, bd, ppDir, "Second push issue", "--type", "task")
bdDolt(t, bd, ppDir, "commit", "-m", "second commit")
bdDolt(t, bd, ppDir, "push")
})
}
// TestEmbeddedDoltConcurrent exercises dolt operations concurrently.
func TestEmbeddedDoltConcurrent(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", "dx")
// Pre-create issues so commits have something to work with
for i := 0; i < 4; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("dolt-concurrent-%d", i), "--type", "task")
}
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}
var args []string
expectFail := false
switch worker % 3 {
case 0:
args = []string{"dolt", "commit"}
case 1:
// Status is an embedded-mode inspection command.
args = []string{"dolt", "status"}
case 2:
// Server-only command should fail fast.
args = []string{"dolt", "start"}
expectFail = true
}
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if expectFail {
if err == nil {
r.err = fmt.Errorf("expected dolt %s to fail in embedded mode", args[1])
} else if !strings.Contains(string(out), "not supported in embedded mode") {
r.err = fmt.Errorf("unexpected error from dolt %s: %s", args[1], out)
}
} else {
if err != nil {
r.err = fmt.Errorf("dolt %s (worker %d): %v\n%s", strings.Join(args[1:], " "), 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)
}
}
}
// TestAdminEmbeddedBlocked verifies that bd admin subcommands are blocked in
// embedded mode. The guard was previously on adminCmd.PersistentPreRunE which
// fired before cmdCtx.ServerMode was populated, causing a false "embedded mode"
// error even in server-mode projects. The guard is now inside each subcommand's
// Run func, after store init. This test ensures embedded mode still correctly
// rejects admin commands.
func TestEmbeddedAdminBlocked(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", "ta")
blockedCmds := []struct {
name string
args []string
}{
{"compact --dolt", []string{"admin", "compact", "--dolt"}},
{"cleanup --dry-run", []string{"admin", "cleanup", "--dry-run"}},
{"reset", []string{"admin", "reset"}},
}
for _, tc := range blockedCmds {
tc := tc
t.Run("blocked_"+tc.name, func(t *testing.T) {
t.Parallel()
cmd := exec.Command(bd, tc.args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Errorf("bd %s should fail in embedded mode but succeeded: %s", tc.name, out)
return
}
if !strings.Contains(string(out), "not yet supported in embedded mode") {
t.Errorf("bd %s: expected 'not yet supported in embedded mode', got: %s", tc.name, out)
}
})
}
}
// TestAdminEmbeddedCompactReadOnlyAllowed verifies read-only compact modes are
// not blocked by the embedded-mode admin guard.
func TestEmbeddedAdminCompactReadOnlyAllowed(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", "ta")
readOnlyCmds := []struct {
name string
args []string
}{
{"compact --stats", []string{"admin", "compact", "--stats"}},
{"compact --dolt --dry-run", []string{"admin", "compact", "--dolt", "--dry-run"}},
}
for _, tc := range readOnlyCmds {
tc := tc
t.Run("allowed_"+tc.name, func(t *testing.T) {
t.Parallel()
cmd := exec.Command(bd, tc.args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("bd %s should be allowed in embedded mode: %v\n%s", tc.name, err, out)
return
}
if strings.Contains(string(out), "not yet supported in embedded mode") {
t.Errorf("bd %s hit embedded-mode guard unexpectedly: %s", tc.name, out)
}
})
}
}