forked from ingyamilmolinar/doctorgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
304 lines (277 loc) · 9.09 KB
/
Copy pathparser_test.go
File metadata and controls
304 lines (277 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"sync"
"testing"
"time"
)
func TestAndroidParser(t *testing.T) {
androidParser, err := newParser(logger.Sugar(), "^(?P<DATE>\\d{2}-\\d{2})\\s(?P<TIME>\\d{2}:\\d{2}:\\d{2}.\\d{3})\\s+(?P<PID>\\d+)\\s+(?P<TID>\\d+)\\s+(?P<LEVEL>[A-Z])\\s+(?P<TAG>[^:]+):\\s(?P<MESSAGE>.+)$",
[]variableMatcher{}, []variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
}, []variableMatcher{})
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &androidParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "03-17 16:16:09.141 1702 1820 D DisplayPowerController: Animating brightness: target=38, rate=200",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"DATE": "03-17",
"TIME": "16:16:09.141",
"PID": "1702",
"TID": "1820",
"LEVEL": "D",
"TAG": "DisplayPowerController",
"MESSAGE": "Animating brightness: target=38, rate=200",
},
}
testParser(t, androidParser, expectedEntry, 2000, "testlogs/Android_2k.log")
}
func TestApacheParser(t *testing.T) {
apacheParser, err := newParser(logger.Sugar(), "^\\[(?P<DATE>\\w{3} \\w{3} \\d{2} \\d{2}:\\d{2}:\\d{2} \\d{4})\\] \\[(?P<SEVERITY>\\w+)\\] (?P<MESSAGE>.*)$", []variableMatcher{}, []variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
}, []variableMatcher{})
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &apacheParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "[Mon Dec 05 19:15:57 2005] [error] mod_jk child workerEnv in error state 6",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"DATE": "Mon Dec 05 19:15:57 2005",
"SEVERITY": "error",
"MESSAGE": "mod_jk child workerEnv in error state 6",
},
}
testParser(t, apacheParser, expectedEntry, 2000, "testlogs/Apache_2k.log")
}
func TestHDFSParser(t *testing.T) {
hdfsParser, err := newParser(logger.Sugar(), "^(?P<DATE>\\d{6})\\s(?P<TIME>\\d{6})\\s(?P<PID>\\d+)\\s(?P<LEVEL>\\w+)\\s(?P<CLASS>[^\\s]+):\\s(?P<MESSAGE>.*)$",
[]variableMatcher{}, []variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
}, []variableMatcher{})
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &hdfsParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "081111 102017 26347 INFO dfs.DataNode$DataXceiver: Receiving block blk_4343207286455274569 src: /10.250.9.207:59759 dest: /10.250.9.207:50010",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"DATE": "081111",
"TIME": "102017",
"PID": "26347",
"LEVEL": "INFO",
"CLASS": "dfs.DataNode$DataXceiver",
"MESSAGE": "Receiving block blk_4343207286455274569 src: /10.250.9.207:59759 dest: /10.250.9.207:50010",
},
}
testParser(t, hdfsParser, expectedEntry, 2000, "testlogs/HDFS_2k.log")
}
func TestHadoopParser(t *testing.T) {
hadoopParser, err := newParser(logger.Sugar(), "^(?P<TIMESTAMP>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2},\\d{3})\\s+(?P<LEVEL>[A-Z]+)\\s+\\[(?P<THREAD>[^\\]]+)\\] (?P<CLASS>[^:]+): (?P<MESSAGE>.+)$",
[]variableMatcher{}, []variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
}, []variableMatcher{})
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &hadoopParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "2015-10-18 18:10:55,202 WARN [LeaseRenewer:msrabi@msra-sa-41:9000] org.apache.hadoop.ipc.Client: Address change detected. Old: msra-sa-41/10.190.173.170:9000 New: msra-sa-41:9000",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"TIMESTAMP": "2015-10-18 18:10:55,202",
"LEVEL": "WARN",
"THREAD": "LeaseRenewer:msrabi@msra-sa-41:9000",
"CLASS": "org.apache.hadoop.ipc.Client",
"MESSAGE": "Address change detected. Old: msra-sa-41/10.190.173.170:9000 New: msra-sa-41:9000",
},
}
testParser(t, hadoopParser, expectedEntry, 2000, "testlogs/Hadoop_2k.log")
}
func TestLinuxParser(t *testing.T) {
linuxParser, err := newParser(logger.Sugar(), "^(?P<DATE>[A-Z][a-z]{2}\\s+\\d{1,2})\\s+(?P<TIME>\\d{2}:\\d{2}:\\d{2})\\s+(?P<HOST>\\S+)\\s+(?P<PROCESS>[^:]+)(\\[(?P<PID>\\d+)\\])?:\\s+(?P<MESSAGE>.+)$",
[]variableMatcher{}, []variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
}, []variableMatcher{})
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &linuxParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "Jul 27 14:42:00 combo kernel: Linux agpgart interface v0.100 (c) Dave Jones",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"DATE": "Jul 27",
"TIME": "14:42:00",
"HOST": "combo",
"PROCESS": "kernel",
"PID": "",
"MESSAGE": "Linux agpgart interface v0.100 (c) Dave Jones",
},
}
testParser(t, linuxParser, expectedEntry, 2000, "testlogs/Linux_2k.log")
}
func TestMacParser(t *testing.T) {
macParser, err := newParser(logger.Sugar(),
"^(?P<MONTH>[A-Z][a-z]{2})\\s+(?P<DAY>\\d{1,2})\\s(?P<TIME>(?:\\d{2}:){2}\\d{2})\\s(?P<HOST>[^\\s]+)\\s(?P<PROCESS>[^\\[]+)\\[(?P<PID>\\d+)\\]:?(?:\\s\\((?P<PID2>\\d+)\\))?:?\\s(?P<MESSAGE>.*)$",
[]variableMatcher{},
[]variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
},
[]variableMatcher{},
)
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &macParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "Jul 8 08:10:46 calvisitor-10-105-162-124 kernel[0]: AppleCamIn::wakeEventHandlerThread",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"MONTH": "Jul",
"DAY": "8",
"TIME": "08:10:46",
"HOST": "calvisitor-10-105-162-124",
"PROCESS": "kernel",
"PID": "0",
"PID2": "",
"MESSAGE": "AppleCamIn::wakeEventHandlerThread",
},
}
testParser(t, macParser, expectedEntry, 2000, "testlogs/Mac_2k.log")
}
func TestSparkParser(t *testing.T) {
sparkParser, err := newParser(logger.Sugar(),
"^(?P<DATE>\\d{2}\\/\\d{2}\\/\\d{2}) (?P<TIME>\\d{2}:\\d{2}:\\d{2}) (?P<LEVEL>[A-Z]+) (?P<CLASS>[a-zA-Z0-9\\.]+): (?P<MESSAGE>.+)$",
[]variableMatcher{},
[]variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
},
[]variableMatcher{},
)
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &sparkParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "17/06/09 20:11:11 INFO storage.BlockManager: Found block rdd_42_32 locally",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"DATE": "17/06/09",
"TIME": "20:11:11",
"LEVEL": "INFO",
"CLASS": "storage.BlockManager",
"MESSAGE": "Found block rdd_42_32 locally",
},
}
testParser(t, sparkParser, expectedEntry, 2000, "testlogs/Spark_2k.log")
}
func TestWindowsParser(t *testing.T) {
windowsParser, err := newParser(logger.Sugar(),
"^(?P<DATE>\\d{4}-\\d{2}-\\d{2}) (?P<TIME>\\d{2}:\\d{2}:\\d{2}),\\s+(?P<LEVEL>[A-Z][a-z]+)\\s+(?P<CLASS>[A-Za-z]+)\\s+(?P<MESSAGE>.*)$",
[]variableMatcher{},
[]variableMatcher{
{
Variable: "LINENO",
Regex: "2000",
},
},
[]variableMatcher{},
)
require.NoError(t, err)
expectedEntry := logEntry{
Parser: &windowsParser,
Filtered: false,
Triggered: true,
Excluded: false,
Text: "2016-09-29 02:04:40, Info CBS Read out cached package applicability for package: Package_for_KB2928120~31bf3856ad364e35~amd64~~6.1.1.2, ApplicableState: 0, CurrentState:0",
LineNo: 2000,
Variables: map[string]string{
"LINENO": "2000",
"DATE": "2016-09-29",
"TIME": "02:04:40",
"LEVEL": "Info",
"CLASS": "CBS",
"MESSAGE": "Read out cached package applicability for package: Package_for_KB2928120~31bf3856ad364e35~amd64~~6.1.1.2, ApplicableState: 0, CurrentState:0",
},
}
testParser(t, windowsParser, expectedEntry, 2000, "testlogs/Windows_2k.log")
}
func testParser(t *testing.T, mainParser parser, expectedLastEntry logEntry, logLines int, filePath string) {
var wg sync.WaitGroup
// create validation function
handler := func(log *zap.SugaredLogger, fileName, outputDir, apiKey, model string, entryToDiagnose logEntry, logContext []logEntry) error {
defer wg.Done()
require.Equal(t, expectedLastEntry, entryToDiagnose)
require.Equal(t, logLines, len(logContext))
// Verify that all entries in the context were parsed with the first parser
for _, entry := range logContext {
require.Equal(t, mainParser.regex, entry.Parser.regex, "Line (%d) was not parsed correctly", entry.LineNo)
}
return nil
}
// Monitor log (will finish and not tail)
wg.Add(1)
go func() {
monitorLogLoop(logger.Sugar(), filePath, "", "", "", logLines, 999999, []parser{
mainParser,
allLineParser,
}, handler, 100*time.Millisecond, false)
}()
waitWithTimeout(t, &wg, 2*time.Second)
}
func waitWithTimeout(t *testing.T, wg *sync.WaitGroup, timeout time.Duration) {
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
return
case <-time.After(timeout):
t.Errorf("Test timeout after (%s)", timeout)
return
}
}