diff --git a/.github/scripts/embedded-cmd-test-shards.txt b/.github/scripts/embedded-cmd-test-shards.txt index 4ee97330a..fd86a752e 100644 --- a/.github/scripts/embedded-cmd-test-shards.txt +++ b/.github/scripts/embedded-cmd-test-shards.txt @@ -120,6 +120,7 @@ 100 48 TestEmbeddedDeferConcurrent +100 49 TestEmbeddedBackupRestoreStrictReadonly 100 49 TestEmbeddedInitAPushConsentAndRemoteBootstrap 100 50 TestEmbeddedDiffConcurrent diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dbc7a7fc..56382868f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -786,6 +786,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 existing gate file read-only and holds it shared as before, so a maintenance operation still sees it and a running one still makes it wait; when no gate file exists it holds nothing. Every other command still creates the files. +- **Strict `--readonly` refuses `bd backup init`, `backup sync`, + `backup remove`, `backup restore` and `bd import`.** None of them checked + readonly mode. On a server-mode store they ran to completion under + `--readonly`: `backup init` and `backup remove` rewrote or emptied the + server's backup registry, `backup sync` wrote the backup destination, + `backup restore --force` replaced the store, and `import` wrote issues. On + an embedded store they failed with the storage layer's `store is read-only` + error instead, after `backup sync` had left `.beads/backup.lock` behind and + `backup restore` had left workspace-gate holder files. They now exit 1 with + the standard `Error: operation '' is not allowed in read-only mode`, + and a restore is refused before it takes the exclusive workspace gate. + `bd import --dry-run` is refused too, as `bd create --dry-run` already is + and as a migration freeze already refuses it. `bd backup status` still runs + under `--readonly`. - **`bd init --server --external` no longer starts a Dolt server of its own.** Init builds its store config by hand and turned auto-start on for every server init, so an `--external` init that named no port launched a diff --git a/cmd/bd/backup_dolt.go b/cmd/bd/backup_dolt.go index 3e2aebbd7..2b8675242 100644 --- a/cmd/bd/backup_dolt.go +++ b/cmd/bd/backup_dolt.go @@ -53,6 +53,8 @@ After adding, run 'bd backup sync' to push your data.`, SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { + CheckReadonly("backup init") + evt := metrics.NewCommandEvent("backup-init") defer func() { if c := metrics.Global(); c != nil { @@ -134,6 +136,8 @@ Run 'bd backup init ' first to configure a destination.`, SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { + CheckReadonly("backup sync") + evt := metrics.NewCommandEvent("backup-sync") defer func() { if c := metrics.Global(); c != nil { @@ -439,6 +443,8 @@ backup configuration. The backup data at the destination is not deleted.`, SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { + CheckReadonly("backup remove") + evt := metrics.NewCommandEvent("backup-remove") defer func() { if c := metrics.Global(); c != nil { diff --git a/cmd/bd/backup_embedded_test.go b/cmd/bd/backup_embedded_test.go index c4e94d9fc..8e3795e64 100644 --- a/cmd/bd/backup_embedded_test.go +++ b/cmd/bd/backup_embedded_test.go @@ -217,3 +217,78 @@ func TestEmbeddedBackupConcurrent(t *testing.T) { 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) + } + }) + } +} diff --git a/cmd/bd/backup_restore.go b/cmd/bd/backup_restore.go index aa508838a..0d81f74e1 100644 --- a/cmd/bd/backup_restore.go +++ b/cmd/bd/backup_restore.go @@ -34,6 +34,8 @@ To initialize and restore in one step, use: bd init && bd backup restore`, SilenceUsage: true, SilenceErrors: true, RunE: func(cmd *cobra.Command, args []string) error { + CheckReadonly("backup restore") + evt := metrics.NewCommandEvent("backup-restore") defer func() { if c := metrics.Global(); c != nil { diff --git a/cmd/bd/backup_restore_test.go b/cmd/bd/backup_restore_test.go index baa363a64..46ea7f574 100644 --- a/cmd/bd/backup_restore_test.go +++ b/cmd/bd/backup_restore_test.go @@ -4,14 +4,22 @@ package main import ( "context" + "encoding/json" + "fmt" "os" "path/filepath" + "slices" + "sort" "strings" "testing" + "time" "github.com/steveyegge/beads/internal/beads" + "github.com/steveyegge/beads/internal/configfile" "github.com/steveyegge/beads/internal/git" + "github.com/steveyegge/beads/internal/storage/dolt" "github.com/steveyegge/beads/internal/testutil" + "github.com/steveyegge/beads/internal/types" ) func TestBackupRestoreMissingDir(t *testing.T) { @@ -84,3 +92,257 @@ func TestSyncProjectIDFromDB_NoWorkspaceUsesActiveWorkspaceError(t *testing.T) { t.Fatalf("syncProjectIDFromDB should not create local .beads, stat err = %v", statErr) } } + +// strictReadonlyRestoreRefusal is the line CheckReadonly prints when a write +// command is run under strict --readonly. +const strictReadonlyRestoreRefusal = "Error: operation 'backup restore' is not allowed in read-only mode\n" + +// assertStrictReadonlyRestoreRefused checks one refused `bd --readonly backup +// restore` run: the standard CheckReadonly refusal (text and exit code), nothing +// on stdout, and none of the storage-layer message that embedded mode gives when +// the command gets past CheckReadonly and opens the store. +func assertStrictReadonlyRestoreRefused(t *testing.T, stdout, stderr string, code int) { + t.Helper() + if code != 1 { + t.Errorf("exit code = %d, want 1\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + if !strings.Contains(stderr, strictReadonlyRestoreRefusal) { + t.Errorf("stderr is missing the standard refusal %q:\n%s", strictReadonlyRestoreRefusal, stderr) + } + if strings.Contains(stderr, "embeddeddolt: store is read-only") { + t.Errorf("stderr carries the storage-layer refusal, so the command opened the store before being refused:\n%s", stderr) + } + if strings.TrimSpace(stdout) != "" { + t.Errorf("stdout should be empty when the restore is refused, got:\n%s", stdout) + } +} + +// workspaceSnapshot is what a refused command must leave alone: the entries of +// the project directory (the workspace gate file and its holder sidecar sit +// beside .beads, not in it) and everything below .beads, content included. +type workspaceSnapshot struct { + projectEntries []string + beads readonlyTreeSnapshot +} + +func snapshotWorkspace(t *testing.T, dir string) workspaceSnapshot { + t.Helper() + entries, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + names := make([]string, 0, len(entries)) + for _, e := range entries { + names = append(names, e.Name()) + } + return workspaceSnapshot{ + projectEntries: names, + beads: snapshotReadonlyTree(t, filepath.Join(dir, ".beads")), + } +} + +// assertWorkspaceUnchanged fails, naming each path, when the workspace under dir +// no longer matches a snapshot taken before a refused command. +func assertWorkspaceUnchanged(t *testing.T, dir string, before workspaceSnapshot) { + t.Helper() + after := snapshotWorkspace(t, dir) + var diffs []string + for _, name := range before.projectEntries { + if !slices.Contains(after.projectEntries, name) { + diffs = append(diffs, "removed: "+name) + } + } + for _, name := range after.projectEntries { + if !slices.Contains(before.projectEntries, name) { + diffs = append(diffs, "created: "+name) + } + } + for path, want := range before.beads.Entries { + switch got, ok := after.beads.Entries[path]; { + case !ok: + diffs = append(diffs, "removed: .beads/"+path) + case got != want: + diffs = append(diffs, "changed: .beads/"+path) + } + } + for path := range after.beads.Entries { + if _, ok := before.beads.Entries[path]; !ok { + diffs = append(diffs, "created: .beads/"+path) + } + } + if len(diffs) == 0 { + return + } + sort.Strings(diffs) + const show = 20 + if len(diffs) > show { + diffs = append(diffs[:show], fmt.Sprintf("... and %d more", len(diffs)-show)) + } + t.Errorf("the refused restore changed the workspace:\n%s", strings.Join(diffs, "\n")) +} + +// TestBackupRestoreStrictReadonlyServerMode is the server-mode counterpart of +// TestEmbeddedBackupRestoreStrictReadonly. Server mode is where the missing +// refusal mattered: no storage layer stops a server connection, so a +// `bd --readonly backup restore --force` ran to the end and replaced the store. +// It drives the built bd as a subprocess because CheckReadonly exits the +// process. Both the fixture's backup and the restore run inside the Dolt +// server, against the same path on the server's own filesystem, so a local +// and a containerized test server both serve. +func TestBackupRestoreStrictReadonlyServerMode(t *testing.T) { + if testDoltServerPort == 0 { + t.Skip("Dolt test server not available") + } + if testutil.DoltContainerCrashed() { + t.Skipf("Dolt test server crashed: %v", testutil.DoltContainerCrashError()) + } + + bd := buildBDForInitTests(t) + + // newWorkspace builds a server-mode workspace on the test server 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) (dir string, env []string, backupDir string) { + t.Helper() + dir = t.TempDir() + beadsDir := filepath.Join(dir, ".beads") + if err := os.MkdirAll(beadsDir, 0o700); err != nil { + t.Fatal(err) + } + database := uniqueTestDBName(t) + if err := (&configfile.Config{ + Backend: configfile.BackendDolt, + DoltMode: configfile.DoltModeServer, + DoltServerHost: "127.0.0.1", + DoltServerPort: testDoltServerPort, + DoltDatabase: database, + }).Save(beadsDir); err != nil { + t.Fatalf("save workspace metadata: %v", err) + } + + ctx := context.Background() + seedStore, err := dolt.New(ctx, &dolt.Config{ + Path: filepath.Join(beadsDir, "dolt"), + BeadsDir: beadsDir, + ServerHost: "127.0.0.1", + ServerPort: testDoltServerPort, + Database: database, + CreateIfMissing: true, + }) + if err != nil { + t.Fatalf("create the server-mode database: %v", err) + } + t.Cleanup(func() { dropTestDatabase(database, testDoltServerPort) }) + defer func() { _ = seedStore.Close() }() + + if err := seedStore.SetConfig(ctx, "issue_prefix", "rr"); err != nil { + t.Fatalf("set issue_prefix: %v", err) + } + seed := func(id, title string) { + t.Helper() + now := time.Now() + issue := &types.Issue{ + ID: id, + Title: title, + Status: types.StatusOpen, + Priority: 2, + IssueType: types.TypeTask, + CreatedAt: now, + UpdatedAt: now, + } + if err := seedStore.CreateIssue(ctx, issue, "test-user"); err != nil { + t.Fatalf("create issue %s: %v", id, err) + } + } + commit := func(message string) { + t.Helper() + if err := seedStore.Commit(ctx, message); err != nil && !strings.Contains(err.Error(), "nothing to commit") { + t.Fatalf("commit %q: %v", message, err) + } + } + + seed("rr-1", "issue in the backup A") + seed("rr-2", "issue in the backup B") + commit("issues the backup holds") + + backupDir = filepath.Join(t.TempDir(), "dolt-backup") + if err := os.MkdirAll(backupDir, 0o750); err != nil { + t.Fatal(err) + } + if err := seedStore.BackupDatabase(ctx, backupDir); err != nil { + t.Fatalf("back up the database (the Dolt server must be able to write %s): %v", backupDir, err) + } + + seed("rr-3", "issue created after the backup") + commit("issue the backup lacks") + + env = []string{ + "BEADS_TEST_MODE=1", + // The subprocess connects to a testdb_* database on the test server; + // this is the opt-in the test-database firewall asks for. + "BEADS_TEST_SERVER=1", + "BEADS_DIR=" + beadsDir, + "XDG_CONFIG_HOME=" + t.TempDir(), + // Keep the subprocess's circuit-breaker state in the suite's own + // directory rather than the host's temp dir. + "BEADS_TEST_CIRCUIT_DIR=" + os.Getenv("BEADS_TEST_CIRCUIT_DIR"), + } + return dir, env, backupDir + } + + issueCount := func(t *testing.T, dir string, env []string) int { + t.Helper() + stdout, stderr, code := runBDMigrationFreezeWithEnv(t, bd, dir, env, "list", "--json") + if code != 0 { + t.Fatalf("bd list --json exited %d\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + start := strings.Index(stdout, "[") + if start < 0 { + t.Fatalf("no JSON array in bd list --json output:\n%s", stdout) + } + var issues []json.RawMessage + if err := json.Unmarshal([]byte(stdout[start:]), &issues); err != nil { + t.Fatalf("parse bd list --json output: %v\n%s", err, stdout) + } + return len(issues) + } + + // The control proves the fixture really does restore when --readonly is not + // asked for, so "the store is unchanged" below cannot be vacuous. + t.Run("control_restore_without_readonly", func(t *testing.T) { + dir, env, backupDir := newWorkspace(t) + stdout, stderr, code := runBDMigrationFreezeWithEnv(t, bd, dir, env, "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 := issueCount(t, dir, env); 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 + flags []string + }{ + {"readonly", []string{"--readonly"}}, + {"readonly_sandbox", []string{"--readonly", "--sandbox"}}, + } { + t.Run(tc.name, func(t *testing.T) { + dir, env, backupDir := newWorkspace(t) + before := snapshotWorkspace(t, dir) + + args := append(append([]string{}, tc.flags...), "backup", "restore", "--force", backupDir) + stdout, stderr, code := runBDMigrationFreezeWithEnv(t, bd, dir, env, args...) + + assertStrictReadonlyRestoreRefused(t, stdout, stderr, code) + assertWorkspaceUnchanged(t, dir, before) + if got := issueCount(t, dir, env); got != 3 { + t.Errorf("issue count after the refused restore = %d, want 3: the restore replaced the store", got) + } + }) + } +} diff --git a/cmd/bd/doctor_mutation_gate_test.go b/cmd/bd/doctor_mutation_gate_test.go index 44d463c14..a85736d16 100644 --- a/cmd/bd/doctor_mutation_gate_test.go +++ b/cmd/bd/doctor_mutation_gate_test.go @@ -9,16 +9,28 @@ // beside the rest of the freeze suite; both halves share the helpers below and // the hermetic subprocess harness declared there. // +// The same gate for `bd backup init`, `backup remove`, `backup sync` and +// `bd import` sits further down, between the doctor tests it mirrors. +// // This file MUST NOT carry a cgo build tag: it drives a bd binary built with -// the gms_pure_go tag via subprocess, and asserts only on files and output. +// the gms_pure_go tag via subprocess, and asserts on files and output (and, for +// the server rows of the backup/import tests, on the Dolt test server's tables). package main import ( + "context" + "database/sql" + "encoding/json" + "fmt" "os" "path/filepath" + "sort" + "strconv" "strings" "testing" + + "github.com/steveyegge/beads/internal/storage/doltutil" ) // doctorMutationSurface is one class of doctor invocation that mutates the @@ -122,6 +134,306 @@ func TestDoctorMutationBlockedInReadonlyMode(t *testing.T) { } } +// The backup and import commands: the same gate, a different failure. +// +// `bd backup init`, `backup remove`, `backup sync` and `bd import` never called +// CheckReadonly, so strict --readonly only held as far as the store underneath +// happened to refuse. An embedded store is opened read-only, so those commands +// failed, but with the storage layer's text rather than the standard refusal, +// and `backup sync` took the backup lock first and left .beads/backup.lock +// behind. A server-mode store has no such floor: the same commands ran to +// completion and rewrote or emptied the backup registry, wrote the backup +// destination, or imported issues. So every row runs in both flavours, and the +// checks go past the file tree: a server-mode import leaves .beads untouched +// even when it succeeds, so the issue count and the registry are compared too. +// +// `backup status` and `list` only read, and stay allowed; +// TestBackupStatusAndListWorkInReadonlyMode pins that. `bd backup` on its own is +// a command group that only prints help, and is left as it is. + +// backupImportSurface is one command that strict --readonly must refuse. +type backupImportSurface struct { + name string + // args are the command and its arguments; the test appends the global flags. + args []string + // op is the operation label the gate reports. + op string +} + +func backupImportSurfaces(w *readonlyGateWorkspace) []backupImportSurface { + return []backupImportSurface{ + // A destination other than the registered one, so a run that goes + // through shows up as a changed registry. + {name: "backup-init", args: []string{"backup", "init", filepath.Join(w.dir, "other-backup-dest")}, op: "backup init"}, + {name: "backup-remove", args: []string{"backup", "remove"}, op: "backup remove"}, + {name: "backup-sync", args: []string{"backup", "sync"}, op: "backup sync"}, + {name: "import", args: []string{"import", w.importFile}, op: "import"}, + // A preview writes nothing, but strict --readonly already refuses + // `create --dry-run`, so import's is refused the same way. + {name: "import-dry-run", args: []string{"import", "--dry-run", w.importFile}, op: "import"}, + } +} + +// readonlyGateFlagSets are the global flag combinations every row runs under. +// --sandbox changes how bd treats the store (it disables auto-push); the gate +// has to hold with it as well. +var readonlyGateFlagSets = []struct { + name string + flags []string +}{ + {name: "readonly", flags: []string{"--readonly"}}, + {name: "readonly-sandbox", flags: []string{"--readonly", "--sandbox"}}, +} + +// readonlyGateFlavours are the two stores the gate has to hold over. +var readonlyGateFlavours = []struct { + name string + setup func(*testing.T) *readonlyGateWorkspace +}{ + {name: "embedded", setup: newEmbeddedReadonlyGateWorkspace}, + {name: "server", setup: newServerReadonlyGateWorkspace}, +} + +// readonlyGateWorkspace is an initialized workspace holding one issue and one +// registered backup, plus what a refused command has to leave alone. +type readonlyGateWorkspace struct { + bd, dir string + prefix string + dest string // directory behind the registered default backup + importFile string // a one-issue JSONL file for `bd import` + // Server flavour only: the shared test server and this workspace's database + // on it. A zero port means the embedded flavour. + port int + database string +} + +// readonlyGateState is everything a refused command could have changed. +type readonlyGateState struct { + tree readonlyTreeSnapshot // the whole .beads directory + dest readonlyTreeSnapshot // the backup destination + issues int // what `bd list` reports + registry string // server flavour only: dolt_backups as name=url rows +} + +func newEmbeddedReadonlyGateWorkspace(t *testing.T) *readonlyGateWorkspace { + t.Helper() + bd, dir := setupMigrationFreezeWorkspace(t) + w := &readonlyGateWorkspace{bd: bd, dir: dir, prefix: "test"} + w.seed(t) + return w +} + +// newServerReadonlyGateWorkspace is the same workspace on the shared Dolt test +// server. The default lane starts none, so it skips there, as the other +// server-mode readonly test in this package does. +func newServerReadonlyGateWorkspace(t *testing.T) *readonlyGateWorkspace { + t.Helper() + port, err := strconv.Atoi(os.Getenv("BEADS_DOLT_PORT")) + if err != nil || port <= 0 { + t.Skip("shared Dolt test server is unavailable") + } + dir := t.TempDir() + bd := buildBDForInitTests(t) + runGitForBootstrapTest(t, dir, "init", "-q") + runGitForBootstrapTest(t, dir, "config", "core.hooksPath", ".git/hooks") + + // The prefix names the database on the shared server, so it must be unique. + prefix := "rog" + strings.TrimPrefix(uniqueTestDBName(t), "testdb_") + stdout, stderr, code := runBDMigrationFreeze(t, bd, dir, "init", "--server", "--server-host", "127.0.0.1", + "--server-port", strconv.Itoa(port), "--external", "--prefix", prefix, + "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents") + if code != 0 { + t.Fatalf("bd init --server failed (exit %d):\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + var meta struct { + Database string `json:"dolt_database"` + } + data, err := os.ReadFile(filepath.Join(dir, ".beads", "metadata.json")) + if err != nil { + t.Fatalf("reading metadata.json: %v", err) + } + if err := json.Unmarshal(data, &meta); err != nil || meta.Database == "" { + t.Fatalf("metadata.json names no dolt_database (%v):\n%s", err, data) + } + t.Cleanup(func() { + db, err := sql.Open("mysql", doltutil.ServerDSN{Host: "127.0.0.1", Port: port, User: "root"}.String()) + if err != nil { + return + } + defer db.Close() + _, _ = db.ExecContext(context.Background(), fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", meta.Database)) //nolint:gosec // generated test name + }) + + w := &readonlyGateWorkspace{bd: bd, dir: dir, prefix: prefix, port: port, database: meta.Database} + w.seed(t) + return w +} + +// seed gives the workspace one issue, one registered backup and a JSONL file +// whose single issue is not in the workspace yet. +func (w *readonlyGateWorkspace) seed(t *testing.T) { + t.Helper() + w.dest = filepath.Join(w.dir, "backup-dest") + w.importFile = filepath.Join(w.dir, "import.jsonl") + record := fmt.Sprintf(`{"id":"%s-gate1","title":"imported under readonly","status":"open","priority":2,"issue_type":"task","created_at":"2026-01-01T00:00:00Z"}`+"\n", w.prefix) + if err := os.WriteFile(w.importFile, []byte(record), 0o644); err != nil { + t.Fatalf("writing %s: %v", w.importFile, err) + } + if stdout, stderr, code := w.run(t, "create", "seed issue", "-p", "2"); code != 0 { + t.Fatalf("bd create failed (exit %d):\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + w.registerBackup(t) +} + +func (w *readonlyGateWorkspace) run(t *testing.T, args ...string) (stdout, stderr string, exitCode int) { + t.Helper() + return runBDMigrationFreeze(t, w.bd, w.dir, args...) +} + +// registerBackup points the default backup at w.dest. Rows call it before each +// run: a row that went through when it should have been refused may have moved +// or removed the backup, and the next row still needs one to be refused against. +func (w *readonlyGateWorkspace) registerBackup(t *testing.T) { + t.Helper() + if stdout, stderr, code := w.run(t, "backup", "init", "file://"+w.dest); code != 0 { + t.Fatalf("bd backup init failed (exit %d):\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } +} + +func (w *readonlyGateWorkspace) state(t *testing.T) readonlyGateState { + t.Helper() + // Count the issues first: that is itself a strict --readonly read, so + // anything it creates lazily exists before the snapshot instead of showing + // up in it as a change. + s := readonlyGateState{issues: w.issueCount(t)} + s.tree = snapshotReadonlyTree(t, filepath.Join(w.dir, ".beads")) + s.dest = snapshotReadonlyTree(t, w.dest) + if w.port != 0 { + s.registry = w.backupRegistry(t) + } + return s +} + +func (w *readonlyGateWorkspace) issueCount(t *testing.T) int { + t.Helper() + stdout, stderr, code := w.run(t, "list", "--json", "--readonly") + if code != 0 { + t.Fatalf("bd list failed (exit %d):\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + var issues []json.RawMessage + if err := json.Unmarshal([]byte(stdout), &issues); err != nil { + t.Fatalf("bd list --json is not a JSON array: %v\n%s", err, stdout) + } + return len(issues) +} + +// backupRegistry reads the backups the server has registered for this +// workspace's database. No bd command prints them, and the file tree never +// shows them, so a refused run is checked against the server itself. +func (w *readonlyGateWorkspace) backupRegistry(t *testing.T) string { + t.Helper() + db, err := sql.Open("mysql", doltutil.ServerDSN{Host: "127.0.0.1", Port: w.port, User: "root", Database: w.database}.String()) + if err != nil { + t.Fatalf("connecting to the test server: %v", err) + } + defer db.Close() + rows, err := db.QueryContext(context.Background(), "SELECT name, url FROM dolt_backups ORDER BY name") + if err != nil { + t.Fatalf("reading dolt_backups: %v", err) + } + defer rows.Close() + var lines []string + for rows.Next() { + var name, url string + if err := rows.Scan(&name, &url); err != nil { + t.Fatalf("scanning dolt_backups: %v", err) + } + lines = append(lines, name+"="+url) + } + if err := rows.Err(); err != nil { + t.Fatalf("reading dolt_backups: %v", err) + } + return strings.Join(lines, "\n") +} + +// assertUnchanged proves a refusal happened before any mutation, not merely +// that the refusal text was printed. +func (w *readonlyGateWorkspace) assertUnchanged(t *testing.T, before readonlyGateState) { + t.Helper() + after := w.state(t) + if diff := readonlyTreeDiff(before.tree, after.tree); len(diff) > 0 { + t.Errorf("a refused run changed .beads:\n %s", strings.Join(diff, "\n ")) + } + if diff := readonlyTreeDiff(before.dest, after.dest); len(diff) > 0 { + t.Errorf("a refused run changed the backup destination:\n %s", strings.Join(diff, "\n ")) + } + if after.issues != before.issues { + t.Errorf("a refused run changed the issue count: %d -> %d", before.issues, after.issues) + } + if after.registry != before.registry { + t.Errorf("a refused run changed the backup registry:\nbefore:\n%s\nafter:\n%s", before.registry, after.registry) + } +} + +// readonlyTreeDiff names what differs between two snapshots, so a leaked +// mutation reads as "+ backup.lock" rather than as two opaque hash maps. +func readonlyTreeDiff(before, after readonlyTreeSnapshot) []string { + var diff []string + if before.Exists != after.Exists { + diff = append(diff, fmt.Sprintf("exists: %t -> %t", before.Exists, after.Exists)) + } + for path, was := range before.Entries { + if now, ok := after.Entries[path]; !ok { + diff = append(diff, "- "+path) + } else if now != was { + diff = append(diff, "~ "+path) + } + } + for path := range after.Entries { + if _, ok := before.Entries[path]; !ok { + diff = append(diff, "+ "+path) + } + } + sort.Strings(diff) + return diff +} + +// TestBackupAndImportBlockedInReadonlyMode is the readonly gate for the backup +// and import commands. One workspace per flavour serves every row; each row +// re-registers the backup first, so none depends on what the previous one did. +func TestBackupAndImportBlockedInReadonlyMode(t *testing.T) { + for _, flavour := range readonlyGateFlavours { + t.Run(flavour.name, func(t *testing.T) { + w := flavour.setup(t) + for _, tt := range backupImportSurfaces(w) { + t.Run(tt.name, func(t *testing.T) { + for _, flagSet := range readonlyGateFlagSets { + t.Run(flagSet.name, func(t *testing.T) { + w.registerBackup(t) + before := w.state(t) + + args := append(append([]string{}, tt.args...), flagSet.flags...) + stdout, stderr, code := w.run(t, args...) + + want := "Error: operation '" + tt.op + "' is not allowed in read-only mode\n" + if code != 1 { + t.Errorf("exit code = %d, want 1\nstdout:\n%s\nstderr:\n%s", code, stdout, stderr) + } + if stderr != want { + t.Errorf("stderr is not exactly the standard refusal\ngot: %q\nwant: %q", stderr, want) + } + if stdout != "" { + t.Errorf("stdout should be empty when the command is refused, got:\n%s", stdout) + } + w.assertUnchanged(t, before) + }) + } + }) + } + }) + } +} + // TestDoctorDiagnosisWorksInReadonlyMode pins the other half of the contract: // the gate keys on --fix/--clean alone, so every diagnosis mode still runs // under --readonly. Diagnosing a sandboxed workspace is the whole point. @@ -143,6 +455,25 @@ func TestDoctorDiagnosisWorksInReadonlyMode(t *testing.T) { } } +// TestBackupStatusAndListWorkInReadonlyMode is the control for the backup and +// import gate: it keys on the commands that write, so `backup status` and +// `list` still run under --readonly, in both flavours. +func TestBackupStatusAndListWorkInReadonlyMode(t *testing.T) { + for _, flavour := range readonlyGateFlavours { + t.Run(flavour.name, func(t *testing.T) { + w := flavour.setup(t) + for _, args := range [][]string{{"backup", "status"}, {"list"}} { + t.Run(strings.Join(args, "_"), func(t *testing.T) { + stdout, stderr, code := w.run(t, append(append([]string{}, args...), "--readonly")...) + if code != 0 || strings.Contains(stderr, "not allowed in read-only mode") { + t.Errorf("bd %v under --readonly should run (exit %d):\nstdout:\n%s\nstderr:\n%s", args, code, stdout, stderr) + } + }) + } + }) + } +} + // TestDoctorMaintenanceSkippedInReadonlyMode is the readonly twin of // TestDoctorMaintenanceSkippedDuringMigrationFreeze — see that test for why // plain `bd doctor` had hidden writes at all, and for the shared-server setup. diff --git a/cmd/bd/import.go b/cmd/bd/import.go index a100f36a6..5d24329f3 100644 --- a/cmd/bd/import.go +++ b/cmd/bd/import.go @@ -147,14 +147,20 @@ func bulkLoadPoolReadTimeout(cmd *cobra.Command) time.Duration { } func runImport(cmd *cobra.Command, args []string) error { - // Explicit call, not inherited from CheckReadonly: runImport doesn't call - // CheckReadonly at all (a separate, pre-existing gap — readonlyMode - // doesn't gate bd import either), so it can't pick up the freeze check - // folded into CheckReadonly the way create/update/close/remember do. - // The path that makes this call load-bearing rather than redundant is - // `bd import --dry-run`: a preview sets useReadOnly, so it skips the early - // gate in PersistentPreRunE, and runImport is the only chokepoint left. - // Plain `bd import` is already stopped by that early gate. + // Strict --readonly refuses import, a --dry-run preview included (it + // refuses create --dry-run too). Guarded, unlike the write commands' + // unconditional call, because CheckReadonly also answers a migration freeze + // and does it by exiting the process. The freeze is answered below instead, + // by returning the error, so that cleanup still runs. + if readonlyMode { + CheckReadonly("import") + } + + // The freeze check, not inherited from CheckReadonly for the reason above. + // It is load-bearing rather than redundant on `bd import --dry-run`: a + // preview sets useReadOnly, so it skips the early gate in + // PersistentPreRunE, and runImport is the only chokepoint left. Plain + // `bd import` is already stopped by that early gate. if err := migrationFreezeGateFor(cmd, "import"); err != nil { return err } diff --git a/cmd/bd/migration_freeze_gate_test.go b/cmd/bd/migration_freeze_gate_test.go index fa209ad6a..0a58ca336 100644 --- a/cmd/bd/migration_freeze_gate_test.go +++ b/cmd/bd/migration_freeze_gate_test.go @@ -743,8 +743,8 @@ func TestLabelAddBlockedDuringMigrationFreeze(t *testing.T) { // Plain `bd import` is stopped by the early gate in PersistentPreRunE, like // any other write. The `--dry-run` row is the one that pins runImport's OWN // call: a preview sets useReadOnly, so it skips the early gate, and runImport -// never calls CheckReadonly — delete the check from import.go and only this -// row goes red. It also pins the claim the PreRunE comment makes about +// calls CheckReadonly only under strict --readonly, so its freeze check is the +// only one left — delete that check from import.go and only this row goes red. It also pins the claim the PreRunE comment makes about // previews being re-blocked at the per-command chokepoint, which for import is // exactly that call and nothing else. func TestImportBlockedDuringMigrationFreeze(t *testing.T) { diff --git a/cmd/bd/workspace_gate.go b/cmd/bd/workspace_gate.go index acb54e39b..70e5ed492 100644 --- a/cmd/bd/workspace_gate.go +++ b/cmd/bd/workspace_gate.go @@ -409,6 +409,15 @@ func acquireCommandWorkspaceGates(ctx context.Context, cmd *cobra.Command, beads releaseWorkspaceGates() exclusive := commandNeedsExclusiveGate(cmd) + // A restore replaces the store, so strict readonly refuses it before any gate + // file is created or the store is opened. Nothing is held yet, so the exit + // skips no release. Only readonlyMode is checked: CheckReadonly also answers a + // migration freeze, and a frozen restore keeps its place in the chokepoint's + // order. + if exclusive && readonlyMode { + CheckReadonly("backup restore") + } + // No workspace on disk: nothing to guard, and the store-open path will // produce its own (better) "no database found" error. Gating here would // scatter .gate.lock files into arbitrary directories users run bd in.