From 2948987d846676f870260d8454cb953b8e88cb22 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 23 Nov 2026 12:40:31 -0800 Subject: [PATCH 1/2] test: harness parsers take the volume; pattern() refuses a string that does not divide a block Co-Authored-By: Claude Opus 5.5 --- .../cmd/blockvolume/harness_helpers_test.go | 63 +++++++++++++++++++ .../planned_handoff_checks_test.go | 24 +++++-- .../procedure_gate_integration_test.go | 1 - 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/harness_helpers_test.go diff --git a/seaweed-block/cmd/blockvolume/harness_helpers_test.go b/seaweed-block/cmd/blockvolume/harness_helpers_test.go new file mode 100644 index 0000000000..56dec68c19 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/harness_helpers_test.go @@ -0,0 +1,63 @@ +package main_test + +import ( + "bytes" + "fmt" + "testing" +) + +// pattern repeats s to exactly one 4096-byte block. It panics unless len(s) +// divides 4096: a shorter block would be a short NVMe write, which hangs the +// in-test initiator until its deadline instead of failing at once. +func pattern(s string) []byte { + if len(s) == 0 || 4096%len(s) != 0 { + panic(fmt.Sprintf("pattern %q: %d bytes do not divide a 4096-byte block", s, len(s))) + } + return bytes.Repeat([]byte(s), 4096/len(s)) +} + +// TestPatternIsExactlyOneBlock: a 16-byte string fills one block; a 15-byte +// one (it would make 4095 bytes) panics with its name. +func TestPatternIsExactlyOneBlock(t *testing.T) { + if b := pattern("sixteen-bytes!!!"); len(b) != 4096 { + t.Fatalf("pattern of 16 bytes: %d bytes; want 4096", len(b)) + } + defer func() { + if r := recover(); r == nil { + t.Fatal("pattern of 15 bytes returned a block; want a panic (4096 is not a multiple of 15)") + } + }() + pattern("fifteen-bytes!!") +} + +// TestHarnessParsersReadTheNamedVolume: the adoption, plain-detach and +// forced-move parsers read the volume they are given, and refuse a line or +// answer for another volume; the v1 forms are those parsers for v1. +func TestHarnessParsersReadTheNamedVolume(t *testing.T) { + v4 := `2026/11/23 07:30:06 blockvolume: volume v4 adopted as PRIMARY at r1@3 (EV=1) process="n-v4" server="n-v4"` + if e, err := adoptedEpochOf("v4", v4); err != nil || e != 3 { + t.Fatalf("adoptedEpochOf v4: %d %v; want 3", e, err) + } + if _, err := adoptedEpochOf("v1", v4); err == nil { + t.Fatal("adoptedEpochOf v1 read a v4 adoption line") + } + if _, err := adoptedEpoch(v4); err == nil { + t.Fatal("adoptedEpoch (v1) read a v4 adoption line") + } + + seal := `{"volumeID":"v6","headGen":4,"headEpoch":2,"cutLSN":9,"lastWrittenLSN":9}` + if g, err := sealedGenOf(seal, "sealed HEAD generation: 4\n", "v6", 2); err != nil || g != 4 { + t.Fatalf("sealedGenOf v6: %d %v; want 4", g, err) + } + if _, err := sealedGen(seal, "sealed HEAD generation: 4\n", 2); err == nil { + t.Fatal("sealedGen (v1) accepted a v6 detach answer") + } + + move := "forced move issued: volume v6 request force-v6: r1@1 on n-v6 -> r1@2 on n-v7 (kept HEAD gen 3, remote LSN 8)\n" + if e2, err := forcedEpochOf(move, "v6", 1, "n-v6", "n-v7"); err != nil || e2 != 2 { + t.Fatalf("forcedEpochOf v6: %d %v; want 2", e2, err) + } + if _, err := forcedEpoch(move, 1, "n-v6", "n-v7"); err == nil { + t.Fatal("forcedEpoch (v1) read a v6 forced move") + } +} diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go index 14fc8e951e..b63ba6cf9e 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go @@ -27,8 +27,11 @@ func checkShowGranted(out string, e uint64) error { } // adoptedEpoch reads E from blockvolume's adoption line. -func adoptedEpoch(line string) (uint64, error) { - e, err := matchUint(regexp.MustCompile(`blockvolume: volume v1 adopted as PRIMARY at r1@(\d+) `), line) +func adoptedEpoch(line string) (uint64, error) { return adoptedEpochOf("v1", line) } + +// adoptedEpochOf reads E from blockvolume's adoption line for volume vol. +func adoptedEpochOf(vol, line string) (uint64, error) { + e, err := matchUint(regexp.MustCompile(`blockvolume: volume `+regexp.QuoteMeta(vol)+` adopted as PRIMARY at r1@(\d+) `), line) if err == nil && e == 0 { err = fmt.Errorf("adopted at epoch 0: %q", line) } @@ -37,7 +40,10 @@ func adoptedEpoch(line string) (uint64, error) { // sealedGen reads G from a plain detach: one JSON object on stdout and // exactly the sealed-generation line on stderr. -func sealedGen(stdout, stderr string, e uint64) (uint64, error) { +func sealedGen(stdout, stderr string, e uint64) (uint64, error) { return sealedGenOf(stdout, stderr, "v1", e) } + +// sealedGenOf reads G from a plain detach of volume vol at epoch e. +func sealedGenOf(stdout, stderr, vol string, e uint64) (uint64, error) { var a struct { VolumeID string `json:"volumeID"` HeadGen uint64 `json:"headGen"` @@ -48,8 +54,8 @@ func sealedGen(stdout, stderr string, e uint64) (uint64, error) { if err := json.Unmarshal([]byte(stdout), &a); err != nil { return 0, fmt.Errorf("plain detach stdout is not the JSON answer: %v: %q", err, stdout) } - if a.VolumeID != "v1" || a.HeadGen == 0 || a.HeadEpoch != e || a.CutLSN == 0 || a.LastWrittenLSN == 0 { - return 0, fmt.Errorf("plain detach answer %+v; want v1, a generation, epoch %d and both LSNs after a write", a, e) + if a.VolumeID != vol || a.HeadGen == 0 || a.HeadEpoch != e || a.CutLSN == 0 || a.LastWrittenLSN == 0 { + return 0, fmt.Errorf("plain detach answer %+v; want %s, a generation, epoch %d and both LSNs after a write", a, vol, e) } if strings.TrimSpace(stderr) != fmt.Sprintf("sealed HEAD generation: %d", a.HeadGen) { return 0, fmt.Errorf("plain detach stderr %q; want exactly the sealed generation line", stderr) @@ -156,7 +162,13 @@ func checkHeadReopenedBy(out string, e2, g, l uint64, host string) error { // forcedEpoch reads E2 from a forced move's answer (`forced move issued: // ... r1@E on FROM -> r1@E2 on TO (kept HEAD gen N, remote LSN L)`). func forcedEpoch(out string, e uint64, from, to string) (uint64, error) { - re := regexp.MustCompile(fmt.Sprintf(`(?m)^forced move issued: volume v1 request \S+: r1@%d on %s -> r1@(\d+) on %s \(kept HEAD gen \d+, remote LSN \d+\)$`, e, regexp.QuoteMeta(from), regexp.QuoteMeta(to))) + return forcedEpochOf(out, "v1", e, from, to) +} + +// forcedEpochOf reads E2 from a forced move of volume vol from host from to +// host to. +func forcedEpochOf(out, vol string, e uint64, from, to string) (uint64, error) { + re := regexp.MustCompile(fmt.Sprintf(`(?m)^forced move issued: volume %s request \S+: r1@%d on %s -> r1@(\d+) on %s \(kept HEAD gen \d+, remote LSN \d+\)$`, regexp.QuoteMeta(vol), e, regexp.QuoteMeta(from), regexp.QuoteMeta(to))) e2, err := matchUint(re, out) if err == nil && e2 <= e { err = fmt.Errorf("forced move issued epoch %d; want above %d", e2, e) diff --git a/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go index 640bb67930..ca016a62a6 100644 --- a/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go @@ -450,7 +450,6 @@ func (env *handoffEnv) requireHeadHolder(t *testing.T, e uint64, host string) { } } -func pattern(s string) []byte { return bytes.Repeat([]byte(s), 4096/len(s)) } func (env *handoffEnv) start(t *testing.T, name, id string) *daemon { t.Helper() From e60f642d81156f282c0028c2f021baf7f7b051aa Mon Sep 17 00:00:00 2001 From: pingqiu Date: Mon, 23 Nov 2026 12:40:49 -0800 Subject: [PATCH 2/2] test: gofmt Co-Authored-By: Claude Opus 5.5 --- seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go | 4 +++- .../cmd/blockvolume/procedure_gate_integration_test.go | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go b/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go index b63ba6cf9e..c47da75939 100644 --- a/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go +++ b/seaweed-block/cmd/blockvolume/planned_handoff_checks_test.go @@ -40,7 +40,9 @@ func adoptedEpochOf(vol, line string) (uint64, error) { // sealedGen reads G from a plain detach: one JSON object on stdout and // exactly the sealed-generation line on stderr. -func sealedGen(stdout, stderr string, e uint64) (uint64, error) { return sealedGenOf(stdout, stderr, "v1", e) } +func sealedGen(stdout, stderr string, e uint64) (uint64, error) { + return sealedGenOf(stdout, stderr, "v1", e) +} // sealedGenOf reads G from a plain detach of volume vol at epoch e. func sealedGenOf(stdout, stderr, vol string, e uint64) (uint64, error) { diff --git a/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go b/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go index ca016a62a6..756f56dc69 100644 --- a/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go +++ b/seaweed-block/cmd/blockvolume/procedure_gate_integration_test.go @@ -450,7 +450,6 @@ func (env *handoffEnv) requireHeadHolder(t *testing.T, e uint64, host string) { } } - func (env *handoffEnv) start(t *testing.T, name, id string) *daemon { t.Helper() return env.h.start(t, name, env.h.volume, env.volArgs(id)...)