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>
196 lines
5.3 KiB
Go
196 lines
5.3 KiB
Go
package tail
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
// copytruncate where the application regrows the file past the old offset
|
|
// before the next poll: the size never looks smaller, the head changes.
|
|
func TestCopytruncateRegrowIsDetected(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "a.log")
|
|
os.WriteFile(p, []byte("l1\nl2\nl3\n"), 0o644)
|
|
var mu sync.Mutex
|
|
var got []string
|
|
f := NewFollower(p, Pos{})
|
|
f.Handle = func(b []byte) { mu.Lock(); got = append(got, string(b)); mu.Unlock() }
|
|
f.step()
|
|
os.WriteFile(p, []byte("n1\nn2\nn3\nn4\nn5\n"), 0o644) // truncated in place and regrown beyond offset 9
|
|
f.step()
|
|
f.step()
|
|
want := []string{"l1", "l2", "l3", "n1", "n2", "n3", "n4", "n5"}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("lines %v, want %v", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("lines %v, want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// the same while the daemon was stopped: the saved position carries the fingerprint.
|
|
func TestSavedPositionDetectsRegrownFile(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "a.log")
|
|
os.WriteFile(p, []byte("l1\nl2\nl3\n"), 0o644)
|
|
f := NewFollower(p, Pos{})
|
|
f.Handle = func([]byte) {}
|
|
f.step()
|
|
pos := f.Pos()
|
|
f.closeFile()
|
|
if pos.FPLen == 0 {
|
|
t.Fatal("the position must carry a fingerprint")
|
|
}
|
|
os.WriteFile(p, []byte("n1\nn2\nn3\nn4\nn5\n"), 0o644)
|
|
var got []string
|
|
g := NewFollower(p, pos)
|
|
g.Handle = func(b []byte) { got = append(got, string(b)) }
|
|
g.step()
|
|
if len(got) != 5 || got[0] != "n1" {
|
|
t.Fatalf("resumed in the middle of a different file: %v", got)
|
|
}
|
|
}
|
|
|
|
func TestFileAppearingLaterAndAppends(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "later.log")
|
|
var got []string
|
|
f := NewFollower(p, Pos{})
|
|
f.Handle = func(b []byte) { got = append(got, string(b)) }
|
|
f.step()
|
|
if f.Err() == nil {
|
|
t.Fatal("an absent file is reported")
|
|
}
|
|
os.WriteFile(p, []byte("a\nb\n"), 0o644)
|
|
f.step()
|
|
fh, _ := os.OpenFile(p, os.O_APPEND|os.O_WRONLY, 0)
|
|
fh.WriteString("c\n")
|
|
fh.Close()
|
|
f.step()
|
|
if len(got) != 3 || got[2] != "c" || f.Err() != nil {
|
|
t.Fatalf("%v %v", got, f.Err())
|
|
}
|
|
}
|
|
|
|
type atRec struct {
|
|
text string
|
|
end Pos
|
|
}
|
|
|
|
func collectAt(f *Follower) *[]atRec {
|
|
var got []atRec
|
|
f.HandleAt = func(b []byte, end Pos) { got = append(got, atRec{string(b), end}) }
|
|
return &got
|
|
}
|
|
|
|
func TestHandleAtReportsEndPosition(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "a.log")
|
|
os.WriteFile(p, []byte("one\ntwo\nthree\n"), 0o644)
|
|
f := NewFollower(p, Pos{})
|
|
f.Handle = func([]byte) { t.Fatal("Handle called although HandleAt is set") }
|
|
got := collectAt(f)
|
|
f.step()
|
|
if len(*got) != 3 {
|
|
t.Fatalf("got %v", *got)
|
|
}
|
|
ino := (*got)[0].end.Inode
|
|
if ino == 0 {
|
|
t.Fatal("inode missing in end position")
|
|
}
|
|
for i, off := range []int64{4, 8, 14} {
|
|
if (*got)[i].end.Offset != off || (*got)[i].end.Inode != ino {
|
|
t.Errorf("line %d end = %+v, want offset %d", i, (*got)[i].end, off)
|
|
}
|
|
}
|
|
// the end of the last line equals the follower position after the call
|
|
if f.Pos().Offset != 14 {
|
|
t.Errorf("pos %d", f.Pos().Offset)
|
|
}
|
|
// resuming from an end position yields only the new lines
|
|
f2 := NewFollower(p, Pos{Inode: ino, Offset: (*got)[1].end.Offset})
|
|
got2 := collectAt(f2)
|
|
f2.step()
|
|
if len(*got2) != 1 || (*got2)[0].text != "three" {
|
|
t.Fatalf("resume got %v", *got2)
|
|
}
|
|
}
|
|
|
|
func TestHandleAtAcrossRenameRotation(t *testing.T) {
|
|
d := t.TempDir()
|
|
p := filepath.Join(d, "a.log")
|
|
os.WriteFile(p, []byte("a1\na2\n"), 0o644)
|
|
f := NewFollower(p, Pos{})
|
|
got := collectAt(f)
|
|
f.step()
|
|
os.WriteFile(p, []byte("a3\n"), 0o644) // still the old inode: append-like
|
|
f.step()
|
|
os.Rename(p, filepath.Join(d, "a.log.1"))
|
|
os.WriteFile(p, []byte("b1\nb2\n"), 0o644)
|
|
f.step()
|
|
f.step()
|
|
want := []struct {
|
|
text string
|
|
off int64
|
|
}{{"a1", 3}, {"a2", 6}}
|
|
if len(*got) < 4 {
|
|
t.Fatalf("got %v", *got)
|
|
}
|
|
for i, w := range want {
|
|
if (*got)[i].text != w.text || (*got)[i].end.Offset != w.off {
|
|
t.Errorf("rec %d = %+v", i, (*got)[i])
|
|
}
|
|
}
|
|
n := len(*got)
|
|
b1, b2 := (*got)[n-2], (*got)[n-1]
|
|
if b1.text != "b1" || b1.end.Offset != 3 || b2.text != "b2" || b2.end.Offset != 6 {
|
|
t.Fatalf("after rotation: %+v %+v", b1, b2)
|
|
}
|
|
if b1.end.Inode == (*got)[0].end.Inode {
|
|
t.Error("inode did not change across rename rotation")
|
|
}
|
|
}
|
|
|
|
func TestHandleAtAcrossCopytruncate(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "a.log")
|
|
os.WriteFile(p, []byte("l1\nl2\nl3\n"), 0o644)
|
|
f := NewFollower(p, Pos{})
|
|
got := collectAt(f)
|
|
f.step()
|
|
os.Truncate(p, 0)
|
|
f.step()
|
|
os.WriteFile(p, []byte("n1\nn2\n"), 0o644)
|
|
f.step()
|
|
f.step()
|
|
if len(*got) != 5 {
|
|
t.Fatalf("got %v", *got)
|
|
}
|
|
if r := (*got)[2]; r.text != "l3" || r.end.Offset != 9 {
|
|
t.Errorf("%+v", r)
|
|
}
|
|
if r := (*got)[3]; r.text != "n1" || r.end.Offset != 3 {
|
|
t.Errorf("after truncate: %+v", r)
|
|
}
|
|
if r := (*got)[4]; r.text != "n2" || r.end.Offset != 6 {
|
|
t.Errorf("after truncate: %+v", r)
|
|
}
|
|
}
|
|
|
|
func TestHandleAtOversizeAdvancesOffset(t *testing.T) {
|
|
p := filepath.Join(t.TempDir(), "a.log")
|
|
big := make([]byte, 300)
|
|
for i := range big {
|
|
big[i] = 'x'
|
|
}
|
|
os.WriteFile(p, append(append([]byte("ok\n"), big...), []byte("\nnext\n")...), 0o644)
|
|
f := NewFollower(p, Pos{})
|
|
f.MaxLine = 100
|
|
over := 0
|
|
f.Oversize = func() { over++ }
|
|
got := collectAt(f)
|
|
f.step()
|
|
if over != 1 || len(*got) != 2 || (*got)[1].text != "next" || (*got)[1].end.Offset != 3+301+5 {
|
|
t.Fatalf("over=%d got=%v", over, *got)
|
|
}
|
|
}
|