Files
seaweedfs/seaweed-block/cmd/blockmaster/node_registry_guard_test.go
T
pingqiuandClaude Opus 5.5 767d498301 blockmaster: the node registry guard allows the host retire's removal
After start the registry only loses entries: the operator's retire
removes a host that holds no volume, and a retired id is then
unregistered, so the identity checks only get stricter. Adders stay
start-up only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 18:53:57 -08:00

76 lines
2.3 KiB
Go

package main
import (
"go/ast"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"sort"
"strings"
"testing"
)
// The node registry (server id -> registered host) only ever loses entries
// after start: the identity checks (the registered-host refusal beside the
// certificate identity) assume no one adds or rebinds a server id while
// blockmaster serves. Its only adder is the cluster-spec import at start-up.
// Its one remover after start is the operator's host retire, which takes
// only a host that holds no volume: a retired id is then unregistered, so a
// holder RPC under it is refused (refuseWorkspaceHolderHost) and the checks
// only get stricter. This guard fails when any other non-test caller of
// RegisterNode or RemoveNode appears anywhere.
func TestNodeRegistryIsWrittenOnlyAtStartup(t *testing.T) {
root, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
t.Fatal(err)
}
var sites []string
fset := token.NewFileSet()
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
switch d.Name() {
case ".git", "vendor", "node_modules", "testdata":
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
f, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return err
}
rel, _ := filepath.Rel(root, path)
for _, decl := range f.Decls {
fn, ok := decl.(*ast.FuncDecl)
if !ok || fn.Body == nil {
continue
}
ast.Inspect(fn.Body, func(n ast.Node) bool {
call, ok := n.(*ast.CallExpr)
if !ok {
return true
}
if sel, ok := call.Fun.(*ast.SelectorExpr); ok && (sel.Sel.Name == "RegisterNode" || sel.Sel.Name == "RemoveNode") {
sites = append(sites, filepath.ToSlash(rel)+":"+fn.Name.Name+"."+sel.Sel.Name)
}
return true
})
}
return nil
})
if err != nil {
t.Fatal(err)
}
sort.Strings(sites)
want := []string{"cmd/blockmaster/main.go:importLifecycleNodeInventory.RegisterNode", "core/host/master/format_gate.go:RetireHost.RemoveNode"}
if strings.Join(sites, ",") != strings.Join(want, ",") {
t.Fatalf("node registry writers = %v, want only %v: an adder after start, or another remover, breaks the identity checks' assumption", sites, want)
}
}