Преглед изворни кода

add extra tests for exprlib visitors (#187)

* add extra tests for exprlib visitors
Thibault "bui" Koechlin пре 5 година
родитељ
комит
1398a74c6d
2 измењених фајлова са 102 додато и 2 уклоњено
  1. 1 1
      .github/workflows/ci_golangci-lint.yml
  2. 101 1
      pkg/exprhelpers/exprlib_test.go

+ 1 - 1
.github/workflows/ci_golangci-lint.yml

@@ -18,7 +18,7 @@ jobs:
           # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
           version: v1.26
           # Optional: golangci-lint command line arguments.
-          args: --issues-exit-code=0
+          args: --issues-exit-code=0 --timeout 5m
           only-new-issues: true
 
 

+ 101 - 1
pkg/exprhelpers/exprlib_test.go

@@ -1,7 +1,10 @@
 package exprhelpers
 
 import (
-	"log"
+	"fmt"
+
+	log "github.com/sirupsen/logrus"
+
 	"testing"
 
 	"github.com/antonmedv/expr"
@@ -13,6 +16,93 @@ var (
 	TestFolder = "tests"
 )
 
+func TestVisitor(t *testing.T) {
+	if err := Init(); err != nil {
+		log.Fatalf(err.Error())
+	}
+
+	tests := []struct {
+		name   string
+		filter string
+		result bool
+		env    map[string]interface{}
+		err    error
+	}{
+		{
+			name:   "debug : no variable",
+			filter: "'crowdsec' startsWith 'crowdse'",
+			result: true,
+			err:    nil,
+			env:    map[string]interface{}{},
+		},
+		{
+			name:   "debug : simple variable",
+			filter: "'crowdsec' startsWith static_one && 1 == 1",
+			result: true,
+			err:    nil,
+			env:    map[string]interface{}{"static_one": string("crowdse")},
+		},
+		{
+			name:   "debug : simple variable re-used",
+			filter: "static_one.foo == 'bar' && static_one.foo != 'toto'",
+			result: true,
+			err:    nil,
+			env:    map[string]interface{}{"static_one": map[string]string{"foo": "bar"}},
+		},
+		{
+			name:   "debug : can't compile",
+			filter: "static_one.foo.toto == 'lol'",
+			result: false,
+			err:    fmt.Errorf("bad syntax"),
+			env:    map[string]interface{}{"static_one": map[string]string{"foo": "bar"}},
+		},
+		{
+			name:   "debug : can't compile #2",
+			filter: "static_one.f!oo.to/to == 'lol'",
+			result: false,
+			err:    fmt.Errorf("bad syntax"),
+			env:    map[string]interface{}{"static_one": map[string]string{"foo": "bar"}},
+		},
+		{
+			name:   "debug : can't compile #3",
+			filter: "",
+			result: false,
+			err:    fmt.Errorf("bad syntax"),
+			env:    map[string]interface{}{"static_one": map[string]string{"foo": "bar"}},
+		},
+	}
+
+	log.SetLevel(log.DebugLevel)
+	clog := log.WithFields(log.Fields{
+		"type": "test",
+	})
+
+	for _, test := range tests {
+		compiledFilter, err := expr.Compile(test.filter, expr.Env(GetExprEnv(test.env)))
+		if err != nil && test.err == nil {
+			log.Fatalf("compile: %s", err.Error())
+		}
+		debugFilter, err := NewDebugger(test.filter, expr.Env(GetExprEnv(test.env)))
+		if err != nil && test.err == nil {
+			log.Fatalf("debug: %s", err.Error())
+		}
+
+		if compiledFilter != nil {
+			result, err := expr.Run(compiledFilter, GetExprEnv(test.env))
+			if err != nil && test.err == nil {
+				log.Fatalf("run : %s", err.Error())
+			}
+			if isOk := assert.Equal(t, test.result, result); !isOk {
+				t.Fatalf("test '%s' : NOK", test.filter)
+			}
+		}
+
+		if debugFilter != nil {
+			debugFilter.Run(clog, test.result, GetExprEnv(test.env))
+		}
+	}
+}
+
 func TestRegexpInFile(t *testing.T) {
 	if err := Init(); err != nil {
 		log.Fatalf(err.Error())
@@ -192,6 +282,16 @@ func TestAtof(t *testing.T) {
 	}
 
 	log.Printf("test 'Atof()' : OK")
+
+	//bad float
+	testFloat = "1aaa.5"
+	expectedFloat = 0.0
+
+	if Atof(testFloat) != expectedFloat {
+		t.Fatalf("Atof should returned a negative value (error) as a float got")
+	}
+
+	log.Printf("test 'Atof()' : OK")
 }
 
 func TestUpper(t *testing.T) {