forked from vjeantet/grok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grok_test.go
721 lines (635 loc) · 23.1 KB
/
grok_test.go
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
package grok
import (
"bufio"
"fmt"
"strings"
"testing"
)
func TestNew(t *testing.T) {
g, _ := New()
if len(g.patterns) == 0 {
t.Fatal("the Grok object should have some patterns pre loaded")
}
g, _ = NewWithConfig(&Config{NamedCapturesOnly: true})
if len(g.patterns) == 0 {
t.Fatal("the Grok object should have some patterns pre loaded")
}
}
func TestParseWithDefaultCaptureMode(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != "" {
t.Fatalf("%s should be '%s' have '%s'", "TIME", "", captures["TIME"])
}
}
g, _ = New()
if captures, err := g.Parse("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != "22:58:32" {
t.Fatalf("%s should be '%s' have '%s'", "TIME", "22:58:32", captures["TIME"])
}
}
}
func TestMultiParseWithDefaultCaptureMode(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
res, _ := g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:23:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["TIME"]) != 0 {
t.Fatalf("DAY should be an array of 0 elements, but is '%s'", res["TIME"])
}
g, _ = New()
res, _ = g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:23:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["TIME"]) != 2 {
t.Fatalf("TIME should be an array of 2 elements, but is '%s'", res["TIME"])
}
if len(res["timestamp"]) != 2 {
t.Fatalf("timestamp should be an array of 2 elements, but is '%s'", res["timestamp"])
}
}
func TestNewWithNoDefaultPatterns(t *testing.T) {
g, _ := NewWithConfig(&Config{SkipDefaultPatterns: true})
if len(g.patterns) != 0 {
t.Fatal("Using SkipDefaultPatterns the Grok object should not have any patterns pre loaded")
}
}
func TestAddPatternErr(t *testing.T) {
name := "Error"
pattern := "%{ERR}"
g, _ := New()
err := g.addPattern(name, pattern)
if err == nil {
t.Fatalf("AddPattern should returns an error when path is invalid")
}
}
func TestAddPatternsFromPathErr(t *testing.T) {
g, _ := New()
err := g.AddPatternsFromPath("./Lorem ipsum Minim qui in.")
if err == nil {
t.Fatalf("AddPatternsFromPath should returns an error when path is invalid")
}
}
func TestConfigPatternsDir(t *testing.T) {
g, err := NewWithConfig(&Config{PatternsDir: []string{"./patterns"}})
if err != nil {
t.Error(err)
}
if captures, err := g.Parse("%{SYSLOGLINE}", `Sep 12 23:19:02 docker syslog-ng[25389]: syslog-ng starting up; version='3.5.3'`); err != nil {
t.Fatalf("error : %s", err.Error())
} else {
// pp.Print(captures)
if captures["program"] != "syslog-ng" {
t.Fatalf("%s should be '%s' have '%s'", "program", "syslog-ng", captures["program"])
}
}
}
func TestAddPatternsFromPathFileOpenErr(t *testing.T) {
t.Skipped()
}
func TestAddPatternsFromPathFile(t *testing.T) {
g, _ := New()
err := g.AddPatternsFromPath("./patterns/grok-patterns")
if err != nil {
t.Fatalf("err %#v", err)
}
}
func TestAddPattern(t *testing.T) {
name := "DAYO"
pattern := "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)"
g, _ := New()
cPatterns := len(g.patterns)
g.AddPattern(name, pattern)
g.AddPattern(name+"2", pattern)
if len(g.patterns) != cPatterns+2 {
t.Fatalf("%d Default patterns should be available, have %d", cPatterns+2, len(g.patterns))
}
g, _ = NewWithConfig(&Config{NamedCapturesOnly: true})
cPatterns = len(g.patterns)
g.AddPattern(name, pattern)
g.AddPattern(name+"2", pattern)
if len(g.patterns) != cPatterns+2 {
t.Fatalf("%d NamedCapture patterns should be available, have %d", cPatterns+2, len(g.patterns))
}
}
func TestMatch(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
if r, err := g.Match("%{MONTH}", "June"); !r {
t.Fatalf("June should match %s: err=%s", "%{MONTH}", err.Error())
}
}
func TestDoesNotMatch(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
if r, _ := g.Match("%{MONTH}", "13"); r {
t.Fatalf("13 should not match %s", "%{MONTH}")
}
}
func TestErrorMatch(t *testing.T) {
g, _ := New()
if _, err := g.Match("(", "13"); err == nil {
t.Fatal("Error expected")
}
}
func TestShortName(t *testing.T) {
g, _ := New()
g.AddPattern("A", "a")
m, err := g.Match("%{A}", "a")
if err != nil {
t.Fatal("a should match %%{A}: err=%s", err.Error())
}
if !m {
t.Fatal("%%{A} didn't match 'a'")
}
}
func TestDayCompile(t *testing.T) {
g, _ := New()
g.AddPattern("DAY", "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)")
pattern := "%{DAY}"
_, err := g.compile(pattern)
if err != nil {
t.Fatal("Error:", err)
}
}
func TestErrorCompile(t *testing.T) {
g, _ := New()
_, err := g.compile("(")
if err == nil {
t.Fatal("Error:", err)
}
}
func TestNamedCaptures(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
captures, _ := g.Parse(pattern, text)
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
check("jour", "Tue",
"%{DAY:jour}",
"Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157",
)
}
func TestErrorCaptureUnknowPattern(t *testing.T) {
g, _ := New()
pattern := "%{UNKNOWPATTERN}"
_, err := g.Parse(pattern, "")
if err == nil {
t.Fatal("Expected error not set")
}
}
func TestParse(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
res, _ := g.Parse("%{DAY}", "Tue qds")
if res["DAY"] != "Tue" {
t.Fatalf("DAY should be 'Tue' have '%s'", res["DAY"])
}
}
func TestErrorParseToMultiMap(t *testing.T) {
g, _ := New()
pattern := "%{UNKNOWPATTERN}"
_, err := g.ParseToMultiMap(pattern, "")
if err == nil {
t.Fatal("Expected error not set")
}
}
func TestParseToMultiMap(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
res, _ := g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:23:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["TIME"]) != 2 {
t.Fatalf("DAY should be an array of 3 elements, but is '%s'", res["TIME"])
}
if res["TIME"][0] != "23:58:32" {
t.Fatalf("TIME[0] should be '23:58:32' have '%s'", res["TIME"][0])
}
if res["TIME"][1] != "22:58:32" {
t.Fatalf("TIME[1] should be '22:58:32' have '%s'", res["TIME"][1])
}
}
func TestParseToMultiMapOnlyNamedCaptures(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPatternsFromPath("./patterns")
res, _ := g.ParseToMultiMap("%{COMMONAPACHELOG} %{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207 127.0.0.1 - - [24/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
if len(res["timestamp"]) != 2 {
t.Fatalf("timestamp should be an array of 2 elements, but is '%s'", res["timestamp"])
}
if res["timestamp"][0] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("timestamp[0] should be '23/Apr/2014:22:58:32 +0200' have '%s'", res["DAY"][0])
}
if res["timestamp"][1] != "24/Apr/2014:22:58:32 +0200" {
t.Fatalf("timestamp[1] should be '24/Apr/2014:22:58:32 +0200' have '%s'", res["DAY"][1])
}
}
func TestCaptureAll(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
check("timestamp", "23/Apr/2014:22:58:32 +0200",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("TIME", "22:58:32",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("SECOND", `17,1599`, "%{TIMESTAMP_ISO8601}", `s d9fq4999s ../ sdf 2013-11-06 04:50:17,1599sd`)
check("HOSTNAME", `google.com`, "%{HOSTPORT}", `google.com:8080`)
//HOSTPORT
check("POSINT", `8080`, "%{HOSTPORT}", `google.com:8080`)
}
func TestNamedCapture(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
check("timestamp", "23/Apr/2014:22:58:32 +0200",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("TIME", "",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("SECOND", ``, "%{TIMESTAMP_ISO8601}", `s d9fq4999s ../ sdf 2013-11-06 04:50:17,1599sd`)
check("HOSTNAME", ``, "%{HOSTPORT}", `google.com:8080`)
//HOSTPORT
check("POSINT", ``, "%{HOSTPORT}", `google.com:8080`)
}
func TestRemoveEmptyValues(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true, RemoveEmptyValues: true})
capturesExists := func(key, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if _, ok := captures[key]; ok {
t.Fatalf("%s should be absent", key)
}
}
}
capturesExists("rawrequest", "%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
}
func TestCapturesAndNamedCapture(t *testing.T) {
check := func(key, value, pattern, text string) {
g, _ := New()
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
checkNamed := func(key, value, pattern, text string) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
check("DAY", "Tue",
"%{DAY}",
"Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157",
)
checkNamed("jour", "Tue",
"%{DAY:jour}",
"Tue May 15 11:21:42 [conn1047685] moveChunk deleted: 7157",
)
check("clientip", "127.0.0.1",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("verb", "GET",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("timestamp", "23/Apr/2014:22:58:32 +0200",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
check("bytes", "207",
"%{COMMONAPACHELOG}",
`127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`,
)
//PATH
check("WINPATH", `c:\winfows\sdf.txt`, "%{WINPATH}", `s dfqs c:\winfows\sdf.txt`)
check("WINPATH", `\\sdf\winfows\sdf.txt`, "%{WINPATH}", `s dfqs \\sdf\winfows\sdf.txt`)
check("UNIXPATH", `/usr/lib/`, "%{UNIXPATH}", `s dfqs /usr/lib/ sqfd`)
check("UNIXPATH", `/usr/lib`, "%{UNIXPATH}", `s dfqs /usr/lib sqfd`)
check("UNIXPATH", `/usr/`, "%{UNIXPATH}", `s dfqs /usr/ sqfd`)
check("UNIXPATH", `/usr`, "%{UNIXPATH}", `s dfqs /usr sqfd`)
check("UNIXPATH", `/`, "%{UNIXPATH}", `s dfqs / sqfd`)
//YEAR
check("YEAR", `4999`, "%{YEAR}", `s d9fq4999s ../ sdf`)
check("YEAR", `79`, "%{YEAR}", `s d79fq4999s ../ sdf`)
check("TIMESTAMP_ISO8601", `2013-11-06 04:50:17,1599`, "%{TIMESTAMP_ISO8601}", `s d9fq4999s ../ sdf 2013-11-06 04:50:17,1599sd`)
//MAC
check("MAC", `01:02:03:04:ab:cf`, "%{MAC}", `s d9fq4999s ../ sdf 2013- 01:02:03:04:ab:cf 11-06 04:50:17,1599sd`)
check("MAC", `01-02-03-04-ab-cd`, "%{MAC}", `s d9fq4999s ../ sdf 2013- 01-02-03-04-ab-cd 11-06 04:50:17,1599sd`)
//QUOTEDSTRING
check("QUOTEDSTRING", `"lkj"`, "%{QUOTEDSTRING}", `qsdklfjqsd fk"lkj"mkj`)
check("QUOTEDSTRING", `'lkj'`, "%{QUOTEDSTRING}", `qsdklfjqsd fk'lkj'mkj`)
check("QUOTEDSTRING", `"fk'lkj'm"`, "%{QUOTEDSTRING}", `qsdklfjqsd "fk'lkj'm"kj`)
check("QUOTEDSTRING", `'fk"lkj"m'`, "%{QUOTEDSTRING}", `qsdklfjqsd 'fk"lkj"m'kj`)
//BASE10NUM
check("BASE10NUM", `1`, "%{BASE10NUM}", `1`) // this is a nice one
check("BASE10NUM", `8080`, "%{BASE10NUM}", `qsfd8080qsfd`)
}
// Should be run with -race
func TestConcurentParse(t *testing.T) {
g, _ := New()
g.AddPatternsFromPath("./patterns")
check := func(key, value, pattern, text string) {
if captures, err := g.Parse(pattern, text); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures[key] != value {
t.Fatalf("%s should be '%s' have '%s'", key, value, captures[key])
}
}
}
go check("QUOTEDSTRING", `"lkj"`, "%{QUOTEDSTRING}", `qsdklfjqsd fk"lkj"mkj`)
go check("QUOTEDSTRING", `'lkj'`, "%{QUOTEDSTRING}", `qsdklfjqsd fk'lkj'mkj`)
go check("QUOTEDSTRING", `'lkj'`, "%{QUOTEDSTRING}", `qsdklfjqsd fk'lkj'mkj`)
go check("QUOTEDSTRING", `"fk'lkj'm"`, "%{QUOTEDSTRING}", `qsdklfjqsd "fk'lkj'm"kj`)
go check("QUOTEDSTRING", `'fk"lkj"m'`, "%{QUOTEDSTRING}", `qsdklfjqsd 'fk"lkj"m'kj`)
}
func TestPatterns(t *testing.T) {
g, _ := NewWithConfig(&Config{SkipDefaultPatterns: true})
if len(g.patterns) != 0 {
t.Fatalf("Patterns should return 0, have '%d'", len(g.patterns))
}
name := "DAY0"
pattern := "(?:Mon(?:day)?|Tue(?:sday)?|Wed(?:nesday)?|Thu(?:rsday)?|Fri(?:day)?|Sat(?:urday)?|Sun(?:day)?)"
g.AddPattern(name, pattern)
g.AddPattern(name+"1", pattern)
if len(g.patterns) != 2 {
t.Fatalf("Patterns should return 2, have '%d'", len(g.patterns))
}
}
func TestParseTypedWithDefaultCaptureMode(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.ParseTyped("%{IPV4:ip:string} %{NUMBER:status:int} %{NUMBER:duration:float}", `127.0.0.1 200 0.8`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["ip"] != "127.0.0.1" {
t.Fatalf("%s should be '%s' have '%s'", "ip", "127.0.0.1", captures["ip"])
} else {
if captures["status"] != 200 {
t.Fatalf("%s should be '%d' have '%d'", "status", 200, captures["status"])
} else {
if captures["duration"] != 0.8 {
t.Fatalf("%s should be '%f' have '%f'", "duration", 0.8, captures["duration"])
}
}
}
}
}
func TestParseTypedWithNoTypeInfo(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.ParseTyped("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != nil {
t.Fatalf("%s should be nil have '%s'", "TIME", captures["TIME"])
}
}
g, _ = New()
if captures, err := g.ParseTyped("%{COMMONAPACHELOG}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["timestamp"] != "23/Apr/2014:22:58:32 +0200" {
t.Fatalf("%s should be '%s' have '%s'", "timestamp", "23/Apr/2014:22:58:32 +0200", captures["timestamp"])
}
if captures["TIME"] != "22:58:32" {
t.Fatalf("%s should be '%s' have '%s'", "TIME", "22:58:32", captures["TIME"])
}
}
}
func TestParseTypedWithIntegerTypeCoercion(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
if captures, err := g.ParseTyped("%{WORD:coerced:int}", `5.75`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["coerced"] != 5 {
t.Fatalf("%s should be '%s' have '%s'", "coerced", "5", captures["coerced"])
}
}
}
func TestParseTypedWithUnknownType(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
if _, err := g.ParseTyped("%{WORD:word:unknown}", `hello`); err == nil {
t.Fatalf("parsing an unknown type must result in a conversion error")
}
}
func TestParseTypedErrorCaptureUnknowPattern(t *testing.T) {
g, _ := New()
pattern := "%{UNKNOWPATTERN}"
_, err := g.ParseTyped(pattern, "")
if err == nil {
t.Fatal("Expected error not set")
}
}
func TestParseTypedWithTypedParents(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
g.AddPattern("TESTCOMMON", `%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes:int}|-)`)
if captures, err := g.ParseTyped("%{TESTCOMMON}", `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["bytes"] != 207 {
t.Fatalf("%s should be '%s' have '%s'", "bytes", "207", captures["bytes"])
}
}
}
func TestParseTypedWithSemanticHomonyms(t *testing.T) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true, SkipDefaultPatterns: true})
g.AddPattern("BASE10NUM", `([+-]?(?:[0-9]+(?:\.[0-9]+)?)|\.[0-9]+)`)
g.AddPattern("NUMBER", `(?:%{BASE10NUM})`)
g.AddPattern("MYNUM", `%{NUMBER:bytes:int}`)
g.AddPattern("MYSTR", `%{NUMBER:bytes:string}`)
if captures, err := g.ParseTyped("%{MYNUM}", `207`); err != nil {
t.Fatalf("error can not scapture : %s", err.Error())
} else {
if captures["bytes"] != 207 {
t.Fatalf("%s should be %#v have %#v", "bytes", 207, captures["bytes"])
}
}
if captures, err := g.ParseTyped("%{MYSTR}", `207`); err != nil {
t.Fatalf("error can not capture : %s", err.Error())
} else {
if captures["bytes"] != "207" {
t.Fatalf("%s should be %#v have %#v", "bytes", "207", captures["bytes"])
}
}
}
var resultNew *Grok
func BenchmarkNew(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var g *Grok
// run the check function b.N times
for n := 0; n < b.N; n++ {
g, _ = NewWithConfig(&Config{NamedCapturesOnly: true})
}
resultNew = g
}
func BenchmarkCaptures(b *testing.B) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
b.ReportAllocs()
b.ResetTimer()
// run the check function b.N times
for n := 0; n < b.N; n++ {
g.Parse(`%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-)`, `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
}
}
func BenchmarkCapturesTypedFake(b *testing.B) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
b.ReportAllocs()
b.ResetTimer()
// run the check function b.N times
for n := 0; n < b.N; n++ {
g.Parse(`%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion})?|%{DATA:rawrequest})" %{NUMBER:response} (?:%{NUMBER:bytes}|-)`, `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
}
}
func BenchmarkCapturesTypedReal(b *testing.B) {
g, _ := NewWithConfig(&Config{NamedCapturesOnly: true})
b.ReportAllocs()
b.ResetTimer()
// run the check function b.N times
for n := 0; n < b.N; n++ {
g.ParseTyped(`%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \[%{HTTPDATE:timestamp}\] "(?:%{WORD:verb} %{NOTSPACE:request}(?: HTTP/%{NUMBER:httpversion:int})?|%{DATA:rawrequest})" %{NUMBER:response:int} (?:%{NUMBER:bytes:int}|-)`, `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207`)
}
}
func TestGrok_AddPatternsFromMap_not_exist(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("AddPatternsFromMap panics: %v", r)
}
}()
g, _ := NewWithConfig(&Config{SkipDefaultPatterns: true})
err := g.AddPatternsFromMap(map[string]string{
"SOME": "%{NOT_EXIST}",
})
if err == nil {
t.Errorf("AddPatternsFromMap should returns an error")
}
}
func TestGrok_AddPatternsFromMap_simple(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("AddPatternsFromMap panics: %v", r)
}
}()
g, _ := NewWithConfig(&Config{SkipDefaultPatterns: true})
err := g.AddPatternsFromMap(map[string]string{
"NO3": `\d{3}`,
})
if err != nil {
t.Errorf("AddPatternsFromMap returns an error: %v", err)
}
mss, err := g.Parse("%{NO3:match}", "333")
if err != nil {
t.Error("parsing error:", err)
t.FailNow()
}
if mss["match"] != "333" {
t.Errorf("bad match: expected 333, got %s", mss["match"])
}
}
func TestGrok_AddPatternsFromMap_complex(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("AddPatternsFromMap panics: %v", r)
}
}()
g, _ := NewWithConfig(&Config{
SkipDefaultPatterns: true,
NamedCapturesOnly: true,
})
err := g.AddPatternsFromMap(map[string]string{
"NO3": `\d{3}`,
"NO6": "%{NO3}%{NO3}",
})
if err != nil {
t.Errorf("AddPatternsFromMap returns an error: %v", err)
}
mss, err := g.Parse("%{NO6:number}", "333666")
if err != nil {
t.Error("parsing error:", err)
t.FailNow()
}
if mss["number"] != "333666" {
t.Errorf("bad match: expected 333666, got %s", mss["match"])
}
}
func TestParseStream(t *testing.T) {
g, _ := New()
pTest := func(m map[string]string) error {
ts, ok := m["timestamp"]
if !ok {
t.Error("timestamp not found")
}
if len(ts) == 0 {
t.Error("empty timestamp")
}
return nil
}
const testLog = `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [23/Apr/2014:22:59:32 +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [23/Apr/2014:23:00:32 +0200] "GET /index.php HTTP/1.1" 404 207
`
r := bufio.NewReader(strings.NewReader(testLog))
if err := g.ParseStream(r, "%{COMMONAPACHELOG}", pTest); err != nil {
t.Fatal(err)
}
}
func TestParseStreamError(t *testing.T) {
g, _ := New()
pTest := func(m map[string]string) error {
if _, ok := m["timestamp"]; !ok {
return fmt.Errorf("timestamp not found")
}
return nil
}
const testLog = `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [xxxxxxxxxxxxxxxxxxxx +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [23/Apr/2014:23:00:32 +0200] "GET /index.php HTTP/1.1" 404 207
`
r := bufio.NewReader(strings.NewReader(testLog))
if err := g.ParseStream(r, "%{COMMONAPACHELOG}", pTest); err == nil {
t.Fatal("Error expected")
}
}