github.com/crowdsecurity/crowdsec@v1.6.1/pkg/parser/node_test.go (about)

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