瀏覽代碼

add more tests for pkg/parser (config loading) (#172)

Thibault "bui" Koechlin 5 年之前
父節點
當前提交
8128dcf61b
共有 2 個文件被更改,包括 66 次插入4 次删除
  1. 4 4
      pkg/parser/node.go
  2. 62 0
      pkg/parser/node_test.go

+ 4 - 4
pkg/parser/node.go

@@ -404,10 +404,10 @@ func (n *Node) compile(pctx *UnixParserCtx) error {
 		n.logger.Debugf("+ Regexp Compilation '%s'", n.Grok.RegexpName)
 		n.Grok.RunTimeRegexp, err = pctx.Grok.Get(n.Grok.RegexpName)
 		if err != nil {
-			n.logger.Fatalf("Unable to find grok '%s' : %v\n", n.Grok.RegexpName, err)
+			return fmt.Errorf("Unable to find grok '%s' : %v", n.Grok.RegexpName, err)
 		}
 		if n.Grok.RunTimeRegexp == nil {
-			n.logger.Fatalf("Didn't find regexp : %s", n.Grok.RegexpName)
+			return fmt.Errorf("Empty grok '%s'", n.Grok.RegexpName)
 		}
 		n.logger.Debugf("%s regexp: %s", n.Grok.RegexpName, n.Grok.RunTimeRegexp.Regexp.String())
 		valid = true
@@ -415,11 +415,11 @@ func (n *Node) compile(pctx *UnixParserCtx) error {
 		//n.logger.Debugf("+ Regexp Compilation '%s'", n.Grok.RegexpValue)
 		n.Grok.RunTimeRegexp, err = pctx.Grok.Compile(n.Grok.RegexpValue)
 		if err != nil {
-			n.logger.Fatalf("Failed to compile grok '%s': %v\n", n.Grok.RegexpValue, err)
+			return fmt.Errorf("Failed to compile grok '%s': %v\n", n.Grok.RegexpValue, err)
 		}
 		if n.Grok.RunTimeRegexp == nil {
 			// We shouldn't be here because compilation succeeded, so regexp shouldn't be nil
-			n.logger.Fatalf("Grok compilation failure: %s", n.Grok.RegexpValue)
+			return fmt.Errorf("Grok compilation failure: %s", n.Grok.RegexpValue)
 		}
 		n.logger.Debugf("%s regexp : %s", n.Grok.RegexpValue, n.Grok.RunTimeRegexp.Regexp.String())
 		valid = true

+ 62 - 0
pkg/parser/node_test.go

@@ -0,0 +1,62 @@
+package parser
+
+import (
+	"testing"
+
+	"github.com/crowdsecurity/crowdsec/pkg/types"
+)
+
+func TestParserConfigs(t *testing.T) {
+	var p UnixParser
+	pctx, err := p.Init(map[string]interface{}{"patterns": "../../config/patterns/", "data": "./tests/"})
+	if err != nil {
+		t.Fatalf("unable to load patterns : %s", err)
+	}
+
+	/*the actual tests*/
+	var CfgTests = []struct {
+		NodeCfg  *Node
+		Compiles bool
+		Valid    bool
+	}{
+		//valid node with grok pattern
+		{&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
+		//bad filter
+		{&Node{Debug: true, Stage: "s00", Filter: "ratata"}, false, false},
+		//empty node
+		{&Node{Debug: true, Stage: "s00", Filter: "true"}, false, false},
+		//bad subgrok
+		{&Node{Debug: true, Stage: "s00", SubGroks: map[string]string{"FOOBAR": "[a-$"}}, false, true},
+		//valid node with grok pattern
+		{&Node{Debug: true, Stage: "s00", SubGroks: map[string]string{"FOOBAR": "[a-z]"}, Grok: types.GrokPattern{RegexpValue: "^x%{FOOBAR:extr}$", TargetField: "t"}}, true, true},
+		//bad node success
+		{&Node{Debug: true, Stage: "s00", OnSuccess: "ratat", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, false, false},
+		//ok node success
+		{&Node{Debug: true, Stage: "s00", OnSuccess: "continue", Grok: types.GrokPattern{RegexpValue: "^x%{DATA:extr}$", TargetField: "t"}}, true, true},
+		//valid node with grok sub-pattern used by name
+		{&Node{Debug: true, Stage: "s00", SubGroks: map[string]string{"FOOBARx": "[a-z] %{DATA:lol}$"}, Grok: types.GrokPattern{RegexpName: "FOOBARx", TargetField: "t"}}, true, true},
+		//node with unexisting grok pattern
+		{&Node{Debug: true, Stage: "s00", Grok: types.GrokPattern{RegexpName: "RATATA", TargetField: "t"}}, false, true},
+
+		//bad grok pattern
+		//{&Node{Debug: true, Grok: []GrokPattern{ GrokPattern{}, }}, false},
+	}
+	for idx := range CfgTests {
+		err := CfgTests[idx].NodeCfg.compile(pctx)
+		if CfgTests[idx].Compiles == true && err != nil {
+			t.Fatalf("Compile: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err)
+		}
+		if CfgTests[idx].Compiles == false && err == nil {
+			t.Fatalf("Compile: (%d/%d) expected errror", idx+1, len(CfgTests))
+		}
+
+		err = CfgTests[idx].NodeCfg.validate(pctx)
+		if CfgTests[idx].Valid == true && err != nil {
+			t.Fatalf("Valid: (%d/%d) expected valid, got : %s", idx+1, len(CfgTests), err)
+		}
+		if CfgTests[idx].Valid == false && err == nil {
+			t.Fatalf("Valid: (%d/%d) expected error", idx+1, len(CfgTests))
+		}
+
+	}
+}