|
@@ -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 != "" {
|
|
|
|
- assert.Contains(t, err.Error(), test.expectedErr)
|
|
|
|
- } else {
|
|
|
|
- assert.Equal(t, err, nil)
|
|
|
|
- }
|
|
|
|
|
|
+ cstest.AssertErrorContains(t, err, test.expectedErr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
func TestOneShot(t *testing.T) {
|
|
func TestOneShot(t *testing.T) {
|
|
tests := []struct {
|
|
tests := []struct {
|
|
- config string
|
|
|
|
- expectedErr string
|
|
|
|
- expectedOutput string
|
|
|
|
- expectedLines int
|
|
|
|
- logLevel log.Level
|
|
|
|
- setup func()
|
|
|
|
- afterConfigure func()
|
|
|
|
- teardown func()
|
|
|
|
|
|
+ config string
|
|
|
|
+ expectedConfigErr string
|
|
|
|
+ expectedErr string
|
|
|
|
+ expectedOutput string
|
|
|
|
+ expectedLines int
|
|
|
|
+ logLevel log.Level
|
|
|
|
+ setup 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",
|
|
|
|
- expectedOutput: "",
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
- expectedLines: 0,
|
|
|
|
|
|
+ expectedConfigErr: "",
|
|
|
|
+ expectedErr: "failed opening /etc/shadow: open /etc/shadow: permission denied",
|
|
|
|
+ expectedOutput: "",
|
|
|
|
+ logLevel: log.WarnLevel,
|
|
|
|
+ expectedLines: 0,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
config: `
|
|
config: `
|
|
mode: cat
|
|
mode: cat
|
|
filename: /`,
|
|
filename: /`,
|
|
- expectedErr: "",
|
|
|
|
- expectedOutput: "/ is a directory, ignoring it",
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
- expectedLines: 0,
|
|
|
|
|
|
+ expectedConfigErr: "",
|
|
|
|
+ expectedErr: "",
|
|
|
|
+ expectedOutput: "/ is a directory, ignoring it",
|
|
|
|
+ logLevel: log.WarnLevel,
|
|
|
|
+ expectedLines: 0,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
config: `
|
|
config: `
|
|
mode: cat
|
|
mode: cat
|
|
filename: "[*-.log"`,
|
|
filename: "[*-.log"`,
|
|
- expectedErr: "Glob failure: syntax error in pattern",
|
|
|
|
- expectedOutput: "",
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
- expectedLines: 0,
|
|
|
|
|
|
+ expectedConfigErr: "Glob failure: syntax error in pattern",
|
|
|
|
+ expectedErr: "",
|
|
|
|
+ expectedOutput: "",
|
|
|
|
+ logLevel: log.WarnLevel,
|
|
|
|
+ expectedLines: 0,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
config: `
|
|
config: `
|
|
mode: cat
|
|
mode: cat
|
|
filename: /do/not/exist`,
|
|
filename: /do/not/exist`,
|
|
- expectedErr: "",
|
|
|
|
- expectedOutput: "No matching files for pattern /do/not/exist",
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
- expectedLines: 0,
|
|
|
|
|
|
+ expectedConfigErr: "",
|
|
|
|
+ expectedErr: "",
|
|
|
|
+ expectedOutput: "No matching files for pattern /do/not/exist",
|
|
|
|
+ logLevel: log.WarnLevel,
|
|
|
|
+ expectedLines: 0,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
config: `
|
|
config: `
|
|
mode: cat
|
|
mode: cat
|
|
filename: test_files/test.log`,
|
|
filename: test_files/test.log`,
|
|
- expectedErr: "",
|
|
|
|
- expectedOutput: "",
|
|
|
|
- expectedLines: 5,
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
|
|
+ expectedConfigErr: "",
|
|
|
|
+ expectedErr: "",
|
|
|
|
+ expectedOutput: "",
|
|
|
|
+ expectedLines: 5,
|
|
|
|
+ logLevel: log.WarnLevel,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
config: `
|
|
config: `
|
|
mode: cat
|
|
mode: cat
|
|
filename: test_files/test.log.gz`,
|
|
filename: test_files/test.log.gz`,
|
|
- expectedErr: "",
|
|
|
|
- expectedOutput: "",
|
|
|
|
- expectedLines: 5,
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
|
|
+ expectedConfigErr: "",
|
|
|
|
+ expectedErr: "",
|
|
|
|
+ expectedOutput: "",
|
|
|
|
+ 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",
|
|
|
|
- expectedOutput: "",
|
|
|
|
- expectedLines: 0,
|
|
|
|
- logLevel: log.WarnLevel,
|
|
|
|
|
|
+ expectedConfigErr: "",
|
|
|
|
+ expectedErr: "failed to read gz test_files/bad.gz: unexpected EOF",
|
|
|
|
+ expectedOutput: "",
|
|
|
|
+ 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 != "" {
|
|
|
|
- assert.Contains(t, err.Error(), ts.expectedErr)
|
|
|
|
|
|
+ cstest.AssertErrorContains(t, err, ts.expectedConfigErr)
|
|
|
|
+ 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)
|
|
-
|
|
|
|
- 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 {
|
|
if ts.expectedLines != 0 {
|
|
fd, err := os.Create("test_files/stream.log")
|
|
fd, err := os.Create("test_files/stream.log")
|