From 1398a74c6d788370c1c03a900d2860b6e8a22a00 Mon Sep 17 00:00:00 2001 From: "Thibault \"bui\" Koechlin" Date: Thu, 20 Aug 2020 11:53:47 +0200 Subject: [PATCH] add extra tests for exprlib visitors (#187) * add extra tests for exprlib visitors --- .github/workflows/ci_golangci-lint.yml | 2 +- pkg/exprhelpers/exprlib_test.go | 102 ++++++++++++++++++++++++- 2 files changed, 102 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci_golangci-lint.yml b/.github/workflows/ci_golangci-lint.yml index 1841d8c1b..b0aad2e38 100644 --- a/.github/workflows/ci_golangci-lint.yml +++ b/.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 diff --git a/pkg/exprhelpers/exprlib_test.go b/pkg/exprhelpers/exprlib_test.go index 32a288673..745ab7475 100644 --- a/pkg/exprhelpers/exprlib_test.go +++ b/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) {