From 67f52efa0d211b9802708f752bcc018a0efb3007 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Tue, 1 Dec 2026 04:15:53 -0800 Subject: [PATCH] test: the backup fake's state is read and set under its lock (WIP: transport race next) Co-Authored-By: Claude Opus 5.5 --- .../cmd/sw-block/volume_backup_test.go | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/seaweed-block/cmd/sw-block/volume_backup_test.go b/seaweed-block/cmd/sw-block/volume_backup_test.go index 0a3ccad9e6..bebf56f740 100644 --- a/seaweed-block/cmd/sw-block/volume_backup_test.go +++ b/seaweed-block/cmd/sw-block/volume_backup_test.go @@ -92,6 +92,26 @@ func newSnapHolder(t *testing.T) *snapHolder { return h } +// The test reads and sets the fake's state through these, under its lock: +// the handler runs on the server's goroutines. +func (h *snapHolder) setMode(m string) { h.mu.Lock(); h.mode = m; h.mu.Unlock() } + +func (h *snapHolder) count() int { h.mu.Lock(); defer h.mu.Unlock(); return len(h.snaps) } + +func (h *snapHolder) label(name string) string { + h.mu.Lock() + defer h.mu.Unlock() + return h.snaps[name] +} + +func (h *snapHolder) postCount() int { + h.mu.Lock() + defer h.mu.Unlock() + return h.posts +} + +func (h *snapHolder) put(name, label string) { h.mu.Lock(); h.snaps[name] = label; h.mu.Unlock() } + // restoreMaster is a running master's restore create (journaled: the same // request id completes it, another id is refused), GetVolume, and the // workspace binding and placement, behind real loopback gRPC. @@ -468,19 +488,19 @@ func TestVolumeBackupTakesOneSnapshotAndRerunReplays(t *testing.T) { sourceEnv(t, env, "127.0.0.1:1", strings.TrimPrefix(sh.srv.URL, "http://"), true) // No generated default: without --name the command refuses before // any request (a name it chose would be lost with its terminal). - if code, _, errs := runBackup(env, "vol-a"); code != 2 || !strings.Contains(errs, "--name is required") || sh.posts != 0 { - t.Fatalf("no --name: %d %s (posts %d)", code, errs, sh.posts) + if code, _, errs := runBackup(env, "vol-a"); code != 2 || !strings.Contains(errs, "--name is required") || sh.postCount() != 0 { + t.Fatalf("no --name: %d %s (posts %d)", code, errs, sh.postCount()) } code, out, errs := runBackup(env, "vol-a", "--name", "backup-20261128T230000Z") if code != 0 || !strings.Contains(out, "snapshot backup-20261128T230000Z of vol-a") || !strings.Contains(out, "sudo sw-block volume restore vol-a --snapshot backup-20261128T230000Z --as ") { t.Fatalf("backup: %d\n%s\n%s", code, out, errs) } - if got := sh.snaps["backup-20261128T230000Z"]; got != backupTag(derivedID("backup", "vol-a", "backup-20261128T230000Z")) { + if got := sh.label("backup-20261128T230000Z"); got != backupTag(derivedID("backup", "vol-a", "backup-20261128T230000Z")) { t.Fatalf("request id %q is not derived from the volume and the name", got) } code, out, _ = runBackup(env, "vol-a", "--name", "backup-20261128T230000Z") - if code != 0 || !strings.Contains(out, "snapshot backup-20261128T230000Z of vol-a") || len(sh.snaps) != 1 { - t.Fatalf("rerun: %d %s (snapshots %d)", code, out, len(sh.snaps)) + if code != 0 || !strings.Contains(out, "snapshot backup-20261128T230000Z of vol-a") || sh.count() != 1 { + t.Fatalf("rerun: %d %s (snapshots %d)", code, out, sh.count()) } } @@ -502,7 +522,7 @@ func TestVolumeBackupInterruptedResumesWithTheSameCommand(t *testing.T) { return nil } } else { - sh.mode = c + sh.setMode(c) } code, _, errs := runBackup(env, "vol-a", "--name", "nightly") wantCode := 1 @@ -512,9 +532,10 @@ func TestVolumeBackupInterruptedResumesWithTheSameCommand(t *testing.T) { if code != wantCode || !strings.Contains(errs, "run the same command") || !strings.Contains(errs, "sudo sw-block volume backup vol-a --name nightly") || !strings.Contains(errs, "keeps what it uploaded") { t.Fatalf("interrupted backup: %d %s", code, errs) } - env.Step, sh.mode = nil, "" - if code, _, errs := runBackup(env, "vol-a", "--name", "nightly"); code != 0 || len(sh.snaps) != 1 { - t.Fatalf("rerun: %d %s (snapshots %d)", code, errs, len(sh.snaps)) + env.Step = nil + sh.setMode("") + if code, _, errs := runBackup(env, "vol-a", "--name", "nightly"); code != 0 || sh.count() != 1 { + t.Fatalf("rerun: %d %s (snapshots %d)", code, errs, sh.count()) } }) } @@ -530,13 +551,13 @@ func TestVolumeBackupIncompleteAnswerIsUnknown(t *testing.T) { env := h.env(t) sh := newSnapHolder(t) sourceEnv(t, env, "127.0.0.1:1", strings.TrimPrefix(sh.srv.URL, "http://"), true) - sh.mode = "missing-" + f + sh.setMode("missing-" + f) code, out, errs := runBackup(env, "vol-a", "--name", "n1") if code != 1 || !strings.Contains(errs, "outcome is unknown") || strings.Contains(out, "head generation") { t.Fatalf("missing %s: %d %s %s", f, code, out, errs) } - sh.mode = "" - if code, out, errs := runBackup(env, "vol-a", "--name", "n1"); code != 0 || !strings.Contains(out, "cut LSN 42") || len(sh.snaps) != 1 { + sh.setMode("") + if code, out, errs := runBackup(env, "vol-a", "--name", "n1"); code != 0 || !strings.Contains(out, "cut LSN 42") || sh.count() != 1 { t.Fatalf("rerun: %d %s %s", code, out, errs) } }) @@ -551,20 +572,20 @@ func TestVolumeBackupRefusals(t *testing.T) { t.Fatalf("unknown volume: %d %s", code, errs) } sourceEnv(t, env, "127.0.0.1:1", strings.TrimPrefix(sh.srv.URL, "http://"), false) - if code, _, errs := runBackup(env, "vol-a", "--name", "n1"); code != 2 || !strings.Contains(errs, "is walstore") || sh.posts != 0 { + if code, _, errs := runBackup(env, "vol-a", "--name", "n1"); code != 2 || !strings.Contains(errs, "is walstore") || sh.postCount() != 0 { t.Fatalf("walstore: %d %s", code, errs) } sourceEnv(t, env, "127.0.0.1:1", strings.TrimPrefix(sh.srv.URL, "http://"), true) - sh.mode = "refuse" + sh.setMode("refuse") if code, _, errs := runBackup(env, "vol-a", "--name", "n1"); code != 2 || !strings.Contains(errs, "not_primary") { t.Fatalf("refused: %d %s", code, errs) } - sh.mode = "" + sh.setMode("") // The holder answers an existing name with the snapshot already there: // one another backup took (another label) or one taken without a label // (ops chunk snapshot, the schedule) is refused, never reported as ours. - sh.snaps["taken"] = "someone-else" - sh.snaps["plain"] = "" + sh.put("taken", "someone-else") + sh.put("plain", "") for _, name := range []string{"plain"} { if code, _, errs := runBackup(env, "vol-a", "--name", name); code != 2 || !strings.Contains(errs, "snapshot_name_taken") || !strings.Contains(errs, "choose a new snapshot name") || strings.Contains(errs, "run the same command") { t.Fatalf("name %s without our label: %d %s", name, code, errs)