From 2eed189ba0be01c896c9f4d749e7f9b8d457a506 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 28 Nov 2026 12:35:17 -0800 Subject: [PATCH] test: v1.0.1 keeps the v1.0.0 on-disk encodings (golden guard) Layout: fixtures for HEAD (unclaimed; live with two snapshots, metadata and a dependent; with an open snap session; detached; deleting; a tombstone), VOLUME, a root and its segment, written once by a fixed-input generator on the v1.0.0 tag checkout (25a3b755). Today's encoders must produce those bytes, decode then encode must return them, and each fixture's git blob hash must be the one written on the tag. Storage: the v1.0.0 vectors in testdata/format_v3 gain the single-block write record (generated on the tag), checked by encode and by the scanner's decode; every format_v3 file's git blob hash is checked against the tag's. A failing guard is fixed by restoring the encoder or by the version-bump-and-refusal path, never by rewriting a fixture. Co-Authored-By: Claude Opus 5.5 --- .../chunkstore/layout/golden_v100_test.go | 231 ++++++++++++++++++ .../testdata/golden-v1.0.0/.gitattributes | 1 + .../layout/testdata/golden-v1.0.0/README.md | 16 ++ .../testdata/golden-v1.0.0/head-deleting.bin | 1 + .../testdata/golden-v1.0.0/head-detached.bin | 1 + .../golden-v1.0.0/head-live-snapshots.bin | 1 + .../golden-v1.0.0/head-snap-session.bin | 1 + .../testdata/golden-v1.0.0/head-tombstone.bin | 1 + .../testdata/golden-v1.0.0/head-unclaimed.bin | 1 + .../layout/testdata/golden-v1.0.0/root.bin | Bin 0 -> 42 bytes .../testdata/golden-v1.0.0/segment-0.bin | Bin 0 -> 115 bytes .../layout/testdata/golden-v1.0.0/volume.bin | 1 + .../core/storage/golden_v100_test.go | 168 +++++++++++++ .../core/storage/testdata/format_v3/write.bin | Bin 0 -> 4166 bytes 14 files changed, 423 insertions(+) create mode 100644 seaweed-block/core/frontend/chunkstore/layout/golden_v100_test.go create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/.gitattributes create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/README.md create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-deleting.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-detached.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-live-snapshots.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-snap-session.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-tombstone.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-unclaimed.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/root.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/segment-0.bin create mode 100644 seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/volume.bin create mode 100644 seaweed-block/core/storage/golden_v100_test.go create mode 100644 seaweed-block/core/storage/testdata/format_v3/write.bin diff --git a/seaweed-block/core/frontend/chunkstore/layout/golden_v100_test.go b/seaweed-block/core/frontend/chunkstore/layout/golden_v100_test.go new file mode 100644 index 0000000000..b6267c90ee --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/golden_v100_test.go @@ -0,0 +1,231 @@ +package layout + +import ( + "bytes" + "crypto/sha1" + "encoding/hex" + "fmt" + "os" + "path/filepath" + "sort" + "testing" +) + +// The v1.0.0 layout encodings: HEAD (in each state it is written in), +// VOLUME, segment and root, from fixed inputs. The fixtures in +// testdata/golden-v1.0.0 were written by this generator on the v1.0.0 tag +// checkout (see the README there) and are never regenerated on main. + +const goldenDir = "testdata/golden-v1.0.0" + +// goldenFixMessage is the failure text of every golden check. +const goldenFixMessage = "the on-disk encoding changed from v1.0.0. Fix it by restoring the encoder, or by the format path (a version bump, and a refusal of the old format first); never by rewriting the fixture" + +func goldenID(b byte) VolumeID { + var id VolumeID + for i := range id { + id[i] = b + byte(i) + } + return id +} + +// goldenLayout builds every fixture from fixed inputs. +func goldenLayout(t *testing.T) map[string][]byte { + t.Helper() + src, tgt := goldenID(0x10), goldenID(0x40) + out := map[string][]byte{} + put := func(name string, b []byte, err error) { + t.Helper() + if err != nil { + t.Fatalf("%s: %v", name, err) + } + out[name] = b + } + ch1 := NameChunk(1, bytes.Repeat([]byte{0x5a}, 4096)) + ch7 := NameChunk(7, bytes.Repeat([]byte{0xa7}, 4096)) + manifest := map[uint64]Ref{ + 1: {Owner: "src", OwnerID: src, Chunk: ch1}, + 7: {Owner: "src", OwnerID: src, Chunk: ch7}, + } + root, segs, err := Build(manifest) + if err != nil { + t.Fatal(err) + } + rn, rb, err := NameRoot(root) + put("root.bin", rb, err) + names := make([]SegName, 0, len(segs)) + for n := range segs { + names = append(names, n) + } + sort.Slice(names, func(a, b int) bool { return names[a].Shard < names[b].Shard }) + for i, n := range names { + out["segment-"+string(rune('0'+i))+".bin"] = segs[n] + } + vol, err := (Volume{ID: src, Name: "src", Format: VolumeFormat, Geometry: Geometry{SizeBytes: 64 * 4096, BlockSize: 4096, ChunkBytes: 4 * 4096}}).Encode() + put("volume.bin", vol, err) + + unclaimed, err := Unclaimed(src, nil).Encode() + put("head-unclaimed.bin", unclaimed, err) + live := Head{ + VolumeID: src, Epoch: 3, Holder: "node-a/r1", ClaimID: "claim-1", + Activity: Activity{Kind: ActIdle}, Current: &rn, Position: 42, State: StateLive, + Snapshots: map[string]Snapshot{ + "daily": {Root: rn, Position: 40, Meta: SnapshotMeta{CreatedNs: 1795800000000000000, Class: ClassScheduled, Labels: map[string]string{"k": "v"}}, + Dependents: []Dependent{{Target: "tgt", TargetID: tgt, Root: rn}}}, + "keep": {Root: rn, Position: 41, Meta: SnapshotMeta{CreatedNs: 1795800001000000000, Class: ClassPinned, ExpiresNs: 1795900000000000000}}, + }, + } + b, err := live.Encode() + put("head-live-snapshots.bin", b, err) + snap := live + snap.Activity = Activity{Kind: ActSnap, Session: "s-0001"} + b, err = snap.Encode() + put("head-snap-session.bin", b, err) + detached := live + detached.State = StateDetached + b, err = detached.Encode() + put("head-detached.bin", b, err) + deleting := live + deleting.Snapshots = nil + deleting.State = StateDeleting + b, err = deleting.Encode() + put("head-deleting.bin", b, err) + tomb := Head{VolumeID: tgt, Activity: Activity{Kind: ActIdle}, State: StateDeleted} + b, err = tomb.Encode() + put("head-tombstone.bin", b, err) + return out +} + +// TestWriteGoldenV100 writes the fixtures when SW_GOLDEN_WRITE names a +// directory. It is run once, on the v1.0.0 tag checkout; on main it is a +// no-op. +func TestWriteGoldenV100(t *testing.T) { + dir := os.Getenv("SW_GOLDEN_WRITE") + if dir == "" { + t.Skip("set SW_GOLDEN_WRITE on the v1.0.0 tag checkout to write the fixtures") + } + for name, b := range goldenLayout(t) { + if err := os.WriteFile(filepath.Join(dir, name), b, 0o644); err != nil { + t.Fatal(err) + } + } +} + +func readGolden(t *testing.T, name string) []byte { + t.Helper() + b, err := os.ReadFile(filepath.Join(goldenDir, name)) + if err != nil { + t.Fatalf("%s: %v", name, err) + } + return b +} + +// Today's encoders produce the v1.0.0 bytes from the same inputs, and the +// fixture set is exactly the generator's. +func TestLayoutEncodingsAreTheV100Bytes(t *testing.T) { + got := goldenLayout(t) + entries, err := os.ReadDir(goldenDir) + if err != nil { + t.Fatal(err) + } + files := map[string]bool{} + for _, e := range entries { + if filepath.Ext(e.Name()) == ".bin" { + files[e.Name()] = true + } + } + for name := range got { + if !files[name] { + t.Errorf("%s: no v1.0.0 fixture (the generator gained an output; a new kind needs a fixture from the tag)", name) + } + } + for name := range files { + b, ok := got[name] + if !ok { + t.Errorf("%s: a v1.0.0 fixture the generator no longer produces", name) + continue + } + if want := readGolden(t, name); !bytes.Equal(b, want) { + t.Errorf("%s: %s\n got %s\n want %s", name, goldenFixMessage, hex.EncodeToString(b), hex.EncodeToString(want)) + } + } +} + +// goldenAtV100 is each fixture's git blob hash as written on the v1.0.0 +// tag checkout. A fixture edit fails here, so it cannot hide a format change. +var goldenAtV100 = map[string]string{ + "head-deleting.bin": "d790a67071549b07bff1f3dd56c16eda5fa26d57", + "head-detached.bin": "00e72a4222045c64a4b640f75b89bb4e01a6102f", + "head-live-snapshots.bin": "c70763ccaf1d363461ed9f4d089305cd4c431ce0", + "head-snap-session.bin": "1cf9c307780e61fc5c6d5d8638d3d84db318f887", + "head-tombstone.bin": "b0edd6abf0ab54fa6e73697190bae09b57ea1db5", + "head-unclaimed.bin": "f519aadbfb53a928bc44b17a2c78a1115fcbb927", + "root.bin": "690c5464e34b2ec14f7a3675d29bff4fa973822b", + "segment-0.bin": "d8e126128b9dba2db149b4bb4ccec69f466597a3", + "volume.bin": "315104ffb82768de219a93ecd4a1ac23597d19d2", +} + +// The layout fixtures are the bytes written on the v1.0.0 tag. +func TestLayoutGoldenFixturesAreTheV100Files(t *testing.T) { + for name, want := range goldenAtV100 { + b := readGolden(t, name) + h := sha1.New() + fmt.Fprintf(h, "blob %d\x00", len(b)) + h.Write(b) + if got := hex.EncodeToString(h.Sum(nil)); got != want { + t.Errorf("%s: blob %s, written on v1.0.0 as %s: %s", name, got, want, goldenFixMessage) + } + } +} + +// Every v1.0.0 fixture decodes, and encodes again to the same bytes. +func TestLayoutV100FixturesRoundTrip(t *testing.T) { + check := func(name string, again []byte, err error) { + t.Helper() + if err != nil { + t.Errorf("%s: decode/encode: %v: %s", name, err, goldenFixMessage) + return + } + if want := readGolden(t, name); !bytes.Equal(again, want) { + t.Errorf("%s: decode then encode differs: %s", name, goldenFixMessage) + } + } + for _, c := range []struct { + name string + vol VolumeID + }{ + {"head-unclaimed.bin", goldenID(0x10)}, {"head-live-snapshots.bin", goldenID(0x10)}, {"head-snap-session.bin", goldenID(0x10)}, + {"head-detached.bin", goldenID(0x10)}, {"head-deleting.bin", goldenID(0x10)}, {"head-tombstone.bin", goldenID(0x40)}, + } { + h, err := DecodeHead(readGolden(t, c.name), 5, c.vol) + if err != nil { + check(c.name, nil, err) + continue + } + b, err := h.Encode() + check(c.name, b, err) + } + v, err := DecodeVolume(readGolden(t, "volume.bin")) + if err == nil { + b, e := v.Encode() + check("volume.bin", b, e) + } else { + check("volume.bin", nil, err) + } + r, err := DecodeRoot(readGolden(t, "root.bin")) + if err == nil { + b, e := r.Encode() + check("root.bin", b, e) + } else { + check("root.bin", nil, err) + } + for _, name := range []string{"segment-0.bin"} { + s, err := DecodeSegment(readGolden(t, name)) + if err == nil { + b, e := s.Encode() + check(name, b, e) + } else { + check(name, nil, err) + } + } +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/.gitattributes b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/.gitattributes new file mode 100644 index 0000000000..e72d32c601 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/.gitattributes @@ -0,0 +1 @@ +*.bin -text diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/README.md b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/README.md new file mode 100644 index 0000000000..6c4f61c120 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/README.md @@ -0,0 +1,16 @@ +# v1.0.0 layout encodings + +Generated once by `TestWriteGoldenV100` (`golden_v100_test.go`), run with `SW_GOLDEN_WRITE` on a checkout of the tag **v1.0.0**, commit **bef007fddf341944faa2eb0ed36e1b3aefda96e8**. The inputs are fixed: volume ids, chunk payloads, snapshot metadata. They are never regenerated on main. + +| file | what | +|---|---| +| head-unclaimed.bin | a new volume's HEAD | +| head-live-snapshots.bin | a claimed live HEAD with a current root, two snapshots (scheduled with labels and a dependent; pinned with an expiry) | +| head-snap-session.bin | the same HEAD with an open snap session | +| head-detached.bin | the same HEAD, detached | +| head-deleting.bin | the HEAD of a volume being deleted | +| head-tombstone.bin | a restore target's tombstone (deleted, idle) | +| volume.bin | a VOLUME record | +| root.bin, segment-0.bin | the root and segment of a two-chunk image | + +`TestLayoutEncodingsAreTheV100Bytes` checks today's encoders against these bytes. `TestLayoutV100FixturesRoundTrip` checks that decode then encode returns them. `TestLayoutGoldenFixturesAreTheV100Files` checks their git blob hashes, so editing a fixture fails too. A failing check is fixed by restoring the encoder, or by the format path (a version bump, and a refusal of the old format first), never by rewriting a fixture. diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-deleting.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-deleting.bin new file mode 100644 index 0000000000..d790a67071 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-deleting.bin @@ -0,0 +1 @@ +{"volume_id":"101112131415161718191a1b1c1d1e1f","epoch":3,"holder":"node-a/r1","claim_id":"claim-1","activity":{"kind":"idle"},"current":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":42,"state":"deleting"} \ No newline at end of file diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-detached.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-detached.bin new file mode 100644 index 0000000000..00e72a4222 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-detached.bin @@ -0,0 +1 @@ +{"volume_id":"101112131415161718191a1b1c1d1e1f","epoch":3,"holder":"node-a/r1","claim_id":"claim-1","activity":{"kind":"idle"},"current":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":42,"snapshots":{"daily":{"root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":40,"created_ns":1795800000000000000,"class":"scheduled","labels":{"k":"v"},"dependents":[{"target":"tgt","target_id":"404142434445464748494a4b4c4d4e4f","root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c"}]},"keep":{"root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":41,"created_ns":1795800001000000000,"class":"pinned","expires_ns":1795900000000000000}},"state":"detached"} \ No newline at end of file diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-live-snapshots.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-live-snapshots.bin new file mode 100644 index 0000000000..c70763ccaf --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-live-snapshots.bin @@ -0,0 +1 @@ +{"volume_id":"101112131415161718191a1b1c1d1e1f","epoch":3,"holder":"node-a/r1","claim_id":"claim-1","activity":{"kind":"idle"},"current":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":42,"snapshots":{"daily":{"root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":40,"created_ns":1795800000000000000,"class":"scheduled","labels":{"k":"v"},"dependents":[{"target":"tgt","target_id":"404142434445464748494a4b4c4d4e4f","root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c"}]},"keep":{"root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":41,"created_ns":1795800001000000000,"class":"pinned","expires_ns":1795900000000000000}}} \ No newline at end of file diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-snap-session.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-snap-session.bin new file mode 100644 index 0000000000..1cf9c30778 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-snap-session.bin @@ -0,0 +1 @@ +{"volume_id":"101112131415161718191a1b1c1d1e1f","epoch":3,"holder":"node-a/r1","claim_id":"claim-1","activity":{"kind":"snap","session":"s-0001"},"current":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":42,"snapshots":{"daily":{"root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":40,"created_ns":1795800000000000000,"class":"scheduled","labels":{"k":"v"},"dependents":[{"target":"tgt","target_id":"404142434445464748494a4b4c4d4e4f","root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c"}]},"keep":{"root":"0cf567cebed76e9cf29e201021bd84f9259f56954d659e28d67c6a0086d3a33c","position":41,"created_ns":1795800001000000000,"class":"pinned","expires_ns":1795900000000000000}}} \ No newline at end of file diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-tombstone.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-tombstone.bin new file mode 100644 index 0000000000..b0edd6abf0 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-tombstone.bin @@ -0,0 +1 @@ +{"volume_id":"404142434445464748494a4b4c4d4e4f","epoch":0,"holder":"","activity":{"kind":"idle"},"position":0,"state":"deleted"} \ No newline at end of file diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-unclaimed.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-unclaimed.bin new file mode 100644 index 0000000000..f519aadbfb --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/head-unclaimed.bin @@ -0,0 +1 @@ +{"volume_id":"101112131415161718191a1b1c1d1e1f","epoch":0,"holder":"","activity":{"kind":"idle"},"position":0} \ No newline at end of file diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/root.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/root.bin new file mode 100644 index 0000000000000000000000000000000000000000..690c5464e34b2ec14f7a3675d29bff4fa973822b GIT binary patch literal 42 ycmWFzcM9_N4`F0vxZ-kYk_U5t_taMt%dirVO^?3_|g8Knk84^1H literal 0 HcmV?d00001 diff --git a/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/segment-0.bin b/seaweed-block/core/frontend/chunkstore/layout/testdata/golden-v1.0.0/segment-0.bin new file mode 100644 index 0000000000000000000000000000000000000000..d8e126128b9dba2db149b4bb4ccec69f466597a3 GIT binary patch literal 115 zcmWFzcM5iOXJTMtWG*gB77!E?77-N_mync_mXVc{|I9SCcBvAl#q!5brP?Kq&z{F{ zM|icO