beads/cmd/bd/backup_embedded_test.go
Julian Knutsen c67a36e5e7
fix(cli): refuse backup init/sync/remove/restore and import under strict --readonly (#7673)
* fix(cli): refuse backup init/sync/remove and import under --readonly

Strict --readonly is meant to refuse every command that writes, with
"Error: operation '<op>' is not allowed in read-only mode" and exit 1.
bd backup init, backup sync, backup remove and bd import never called
CheckReadonly, so that gate never refused them:

- On a server-mode store they ran to completion under --readonly:
  backup init and backup remove rewrote or emptied the server's
  dolt_backups registry, backup sync wrote the backup destination, and
  import wrote issues.
- On an embedded store they failed only because the store was opened
  read-only, with the storage layer's "store is read-only" text, and
  backup sync took the backup lock first and left .beads/backup.lock
  behind.

The three backup verbs now call CheckReadonly first in their RunE, as
the other write commands do. Its migration-freeze half is redundant for
them: their leaf names are not on the read-only list and they have no
preview flag, so PersistentPreRunE's early freeze gate refuses them
first.

bd import calls it only when --readonly is set. CheckReadonly also
answers a migration freeze, and does so by exiting the process, while
runImport answers a freeze by returning the error so cleanup still
runs; guarding the call leaves that path exactly as it was.

bd import --dry-run is refused too. The preview opens the store
read-only and writes nothing, so this is a policy choice. It matches
create --dry-run (also refused under --readonly) and import's own
freeze posture: a migration freeze already refuses import --dry-run
(TestImportBlockedDuringMigrationFreeze), and CheckReadonly treats the
two gates as one. The cost is small, since the same preview runs
without --readonly. bd backup status and bd backup itself (a command
group that only prints help) stay allowed.

TestBackupAndImportBlockedInReadonlyMode runs each command, plus
import --dry-run, under --readonly and --readonly --sandbox against an
embedded workspace and one on the Dolt test server. Each row expects
exit 1, exactly the standard refusal on stderr, an empty stdout, and
nothing changed: the .beads tree, the backup destination, the issue
count, and, in server mode, the dolt_backups registry.
TestBackupStatusAndListWorkInReadonlyMode is the control showing that
backup status and list still run.

Ported from the versioned-beads fork: b86df6a9eb (test) and
5a067acc56 (fix). The comment on TestImportBlockedDuringMigrationFreeze
is updated, since runImport now calls CheckReadonly under --readonly.

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(backup): refuse --readonly backup restore before the exclusive gate

A restore replaces the store, so strict --readonly must refuse it.
Nothing did:

- In server mode no storage layer stops the connection, so
  `bd --readonly backup restore --force` ran to the end: exit 0,
  "Restore complete", and the store was replaced.
- In embedded mode the command took the EXCLUSIVE workspace gate first,
  leaving the holder sidecars (.beads.gate.lock.info,
  .beads/embeddeddolt.gate.lock.info) behind, and was stopped only
  later by the storage layer's "store is read-only".

acquireCommandWorkspaceGates now calls CheckReadonly("backup restore")
first for the exclusive (restore) case when readonlyMode is set. That
is before any gate file is created or the store is opened, so nothing
is held and the exit skips no release. The guard checks readonlyMode
alone, so a restore without --readonly, including one refused by a
migration freeze, keeps its existing order. The restore RunE also calls
CheckReadonly first, as every write command does.

This does not depend on the strict-readonly SHARED-gate change
(workspacegate.Options.ReadOnly): the refusal comes before any gate is
built.

The one visible change in proxied-server mode, where every --readonly
command is already refused: `bd --readonly backup restore` now gets
this standard refusal (plain text, exit 1). Before, the proxy
capability refusal answered it (exit 1, typed JSON under --json), and
only after the exclusive gate had been taken.

TestEmbeddedBackupRestoreStrictReadonly and
TestBackupRestoreStrictReadonlyServerMode run the built bd as a
subprocess, because CheckReadonly exits. The workspace's live store
holds three issues and its backup holds two. Each test asserts the
standard refusal with exit 1 and an empty stdout, no storage-layer
message, an unchanged project directory and .beads tree, and an
unchanged issue count. Each runs under --readonly and under
--readonly --sandbox. A control restores without --readonly and
expects two issues, so "unchanged" cannot be vacuous.

Ported from the versioned-beads fork: 1cd5f1c202 (test) and
6ac79d6207 (fix). The fork's test used a dirEntryNames helper that
belongs to its separate SHARED-gate change. It is inlined here so this
change compiles and lands on its own in either order. The server test's
comment is corrected too: the fixture's backup and the restore both run
inside the server at the same path, so it passes against a
containerized test server as well as a local one.

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Jim Wordelman <jim@wordelman.name>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 08:48:37 +00:00

294 lines
9.4 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"testing"
)
// bdBackup runs "bd backup" with extra args. Returns combined output.
func bdBackup(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"backup"}, 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 backup %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdBackupFail runs "bd backup" expecting failure. Returns combined output.
func bdBackupFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"backup"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("bd backup %s should have failed, got: %s", strings.Join(args, " "), out)
}
return string(out)
}
func TestEmbeddedBackup(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt backup tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
t.Run("status_no_backup", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "bkstat0")
out := bdBackup(t, bd, dir, "status")
if !strings.Contains(out, "No backup has been performed") {
t.Errorf("expected 'No backup has been performed', got: %s", out)
}
})
t.Run("status_after_init_sync", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "bkstat1")
bdCreateSilent(t, bd, dir, "status test issue")
backupDest := filepath.Join(t.TempDir(), "dolt-backup-status")
backupURL := "file://" + backupDest
bdBackup(t, bd, dir, "init", backupURL)
bdBackup(t, bd, dir, "sync")
out := bdBackup(t, bd, dir, "status")
if out == "" {
t.Error("expected non-empty status output")
}
if strings.Contains(out, "Database size: 0 B") {
t.Errorf("embedded backup status reported an empty database: %s", out)
}
jsonOut := bdBackup(t, bd, dir, "status", "--json")
var status struct {
DatabaseSize struct {
Bytes int64 `json:"bytes"`
} `json:"database_size"`
}
if err := json.Unmarshal([]byte(jsonOut), &status); err != nil {
t.Fatalf("parse backup status JSON: %v\noutput: %s", err, jsonOut)
}
if status.DatabaseSize.Bytes <= 0 {
t.Errorf("database_size.bytes = %d, want > 0; output: %s", status.DatabaseSize.Bytes, jsonOut)
}
})
t.Run("init_and_sync", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "bkinit")
bdCreateSilent(t, bd, dir, "init sync test issue")
// Create a backup destination directory
backupDest := filepath.Join(t.TempDir(), "dolt-backup")
backupURL := "file://" + backupDest
// Init backup destination
out := bdBackup(t, bd, dir, "init", backupURL)
if !strings.Contains(out, "Backup destination configured") {
t.Errorf("expected 'Backup destination configured', got: %s", out)
}
// Sync to backup
out = bdBackup(t, bd, dir, "sync")
if !strings.Contains(out, "Backup synced") {
t.Errorf("expected 'Backup synced', got: %s", out)
}
// Verify backup destination has data
if _, err := os.Stat(backupDest); os.IsNotExist(err) {
t.Error("backup destination directory should exist after sync")
}
})
t.Run("sync_without_init", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "bksync0")
out := bdBackupFail(t, bd, dir, "sync")
if !strings.Contains(out, "no backup") && !strings.Contains(out, "not found") &&
!strings.Contains(out, "No backup") {
t.Errorf("expected backup-not-configured error, got: %s", out)
}
})
t.Run("restore_from_backup", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "bksrc")
bdCreateSilent(t, bd, dir, "restore test issue A")
bdCreateSilent(t, bd, dir, "restore test issue B")
// Create a Dolt backup via init + sync
backupDest := filepath.Join(t.TempDir(), "dolt-backup-restore")
if err := os.MkdirAll(backupDest, 0750); err != nil {
t.Fatal(err)
}
backupURL := "file://" + backupDest
bdBackup(t, bd, dir, "init", backupURL)
bdBackup(t, bd, dir, "sync")
// Restore into a fresh environment. Use a prefix that differs
// from the source ("bksrc") so the restored database name
// doesn't collide with the target's own database.
dir2, _, _ := bdInit(t, bd, "--prefix", "bkdst")
// The restore will create a database named "bkdst" from the
// backup, but "bkdst" already exists from bd init. Use
// bdBackupFail to confirm the expected "already exists" error.
out := bdBackupFail(t, bd, dir2, "restore", backupDest)
if !strings.Contains(out, "already exists") {
t.Errorf("expected 'already exists' error, got: %s", out)
}
})
}
func TestEmbeddedBackupConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt backup tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "bkconc")
// Seed some data
for i := 0; i < 5; i++ {
bdCreateSilent(t, bd, dir, fmt.Sprintf("concurrent backup issue %d", i))
}
// Set up a backup destination for sync
backupDest := filepath.Join(t.TempDir(), "dolt-backup-concurrent")
backupURL := "file://" + backupDest
bdBackup(t, bd, dir, "init", backupURL)
const numWorkers = 5
type result struct {
worker int
out string
err error
}
results := make([]result, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
cmd := exec.Command(bd, "backup", "sync")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
results[worker] = result{worker: worker, out: string(out), err: err}
}(w)
}
wg.Wait()
successes := 0
for _, r := range results {
if strings.Contains(r.out, "panic") {
t.Errorf("worker %d panicked:\n%s", r.worker, r.out)
}
if r.err == nil {
successes++
} else if !strings.Contains(r.out, "one writer at a time") {
t.Errorf("worker %d failed with unexpected error: %v\n%s", r.worker, r.err, r.out)
}
}
if successes < 1 {
t.Errorf("expected at least 1 successful backup, got %d", successes)
}
t.Logf("%d/%d backup workers succeeded", successes, numWorkers)
if _, err := os.Stat(backupDest); os.IsNotExist(err) {
t.Error("backup destination should exist after concurrent syncs")
}
}
// TestEmbeddedBackupRestoreStrictReadonly pins the refusal of a strict
// --readonly restore. A restore replaces the store, so CheckReadonly must stop
// it with the standard message before the command takes the exclusive workspace
// gate. Embedded mode used to be stopped only by the storage layer
// ("embeddeddolt: store is read-only"), after the gate had already left its
// holder files in the workspace. The control proves the fixture really does
// restore when --readonly is not asked for, so "the store is unchanged" below
// cannot be vacuous.
func TestEmbeddedBackupRestoreStrictReadonly(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt backup tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// newWorkspace returns a workspace whose live store holds one issue its
// backup does not (three against two), so a restore that really ran shows up
// as a changed issue count. Each case gets its own: a restore that runs
// replaces the store and would hide a leak from the next.
newWorkspace := func(t *testing.T, prefix string) (dir, backupDir string) {
t.Helper()
dir, _, _ = bdInit(t, bd, "--prefix", prefix)
bdCreateSilent(t, bd, dir, "issue in the backup A")
bdCreateSilent(t, bd, dir, "issue in the backup B")
backupDir = filepath.Join(t.TempDir(), "dolt-backup")
if err := os.MkdirAll(backupDir, 0o750); err != nil {
t.Fatal(err)
}
bdBackup(t, bd, dir, "init", "file://"+backupDir)
bdBackup(t, bd, dir, "sync")
bdCreateSilent(t, bd, dir, "issue created after the backup")
return dir, backupDir
}
t.Run("control_restore_without_readonly", func(t *testing.T) {
dir, backupDir := newWorkspace(t, "rrctl")
stdout, stderr, code := runBDMigrationFreeze(t, bd, dir, "backup", "restore", "--force", backupDir)
if code != 0 {
t.Fatalf("restore without --readonly exited %d\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr)
}
if !strings.Contains(stdout, "Restore complete") {
t.Errorf("stdout is missing %q:\n%s", "Restore complete", stdout)
}
if got := len(bdListJSON(t, bd, dir)); got != 2 {
t.Errorf("issue count after a real restore = %d, want 2: the backup holds two issues and the live store held three", got)
}
})
for _, tc := range []struct {
name string
prefix string
flags []string
}{
{"readonly", "rrro", []string{"--readonly"}},
{"readonly_sandbox", "rrrosb", []string{"--readonly", "--sandbox"}},
} {
t.Run(tc.name, func(t *testing.T) {
dir, backupDir := newWorkspace(t, tc.prefix)
before := snapshotWorkspace(t, dir)
args := append(append([]string{}, tc.flags...), "backup", "restore", "--force", backupDir)
stdout, stderr, code := runBDMigrationFreeze(t, bd, dir, args...)
assertStrictReadonlyRestoreRefused(t, stdout, stderr, code)
assertWorkspaceUnchanged(t, dir, before)
if got := len(bdListJSON(t, bd, dir)); got != 3 {
t.Errorf("issue count after the refused restore = %d, want 3: the restore replaced the store", got)
}
})
}
}