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" |
| 24 | ) |
| 25 | |
| 26 | func TestParse(t *testing.T) { |
| 27 | // Injected 'now'. Used to make these tests reproducible and to allow for |
| 28 | // testing the log-across-year edgecase. |
| 29 | // Fri 12 Mar 2021 03:46:26 PM UTC |
| 30 | now := time.Unix(1615563986, 123456789) |
| 31 | // Sat 01 Jan 2000 12:00:01 AM UTC |
| 32 | nowNewYear := time.Unix(946684801, 0) |
| 33 | |
| 34 | for i, te := range []struct{ |
| 35 | now time.Time |
| 36 | line string |
| 37 | want *LeveledPayload |
| 38 | }{ |
| 39 | // 0: Simple case: everything should parse correctly. |
| 40 | { now, "E0312 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", &LeveledPayload{ |
| 41 | messages: []string{"Caches are synced for attach detach"}, |
| 42 | timestamp: time.Date(2021, 03, 12, 14, 20, 4, 240540000, time.UTC), |
| 43 | severity: ERROR, |
| 44 | file: "shared_informer.go", |
| 45 | line: 247, |
| 46 | } }, |
| 47 | // 1: Mumbling line, should fail. |
| 48 | { now, "Application starting up...", nil}, |
| 49 | // 2: Empty line, should fail. |
| 50 | { now, "", nil}, |
| 51 | // 3: Line from the future, should fail. |
| 52 | { now, "I1224 14:20:04.240540 204 john_titor.go:247] I'm sorry, what day is it today? Uuuh, and what year?", nil }, |
| 53 | // 4: Log-across-year edge case. The log was emitted right before a year |
| 54 | // rollover, and parsed right after it. It should be attributed to the |
| 55 | // previous year. |
| 56 | { nowNewYear, "I1231 23:59:43.123456 123 fry.go:123] Here's to another lousy millenium!", &LeveledPayload{ |
| 57 | messages: []string{"Here's to another lousy millenium!"}, |
| 58 | timestamp: time.Date(1999, 12, 31, 23, 59, 43, 123456000, time.UTC), |
| 59 | severity: INFO, |
| 60 | file: "fry.go", |
| 61 | line: 123, |
| 62 | }}, |
| 63 | // 5: Invalid severity, should fail. |
| 64 | { now, "D0312 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", nil }, |
| 65 | // 6: Invalid time, should fail. |
| 66 | { now, "D0312 25:20:04.240540 204 shared_informer.go:247] Caches are synced for attach detach", nil }, |
| 67 | // 7: Simple case without sub-second timing: everything should parse correctly |
| 68 | { now, "E0312 14:20:04 204 shared_informer.go:247] Caches are synced for attach detach", &LeveledPayload{ |
| 69 | messages: []string{"Caches are synced for attach detach"}, |
| 70 | timestamp: time.Date(2021, 03, 12, 14, 20, 4, 0, time.UTC), |
| 71 | severity: ERROR, |
| 72 | file: "shared_informer.go", |
| 73 | line: 247, |
| 74 | } }, |
| 75 | } { |
| 76 | got := parse(te.now, te.line) |
| 77 | if diff := cmp.Diff(te.want, got, cmp.AllowUnexported(LeveledPayload{})); diff != "" { |
| 78 | t.Errorf("%d: mismatch (-want +got):\n%s", i, diff) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |