-
Notifications
You must be signed in to change notification settings - Fork 10
/
logstash_config_test.go
997 lines (925 loc) · 17 KB
/
logstash_config_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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
package config_test
import (
"fmt"
"io/ioutil"
"log"
"strings"
"testing"
. "github.com/breml/logstash-config"
"github.com/breml/logstash-config/ast"
)
func ExampleParseReader() {
logstashConfig := `filter {
mutate {
add_tag => [
"tag"
]
}
}`
got, err := ParseReader("example.conf", strings.NewReader(logstashConfig))
if err != nil {
log.Fatalf("Parse error: %s\n", err)
}
// Output: filter {
// mutate {
// add_tag => [
// "tag"
// ]
// }
//}
fmt.Println(got)
}
func TestParserIdentic(t *testing.T) {
cases := []struct {
name string
input string
}{
{
name: "Empty file",
input: ``,
},
{
name: "Single PluginSection",
input: `input {}
`,
},
{
name: "All PluginSections empty",
input: `input {}
filter {}
output {}
`,
},
{
name: "Plugin without attributes",
input: `input {
stdin {}
}
`,
},
{
name: "Multiple plugins",
input: `input {
stdin {}
file {}
}
filter {
mutate {}
mutate {}
mutate {}
}
output {
stdout {}
}
`,
},
{
name: "Plugin with all attribute types",
input: ``,
},
{
name: "Simple if (without else) branch",
input: `filter {
if 1 == 1 {
date {}
}
}
`,
},
{
name: "if with else-if and a final else branch",
input: `filter {
if 1 == 1 {
date {}
} else if 1 == 1 {
date {}
} else {
date {}
}
}
`,
},
{
name: "if with multiple else-if and a final else branch, multiple plugins in each branch",
input: `filter {
if 1 == 1 {
date {}
date {}
} else if 1 == 1 {
date {}
date {}
date {}
} else if 1 == 1 {
date {}
date {}
date {}
} else {
date {}
date {}
date {}
}
}
`,
},
{
name: "if with multiple else-if and a final else branch, test for different condition types",
input: `filter {
if 1 != 1 {
date {}
} else if 1 <= 1 {
date {}
} else if 1 >= 1 {
date {}
} else if 1 < 1 {
date {}
} else if 1 > 1 {
date {}
}
}
`,
},
{
name: "if with multiple compare operators",
input: `filter {
if "true" == "true" and "true1" == "true1" or "true2" == "true2" nand "true3" == "true3" xor "true4" == "true4" {
plugin {}
}
}
`,
},
{
name: "Condition in parentheses",
input: `filter {
if ("tag" in [tags]) {
plugin {}
}
}
`,
},
{
name: "Multiple conditions in parentheses",
input: `filter {
if ("tag" in [tags] or ("true" == "true" and 1 == 1)) {
plugin {}
}
}
`,
},
{
name: "Negative condition",
input: `filter {
if !("true" == "true") {
plugin {}
}
}
`,
},
{
name: "Negative Selector for value in subfield",
input: `filter {
if ![field][subfield] {
plugin {}
}
}
`,
},
{
name: "InExpression",
input: `filter {
if "tag" in [tags] {
plugin {}
}
}
`,
},
{
name: "NotInExpression",
input: `filter {
if "tag" not in [field][subfield] {
plugin {}
}
}
`,
},
{
name: "RegexpExpression (Match)",
input: `filter {
if [field] =~ /.*/ {
plugin {}
}
}
`,
},
{
name: "RegexpExpression (Not Match)",
input: `filter {
if [field] !~ /.*/ {
plugin {}
}
}
`,
},
{
name: "Rvalue",
input: `filter {
if "string" or 10 or [field][subfield] or /.*/ {
plugin {}
}
}
`,
},
{
name: "Empty array",
input: `filter {
plugin {
value => []
}
}
`,
},
{
name: "Multiple filter sections",
input: `filter {}
filter {}
filter {}
`,
},
// Comments
{
name: "plugin section comment",
input: `# Comment
filter {}
`,
},
{
name: "file header and plugin section comment",
input: `# Comment
# Comment 2
# Comment 3
filter {}
`,
},
{
name: "plugin section comment with whitespace after",
input: `# Comment
filter {}
`,
},
{
name: "file footer comment",
input: `filter {}
output {}
# Comment after
`,
},
{
name: "foobar",
input: `input {
stdin {
# Comment
codec => rubydebug {
# Comment
string => "a string"
# Comment
}
# Comment
}
}
`,
},
{
name: "Empty array with comment",
input: `input {
stdin {
arrayvalue => [
# Comment
]
}
}
`,
},
{
name: "Empty hash with comment",
input: `input {
stdin {
hashvalue => {
# Comment
}
}
}
`,
},
{
name: "comment only if, else-if and else",
input: `filter {
if 1 == 1 {
# Comment
} else if 1 == 1 {
# Comment
} else {
# Comment
}
}
`,
},
// https://github.com/magnusbaeck/logstash-filter-verifier/issues/104
{
name: "large numbers with and without precission",
input: `filter {
mutate {
add_field => {
"largeint" => 2000000
"largeint_negative" => -2000000
"int32max" => 2147483647
"int32max_negative" => -2147483647
"float_with_5_precision" => 0.00001
"float_with_5_precision_negative" => -0.00001
"float_with_10_precision" => 0.0000000001
"float_with_10_precision_negative" => -0.0000000001
"largefloat_with_10_precision" => 2000000.0000000005
"largefloat_with_10_precision_negative" => -2000000.0000000005
"int32max_with_10_precision" => 2147483647.0000009537
"int32max_with_10_precision_negative" => -2147483647.0000009537
}
}
}
`,
},
{
name: "multiline string",
input: `filter {
mutate {
add_field => {
"largeint" => "a string
with multiple
lines"
}
}
}
`,
},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
got1, err := ParseReader("test", strings.NewReader(test.input))
if err != nil {
t.Fatalf("Expected to parse without error: %s, input:\n%s", err, test.input)
}
got := fmt.Sprintf("%v", got1)
if test.input != got {
t.Errorf("Expected parsed input to print the same as input:\n%s", printDiff(test.input, got))
}
})
}
}
func TestParserIdenticFile(t *testing.T) {
cases := []string{
"plugin_with_all_attribute_types",
"plugin_with_all_attribute_types_with_comments",
"if_else-if_and_else_branch_with_comments",
}
for _, test := range cases {
t.Run(test, func(t *testing.T) {
inputFilename := "testdata/identic/" + test + ".conf"
got1, err := ParseFile(inputFilename)
if err != nil {
t.Fatalf("Expected %q to parse without error: %v", test, err)
}
expected, err := ioutil.ReadFile(inputFilename)
if err != nil {
t.Fatalf("Unable to read file %q: %v", inputFilename, err)
}
got := fmt.Sprintf("%v", got1)
if string(expected) != got {
t.Errorf("Expected %q parsed input to print the same as input:\n%s", inputFilename, printDiff(string(expected), got))
}
})
}
}
func TestParser(t *testing.T) {
cases := []struct {
name string
input string
expected string
}{
{
name: "Whitespace, tab and newlines only",
input: `
`,
expected: ``,
},
{
name: "Single comment (one line without newline)",
input: `# comment only`,
expected: ``,
},
{
name: "Comment surrounded by empty lines",
input: `
# comment only
`,
expected: ``,
},
{
name: "Reformat plugins",
input: `input { stdin {} }`,
expected: `input {
stdin {}
}
`,
},
{
name: "Comments and whitespace",
input: `input {
# Comment
stdin {
# Comment
}
}`,
expected: `input {
# Comment
stdin {
# Comment
}
}
`,
},
{
name: "Multiple filter sections without empty lines",
input: `filter {}
filter {}
filter {}
`,
expected: `filter {}
filter {}
filter {}
`,
},
// Comment
{
name: "plugin section comment with whitespace before",
input: `
# Comment
filter {}
`,
expected: `# Comment
filter {}
`,
},
{
name: "plugin section comment with whitespace before and after",
input: `
# Comment
filter {}
`,
expected: `# Comment
filter {}
`,
},
{
name: "file header, footer and plugin section comments with whitespace before and after",
input: `# Pre Filter comment
# Filter comment
filter {}
# Input comment
input {}
# File footer comment
`,
expected: `# Input comment
input {}
# Pre Filter comment
# Filter comment
filter {}
# File footer comment
`,
},
{
name: "file footer comment without spaceBefore",
input: `filter {}
# Comment after
`,
expected: `filter {}
# Comment after
`,
},
{
name: "pluginSection footer comment with and without spaceBefore",
input: `filter {
plugin {}
# pluginSection footer comment
}
filter {
plugin {}
# pluginSection footer comment
}
`,
expected: `filter {
plugin {}
# pluginSection footer comment
}
filter {
plugin {}
# pluginSection footer comment
}
`,
},
{
name: "pluginSection footer comment empty block with and without spaceBefore",
input: `filter {
# pluginSection footer comment
}
filter {
# pluginSection footer comment
}
`,
expected: `filter {
# pluginSection footer comment
}
filter {
# pluginSection footer comment
}
`,
},
{
name: "plugins without comments",
input: `filter {
plugin {}
otherplugin {}
thirdplugin {}
}
`,
expected: `filter {
plugin {}
otherplugin {}
thirdplugin {}
}
`,
},
{
name: "plugins with comments",
input: `filter {
# plugin comment
plugin {}
# otherplugin comment
otherplugin {}
# third plugin
thirdplugin {}
}
`,
expected: `filter {
# plugin comment
plugin {}
# otherplugin comment
otherplugin {}
# third plugin
thirdplugin {}
}
`,
},
{
name: "plugin with multiple comments",
input: `filter {
# additional comment
# plugin comment
# multiple lines
plugin {}
# footer comment
}
`,
expected: `filter {
# additional comment
# plugin comment
# multiple lines
plugin {}
# footer comment
}
`,
},
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
got1, err := ParseReader("test", strings.NewReader(test.input))
if err != nil {
t.Fatalf("Expected to parse without error: %s, input:\n|%s|", err, test.input)
}
got := fmt.Sprintf("%v", got1)
if test.expected != got {
t.Errorf("Expected output does not match parsed output:\n%s", printDiff(test.expected, got))
}
})
}
}
func TestParserFile(t *testing.T) {
cases := []string{
"comments_everywhere",
}
for _, test := range cases {
t.Run(test, func(t *testing.T) {
inputFilename := "testdata/parser/" + test + ".conf"
expectedFilename := "testdata/parser/" + test + ".expected.conf"
got1, err := ParseFile(inputFilename)
if err != nil {
t.Fatalf("Expected %q to parse without error: %v", test, err)
}
expected, err := ioutil.ReadFile(expectedFilename)
if err != nil {
t.Fatalf("Unable to read file %q: %v", expectedFilename, err)
}
got := fmt.Sprintf("%v", got1)
if string(expected) != got {
fmt.Println(got)
t.Errorf("Expected %q parsed input to print the same as input:\n%s", inputFilename, printDiff(string(expected), got))
}
})
}
}
func TestParseErrors(t *testing.T) {
cases := []struct {
name string
input string
expectedError string
}{
{
name: "misspelled plugin section",
input: `FILTER {
if 1 == 1 {
plugin{}
}
}`,
expectedError: `expect plugin type`,
},
{
name: "missing closing curly bracket (pluginsection)",
input: `filter {
if 1 == 1 {
plugin{}
}
`,
expectedError: `expect closing curly bracket`,
},
{
name: "missing closing curly bracket (plugin)",
input: `filter {
plugin {}
plugin2 {
plugin3 {}
}`,
expectedError: `expect closing curly bracket`,
},
{
name: "invalid value",
input: `filter {
plugin {
value => #invalid#
}
}`,
expectedError: `expect closing curly bracket`,
},
{
name: "invalid array value",
input: `filter {
plugin {
value => [ #invalid# ]
}
}`,
// Upstream grammar from Logstash is wrong: https://github.com/elastic/logstash/issues/6580
// expectedError: `invalid array value`,
expectedError: `expect closing square bracket`,
},
{
name: "missing closing double quotes",
input: `filter {
plugin {
value => "invalid
}
}`,
expectedError: `expect closing double quotes (")`,
},
{
name: "missing closing single quotes",
input: `filter {
plugin {
value => 'invalid
}
}`,
expectedError: `expect closing single quote (')`,
},
{
name: "missing closing double slash for regexp",
input: `filter {
if [field] =~ /.*
plugin {}
}
}`,
expectedError: `expect closing slash (/) for regexp`,
},
{
name: "missing closing squre bracket",
input: `filter {
plugin {
value => [ "bar"
}
}`,
expectedError: `expect closing square bracket`,
},
{
name: "missing closing curly bracket (value)",
input: `filter {
plugin {
value => { "bar" => "bar"
}
}`,
expectedError: `expect closing curly bracket`,
},
{
name: "missing closing curly bracket (if)",
input: `filter {
if 1 == 1 {
plugin {}
else {
plugin2 {}
}
}`,
expectedError: `expect closing curly bracket`,
},
{
name: "missing closing curly bracket (else)",
input: `filter {
if 1 == 1 {
plugin {}
} else {
plugin2 {}
}`,
expectedError: `expect closing curly bracket`,
},
{
name: "missing closing curly bracket (else if)",
input: `filter {
if 1 == 1 {
plugin {}
} else if 2 == 2 {
plugin2 {}
else {
plugin3 {}
}
}`,
expectedError: `expect closing curly bracket`,
},
// {
// name: "not valid if condition"
// input: `filter {
// if this is not valid {
// plugin {}
// }
// }`,
// expectedError: `xxxxxxxxxxxxx`,
// },
{
name: "missing closing parenthesis",
input: `filter {
if !( 1 == 1 {
plugin {}
}
}`,
expectedError: `expect closing parenthesis`,
},
{
name: "invalid value for expression",
input: `filter {
if [test] == #test# {
plugin {}
}
}`,
expectedError: ` invalid value for expression`,
},
{
name: "invalid boolean operator",
input: `filter {
if 1 == 1 nor 2 == 2 {
plugin{}
}
}`,
expectedError: `expect boolean operator`,
},
{
name: "invalid comparison operator",
input: `filter {
if [field] ?~ /.*/ {
plugin{}
}
}`,
expectedError: `expect regexp comparison operator`,
},
{
name: "invalid compare operator",
input: `filter {
if "test" =! "test" {
plugin{}
}
}`,
expectedError: `expect compare operator`,
},
{
name: "invalid in operator",
input: `filter {
if "test" on [field] {
plugin{}
}
}`,
expectedError: `expect in operator`,
},
{
name: "invalid not in operator",
input: `filter {
if "test" no in [field] {
plugin{}
}
}`,
expectedError: `expect not in operator`,
},
{
name: "missing closing square bracket",
input: `filter {
if "test" in [field][subfield {
plugin{}
}
}`,
expectedError: `expect closing square bracket`,
},
}
var errMsg string
var hasErr bool
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
_, err := ParseReader("parse errors", strings.NewReader(test.input))
if err == nil {
t.Errorf("Expected parsing to fail with error: %s, input: %s", test.expectedError, test.input)
} else {
if errMsg, hasErr = GetFarthestFailure(); !hasErr {
errMsg = err.Error()
}
if !strings.Contains(errMsg, test.expectedError) {
t.Errorf("Expected parsing to fail with error containing: %s, got error: %s, input: %s", test.expectedError, errMsg, test.input)
}
}
})
}
}
func TestParseExceptionalComments(t *testing.T) {
cases := []string{
"comments_everywhere",
}
for _, test := range cases {
t.Run(test, func(t *testing.T) {
inputFilename := "testdata/exceptional_comments/" + test + ".conf"
got, err := ParseFile(
inputFilename,
ExceptionalCommentsWarning(true),
)
if err != nil {
t.Fatalf("Expected %q to parse without error: %v", test, err)
}
config, ok := got.(ast.Config)
if !ok {
t.Fatalf("Expected to parse to Config")
}
body, err := ioutil.ReadFile(inputFilename)
if err != nil {
t.Fatalf("Unable to read file %q: %v", inputFilename, err)
}
exceptionalCommentCount := strings.Count(string(body), "exceptional_comment")
if exceptionalCommentCount != len(config.Warnings) {
for _, line := range config.Warnings {
t.Log("line", line)
}
t.Fatalf("Expected %d warnings, got %d", exceptionalCommentCount, len(config.Warnings))
}
})
}
}
func TestParseIgnoreComments(t *testing.T) {
cases := []string{
"comments_everywhere",
}
for _, test := range cases {
t.Run(test, func(t *testing.T) {
inputFilename := "testdata/ignore_comments/" + test + ".conf"
expectedFilename := "testdata/ignore_comments/" + test + ".expected.conf"
got1, err := ParseFile(
inputFilename,
IgnoreComments(true),
)
if err != nil {
t.Fatalf("Expected %q to parse without error: %v", test, err)
}
expected, err := ioutil.ReadFile(expectedFilename)
if err != nil {
t.Fatalf("Unable to read file %q: %v", expectedFilename, err)
}
got := fmt.Sprintf("%v", got1)
if string(expected) != got {
t.Errorf("Expected %q parsed input to print the same as input:\n%s", inputFilename, printDiff(string(expected), got))
}
})
}
}