blob: 788a7eb33ea0d34d81532bd7744505f6c872e68b [file] [log] [blame]
Serge Bazanski0ab4eda2021-03-12 17:43:57 +01001// 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
17package logtree
18
19import (
20 "testing"
21 "time"
22
23 "github.com/google/go-cmp/cmp"
Serge Bazanski3c5d0632024-09-12 10:49:12 +000024
25 "source.monogon.dev/go/logging"
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010026)
27
28func 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 Bazanski96043bc2021-10-05 12:10:13 +020036 for i, te := range []struct {
37 now time.Time
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010038 line string
39 want *LeveledPayload
40 }{
41 // 0: Simple case: everything should parse correctly.
Serge Bazanski96043bc2021-10-05 12:10:13 +020042 {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 Bazanski0ab4eda2021-03-12 17:43:57 +010044 timestamp: time.Date(2021, 03, 12, 14, 20, 4, 240540000, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000045 severity: logging.ERROR,
Serge Bazanski96043bc2021-10-05 12:10:13 +020046 file: "shared_informer.go",
47 line: 247,
48 }},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010049 // 1: Mumbling line, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020050 {now, "Application starting up...", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010051 // 2: Empty line, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020052 {now, "", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010053 // 3: Line from the future, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020054 {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 +010055 // 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 Bazanski96043bc2021-10-05 12:10:13 +020058 {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 Bazanski0ab4eda2021-03-12 17:43:57 +010060 timestamp: time.Date(1999, 12, 31, 23, 59, 43, 123456000, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000061 severity: logging.INFO,
Serge Bazanski96043bc2021-10-05 12:10:13 +020062 file: "fry.go",
63 line: 123,
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010064 }},
65 // 5: Invalid severity, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020066 {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 +010067 // 6: Invalid time, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020068 {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 +010069 // 7: Simple case without sub-second timing: everything should parse correctly
Serge Bazanski96043bc2021-10-05 12:10:13 +020070 {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 Bazanski0ab4eda2021-03-12 17:43:57 +010072 timestamp: time.Date(2021, 03, 12, 14, 20, 4, 0, time.UTC),
Serge Bazanski3c5d0632024-09-12 10:49:12 +000073 severity: logging.ERROR,
Serge Bazanski96043bc2021-10-05 12:10:13 +020074 file: "shared_informer.go",
75 line: 247,
76 }},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010077 } {
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}