SPA (cmd/frontend/web): - New "lb buckets" column backed by a 1s-debounced GetVPPLBState fetch loop with leading+trailing edge coalesce. - Per-frontend health icon (✅/⚠️/❗/‼️/❓) in the Zippy header, gated by a settling flag that suppresses ‼️ until the next lb-state reconciliation after a backend transition or weight change. - In-place leaf merge on lb-state so stable bucket values (e.g. "0") don't retrigger the Flash animation on every refresh. - Zippy cards remember open state in a cookie, default closed on fresh load; fixed-width frontend-title-name + reserved icon slot so headers line up across all cards. - Clock-drift watchdog in sse.ts that forces a fresh EventSource on laptop-wake so the broker emits a resync instead of hanging on a dead half-open socket. Frontend service (cmd/frontend): - maglevClient.lbStateLoop, trigger on backend transitions + vpp-connect, best-effort fetch on refreshAll. - Admin handlers explicitly wake the lb-state loop after lifecycle ops and set-weight (the latter emits no transition event on the maglevd side, so the WatchEvents path wouldn't have caught it). - /favicon.ico served from embedded web/public IPng logo. VPP integration: - internal/vpp/lbstate.go: dumpASesForVIP drops Pfx from the dump request (setting it silently wipes IPv4 replies in the LB plugin) and filters results by prefix on the response side instead, which also demuxes multi-VIP-on-same-port cases correctly. maglevc: - Walk now returns the unconsumed token tail; dispatch and the question listener reject unknown commands with a targeted error instead of dumping the full command tree prefixed with garbage. - On '?', echo the current line (including the '?') before the help list so the output reads like birdc. Checker / prober: - internal/checker: ±10% jitter on NextInterval so probes across restart don't all fire on the same tick. - internal/prober: HTTP User-Agent now carries the build version and project URL. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
193 lines
5.7 KiB
Go
193 lines
5.7 KiB
Go
// Copyright (c) 2026, Pim van Pelt <pim@ipng.ch>
|
|
|
|
package main
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestExpandPathsRoot(t *testing.T) {
|
|
root := buildTree()
|
|
lines := expandPaths(root, "", make(map[*Node]bool))
|
|
|
|
// Should include well-known leaf paths.
|
|
want := []string{
|
|
"show version",
|
|
"show frontends",
|
|
"show frontends <name>",
|
|
"show backends",
|
|
"show backends <name>",
|
|
"show healthchecks",
|
|
"show healthchecks <name>",
|
|
"set backend <name> pause",
|
|
"set backend <name> resume",
|
|
"set backend <name> disable",
|
|
"set backend <name> enable",
|
|
"set frontend <name> pool <pool> backend <backend> weight <weight>",
|
|
"set frontend <name> pool <pool> backend <backend> weight <weight> flush",
|
|
"watch events",
|
|
"watch events <opt>",
|
|
"config check",
|
|
"show vpp info",
|
|
"show vpp lb state",
|
|
"show vpp lb counters",
|
|
"config reload",
|
|
"sync vpp lb state",
|
|
"sync vpp lb state <name>",
|
|
"quit",
|
|
"exit",
|
|
}
|
|
|
|
paths := make(map[string]bool, len(lines))
|
|
for _, l := range lines {
|
|
paths[l.path] = true
|
|
}
|
|
|
|
for _, w := range want {
|
|
if !paths[w] {
|
|
t.Errorf("expandPaths(root) missing %q", w)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestExpandPathsShow(t *testing.T) {
|
|
root := buildTree()
|
|
showNode, _, _ := Walk(root, []string{"show"})
|
|
lines := expandPaths(showNode, "show", make(map[*Node]bool))
|
|
|
|
for _, l := range lines {
|
|
if !strings.HasPrefix(l.path, "show ") {
|
|
t.Errorf("unexpected path %q: should start with 'show '", l.path)
|
|
}
|
|
if l.help == "" {
|
|
t.Errorf("path %q has empty help", l.path)
|
|
}
|
|
}
|
|
// version, frontends, frontends <name>, backends, backends <name>,
|
|
// healthchecks, healthchecks <name>, vpp info, vpp lb state,
|
|
// vpp lb counters = 10 lines
|
|
if len(lines) != 10 {
|
|
t.Errorf("expected exactly 10 show subcommands, got %d", len(lines))
|
|
}
|
|
}
|
|
|
|
func TestExpandPathsNoCycles(t *testing.T) {
|
|
root := buildTree()
|
|
// watch events has a self-referencing slot; expandPaths must terminate.
|
|
watchEvents, _, _ := Walk(root, []string{"watch", "events"})
|
|
lines := expandPaths(watchEvents, "watch events", make(map[*Node]bool))
|
|
|
|
// Should produce exactly 2 lines: "watch events" and "watch events <opt>".
|
|
if len(lines) != 2 {
|
|
t.Errorf("watch events: expected 2 lines, got %d: %v", len(lines), lines)
|
|
}
|
|
}
|
|
|
|
func TestExpandPathsSetBackendName(t *testing.T) {
|
|
root := buildTree()
|
|
// Walk to the name slot so displayPrefix carries the actual arg.
|
|
node, _, _ := Walk(root, []string{"set", "backend", "mybackend"})
|
|
lines := expandPaths(node, "set backend mybackend", make(map[*Node]bool))
|
|
|
|
want := []string{
|
|
"set backend mybackend pause",
|
|
"set backend mybackend resume",
|
|
"set backend mybackend disable",
|
|
"set backend mybackend enable",
|
|
}
|
|
if len(lines) != len(want) {
|
|
t.Fatalf("expected %d lines, got %d: %v", len(want), len(lines), lines)
|
|
}
|
|
for i, w := range want {
|
|
if lines[i].path != w {
|
|
t.Errorf("line %d: got %q, want %q", i, lines[i].path, w)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPrefixMatchCollapsedNouns(t *testing.T) {
|
|
root := buildTree()
|
|
|
|
// "sh ba" → show backends (list all) via prefix matching.
|
|
node, args, rem := Walk(root, []string{"sh", "ba"})
|
|
if node.Run == nil {
|
|
t.Fatal("'sh ba' did not reach a Run node")
|
|
}
|
|
if len(args) != 0 {
|
|
t.Errorf("'sh ba' should have 0 args, got %v", args)
|
|
}
|
|
if len(rem) != 0 {
|
|
t.Errorf("'sh ba' should fully consume tokens, got remaining %v", rem)
|
|
}
|
|
|
|
// "sh ba nginx0" → show backends <name> (get specific) via slot.
|
|
node, args, rem = Walk(root, []string{"sh", "ba", "nginx0"})
|
|
if node.Run == nil {
|
|
t.Fatal("'sh ba nginx0' did not reach a Run node")
|
|
}
|
|
if len(args) != 1 || args[0] != "nginx0" {
|
|
t.Errorf("'sh ba nginx0' args: got %v, want [nginx0]", args)
|
|
}
|
|
if len(rem) != 0 {
|
|
t.Errorf("'sh ba nginx0' should fully consume tokens, got remaining %v", rem)
|
|
}
|
|
|
|
// "sh fr" → show frontends (list all).
|
|
node, _, _ = Walk(root, []string{"sh", "fr"})
|
|
if node.Run == nil {
|
|
t.Fatal("'sh fr' did not reach a Run node")
|
|
}
|
|
|
|
// "sh he icmp" → show healthchecks icmp (get specific).
|
|
node, args, _ = Walk(root, []string{"sh", "he", "icmp"})
|
|
if node.Run == nil {
|
|
t.Fatal("'sh he icmp' did not reach a Run node")
|
|
}
|
|
if len(args) != 1 || args[0] != "icmp" {
|
|
t.Errorf("'sh he icmp' args: got %v, want [icmp]", args)
|
|
}
|
|
}
|
|
|
|
func TestWalkUnknownTokens(t *testing.T) {
|
|
root := buildTree()
|
|
|
|
// A bare unknown word leaves every token unconsumed and anchors
|
|
// the returned node at the root — callers must treat this as
|
|
// "unknown command" rather than silently showing the whole tree.
|
|
node, _, rem := Walk(root, []string{"foo"})
|
|
if node != root {
|
|
t.Errorf("'foo' should leave walk at root, got %q", node.Word)
|
|
}
|
|
if len(rem) != 1 || rem[0] != "foo" {
|
|
t.Errorf("'foo' remaining: got %v, want [foo]", rem)
|
|
}
|
|
|
|
// Partial consumption: "show" matches but "bogus" doesn't. The
|
|
// returned remaining is the first unmatched token onwards so the
|
|
// caller can point at exactly what was wrong.
|
|
node, _, rem = Walk(root, []string{"show", "bogus", "tail"})
|
|
if node.Word != "show" {
|
|
t.Errorf("'show bogus tail' should stop at show, got %q", node.Word)
|
|
}
|
|
if len(rem) != 2 || rem[0] != "bogus" || rem[1] != "tail" {
|
|
t.Errorf("'show bogus tail' remaining: got %v, want [bogus tail]", rem)
|
|
}
|
|
}
|
|
|
|
func TestExpandPathsWeightSlotWalk(t *testing.T) {
|
|
// Verify the weight command is fully walkable (fixes bug: setWeightValue
|
|
// and setFrontendPoolName were non-slot nodes that couldn't capture tokens).
|
|
root := buildTree()
|
|
node, args, _ := Walk(root, []string{"set", "frontend", "web", "pool", "primary", "backend", "be0", "weight", "42"})
|
|
if node.Run == nil {
|
|
t.Fatal("Walk did not reach a Run node for full weight command")
|
|
}
|
|
if len(args) != 4 {
|
|
t.Errorf("expected 4 args (name, pool, backend, weight), got %d: %v", len(args), args)
|
|
}
|
|
if args[3] != "42" {
|
|
t.Errorf("args[3] (weight): got %q, want 42", args[3])
|
|
}
|
|
}
|