mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 12:25:53 +02:00
update plugin tests
This commit is contained in:
parent
f5be055854
commit
0c1f22dba5
4 changed files with 23 additions and 21 deletions
|
@ -28,6 +28,7 @@ type PluginSuite struct {
|
||||||
notifDir string // (config_paths.notification_dir)
|
notifDir string // (config_paths.notification_dir)
|
||||||
pluginBinary string // full path to the plugin binary (unique for each test)
|
pluginBinary string // full path to the plugin binary (unique for each test)
|
||||||
pluginConfig string // full path to the notification config (unique for each test)
|
pluginConfig string // full path to the notification config (unique for each test)
|
||||||
|
outFile string // full path to the output file (unique for each test)
|
||||||
|
|
||||||
pluginBroker *PluginBroker
|
pluginBroker *PluginBroker
|
||||||
}
|
}
|
||||||
|
@ -125,6 +126,9 @@ func (s *PluginSuite) SetupSubTest() {
|
||||||
s.pluginConfig = filepath.Join(s.notifDir, "dummy.yaml")
|
s.pluginConfig = filepath.Join(s.notifDir, "dummy.yaml")
|
||||||
err = copyFile("testdata/dummy.yaml", s.pluginConfig)
|
err = copyFile("testdata/dummy.yaml", s.pluginConfig)
|
||||||
require.NoError(t, err, "while copying plugin config")
|
require.NoError(t, err, "while copying plugin config")
|
||||||
|
|
||||||
|
s.outFile = filepath.Join(t.TempDir(), "out")
|
||||||
|
t.Setenv("OUTFILE", s.outFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PluginSuite) TearDownSubTest() {
|
func (s *PluginSuite) TearDownSubTest() {
|
||||||
|
@ -139,8 +143,6 @@ func (s *PluginSuite) TearDownSubTest() {
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Remove("./out")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *PluginSuite) InitBroker(ctx context.Context, procCfg *csconfig.PluginCfg) (*PluginBroker, error) {
|
func (s *PluginSuite) InitBroker(ctx context.Context, procCfg *csconfig.PluginCfg) (*PluginBroker, error) {
|
||||||
|
|
|
@ -163,7 +163,7 @@ func (s *PluginSuite) TestBrokerNoThreshold() {
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
// we expect one now
|
// we expect one now
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
err = json.Unmarshal(content, &alerts)
|
err = json.Unmarshal(content, &alerts)
|
||||||
|
@ -171,7 +171,7 @@ func (s *PluginSuite) TestBrokerNoThreshold() {
|
||||||
assert.Len(t, alerts, 1)
|
assert.Len(t, alerts, 1)
|
||||||
|
|
||||||
// remove it
|
// remove it
|
||||||
os.Remove("./out")
|
os.Remove(s.outFile)
|
||||||
|
|
||||||
// and another one
|
// and another one
|
||||||
log.Printf("second send")
|
log.Printf("second send")
|
||||||
|
@ -180,7 +180,7 @@ func (s *PluginSuite) TestBrokerNoThreshold() {
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
// we expect one again, as we cleaned the file
|
// we expect one again, as we cleaned the file
|
||||||
content, err = os.ReadFile("./out")
|
content, err = os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
err = json.Unmarshal(content, &alerts)
|
err = json.Unmarshal(content, &alerts)
|
||||||
|
@ -216,10 +216,10 @@ func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_TimeFirst() {
|
||||||
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
// because of group threshold, we shouldn't have data yet
|
// because of group threshold, we shouldn't have data yet
|
||||||
assert.NoFileExists(t, "./out")
|
assert.NoFileExists(t, s.outFile)
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
// after 1 seconds, we should have data
|
// after 1 seconds, we should have data
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
var alerts []models.Alert
|
var alerts []models.Alert
|
||||||
|
@ -254,13 +254,13 @@ func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_CountFirst() {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
// because of group threshold, we shouldn't have data yet
|
// because of group threshold, we shouldn't have data yet
|
||||||
assert.NoFileExists(t, "./out")
|
assert.NoFileExists(t, s.outFile)
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
|
||||||
// and now we should
|
// and now we should
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
var alerts []models.Alert
|
var alerts []models.Alert
|
||||||
|
@ -295,7 +295,7 @@ func (s *PluginSuite) TestBrokerRunGroupThreshold() {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
// because of group threshold, we shouldn't have data yet
|
// because of group threshold, we shouldn't have data yet
|
||||||
assert.NoFileExists(t, "./out")
|
assert.NoFileExists(t, s.outFile)
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
|
@ -303,7 +303,7 @@ func (s *PluginSuite) TestBrokerRunGroupThreshold() {
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
// and now we should
|
// and now we should
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
decoder := json.NewDecoder(bytes.NewReader(content))
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
||||||
|
@ -347,11 +347,11 @@ func (s *PluginSuite) TestBrokerRunTimeThreshold() {
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
|
|
||||||
// we shouldn't have data yet
|
// we shouldn't have data yet
|
||||||
assert.NoFileExists(t, "./out")
|
assert.NoFileExists(t, s.outFile)
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
|
|
||||||
// and now we should
|
// and now we should
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
var alerts []models.Alert
|
var alerts []models.Alert
|
||||||
|
@ -372,16 +372,16 @@ func (s *PluginSuite) TestBrokerRunSimple() {
|
||||||
tomb := tomb.Tomb{}
|
tomb := tomb.Tomb{}
|
||||||
go pb.Run(&tomb)
|
go pb.Run(&tomb)
|
||||||
|
|
||||||
assert.NoFileExists(t, "./out")
|
assert.NoFileExists(t, s.outFile)
|
||||||
|
|
||||||
defer os.Remove("./out")
|
defer os.Remove(s.outFile)
|
||||||
|
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
// make it wait a bit, CI can be slow
|
// make it wait a bit, CI can be slow
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
decoder := json.NewDecoder(bytes.NewReader(content))
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
||||||
|
|
|
@ -76,16 +76,16 @@ func (s *PluginSuite) TestBrokerRun() {
|
||||||
tomb := tomb.Tomb{}
|
tomb := tomb.Tomb{}
|
||||||
go pb.Run(&tomb)
|
go pb.Run(&tomb)
|
||||||
|
|
||||||
assert.NoFileExists(t, "./out")
|
assert.NoFileExists(t, s.outFile)
|
||||||
defer os.Remove("./out")
|
defer os.Remove(s.outFile)
|
||||||
|
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
||||||
time.Sleep(time.Second * 4)
|
time.Sleep(time.Second * 4)
|
||||||
|
|
||||||
assert.FileExists(t, ".\\out")
|
assert.FileExists(t, s.outFile)
|
||||||
|
|
||||||
content, err := os.ReadFile("./out")
|
content, err := os.ReadFile(s.outFile)
|
||||||
require.NoError(t, err, "Error reading file")
|
require.NoError(t, err, "Error reading file")
|
||||||
|
|
||||||
decoder := json.NewDecoder(bytes.NewReader(content))
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
||||||
|
|
2
pkg/csplugin/testdata/dummy.yaml
vendored
2
pkg/csplugin/testdata/dummy.yaml
vendored
|
@ -18,5 +18,5 @@ format: |
|
||||||
{{.|toJson}}
|
{{.|toJson}}
|
||||||
|
|
||||||
#
|
#
|
||||||
output_file: ./out # notifications will be appended here. optional
|
output_file: ${OUTFILE}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue