| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 3 | |
| 4 | package logtree |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/google/go-cmp/cmp" |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 11 | |
| 12 | "source.monogon.dev/go/logging" |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | func 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 Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 23 | for i, te := range []struct { |
| 24 | now time.Time |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 25 | line string |
| 26 | want *LeveledPayload |
| 27 | }{ |
| 28 | // 0: Simple case: everything should parse correctly. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 29 | {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 Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 31 | timestamp: time.Date(2021, 03, 12, 14, 20, 4, 240540000, time.UTC), |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 32 | severity: logging.ERROR, |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 33 | file: "shared_informer.go", |
| 34 | line: 247, |
| 35 | }}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 36 | // 1: Mumbling line, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 37 | {now, "Application starting up...", nil}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 38 | // 2: Empty line, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 39 | {now, "", nil}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 40 | // 3: Line from the future, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 41 | {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 Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 42 | // 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 Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 45 | {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 Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 47 | timestamp: time.Date(1999, 12, 31, 23, 59, 43, 123456000, time.UTC), |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 48 | severity: logging.INFO, |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 49 | file: "fry.go", |
| 50 | line: 123, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 51 | }}, |
| 52 | // 5: Invalid severity, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 53 | {now, "D0312 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", nil}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 54 | // 6: Invalid time, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 55 | {now, "D0312 25:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", nil}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 56 | // 7: Simple case without sub-second timing: everything should parse correctly |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 57 | {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 Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 59 | timestamp: time.Date(2021, 03, 12, 14, 20, 4, 0, time.UTC), |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 60 | severity: logging.ERROR, |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 61 | file: "shared_informer.go", |
| 62 | line: 247, |
| 63 | }}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 64 | } { |
| 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 | } |