Files
mesh-catalog/modules/nats/cmd/nats-tools/snapshots_test.go
T

220 lines
8.9 KiB
Go

package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"testing"
"time"
"git.novox.be/novox/mesh-contracts/go/seats/meshbroker"
stdio "git.novox.be/novox/mesh-sdk/go"
"nats-tools/internal/keeper"
)
// The seat mesh-broker's snapshot verbs and nats_snapshots (design 47 G §681, A-44, A-45, A-59): snapshot-get answers the fields and
// sends the chunks of at most 512 KiB, each acknowledged before the next, to a caller granted the chunk subjects, and never a
// protected snapshot; the other three verbs are handed to the keeper with the proved caller and answered in the one acting
// form; nats_snapshots lists each snapshot with its expired and recorded.
func TestTheSnapshotVerbsAreServedByTheBundle(t *testing.T) {
at := time.Date(2026, 11, 4, 12, 0, 0, 0, time.UTC)
file := bytes.Repeat([]byte("snap"), (ChunkSize*5/2)/4) // two whole chunks and a half
sum := sha256.Sum256(file)
digest := "sha256:" + hex.EncodeToString(sum[:])
files := map[string][]byte{"/k/ok.tar": file, "/k/damaged.tar": []byte("other bytes"), "/k/protected.tar": file}
held := map[string]keeper.Request{
"r1": {ID: "r1", Verb: "drop", Module: "m", Local: "drafts", At: at, Path: "/k/ok.tar", Size: int64(len(file)), Digest: digest},
"r2": {ID: "r2", Verb: "drop", Module: "m", Local: "secret", At: at, Path: "/k/protected.tar", Size: int64(len(file)), Digest: digest, Protected: true},
"r3": {ID: "r3", Verb: "drop", Module: "m", Local: "bad", At: at, Path: "/k/damaged.tar", Size: int64(len(file)), Digest: digest},
"r4": {ID: "r4", Verb: "drop", Module: "m", Local: "gone", At: at, Path: "/k/gone.tar", Size: 5, Digest: digest},
"r5": {ID: "r5", Verb: "recreate", Module: "m", Local: "old", At: at.AddDate(0, 0, -400), Snapshot: "/before/g.tar"},
"r6": {ID: "r6", Verb: "drop", Module: "m", Local: "aged", At: at.AddDate(0, 0, -91), Path: "/k/ok.tar", Size: 1, Digest: digest},
}
var handedOver []keeper.Request
rs := requestStore{
Put: func(key string, r keeper.Request) error {
handedOver = append(handedOver, r)
// The keeper's answer at once, as its watch writes it.
r.Done = at
switch r.Of {
case "busy":
r.Outcome, r.ActID = "busy", "a-running"
case "bad":
r.Outcome, r.Error = "error", "names live: the stream KV_m_drafts exists"
default:
r.Outcome, r.ActID = "accepted", key
}
held[key] = r
return nil
},
Get: func(key string) (*keeper.Request, error) {
r, ok := held[key]
if !ok {
return nil, nil
}
return &r, nil
},
Keys: func() ([]string, error) {
var out []string
for k := range held {
out = append(out, k)
}
return out, nil
},
Now: func() time.Time { return at },
}
chunks := make(chan string, 8)
var published []Chunk
var ackHandler func(subject string, body json.RawMessage) (any, error)
var journal []string
var mu sync.Mutex
locked := func(f func()) { mu.Lock(); defer mu.Unlock(); f() }
granted := true
w := &snapshotWorld{
Open: func(path string) (io.ReadCloser, error) {
b, ok := files[path]
if !ok {
return nil, os.ErrNotExist
}
return io.NopCloser(bytes.NewReader(b)), nil
},
Publish: func(subject string, body any, id string) error {
c := body.(Chunk)
locked(func() { published = append(published, c) })
chunks <- subject + " " + id
return nil
},
Answer: func(subject string, handle func(string, json.RawMessage) (any, error)) error {
if subject != "mesh.seat.mesh-broker.ack.snapshot-get.*.*" {
t.Errorf("the acknowledgements are heard on %s", subject)
}
ackHandler = handle
return nil
},
Granted: func(string, string) bool { return granted },
Logf: func(f string, a ...any) { locked(func() { journal = append(journal, fmt.Sprintf(f, a...)) }) },
Within: time.Millisecond,
KeepDays: 90,
}
tools := map[string]stdio.Tool{}
for _, tl := range snapshotTools(rs, w) {
tools[tl.Name] = tl
}
get := func(request, call string) meshbroker.SnapshotGetAnswer {
out, err := tools["mesh-broker.snapshot-get"].Run(map[string]any{"request": request, "call": call})
if err != nil {
t.Fatal(err)
}
return out.(meshbroker.SnapshotGetAnswer)
}
call := "0123456789abcdef0123456789abcdef"
for _, row := range []struct{ request, want string }{{"r2", "snapshot protected"}, {"r3", "snapshot damaged"}, {"r4", "snapshot missing"},
{"r5", "snapshot taken before milestone G"}} {
if got := get(row.request, call); got.Error != row.want && !strings.HasPrefix(got.Error, row.want) || got.Chunks != 0 || len(published) != 0 {
t.Errorf("%s: answered %+v, want %q and no chunk", row.request, got, row.want)
}
}
// A caller granted no chunk subjects is answered the fields alone.
granted = false
if got := get("r1", call); got.Error != "" || got.Chunks != 0 || got.Digest != digest || got.Stream != "m_drafts" || got.Size != int64(len(file)) || len(published) != 0 {
t.Errorf("an ungranted caller: %+v, published %d", got, len(published))
}
// A granted one hears each chunk after the answer, the next only once the last is acknowledged.
granted = true
got := get("r1", call)
if got.Chunks != 3 {
t.Fatalf("answered %+v: 2.5 chunks of 512 KiB are three", got)
}
for n := 1; n <= 3; n++ {
want := fmt.Sprintf("mesh.seat.mesh-broker.chunk.snapshot-get.%s.%d %s.%d", call, n, call, n)
if s := <-chunks; s != want {
t.Fatalf("chunk %d sent as %q, want %q", n, s, want)
}
if _, err := ackHandler(fmt.Sprintf("mesh.seat.mesh-broker.ack.snapshot-get.%s.%d", call, n), nil); err != nil {
t.Fatal(err)
}
}
var whole []byte
locked(func() {})
for _, c := range published {
if len(c.Data) > ChunkSize {
t.Errorf("chunk %d holds %d bytes, past 512 KiB", c.N, len(c.Data))
}
whole = append(whole, c.Data...)
}
if !bytes.Equal(whole, file) {
t.Errorf("the chunks hold %d bytes, not the file's %d", len(whole), len(file))
}
// A missed acknowledgement ends the send, said in the journal; no second chunk goes.
locked(func() { published = nil })
_ = get("r1", call)
<-chunks
var said []string
var sent int
for deadline := time.Now().Add(5 * time.Second); len(said) == 0 && time.Now().Before(deadline); {
locked(func() { said, sent = append([]string(nil), journal...), len(published) })
runtime.Gosched()
}
if len(said) != 1 || !strings.Contains(said[0], "ends at chunk 1 of 3") || sent != 1 {
t.Errorf("a missed acknowledgement: journal %v, published %d", said, sent)
}
// The three other verbs are handed to the keeper with the proved caller and answered in the acting form.
restore := tools["mesh-broker.snapshot-restore"]
out, err := restore.Run(map[string]any{"request": "r2", "verify": true, "why": "w", "caller": "person.mallory"})
if err != nil {
t.Fatal(err)
}
if a := out.(meshbroker.SnapshotRestoreAnswer); a.Accepted == "" || a.Busy != "" || a.Error != "" {
t.Errorf("restore answered %+v", a)
}
if h := handedOver[len(handedOver)-1]; h.Verb != keeper.VerbSnapshotRestore || h.Of != "r2" || !h.Verify || h.Caller != stdio.Unproven {
t.Errorf("handed %+v: the request, verify and the proved caller, never person.mallory", h)
}
out, _ = restore.Run(map[string]any{"request": "busy", "why": "w"})
if a := out.(meshbroker.SnapshotRestoreAnswer); a.Busy != "a-running" || a.Accepted != "" {
t.Errorf("a second restore answered %+v", a)
}
out, _ = restore.Run(map[string]any{"request": "bad", "why": "w"})
if a := out.(meshbroker.SnapshotRestoreAnswer); !strings.Contains(a.Error, "names live") || a.Accepted != "" {
t.Errorf("a refused restore answered %+v", a)
}
if _, err := restore.Run(map[string]any{"request": "r2"}); err == nil {
t.Error("a restore with no why was handed over")
}
out, _ = tools["mesh-broker.snapshot-restore-cancel"].Run(map[string]any{"act": "a1", "why": "w"})
if h := handedOver[len(handedOver)-1]; h.Verb != keeper.VerbSnapshotRestoreCancel || h.Of != "a1" || out.(meshbroker.SnapshotRestoreCancelAnswer).Accepted == "" {
t.Errorf("cancel handed %+v, answered %+v", h, out)
}
out, _ = tools["mesh-broker.snapshot-remove"].Run(map[string]any{"expired": true, "why": "w"})
if h := handedOver[len(handedOver)-1]; h.Verb != keeper.VerbSnapshotRemove || !h.AllExpired || out.(meshbroker.SnapshotRemoveAnswer).Accepted == "" {
t.Errorf("remove handed %+v, answered %+v", h, out)
}
// nats_snapshots lists every snapshot, the expired and the unrecorded said.
out, err = tools["nats_snapshots"].Run(nil)
if err != nil {
t.Fatal(err)
}
list := out.(map[string]any)["snapshots"].([]map[string]any)
byRequest := map[string]map[string]any{}
for _, s := range list {
byRequest[s["request"].(string)] = s
}
if r := byRequest["r2"]; r["protected"] != true || r["stream"] != "m_secret" {
t.Errorf("r2 listed as %v", r)
}
if r := byRequest["r6"]; r["expired"] != true || r["recorded"] != true {
t.Errorf("r6 listed as %v: 91 days old, recorded", r)
}
if r := byRequest["r5"]; r["recorded"] != false || r["expired"] != false {
t.Errorf("r5 (before G) listed as %v: unrecorded, never expired by age", r)
}
}