CI / Format and vet (push) Successful in 1m15s
CI / Dependency audit (push) Successful in 36s
CI / Tests (Linux, race detector) (push) Successful in 2m7s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Skipped
Release / Build hub image (amd64) (push) Skipped
Release / Build probe image (amd64) (push) Skipped
Release / Build hub image (arm64) (push) Skipped
Release / Build probe image (arm64) (push) Skipped
Release / Publish tags (hub) (push) Successful in 18s
Release / Publish tags (probe) (push) Successful in 18s
Release / Gitea release (push) Successful in 4s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
171 lines
4.5 KiB
Go
171 lines
4.5 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)
|
|
}
|
|
}
|
|
|
|
// Traefik: the first 64 bytes of each rotated generation were identical (same
|
|
// keep-alive client address and port), so the head must cover the whole line.
|
|
func TestHeadCoversFirstLine(t *testing.T) {
|
|
a := []byte(`{"ClientAddr":"195.200.0.252:34352","ClientHost":"195.200.0.252","StartUTC":"2026-11-16T21:06:20.1Z"}` + "\n")
|
|
b := []byte(`{"ClientAddr":"195.200.0.252:34352","ClientHost":"195.200.0.252","StartUTC":"2026-11-16T22:48:21.7Z"}` + "\n")
|
|
if fingerprint(a[:64]) != fingerprint(b[:64]) {
|
|
t.Fatal("fixture: the first 64 bytes should collide")
|
|
}
|
|
if fpMax < len(a) || fingerprint(a) == fingerprint(b) {
|
|
t.Fatal("head fingerprint must tell the two generations apart")
|
|
}
|
|
}
|