blob: d53df3f57c4216c0bd7af9ba0e8a5d327abc8d1e [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"
24)
25
26func 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
Serge Bazanski96043bc2021-10-05 12:10:13 +020034 for i, te := range []struct {
35 now time.Time
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010036 line string
37 want *LeveledPayload
38 }{
39 // 0: Simple case: everything should parse correctly.
Serge Bazanski96043bc2021-10-05 12:10:13 +020040 {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"},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010042 timestamp: time.Date(2021, 03, 12, 14, 20, 4, 240540000, time.UTC),
Serge Bazanski96043bc2021-10-05 12:10:13 +020043 severity: ERROR,
44 file: "shared_informer.go",
45 line: 247,
46 }},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010047 // 1: Mumbling line, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020048 {now, "Application starting up...", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010049 // 2: Empty line, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020050 {now, "", nil},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010051 // 3: Line from the future, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020052 {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 +010053 // 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.
Serge Bazanski96043bc2021-10-05 12:10:13 +020056 {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!"},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010058 timestamp: time.Date(1999, 12, 31, 23, 59, 43, 123456000, time.UTC),
Serge Bazanski96043bc2021-10-05 12:10:13 +020059 severity: INFO,
60 file: "fry.go",
61 line: 123,
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010062 }},
63 // 5: Invalid severity, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020064 {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 +010065 // 6: Invalid time, should fail.
Serge Bazanski96043bc2021-10-05 12:10:13 +020066 {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 +010067 // 7: Simple case without sub-second timing: everything should parse correctly
Serge Bazanski96043bc2021-10-05 12:10:13 +020068 {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"},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010070 timestamp: time.Date(2021, 03, 12, 14, 20, 4, 0, time.UTC),
Serge Bazanski96043bc2021-10-05 12:10:13 +020071 severity: ERROR,
72 file: "shared_informer.go",
73 line: 247,
74 }},
Serge Bazanski0ab4eda2021-03-12 17:43:57 +010075 } {
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}