Merge pull request #513 from seaweedfs/test/harness-volume-helpers

test: harness parsers take the volume; pattern() refuses a partial block
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-11-23 12:53:34 -08:00
commit cd71058f8c
3 files changed
+82 -7

No files matched your search

@@ -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")
}
}
@@ -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)
}
@@ -38,6 +41,11 @@ 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) {
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 +56,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 +164,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)
@@ -450,8 +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()
return env.h.start(t, name, env.h.volume, env.volArgs(id)...)