|
| 1 | +package ingest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + stderrors "errors" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/strrl/lapp/pkg/event" |
| 10 | + "github.com/strrl/lapp/pkg/logsource" |
| 11 | + "github.com/strrl/lapp/pkg/store" |
| 12 | +) |
| 13 | + |
| 14 | +type stubParser struct { |
| 15 | + parse func(context.Context, string) (*ParseResult, error) |
| 16 | +} |
| 17 | + |
| 18 | +func (s stubParser) Parse(ctx context.Context, raw string) (*ParseResult, error) { |
| 19 | + return s.parse(ctx, raw) |
| 20 | +} |
| 21 | + |
| 22 | +func newTestStore(t *testing.T) *store.DuckDBStore { |
| 23 | + t.Helper() |
| 24 | + |
| 25 | + s, err := store.NewDuckDBStore("") |
| 26 | + if err != nil { |
| 27 | + t.Fatalf("NewDuckDBStore: %v", err) |
| 28 | + } |
| 29 | + t.Cleanup(func() { _ = s.Close() }) |
| 30 | + |
| 31 | + if err := s.Init(context.Background()); err != nil { |
| 32 | + t.Fatalf("Init: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + return s |
| 36 | +} |
| 37 | + |
| 38 | +func TestFromRawLineWithoutParserUsesFallbackEvent(t *testing.T) { |
| 39 | + line := &logsource.LogLine{ |
| 40 | + LineNumber: 7, |
| 41 | + Content: " WARN worker stalled for 30s ", |
| 42 | + } |
| 43 | + |
| 44 | + outcome, err := FromRawLine(context.Background(), line, nil) |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("FromRawLine: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + if outcome.ParseErr != nil { |
| 50 | + t.Fatalf("ParseErr: got %v, want nil", outcome.ParseErr) |
| 51 | + } |
| 52 | + if outcome.Event.Text != line.Content { |
| 53 | + t.Fatalf("Event.Text: got %q, want %q", outcome.Event.Text, line.Content) |
| 54 | + } |
| 55 | + if len(outcome.Event.Attrs) != 0 { |
| 56 | + t.Fatalf("Event.Attrs: got %v, want empty map", outcome.Event.Attrs) |
| 57 | + } |
| 58 | + if outcome.Event.Inferred == nil { |
| 59 | + t.Fatal("Event.Inferred: got nil, want non-nil empty inferred") |
| 60 | + } |
| 61 | + if outcome.LogEntry.Raw != line.Content { |
| 62 | + t.Fatalf("LogEntry.Raw: got %q, want %q", outcome.LogEntry.Raw, line.Content) |
| 63 | + } |
| 64 | + if outcome.LogEntry.LineNumber != line.LineNumber || outcome.LogEntry.EndLineNumber != line.LineNumber { |
| 65 | + t.Fatalf("log entry line bounds: got (%d,%d), want (%d,%d)", outcome.LogEntry.LineNumber, outcome.LogEntry.EndLineNumber, line.LineNumber, line.LineNumber) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestStoreRawLinePersistsWhenParserFails(t *testing.T) { |
| 70 | + s := newTestStore(t) |
| 71 | + line := &logsource.LogLine{ |
| 72 | + LineNumber: 12, |
| 73 | + Content: "ERROR checkout request req_123 timed out", |
| 74 | + } |
| 75 | + |
| 76 | + outcome, err := StoreRawLine(context.Background(), s, line, stubParser{ |
| 77 | + parse: func(context.Context, string) (*ParseResult, error) { |
| 78 | + return nil, context.DeadlineExceeded |
| 79 | + }, |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("StoreRawLine: %v", err) |
| 83 | + } |
| 84 | + if !stderrors.Is(outcome.ParseErr, context.DeadlineExceeded) { |
| 85 | + t.Fatalf("ParseErr: got %v, want %v", outcome.ParseErr, context.DeadlineExceeded) |
| 86 | + } |
| 87 | + if outcome.Event.Text != line.Content { |
| 88 | + t.Fatalf("Event.Text: got %q, want %q", outcome.Event.Text, line.Content) |
| 89 | + } |
| 90 | + |
| 91 | + stored, err := s.QueryLogs(context.Background(), store.QueryOpts{}) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("QueryLogs: %v", err) |
| 94 | + } |
| 95 | + if len(stored) != 1 { |
| 96 | + t.Fatalf("stored entries: got %d, want 1", len(stored)) |
| 97 | + } |
| 98 | + if stored[0].Raw != line.Content { |
| 99 | + t.Fatalf("stored raw: got %q, want %q", stored[0].Raw, line.Content) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestStoreRawLineUsesParsedFieldsButKeepsRawText(t *testing.T) { |
| 104 | + s := newTestStore(t) |
| 105 | + ts := time.Date(2026, 3, 10, 21, 0, 0, 0, time.UTC) |
| 106 | + line := &logsource.LogLine{ |
| 107 | + LineNumber: 3, |
| 108 | + Content: "level=info service=auth-api msg=\"user user_456 authenticated\"", |
| 109 | + } |
| 110 | + |
| 111 | + outcome, err := StoreRawLine(context.Background(), s, line, stubParser{ |
| 112 | + parse: func(context.Context, string) (*ParseResult, error) { |
| 113 | + return &ParseResult{ |
| 114 | + Timestamp: &ts, |
| 115 | + Attrs: map[string]string{ |
| 116 | + "level": "info", |
| 117 | + "service": "auth-api", |
| 118 | + }, |
| 119 | + Inferred: &event.Inferred{ |
| 120 | + Pattern: "user <*> authenticated", |
| 121 | + Entity: "auth-api", |
| 122 | + }, |
| 123 | + Labels: map[string]string{ |
| 124 | + "pattern": "user-authenticated", |
| 125 | + }, |
| 126 | + }, nil |
| 127 | + }, |
| 128 | + }) |
| 129 | + if err != nil { |
| 130 | + t.Fatalf("StoreRawLine: %v", err) |
| 131 | + } |
| 132 | + |
| 133 | + if outcome.Event.Text != line.Content { |
| 134 | + t.Fatalf("Event.Text: got %q, want %q", outcome.Event.Text, line.Content) |
| 135 | + } |
| 136 | + if outcome.Event.Timestamp == nil || !outcome.Event.Timestamp.Equal(ts) { |
| 137 | + t.Fatalf("Event.Timestamp: got %v, want %v", outcome.Event.Timestamp, ts) |
| 138 | + } |
| 139 | + if outcome.Event.Attrs["service"] != "auth-api" { |
| 140 | + t.Fatalf("Event.Attrs[service]: got %q, want %q", outcome.Event.Attrs["service"], "auth-api") |
| 141 | + } |
| 142 | + if outcome.Event.Inferred == nil || outcome.Event.Inferred.Pattern != "user <*> authenticated" { |
| 143 | + t.Fatalf("Event.Inferred: got %+v", outcome.Event.Inferred) |
| 144 | + } |
| 145 | + if outcome.LogEntry.Raw != line.Content { |
| 146 | + t.Fatalf("LogEntry.Raw: got %q, want %q", outcome.LogEntry.Raw, line.Content) |
| 147 | + } |
| 148 | + if outcome.LogEntry.Timestamp != ts { |
| 149 | + t.Fatalf("LogEntry.Timestamp: got %v, want %v", outcome.LogEntry.Timestamp, ts) |
| 150 | + } |
| 151 | + if outcome.LogEntry.Labels["pattern"] != "user-authenticated" { |
| 152 | + t.Fatalf("LogEntry.Labels[pattern]: got %q, want %q", outcome.LogEntry.Labels["pattern"], "user-authenticated") |
| 153 | + } |
| 154 | + |
| 155 | + stored, err := s.QueryLogs(context.Background(), store.QueryOpts{}) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("QueryLogs: %v", err) |
| 158 | + } |
| 159 | + if len(stored) != 1 { |
| 160 | + t.Fatalf("stored entries: got %d, want 1", len(stored)) |
| 161 | + } |
| 162 | + if stored[0].Raw != line.Content { |
| 163 | + t.Fatalf("stored raw: got %q, want %q", stored[0].Raw, line.Content) |
| 164 | + } |
| 165 | + if stored[0].Labels["pattern"] != "user-authenticated" { |
| 166 | + t.Fatalf("stored labels[pattern]: got %q, want %q", stored[0].Labels["pattern"], "user-authenticated") |
| 167 | + } |
| 168 | +} |
0 commit comments