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/csconfig"
"github.com/crowdsecurity/crowdsec/pkg/cstest"
"github.com/crowdsecurity/crowdsec/pkg/types"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
@ -184,11 +185,10 @@ wowo: ajsajasjas
t.Fatalf("%s : expected error '%s' in '%s'", test.TestName, test.ExpectedError, err.Error())
}
continue
} else {
}
if err != nil {
t.Fatalf("%s : unexpected error '%s'", test.TestName, err)
}
}
switch test.TestName {
case "basic_valid_config":
@ -290,11 +290,10 @@ func TestLoadAcquisitionFromFile(t *testing.T) {
t.Fatalf("%s : expected error '%s' in '%s'", test.TestName, test.ExpectedError, err.Error())
}
continue
} else {
}
if err != nil {
t.Fatalf("%s : unexpected error '%s'", test.TestName, err)
}
}
if len(dss) != test.ExpectedLen {
t.Fatalf("%s : expected %d datasources got %d", test.TestName, test.ExpectedLen, len(dss))
}
@ -340,7 +339,9 @@ func (f *MockCat) CanRun() error { return nil
func (f *MockCat) GetMetrics() []prometheus.Collector { return nil }
func (f *MockCat) GetAggregMetrics() []prometheus.Collector { return nil }
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 {
srcs, err := LoadAcquisitionFromDSN(test.dsn, map[string]string{"type": "test_label"})
if err != nil && 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)
}
cstest.AssertErrorContains(t, err, test.ExpectedError)
if len(srcs) != test.ExpectedResLen {
t.Fatalf("expected %d results, got %d", test.ExpectedResLen, len(srcs))
}

View file

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

View file

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

View file

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

View file

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

View file

@ -4,6 +4,9 @@ import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func Copy(sourceFile string, destinationFile string) error {
@ -79,3 +82,17 @@ func CopyDir(src string, dest string) error {
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)
}