Skip to content

Commit

Permalink
tests: log.Fatal -> return err
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Jun 5, 2024
1 parent 2865b69 commit 757e5d3
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions pkg/acquisition/modules/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func TestStreamingAcquisition(t *testing.T) {
Topic: "crowdsecplaintext",
})
if w == nil {
log.Fatalf("Unable to setup a kafka producer")
t.Fatal("Unable to setup a kafka producer")
}

for _, ts := range tests {
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestStreamingAcquisitionWithSSL(t *testing.T) {
Topic: "crowdsecssl",
})
if w2 == nil {
log.Fatalf("Unable to setup a kafka producer")
t.Fatal("Unable to setup a kafka producer")
}

for _, ts := range tests {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cwhub/cwhub_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cwhub

import (
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -107,7 +108,7 @@ func (t *mockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// FAKE PARSER
resp, ok := responseByPath[req.URL.Path]
if !ok {
log.Fatalf("unexpected url :/ %s", req.URL.Path)
return nil, fmt.Errorf("unexpected url: %s", req.URL.Path)
}

response.Body = io.NopCloser(strings.NewReader(resp))
Expand Down
28 changes: 14 additions & 14 deletions pkg/exprhelpers/exprlib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ func TestVisitor(t *testing.T) {
for _, test := range tests {
compiledFilter, err := expr.Compile(test.filter, GetExprOptions(test.env)...)
if err != nil && test.err == nil {
log.Fatalf("compile: %s", err)
t.Fatalf("compile: %s", err)
}

if compiledFilter != nil {
result, err := expr.Run(compiledFilter, test.env)
if err != nil && test.err == nil {
log.Fatalf("run : %s", err)
t.Fatalf("run: %s", err)
}

if isOk := assert.Equal(t, test.result, result); !isOk {
Expand Down Expand Up @@ -243,12 +243,12 @@ func TestRegexpCacheBehavior(t *testing.T) {

func TestRegexpInFile(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

err := FileInit(TestFolder, "test_data_re.txt", "regex")
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -286,23 +286,23 @@ func TestRegexpInFile(t *testing.T) {
for _, test := range tests {
compiledFilter, err := expr.Compile(test.filter, GetExprOptions(map[string]interface{}{})...)
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

result, err := expr.Run(compiledFilter, map[string]interface{}{})
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

if isOk := assert.Equal(t, test.result, result); !isOk {
t.Fatalf("test '%s' : NOK", test.name)
t.Fatalf("test '%s': NOK", test.name)
}
}
}

func TestFileInit(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -340,7 +340,7 @@ func TestFileInit(t *testing.T) {
for _, test := range tests {
err := FileInit(TestFolder, test.filename, test.types)
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

switch test.types {
Expand Down Expand Up @@ -376,12 +376,12 @@ func TestFileInit(t *testing.T) {

func TestFile(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

err := FileInit(TestFolder, "test_data.txt", "string")
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -419,12 +419,12 @@ func TestFile(t *testing.T) {
for _, test := range tests {
compiledFilter, err := expr.Compile(test.filter, GetExprOptions(map[string]interface{}{})...)
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

result, err := expr.Run(compiledFilter, map[string]interface{}{})
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

if isOk := assert.Equal(t, test.result, result); !isOk {
Expand Down Expand Up @@ -1426,7 +1426,7 @@ func TestParseUnixTime(t *testing.T) {

func TestIsIp(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down
18 changes: 8 additions & 10 deletions pkg/exprhelpers/jsonextract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ package exprhelpers
import (
"testing"

log "github.com/sirupsen/logrus"

"github.com/antonmedv/expr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestJsonExtract(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

err := FileInit(TestFolder, "test_data_re.txt", "regex")
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -67,12 +65,12 @@ func TestJsonExtract(t *testing.T) {

func TestJsonExtractUnescape(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

err := FileInit(TestFolder, "test_data_re.txt", "regex")
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -115,12 +113,12 @@ func TestJsonExtractUnescape(t *testing.T) {

func TestJsonExtractSlice(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

err := FileInit(TestFolder, "test_data_re.txt", "regex")
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -178,12 +176,12 @@ func TestJsonExtractSlice(t *testing.T) {

func TestJsonExtractObject(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

err := FileInit(TestFolder, "test_data_re.txt", "regex")
if err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/exprhelpers/xml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestXMLGetAttributeValue(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestXMLGetAttributeValue(t *testing.T) {
}
func TestXMLGetNodeValue(t *testing.T) {
if err := Init(nil); err != nil {
log.Fatal(err)
t.Fatal(err)
}

tests := []struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/leakybucket/buckets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestBucket(t *testing.T) {

err = exprhelpers.Init(nil)
if err != nil {
log.Fatalf("exprhelpers init failed: %s", err)
t.Fatalf("exprhelpers init failed: %s", err)
}

if envSetting != "" {
Expand Down Expand Up @@ -125,7 +125,7 @@ func testOneBucket(t *testing.T, hub *cwhub.Hub, dir string, tomb *tomb.Tomb) er
panic(err)
}
if err := yaml.UnmarshalStrict(out.Bytes(), &stages); err != nil {
log.Fatalf("failed unmarshaling %s : %s", stagecfg, err)
t.Fatalf("failed unmarshaling %s : %s", stagecfg, err)
}
files := []string{}
for _, x := range stages {
Expand Down
5 changes: 2 additions & 3 deletions pkg/parser/parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func prepTests() (*UnixParserCtx, EnricherCtx, error) {

err = exprhelpers.Init(nil)
if err != nil {
log.Fatalf("exprhelpers init failed: %s", err)
return nil, ectx, fmt.Errorf("exprhelpers init failed: %w", err)
}

//Load enrichment
Expand All @@ -158,7 +158,7 @@ func prepTests() (*UnixParserCtx, EnricherCtx, error) {
}
ectx, err = Loadplugin()
if err != nil {
log.Fatalf("failed to load plugin geoip : %v", err)
return nil, ectx, fmt.Errorf("failed to load plugin geoip: %v", err)
}
log.Printf("Loaded -> %+v", ectx)

Expand Down Expand Up @@ -299,7 +299,6 @@ func testSubSet(testSet TestFile, pctx UnixParserCtx, nodes []Node) (bool, error
only the keys of the expected part are checked against result
*/
if len(testSet.Results) == 0 && len(results) == 0 {
log.Fatal("No results, no tests, abort.")
return false, errors.New("no tests, no results")
}

Expand Down

0 comments on commit 757e5d3

Please sign in to comment.