node_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package parser
  2. import (
  3. "testing"
  4. yaml "gopkg.in/yaml.v2"
  5. )
  6. func TestParserConfigs(t *testing.T) {
  7. pctx, err := Init(map[string]interface{}{"patterns": "../../config/patterns/", "data": "./tests/"})
  8. if err != nil {
  9. t.Fatalf("unable to load patterns : %s", err)
  10. }
  11. /*the actual tests*/
  12. var CfgTests = []struct {
  13. NodeCfg *Node
  14. Compiles bool
  15. Valid bool
  16. }{
  17. //valid node with grok pattern
  18. {&Node{Debug: true, Stage: "s00", Grok: GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
  19. //bad filter
  20. {&Node{Debug: true, Stage: "s00", Filter: "ratata"}, false, false},
  21. //empty node
  22. {&Node{Debug: true, Stage: "s00", Filter: "true"}, false, false},
  23. //bad subgrok
  24. {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-$")}}}, false, true},
  25. //valid node with grok pattern
  26. {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBAR"), Value: string("[a-z]")}}, Grok: GrokPattern{RegexpValue: "^x%{FOOBAR:extr}$", TargetField: "t"}}, true, true},
  27. //bad node success
  28. {&Node{Debug: true, Stage: "s00", OnSuccess: "ratat", Grok: GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, false, false},
  29. //ok node success
  30. {&Node{Debug: true, Stage: "s00", OnSuccess: "continue", Grok: GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
  31. //valid node with grok sub-pattern used by name
  32. {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{{Key: string("FOOBARx"), Value: string("[a-z] %{DATA:lol}$")}}, Grok: GrokPattern{RegexpName: "FOOBARx", TargetField: "t"}}, true, true},
  33. //node with unexisting grok pattern
  34. {&Node{Debug: true, Stage: "s00", Grok: GrokPattern{RegexpName: "RATATA", TargetField: "t"}}, false, true},
  35. //node with grok pattern dependencies
  36. {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
  37. {Key: string("SUBGROK"), Value: string("[a-z]")},
  38. {Key: string("MYGROK"), Value: string("[a-z]%{SUBGROK}")},
  39. }, Grok: GrokPattern{RegexpValue: "^x%{MYGROK:extr}$", TargetField: "t"}}, true, true},
  40. //node with broken grok pattern dependencies
  41. {&Node{Debug: true, Stage: "s00", SubGroks: yaml.MapSlice{
  42. {Key: string("SUBGROKBIS"), Value: string("[a-z]%{MYGROKBIS}")},
  43. {Key: string("MYGROKBIS"), Value: string("[a-z]")},
  44. }, Grok: GrokPattern{RegexpValue: "^x%{MYGROKBIS:extr}$", TargetField: "t"}}, false, true},
  45. }
  46. for idx := range CfgTests {
  47. err := CfgTests[idx].NodeCfg.compile(pctx, EnricherCtx{})
  48. if CfgTests[idx].Compiles == true && err != nil {
  49. t.Fatalf("Compile: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err)
  50. }
  51. if CfgTests[idx].Compiles == false && err == nil {
  52. t.Fatalf("Compile: (%d/%d) expected error", idx+1, len(CfgTests))
  53. }
  54. err = CfgTests[idx].NodeCfg.validate(pctx, EnricherCtx{})
  55. if CfgTests[idx].Valid == true && err != nil {
  56. t.Fatalf("Valid: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err)
  57. }
  58. if CfgTests[idx].Valid == false && err == nil {
  59. t.Fatalf("Valid: (%d/%d) expected error", idx+1, len(CfgTests))
  60. }
  61. }
  62. }