Refactor unit tests to reduce line count (#1264)

This commit is contained in:
mmetc 2022-02-15 12:50:33 +01:00 committed by GitHub
parent afe1704aa6
commit 9bc7e6ffcf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 106 additions and 148 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration" "github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
"github.com/crowdsecurity/crowdsec/pkg/csconfig" "github.com/crowdsecurity/crowdsec/pkg/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -184,10 +185,9 @@ wowo: ajsajasjas
t.Fatalf("%s : expected error '%s' in '%s'", test.TestName, test.ExpectedError, err.Error()) t.Fatalf("%s : expected error '%s' in '%s'", test.TestName, test.ExpectedError, err.Error())
} }
continue continue
} else { }
if err != nil { if err != nil {
t.Fatalf("%s : unexpected error '%s'", test.TestName, err) t.Fatalf("%s : unexpected error '%s'", test.TestName, err)
}
} }
switch test.TestName { switch test.TestName {
@ -290,10 +290,9 @@ func TestLoadAcquisitionFromFile(t *testing.T) {
t.Fatalf("%s : expected error '%s' in '%s'", test.TestName, test.ExpectedError, err.Error()) t.Fatalf("%s : expected error '%s' in '%s'", test.TestName, test.ExpectedError, err.Error())
} }
continue continue
} else { }
if err != nil { if err != nil {
t.Fatalf("%s : unexpected error '%s'", test.TestName, err) t.Fatalf("%s : unexpected error '%s'", test.TestName, err)
}
} }
if len(dss) != test.ExpectedLen { if len(dss) != test.ExpectedLen {
t.Fatalf("%s : expected %d datasources got %d", test.TestName, test.ExpectedLen, len(dss)) t.Fatalf("%s : expected %d datasources got %d", test.TestName, test.ExpectedLen, len(dss))
@ -336,11 +335,13 @@ func (f *MockCat) OneShotAcquisition(out chan types.Event, tomb *tomb.Tomb) erro
func (f *MockCat) StreamingAcquisition(chan types.Event, *tomb.Tomb) error { func (f *MockCat) StreamingAcquisition(chan types.Event, *tomb.Tomb) error {
return fmt.Errorf("can't run in tail") return fmt.Errorf("can't run in tail")
} }
func (f *MockCat) CanRun() error { return nil } func (f *MockCat) CanRun() error { return nil }
func (f *MockCat) GetMetrics() []prometheus.Collector { return nil } func (f *MockCat) GetMetrics() []prometheus.Collector { return nil }
func (f *MockCat) GetAggregMetrics() []prometheus.Collector { return nil } func (f *MockCat) GetAggregMetrics() []prometheus.Collector { return nil }
func (f *MockCat) Dump() interface{} { return f } func (f *MockCat) Dump() interface{} { return f }
func (f *MockCat) ConfigureByDSN(string, map[string]string, *log.Entry) error { return fmt.Errorf("not supported") } func (f *MockCat) ConfigureByDSN(string, map[string]string, *log.Entry) error {
return fmt.Errorf("not supported")
}
//---- //----
@ -554,15 +555,8 @@ func TestConfigureByDSN(t *testing.T) {
for _, test := range tests { for _, test := range tests {
srcs, err := LoadAcquisitionFromDSN(test.dsn, map[string]string{"type": "test_label"}) srcs, err := LoadAcquisitionFromDSN(test.dsn, map[string]string{"type": "test_label"})
if err != nil && test.ExpectedError != "" { cstest.AssertErrorContains(t, err, test.ExpectedError)
if !strings.Contains(err.Error(), test.ExpectedError) {
t.Fatalf("expected '%s', got '%s'", test.ExpectedError, err.Error())
}
} else if err != nil && test.ExpectedError == "" {
t.Fatalf("got unexpected error '%s'", err.Error())
} else if err == nil && test.ExpectedError != "" {
t.Fatalf("expected error '%s' got none", test.ExpectedError)
}
if len(srcs) != test.ExpectedResLen { if len(srcs) != test.ExpectedResLen {
t.Fatalf("expected %d results, got %d", test.ExpectedResLen, len(srcs)) t.Fatalf("expected %d results, got %d", test.ExpectedResLen, len(srcs))
} }

View file

@ -9,6 +9,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
dockerTypes "github.com/docker/docker/api/types" dockerTypes "github.com/docker/docker/api/types"
"github.com/docker/docker/client" "github.com/docker/docker/client"
@ -53,12 +54,7 @@ container_name:
for _, test := range tests { for _, test := range tests {
f := DockerSource{} f := DockerSource{}
err := f.Configure([]byte(test.config), subLogger) err := f.Configure([]byte(test.config), subLogger)
if test.expectedErr != "" && err == nil { cstest.AssertErrorContains(t, err, test.expectedErr)
t.Fatalf("Expected err %s but got nil !", test.expectedErr)
}
if test.expectedErr != "" {
assert.Contains(t, err.Error(), test.expectedErr)
}
} }
} }
@ -102,11 +98,7 @@ func TestConfigureDSN(t *testing.T) {
for _, test := range tests { for _, test := range tests {
f := DockerSource{} f := DockerSource{}
err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger) err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger)
if test.expectedErr != "" { cstest.AssertErrorContains(t, err, test.expectedErr)
assert.Contains(t, err.Error(), test.expectedErr)
} else {
assert.Equal(t, err, nil)
}
} }
} }
@ -196,14 +188,8 @@ container_name_regexp:
} }
}) })
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
if ts.expectedErr == "" && err != nil { cstest.AssertErrorContains(t, err, ts.expectedErr)
t.Fatalf("Unexpected error : %s", err)
} else if ts.expectedErr != "" && err != nil {
assert.Contains(t, err.Error(), ts.expectedErr)
continue
} else if ts.expectedErr != "" && err == nil {
t.Fatalf("Expected error %s, but got nothing !", ts.expectedErr)
}
if err := readerTomb.Wait(); err != nil { if err := readerTomb.Wait(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -311,15 +297,8 @@ func TestOneShot(t *testing.T) {
} }
tomb := tomb.Tomb{} tomb := tomb.Tomb{}
err := dockerClient.OneShotAcquisition(out, &tomb) err := dockerClient.OneShotAcquisition(out, &tomb)
cstest.AssertErrorContains(t, err, ts.expectedErr)
if ts.expectedErr == "" && err != nil {
t.Fatalf("Unexpected error : %s", err)
} else if ts.expectedErr != "" && err != nil {
assert.Contains(t, err.Error(), ts.expectedErr)
continue
} else if ts.expectedErr != "" && err == nil {
t.Fatalf("Expected error %s, but got nothing !", ts.expectedErr)
}
// else we do the check before actualLines is incremented ... // else we do the check before actualLines is incremented ...
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)
if ts.expectedLines != 0 { if ts.expectedLines != 0 {

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test" "github.com/sirupsen/logrus/hooks/test"
@ -70,87 +71,91 @@ func TestConfigureDSN(t *testing.T) {
for _, test := range tests { for _, test := range tests {
f := FileSource{} f := FileSource{}
err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger) err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger)
if test.expectedErr != "" { cstest.AssertErrorContains(t, err, test.expectedErr)
assert.Contains(t, err.Error(), test.expectedErr)
} else {
assert.Equal(t, err, nil)
}
} }
} }
func TestOneShot(t *testing.T) { func TestOneShot(t *testing.T) {
tests := []struct { tests := []struct {
config string config string
expectedErr string expectedConfigErr string
expectedOutput string expectedErr string
expectedLines int expectedOutput string
logLevel log.Level expectedLines int
setup func() logLevel log.Level
afterConfigure func() setup func()
teardown func() afterConfigure func()
teardown func()
}{ }{
{ {
config: ` config: `
mode: cat mode: cat
filename: /etc/shadow`, filename: /etc/shadow`,
expectedErr: "failed opening /etc/shadow: open /etc/shadow: permission denied", expectedConfigErr: "",
expectedOutput: "", expectedErr: "failed opening /etc/shadow: open /etc/shadow: permission denied",
logLevel: log.WarnLevel, expectedOutput: "",
expectedLines: 0, logLevel: log.WarnLevel,
expectedLines: 0,
}, },
{ {
config: ` config: `
mode: cat mode: cat
filename: /`, filename: /`,
expectedErr: "", expectedConfigErr: "",
expectedOutput: "/ is a directory, ignoring it", expectedErr: "",
logLevel: log.WarnLevel, expectedOutput: "/ is a directory, ignoring it",
expectedLines: 0, logLevel: log.WarnLevel,
expectedLines: 0,
}, },
{ {
config: ` config: `
mode: cat mode: cat
filename: "[*-.log"`, filename: "[*-.log"`,
expectedErr: "Glob failure: syntax error in pattern", expectedConfigErr: "Glob failure: syntax error in pattern",
expectedOutput: "", expectedErr: "",
logLevel: log.WarnLevel, expectedOutput: "",
expectedLines: 0, logLevel: log.WarnLevel,
expectedLines: 0,
}, },
{ {
config: ` config: `
mode: cat mode: cat
filename: /do/not/exist`, filename: /do/not/exist`,
expectedErr: "", expectedConfigErr: "",
expectedOutput: "No matching files for pattern /do/not/exist", expectedErr: "",
logLevel: log.WarnLevel, expectedOutput: "No matching files for pattern /do/not/exist",
expectedLines: 0, logLevel: log.WarnLevel,
expectedLines: 0,
}, },
{ {
config: ` config: `
mode: cat mode: cat
filename: test_files/test.log`, filename: test_files/test.log`,
expectedErr: "", expectedConfigErr: "",
expectedOutput: "", expectedErr: "",
expectedLines: 5, expectedOutput: "",
logLevel: log.WarnLevel, expectedLines: 5,
logLevel: log.WarnLevel,
}, },
{ {
config: ` config: `
mode: cat mode: cat
filename: test_files/test.log.gz`, filename: test_files/test.log.gz`,
expectedErr: "", expectedConfigErr: "",
expectedOutput: "", expectedErr: "",
expectedLines: 5, expectedOutput: "",
logLevel: log.WarnLevel, expectedLines: 5,
logLevel: log.WarnLevel,
}, },
{ {
config: ` config: `
mode: cat mode: cat
filename: test_files/bad.gz`, filename: test_files/bad.gz`,
expectedErr: "failed to read gz test_files/bad.gz: unexpected EOF", expectedConfigErr: "",
expectedOutput: "", expectedErr: "failed to read gz test_files/bad.gz: unexpected EOF",
expectedLines: 0, expectedOutput: "",
logLevel: log.WarnLevel, expectedLines: 0,
logLevel: log.WarnLevel,
}, },
{ {
config: ` config: `
@ -179,12 +184,11 @@ filename: test_files/test_delete.log`,
ts.setup() ts.setup()
} }
err := f.Configure([]byte(ts.config), subLogger) err := f.Configure([]byte(ts.config), subLogger)
if err != nil && ts.expectedErr != "" { cstest.AssertErrorContains(t, err, ts.expectedConfigErr)
assert.Contains(t, err.Error(), ts.expectedErr) if err != nil {
continue continue
} else if err != nil && ts.expectedErr == "" {
t.Fatalf("Unexpected error : %s", err)
} }
if ts.afterConfigure != nil { if ts.afterConfigure != nil {
ts.afterConfigure() ts.afterConfigure()
} }
@ -203,15 +207,11 @@ filename: test_files/test_delete.log`,
}() }()
} }
err = f.OneShotAcquisition(out, &tomb) err = f.OneShotAcquisition(out, &tomb)
cstest.AssertErrorContains(t, err, ts.expectedErr)
if ts.expectedLines != 0 { if ts.expectedLines != 0 {
assert.Equal(t, actualLines, ts.expectedLines) assert.Equal(t, actualLines, ts.expectedLines)
} }
if ts.expectedErr != "" {
if err == nil {
t.Fatalf("Expected error but got nothing ! %+v", ts)
}
assert.Contains(t, err.Error(), ts.expectedErr)
}
if ts.expectedOutput != "" { if ts.expectedOutput != "" {
assert.Contains(t, hook.LastEntry().Message, ts.expectedOutput) assert.Contains(t, hook.LastEntry().Message, ts.expectedOutput)
hook.Reset() hook.Reset()
@ -359,13 +359,7 @@ force_inotify: true`,
}() }()
} }
err = f.StreamingAcquisition(out, &tomb) err = f.StreamingAcquisition(out, &tomb)
cstest.AssertErrorContains(t, err, ts.expectedErr)
if ts.expectedErr != "" {
if err == nil {
t.Fatalf("Expected error but got nothing ! %+v", ts)
}
assert.Contains(t, err.Error(), ts.expectedErr)
}
if ts.expectedLines != 0 { if ts.expectedLines != 0 {
fd, err := os.Create("test_files/stream.log") fd, err := os.Create("test_files/stream.log")

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test" "github.com/sirupsen/logrus/hooks/test"
@ -44,12 +45,7 @@ journalctl_filter:
for _, test := range tests { for _, test := range tests {
f := JournalCtlSource{} f := JournalCtlSource{}
err := f.Configure([]byte(test.config), subLogger) err := f.Configure([]byte(test.config), subLogger)
if test.expectedErr != "" && err == nil { cstest.AssertErrorContains(t, err, test.expectedErr)
t.Fatalf("Expected err %s but got nil !", test.expectedErr)
}
if test.expectedErr != "" {
assert.Contains(t, err.Error(), test.expectedErr)
}
} }
} }
@ -93,11 +89,7 @@ func TestConfigureDSN(t *testing.T) {
for _, test := range tests { for _, test := range tests {
f := JournalCtlSource{} f := JournalCtlSource{}
err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger) err := f.ConfigureByDSN(test.dsn, map[string]string{"type": "testtype"}, subLogger)
if test.expectedErr != "" { cstest.AssertErrorContains(t, err, test.expectedErr)
assert.Contains(t, err.Error(), test.expectedErr)
} else {
assert.Equal(t, err, nil)
}
} }
} }
@ -170,14 +162,11 @@ journalctl_filter:
} }
err = j.OneShotAcquisition(out, &tomb) err = j.OneShotAcquisition(out, &tomb)
if ts.expectedErr == "" && err != nil { cstest.AssertErrorContains(t, err, ts.expectedErr)
t.Fatalf("Unexpected error : %s", err) if err != nil {
} else if ts.expectedErr != "" && err != nil {
assert.Contains(t, err.Error(), ts.expectedErr)
continue continue
} else if ts.expectedErr != "" && err == nil {
t.Fatalf("Expected error %s, but got nothing !", ts.expectedErr)
} }
if ts.expectedLines != 0 { if ts.expectedLines != 0 {
assert.Equal(t, ts.expectedLines, actualLines) assert.Equal(t, ts.expectedLines, actualLines)
} }
@ -250,13 +239,9 @@ journalctl_filter:
} }
err = j.StreamingAcquisition(out, &tomb) err = j.StreamingAcquisition(out, &tomb)
if ts.expectedErr == "" && err != nil { cstest.AssertErrorContains(t, err, ts.expectedErr)
t.Fatalf("Unexpected error : %s", err) if err != nil {
} else if ts.expectedErr != "" && err != nil {
assert.Contains(t, err.Error(), ts.expectedErr)
continue continue
} else if ts.expectedErr != "" && err == nil {
t.Fatalf("Expected error %s, but got nothing !", ts.expectedErr)
} }
if ts.expectedLines != 0 { if ts.expectedLines != 0 {

View file

@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/kinesis"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -138,12 +139,7 @@ stream_arn: arn:aws:kinesis:eu-west-1:123456789012:stream/my-stream`,
for _, test := range tests { for _, test := range tests {
f := KinesisSource{} f := KinesisSource{}
err := f.Configure([]byte(test.config), subLogger) err := f.Configure([]byte(test.config), subLogger)
if test.expectedErr != "" && err == nil { cstest.AssertErrorContains(t, err, test.expectedErr)
t.Fatalf("Expected err %s but got nil !", test.expectedErr)
}
if test.expectedErr != "" {
assert.Contains(t, err.Error(), test.expectedErr)
}
} }
} }

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types" "github.com/crowdsecurity/crowdsec/pkg/types"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/tomb.v2" "gopkg.in/tomb.v2"
@ -54,12 +55,7 @@ listen_addr: 10.0.0`,
for _, test := range tests { for _, test := range tests {
s := SyslogSource{} s := SyslogSource{}
err := s.Configure([]byte(test.config), subLogger) err := s.Configure([]byte(test.config), subLogger)
if test.expectedErr != "" { cstest.AssertErrorContains(t, err, test.expectedErr)
if err == nil {
t.Fatalf("Expected error but got nothing : %+v", test)
}
assert.Contains(t, err.Error(), test.expectedErr)
}
} }
} }
@ -123,14 +119,11 @@ listen_addr: 127.0.0.1`,
tomb := tomb.Tomb{} tomb := tomb.Tomb{}
out := make(chan types.Event) out := make(chan types.Event)
err := s.StreamingAcquisition(out, &tomb) err := s.StreamingAcquisition(out, &tomb)
if ts.expectedErr != "" && err == nil { cstest.AssertErrorContains(t, err, ts.expectedErr)
t.Fatalf("expected error but got nothing : %+v", ts) if err != nil {
} else if ts.expectedErr == "" && err != nil {
t.Fatalf("unexpected error : %s", err)
} else if ts.expectedErr != "" && err != nil {
assert.Contains(t, err.Error(), ts.expectedErr)
continue continue
} }
actualLines := 0 actualLines := 0
go writeToSyslog(ts.logs) go writeToSyslog(ts.logs)
READLOOP: READLOOP:

View file

@ -4,6 +4,9 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"testing"
"github.com/stretchr/testify/assert"
) )
func Copy(sourceFile string, destinationFile string) error { func Copy(sourceFile string, destinationFile string) error {
@ -79,3 +82,17 @@ func CopyDir(src string, dest string) error {
return nil return nil
} }
func AssertErrorContains(t *testing.T, err error, expectedErr string) {
if expectedErr == "" {
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
assert.Equal(t, err, nil)
return
}
if err == nil {
t.Fatalf("Expected '%s', got nil", expectedErr)
}
assert.Contains(t, err.Error(), expectedErr)
}