Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that tails the logs, ships raw lines to the hub over HTTPS with an on-disk spool, and applies enforce decisions locally. The hub keeps one node per probe (primary node = the 1.x data dir, unchanged), does all analysis and serves the UI. Peer federation is removed. 'urutau import' brings 1.x data dirs in with hash and count verification. UI: probes page, host qualified incidents, IPs never wrap, egress heatmap labels, live and replay globes no longer auto-rotate, replay repeat, live traffic metrics. BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone; log path variables move from the hub to the probe. See docs/migration-2.0.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
3.3 KiB
Go
130 lines
3.3 KiB
Go
package probeagent
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire"
|
|
)
|
|
|
|
func mkBatch(seq uint64, n int, pad string) wire.Batch {
|
|
b := wire.Batch{Seq: seq}
|
|
for i := 0; i < n; i++ {
|
|
b.Lines = append(b.Lines, wire.Line{Src: wire.SrcTraefik, Text: fmt.Sprintf("%d-%d%s", seq, i, pad), End: wire.Pos{Inode: 1, Offset: int64(i)}})
|
|
}
|
|
return b
|
|
}
|
|
|
|
func TestSpoolOrderAndReopen(t *testing.T) {
|
|
d := t.TempDir()
|
|
sp, err := OpenSpool(d, 1<<20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for s := uint64(1); s <= 5; s++ {
|
|
if dr, err := sp.Add(mkBatch(s, 3, "")); err != nil || dr != 0 {
|
|
t.Fatal(dr, err)
|
|
}
|
|
}
|
|
sp.Ack(2)
|
|
sp2, err := OpenSpool(d, 1<<20) // reopen
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sp2.MaxSeq() != 5 {
|
|
t.Errorf("MaxSeq %d", sp2.MaxSeq())
|
|
}
|
|
for want := uint64(3); want <= 5; want++ {
|
|
b, ok, err := sp2.Head()
|
|
if err != nil || !ok || b.Seq != want || len(b.Lines) != 3 {
|
|
t.Fatalf("head %d: %+v %v %v", want, b.Seq, ok, err)
|
|
}
|
|
sp2.Ack(want)
|
|
}
|
|
if _, ok, _ := sp2.Head(); ok {
|
|
t.Error("spool not empty")
|
|
}
|
|
if n, f := sp2.Stats(); n != 0 || f != 0 {
|
|
t.Errorf("stats %d %d", n, f)
|
|
}
|
|
}
|
|
|
|
func TestSpoolCapDropsOldestAndCounts(t *testing.T) {
|
|
d := t.TempDir()
|
|
probe := mkBatch(1, 10, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
|
tmp, _ := OpenSpool(t.TempDir(), 1<<30)
|
|
tmp.Add(probe)
|
|
size, _ := tmp.Stats()
|
|
sp, _ := OpenSpool(d, size*3+size/2) // room for 3 batches
|
|
var dropped uint64
|
|
for s := uint64(1); s <= 6; s++ {
|
|
dr, err := sp.Add(mkBatch(s, 10, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dropped += dr
|
|
}
|
|
if dropped != 30 {
|
|
t.Errorf("dropped %d lines, want 30 (3 batches of 10)", dropped)
|
|
}
|
|
b, ok, _ := sp.Head()
|
|
if !ok || b.Seq != 4 {
|
|
t.Errorf("oldest remaining = %d, want 4", b.Seq)
|
|
}
|
|
if _, f := sp.Stats(); f != 3 {
|
|
t.Errorf("files %d", f)
|
|
}
|
|
ents, _ := os.ReadDir(d)
|
|
if len(ents) != 3 {
|
|
t.Errorf("%d files on disk", len(ents))
|
|
}
|
|
}
|
|
|
|
func TestSpoolKeepsLoneOversizeBatch(t *testing.T) {
|
|
sp, _ := OpenSpool(t.TempDir(), 10)
|
|
dr, err := sp.Add(mkBatch(1, 5, ""))
|
|
if err != nil || dr != 0 {
|
|
t.Fatal(dr, err)
|
|
}
|
|
if _, f := sp.Stats(); f != 1 {
|
|
t.Error("newest batch must survive the cap")
|
|
}
|
|
}
|
|
|
|
func TestSpoolCorruptFileSkipped(t *testing.T) {
|
|
d := t.TempDir()
|
|
sp, _ := OpenSpool(d, 1<<20)
|
|
sp.Add(mkBatch(1, 1, ""))
|
|
sp.Add(mkBatch(2, 1, ""))
|
|
os.WriteFile(filepath.Join(d, spoolName(1)), []byte("{broken"), 0o600)
|
|
if _, _, err := sp.Head(); err == nil {
|
|
t.Error("corrupt batch not reported")
|
|
}
|
|
b, ok, err := sp.Head()
|
|
if err != nil || !ok || b.Seq != 2 {
|
|
t.Errorf("next head %v %v %v", b.Seq, ok, err)
|
|
}
|
|
}
|
|
|
|
func TestStateAtomicRoundTrip(t *testing.T) {
|
|
d := t.TempDir()
|
|
st, _ := loadState(d)
|
|
st.commit(func(seq uint64) uint64 { return 7 }, map[string]wire.Pos{"auth": {Inode: 9, Offset: 123, FP: 5, FPLen: 4}})
|
|
st.setLastCmd(4)
|
|
st2, err := loadState(d)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := st2.s
|
|
if s.Seq != 1 || s.LastCmd != 4 || s.Dropped != 7 || s.Positions["auth"].Offset != 123 || s.Positions["auth"].FPLen != 4 {
|
|
t.Errorf("%+v", s)
|
|
}
|
|
os.WriteFile(filepath.Join(d, "state.json"), []byte("garbage"), 0o600)
|
|
st3, err := loadState(d)
|
|
if err != nil || st3.s.Seq != 0 || st3.s.Positions == nil {
|
|
t.Errorf("corrupt state not reset: %+v %v", st3.s, err)
|
|
}
|
|
}
|