Debian Codegraph @

core/parsing_time_test.go

raw
1 package core
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 type detectTimeTestCase struct {
10 name string
11 logLine string
12 wantLayout string
13 }
14
15 func TestDetectTimeFormat(t *testing.T) {
16 testCases := []detectTimeTestCase{
17 {
18 name: "rsyslog no year, 1-digit",
19 logLine: "Apr 8 01:02:03 somehost systemd[1]: Started something.",
20 wantLayout: "Jan _2 15:04:05",
21 },
22 {
23 name: "rsyslog no year, 2-digit",
24 logLine: "Apr 18 01:02:03 somehost systemd[1]: Started something.",
25 wantLayout: "Jan _2 15:04:05",
26 },
27 {
28 name: "ISO8601 non-UTC full with microseconds",
29 logLine: "2024-04-19T14:23:45.123456+02:00 INFO something happened",
30 wantLayout: "2006-01-02T15:04:05.000000Z07:00",
31 },
32 {
33 name: "ISO8601 UTC full with microseconds",
34 logLine: "2024-04-19T14:23:45.123456Z INFO something happened",
35 wantLayout: "2006-01-02T15:04:05.000000Z07:00",
36 },
37 {
38 name: "RFC3339",
39 logLine: "2024-04-19T14:23:45+02:00 Starting server",
40 wantLayout: "2006-01-02T15:04:05Z07:00",
41 },
42 {
43 name: "older journalctl with --output=short-iso-precise",
44 logLine: "2025-05-11T21:33:13.924352+0200 Starting server",
45 wantLayout: "2006-01-02T15:04:05.000000-0700",
46 },
47 {
48 name: "No timestamp in line",
49 logLine: "This is a log line without a timestamp.",
50 wantLayout: "",
51 },
52 }
53
54 for _, tc := range testCases {
55 t.Run(tc.name, func(t *testing.T) {
56 layout := DetectTimeLayout(tc.logLine)
57 assert.Equal(t, tc.wantLayout, layout)
58 })
59 }
60 }
61
62 type timeDescrTestCase struct {
63 name string
64 layout string
65 expected *TimeFormatDescr
66 expectErr string
67 }
68
69 func TestGenerateTimeDescr(t *testing.T) {
70 tests := []timeDescrTestCase{
71 {
72 name: "Traditional syslog",
73 layout: "Jan _2 15:04:05",
74 expected: &TimeFormatDescr{
75 TimestampLayout: "Jan _2 15:04:05",
76 MinuteKeyLayout: "Jan _2 15:04",
77 AWKExpr: TimeFormatAWKExpr{
78 Month: "monthByName[substr($0, 1, 3)]",
79 Year: "yearByMonth[month]",
80 Day: `(substr($0, 5, 1) == " ") ? "0" substr($0, 6, 1) : substr($0, 5, 2)`,
81 HHMM: "substr($0, 8, 5)",
82 MinuteKey: "substr($0, 1, 12)",
83 },
84 },
85 },
86 {
87 name: "ISO8601 with microseconds and timezone",
88 layout: "2006-01-02T15:04:05.000000Z07:00",
89 expected: &TimeFormatDescr{
90 TimestampLayout: "2006-01-02T15:04:05.000000Z07:00",
91 MinuteKeyLayout: "01-02T15:04",
92 AWKExpr: TimeFormatAWKExpr{
93 Month: "substr($0, 6, 2)",
94 Year: "substr($0, 1, 4)",
95 Day: "substr($0, 9, 2)",
96 HHMM: "substr($0, 12, 5)",
97 MinuteKey: "substr($0, 6, 11)",
98 },
99 },
100 },
101 {
102 name: "Custom 24-hour format",
103 layout: "2006-01-02 15:04:05",
104 expected: &TimeFormatDescr{
105 TimestampLayout: "2006-01-02 15:04:05",
106 MinuteKeyLayout: "01-02 15:04",
107 AWKExpr: TimeFormatAWKExpr{
108 Month: "substr($0, 6, 2)",
109 Year: "substr($0, 1, 4)",
110 Day: "substr($0, 9, 2)",
111 HHMM: "substr($0, 12, 5)",
112 MinuteKey: "substr($0, 6, 11)",
113 },
114 },
115 },
116 {
117 name: "ISO8601 without timezone",
118 layout: "2006-01-02T15:04:05",
119 expected: &TimeFormatDescr{
120 TimestampLayout: "2006-01-02T15:04:05",
121 MinuteKeyLayout: "01-02T15:04",
122 AWKExpr: TimeFormatAWKExpr{
123 Month: "substr($0, 6, 2)",
124 Year: "substr($0, 1, 4)",
125 Day: "substr($0, 9, 2)",
126 HHMM: "substr($0, 12, 5)",
127 MinuteKey: "substr($0, 6, 11)",
128 },
129 },
130 },
131 {
132 name: "Seconds are in between, unsupported",
133 layout: "15:04:05 Jan _2 2006",
134 expectErr: "seconds are in between of month, day, hour and min; can't extract MinuteKey",
135 },
136 {
137 name: "Non-fixed length (the date Jan 2 can also be Jan 12), unsupported",
138 layout: "Jan 2 15:04:05",
139 expectErr: "unsupported layout: required components not found",
140 },
141 }
142
143 for _, tc := range tests {
144 t.Run(tc.name, func(t *testing.T) {
145 result, err := GenerateTimeDescr(tc.layout)
146
147 if tc.expectErr != "" {
148 assert.EqualError(t, err, tc.expectErr)
149 assert.Nil(t, result)
150 } else {
151 assert.NoError(t, err)
152 assert.Equal(t, tc.expected, result)
153 }
154 })
155 }
156 }