Files
urutau/internal/tail/tail_test.go
T
Lucas RayanandClaude Opus 5.5 b4a2c2c9ec
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Always-on Go service (stdlib only) that follows the Traefik JSON access
log, the sshd log and the fail2ban log of a host, classifies attacks,
scores sources, keeps bounded aggregates with snapshots, and serves an
embedded dashboard with live updates, offline geolocation (DB-IP Lite),
privacy-friendly traffic analytics, an exportable blocklist and
token-authenticated federation between instances.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 16:13:34 -03:00

158 lines
3.9 KiB
Go

package tail
import (
"compress/gzip"
"context"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"time"
)
type sink struct {
mu sync.Mutex
lines []string
}
func (s *sink) add(b []byte) { s.mu.Lock(); s.lines = append(s.lines, string(b)); s.mu.Unlock() }
func (s *sink) get() []string {
s.mu.Lock()
defer s.mu.Unlock()
return append([]string(nil), s.lines...)
}
func waitFor(t *testing.T, s *sink, n int) {
t.Helper()
for i := 0; i < 200; i++ {
if len(s.get()) >= n {
return
}
time.Sleep(20 * time.Millisecond)
}
t.Fatalf("timeout waiting for %d lines, got %v", n, s.get())
}
func appendTo(t *testing.T, path, text string) {
t.Helper()
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
t.Fatal(err)
}
f.WriteString(text)
f.Close()
}
func start(t *testing.T, path string, pos Pos) (*Follower, *sink, context.CancelFunc) {
f := NewFollower(path, pos)
f.Poll = 10 * time.Millisecond
s := &sink{}
f.Handle = s.add
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() { f.Run(ctx); close(done) }()
return f, s, func() { cancel(); <-done }
}
func TestFollowTruncateAndRename(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "access.log")
appendTo(t, p, "a\nb\npart")
_, s, stop := start(t, p, Pos{})
defer stop()
waitFor(t, s, 2)
appendTo(t, p, "ial\n")
waitFor(t, s, 3)
if got := s.get()[2]; got != "partial" {
t.Fatalf("partial line: %q", got)
}
// copy + truncate rotation (same inode, smaller size)
if err := os.Truncate(p, 0); err != nil {
t.Fatal(err)
}
time.Sleep(60 * time.Millisecond)
appendTo(t, p, "c\n")
waitFor(t, s, 4)
// rename + recreate (new inode); unread lines of the old file must survive
appendTo(t, p, "d\n")
if err := os.Rename(p, p+".1"); err != nil {
t.Fatal(err)
}
appendTo(t, p, "e\nf\n")
waitFor(t, s, 7)
got := strings.Join(s.get(), ",")
if got != "a,b,partial,c,d,e,f" {
t.Fatalf("got %s", got)
}
}
func TestResumeAndOversize(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "x.log")
appendTo(t, p, "one\ntwo\n")
f, s, stop := start(t, p, Pos{})
waitFor(t, s, 2)
pos := f.Pos()
stop()
if pos.Offset != 8 || pos.Inode == 0 {
t.Fatalf("pos %+v", pos)
}
appendTo(t, p, "three\n")
f2, s2, stop2 := start(t, p, pos)
oversize := 0
_ = f2
waitFor(t, s2, 1)
stop2()
if l := s2.get(); len(l) != 1 || l[0] != "three" {
t.Fatalf("resume got %v", l)
}
// oversize line skipped and counted, following lines still read
p2 := filepath.Join(dir, "big.log")
appendTo(t, p2, strings.Repeat("x", 5000)+"\nok\n")
f3 := NewFollower(p2, Pos{})
f3.MaxLine = 1000
f3.Poll = 10 * time.Millisecond
s3 := &sink{}
f3.Handle = s3.add
f3.Oversize = func() { oversize++ }
ctx, cancel := context.WithCancel(context.Background())
go f3.Run(ctx)
waitFor(t, s3, 1)
cancel()
if l := s3.get(); len(l) != 1 || l[0] != "ok" || oversize != 1 {
t.Fatalf("got %v oversize=%d", l, oversize)
}
}
func TestStalePosAndBackups(t *testing.T) {
dir := t.TempDir()
p := filepath.Join(dir, "a.log")
appendTo(t, p, "x\ny\n")
// saved offset beyond the file (truncated while down): restart from zero
_, s, stop := start(t, p, Pos{Inode: 0, Offset: 9999})
waitFor(t, s, 2)
stop()
// backups ordering and gzip reading
for _, n := range []string{"a.log.2.gz", "a.log.1.gz"} {
f, _ := os.Create(filepath.Join(dir, n))
gz := gzip.NewWriter(f)
gz.Write([]byte("from-" + n + "\nsecond\n"))
gz.Close()
f.Close()
}
appendTo(t, p+".1", "plain\n")
b := Backups(p)
// .1.gz wins over .1 for the same index; order is oldest first
if len(b) != 2 || !strings.HasSuffix(b[0], ".2.gz") || !strings.HasSuffix(b[1], ".1.gz") {
t.Fatalf("backups %v", b)
}
var got []string
if err := ReadAll(b[0], 1000, func(l []byte) { got = append(got, string(l)) }, nil); err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[0] != "from-a.log.2.gz" {
t.Fatalf("%v", got)
}
}