test: reproduce node inventory loss at publication rename

This commit is contained in:
pingqiu committed 2026-11-22 10:21:33 -08:00
1 parent 18b773a346
commit 34f5c7656c
2 files changed
+111 -4

No files matched your search

+11 -4
View File
@@ -64,10 +64,11 @@ type NodeRegistration struct {
// NodeInventoryStore persists one JSON registration record per server.
type NodeInventoryStore struct {
mu sync.Mutex
dir string
records map[string]NodeRegistration
published sync.Map // committed NodeRegistration values; holder host checks never take mu (INV-WS-RENEW-NONBLOCKING-001)
mu sync.Mutex
dir string
records map[string]NodeRegistration
published sync.Map // committed NodeRegistration values; holder host checks never take mu (INV-WS-RENEW-NONBLOCKING-001)
persistHookForTest func(string) // nil in production; crash injection at publication boundaries
}
// OpenNodeInventoryStore opens or creates a node inventory store.
@@ -222,9 +223,15 @@ func (s *NodeInventoryStore) putLocked(rec NodeRegistration) error {
if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("lifecycle: replace node %s: %w", rec.ServerID, err)
}
if s.persistHookForTest != nil {
s.persistHookForTest("before-rename")
}
if err := os.Rename(tmpName, path); err != nil {
return fmt.Errorf("lifecycle: rename node %s: %w", rec.ServerID, err)
}
if s.persistHookForTest != nil {
s.persistHookForTest("after-rename")
}
return nil
}
@@ -0,0 +1,100 @@
//go:build linux
package lifecycle
import (
"context"
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
)
func crashNodeRegistration(updated bool) NodeRegistration {
value := "old"
if updated {
value = "new"
}
return NodeRegistration{
ServerID: "node-a", Addr: "127.0.0.1:9100",
SeenAt: time.Unix(1, 0).UTC(),
Labels: map[string]string{"payload": strings.Repeat(value, 4096)},
}
}
// RegisterNode must leave a complete old or new registration across a process
// crash at either side of the publication rename (V1-SPEC r2.46).
func TestNodeInventoryCrashKeepsCompleteRegistration(t *testing.T) {
for _, phase := range []string{"before-rename", "after-rename"} {
t.Run(phase, func(t *testing.T) {
dir := t.TempDir()
s, err := OpenNodeInventoryStore(dir)
if err != nil {
t.Fatal(err)
}
old, next := crashNodeRegistration(false), crashNodeRegistration(true)
if _, err := s.RegisterNode(old); err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=^TestNodeInventoryCrashChild$", "-test.timeout=8s")
cmd.Env = append(os.Environ(), "NODE_INVENTORY_CRASH_DIR="+dir, "NODE_INVENTORY_CRASH_PHASE="+phase)
out, err := cmd.CombinedOutput()
var exit *exec.ExitError
if ctx.Err() != nil || !errors.As(err, &exit) || exit.ExitCode() != 73 {
t.Fatalf("crash point not reached: err=%v context=%v output=%s", err, ctx.Err(), out)
}
raw, err := os.ReadFile(filepath.Join(dir, "node-a.json"))
if err != nil {
t.Fatalf("crash lost the committed registration: %v", err)
}
var got NodeRegistration
if err := json.Unmarshal(raw, &got); err != nil {
t.Fatalf("crash left a partial registration: %v", err)
}
if !reflect.DeepEqual(got, old) && !reflect.DeepEqual(got, next) {
t.Fatal("crash left neither the complete old nor the complete new registration")
}
want := old
if phase == "after-rename" {
want = next
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("wrong generation at %s", phase)
}
reopened, err := OpenNodeInventoryStore(dir)
if err != nil {
t.Fatal(err)
}
if got, ok := reopened.GetNodeSnapshot("node-a"); !ok || !reflect.DeepEqual(got, want) {
t.Fatal("reopened snapshot lost the complete registration")
}
})
}
}
func TestNodeInventoryCrashChild(t *testing.T) {
dir := os.Getenv("NODE_INVENTORY_CRASH_DIR")
if dir == "" {
t.Skip("subprocess crash injector")
}
s, err := OpenNodeInventoryStore(dir)
if err != nil {
t.Fatal(err)
}
s.persistHookForTest = func(phase string) {
if phase == os.Getenv("NODE_INVENTORY_CRASH_PHASE") {
os.Exit(73) // abrupt exit: no deferred cleanup or close
}
}
if _, err := s.RegisterNode(crashNodeRegistration(true)); err != nil {
t.Fatal(err)
}
t.Fatal("write returned without reaching the crash point")
}