blob: 3c25d8abf57adc0b7431f549ff77536755f6352c [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Serge Bazanski0ab4eda2021-03-12 17:43:57 +01002// SPDX-License-Identifier: Apache-2.0
Serge Bazanski0ab4eda2021-03-12 17:43:57 +01003
4package logtree
5
6import (
7 "testing"
8 "time"
9
10 "github.com/google/go-cmp/cmp"
Serge Bazanski3c5d0632024-09-12 10:49:12 +000011
12 "source.monogon.dev/go/logging"
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010013)
14
15func TestParse(t *testing.T) {
16 // Injected 'now'. Used to make these tests reproducible and to allow for
17 // testing the log-across-year edgecase.
18 // Fri 12 Mar 2021 03:46:26 PM UTC
19 now := time.Unix(1615563986, 123456789)
20 // Sat 01 Jan 2000 12:00:01 AM UTC
21 nowNewYear := time.Unix(946684801, 0)
22
Serge Bazanski96043bc2021-10-05 12:10:13 +020023 for i, te := range []struct {
24 now time.Time
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010025 line string
26 want *LeveledPayload
27 }{
28 // 0: Simple case: everything should parse correctly.
Serge Bazanski96043bc2021-10-05 12:10:13 +020029 {now, "E0312 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", &LeveledPayload{
30 messages: []string{"Caches are synced for attach detach"},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010031 timestamp: time.Date(2021, 03, 12, 14, 20, 4, 240540000, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000032 severity: logging.ERROR,
Serge Bazanski96043bc2021-10-05 12:10:13 +020033 file: "shared_informer.go",
34 line: 247,
35 }},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010036 // 1: Mumbling line, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020037 {now, "Application starting up...", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010038 // 2: Empty line, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020039 {now, "", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010040 // 3: Line from the future, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020041 {now, "I1224 14:20:04.240540 204 john_titor.go:247] I'm sorry, what day is it today? Uuuh, and what year?", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010042 // 4: Log-across-year edge case. The log was emitted right before a year
43 // rollover, and parsed right after it. It should be attributed to the
44 // previous year.
Serge Bazanski96043bc2021-10-05 12:10:13 +020045 {nowNewYear, "I1231 23:59:43.123456 123 fry.go:123] Here's to another lousy millenium!", &LeveledPayload{
46 messages: []string{"Here's to another lousy millenium!"},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010047 timestamp: time.Date(1999, 12, 31, 23, 59, 43, 123456000, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000048 severity: logging.INFO,
Serge Bazanski96043bc2021-10-05 12:10:13 +020049 file: "fry.go",
50 line: 123,
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010051 }},
52 // 5: Invalid severity, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020053 {now, "D0312 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010054 // 6: Invalid time, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020055 {now, "D0312 25:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010056 // 7: Simple case without sub-second timing: everything should parse correctly
Serge Bazanski96043bc2021-10-05 12:10:13 +020057 {now, "E0312 14:20:04 204 shared_informer.go:247] Caches are synced for attach detach", &LeveledPayload{
58 messages: []string{"Caches are synced for attach detach"},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010059 timestamp: time.Date(2021, 03, 12, 14, 20, 4, 0, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000060 severity: logging.ERROR,
Serge Bazanski96043bc2021-10-05 12:10:13 +020061 file: "shared_informer.go",
62 line: 247,
63 }},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010064 } {
65 got := parse(te.now, te.line)
66 if diff := cmp.Diff(te.want, got, cmp.AllowUnexported(LeveledPayload{})); diff != "" {
67 t.Errorf("%d: mismatch (-want +got):\n%s", i, diff)
68 }
69 }
70}