| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package logtree |
| 18 | |
| 19 | import ( |
| 20 | "testing" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/google/go-cmp/cmp" |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame^] | 24 | |
| 25 | "source.monogon.dev/go/logging" |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func TestParse(t *testing.T) { |
| 29 | // Injected 'now'. Used to make these tests reproducible and to allow for |
| 30 | // testing the log-across-year edgecase. |
| 31 | // Fri 12 Mar 2021 03:46:26 PM UTC |
| 32 | now := time.Unix(1615563986, 123456789) |
| 33 | // Sat 01 Jan 2000 12:00:01 AM UTC |
| 34 | nowNewYear := time.Unix(946684801, 0) |
| 35 | |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 36 | for i, te := range []struct { |
| 37 | now time.Time |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 38 | line string |
| 39 | want *LeveledPayload |
| 40 | }{ |
| 41 | // 0: Simple case: everything should parse correctly. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 42 | {now, "E0312 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", &LeveledPayload{ |
| 43 | messages: []string{"Caches are synced for attach detach"}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 44 | timestamp: time.Date(2021, 03, 12, 14, 20, 4, 240540000, time.UTC), |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame^] | 45 | severity: logging.ERROR, |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 46 | file: "shared_informer.go", |
| 47 | line: 247, |
| 48 | }}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 49 | // 1: Mumbling line, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 50 | {now, "Application starting up...", nil}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 51 | // 2: Empty line, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 52 | {now, "", nil}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 53 | // 3: Line from the future, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 54 | {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] | 55 | // 4: Log-across-year edge case. The log was emitted right before a year |
| 56 | // rollover, and parsed right after it. It should be attributed to the |
| 57 | // previous year. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 58 | {nowNewYear, "I1231 23:59:43.123456 123 fry.go:123] Here's to another lousy millenium!", &LeveledPayload{ |
| 59 | messages: []string{"Here's to another lousy millenium!"}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 60 | timestamp: time.Date(1999, 12, 31, 23, 59, 43, 123456000, time.UTC), |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame^] | 61 | severity: logging.INFO, |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 62 | file: "fry.go", |
| 63 | line: 123, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 64 | }}, |
| 65 | // 5: Invalid severity, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 66 | {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] | 67 | // 6: Invalid time, should fail. |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 68 | {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] | 69 | // 7: Simple case without sub-second timing: everything should parse correctly |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 70 | {now, "E0312 14:20:04 204 shared_informer.go:247] Caches are synced for attach detach", &LeveledPayload{ |
| 71 | messages: []string{"Caches are synced for attach detach"}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 72 | timestamp: time.Date(2021, 03, 12, 14, 20, 4, 0, time.UTC), |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame^] | 73 | severity: logging.ERROR, |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 74 | file: "shared_informer.go", |
| 75 | line: 247, |
| 76 | }}, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 77 | } { |
| 78 | got := parse(te.now, te.line) |
| 79 | if diff := cmp.Diff(te.want, got, cmp.AllowUnexported(LeveledPayload{})); diff != "" { |
| 80 | t.Errorf("%d: mismatch (-want +got):\n%s", i, diff) |
| 81 | } |
| 82 | } |
| 83 | } |