2022-05-17 10:14:59 +00:00
|
|
|
//go:build linux || freebsd || netbsd || openbsd || solaris || !windows
|
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
package csplugin
|
|
|
|
|
|
|
|
import (
|
2023-06-05 10:55:03 +00:00
|
|
|
"bytes"
|
2022-04-19 17:12:23 +00:00
|
|
|
"encoding/json"
|
2023-06-05 10:55:03 +00:00
|
|
|
"io"
|
2021-08-25 09:43:29 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
2022-03-29 12:20:26 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-03-29 12:20:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-10-17 12:17:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-03-29 12:20:26 +00:00
|
|
|
"gopkg.in/tomb.v2"
|
2022-04-19 17:12:23 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-07-28 14:35:08 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/cstest"
|
2023-05-25 13:37:44 +00:00
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
2021-08-25 09:43:29 +00:00
|
|
|
)
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) permissionSetter(perm os.FileMode) func(*testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
err := os.Chmod(s.pluginBinary, perm)
|
|
|
|
require.NoError(t, err, "chmod %s %s", perm, s.pluginBinary)
|
|
|
|
}
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 09:31:41 +00:00
|
|
|
func (s *PluginSuite) readconfig() PluginConfig {
|
2023-06-01 08:33:08 +00:00
|
|
|
var config PluginConfig
|
|
|
|
t := s.T()
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
orig, err := os.ReadFile(s.pluginConfig)
|
2023-06-22 09:31:41 +00:00
|
|
|
require.NoError(t, err, "unable to read config file %s", s.pluginConfig)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
err = yaml.Unmarshal(orig, &config)
|
2023-06-22 09:31:41 +00:00
|
|
|
require.NoError(t, err, "unable to unmarshal config file")
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
return config
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) writeconfig(config PluginConfig) {
|
|
|
|
t := s.T()
|
|
|
|
data, err := yaml.Marshal(&config)
|
2023-06-22 09:31:41 +00:00
|
|
|
require.NoError(t, err, "unable to marshal config file")
|
2023-06-01 08:33:08 +00:00
|
|
|
|
|
|
|
err = os.WriteFile(s.pluginConfig, data, 0644)
|
2023-06-22 09:31:41 +00:00
|
|
|
require.NoError(t, err, "unable to write config file %s", s.pluginConfig)
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerInit() {
|
2022-03-29 12:20:26 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2022-10-17 12:17:23 +00:00
|
|
|
action func(*testing.T)
|
2022-03-29 12:20:26 +00:00
|
|
|
procCfg csconfig.PluginCfg
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr string
|
2022-03-29 12:20:26 +00:00
|
|
|
}{
|
|
|
|
{
|
2023-06-22 09:31:41 +00:00
|
|
|
name: "valid config",
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group writable binary",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "notification-dummy is world writable",
|
2023-06-01 08:33:08 +00:00
|
|
|
action: s.permissionSetter(0o722),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group writable binary",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "notification-dummy is group writable",
|
2023-06-01 08:33:08 +00:00
|
|
|
action: s.permissionSetter(0o724),
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no plugin dir",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: cstest.FileNotFoundMessage,
|
2023-06-01 08:33:08 +00:00
|
|
|
action: func(t *testing.T) {
|
|
|
|
err := os.RemoveAll(s.runDir)
|
|
|
|
require.NoError(t, err)
|
|
|
|
},
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no plugin binary",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "binary for plugin dummy_default not found",
|
|
|
|
action: func(t *testing.T) {
|
2023-06-01 08:33:08 +00:00
|
|
|
err := os.Remove(s.pluginBinary)
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only specify user",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "both plugin user and group must be set",
|
2022-03-29 12:20:26 +00:00
|
|
|
procCfg: csconfig.PluginCfg{
|
|
|
|
User: "123445555551122toto",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "only specify group",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "both plugin user and group must be set",
|
2022-03-29 12:20:26 +00:00
|
|
|
procCfg: csconfig.PluginCfg{
|
|
|
|
Group: "123445555551122toto",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Fails to run as root",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "operation not permitted",
|
2022-03-29 12:20:26 +00:00
|
|
|
procCfg: csconfig.PluginCfg{
|
|
|
|
User: "root",
|
|
|
|
Group: "root",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Invalid user and group",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "unknown user toto1234",
|
2022-03-29 12:20:26 +00:00
|
|
|
procCfg: csconfig.PluginCfg{
|
|
|
|
User: "toto1234",
|
|
|
|
Group: "toto1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Valid user and invalid group",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "unknown group toto1234",
|
2022-03-29 12:20:26 +00:00
|
|
|
procCfg: csconfig.PluginCfg{
|
|
|
|
User: "nobody",
|
|
|
|
Group: "toto1234",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
2023-06-01 08:33:08 +00:00
|
|
|
s.Run(tc.name, func() {
|
|
|
|
t := s.T()
|
2022-10-17 12:17:23 +00:00
|
|
|
if tc.action != nil {
|
|
|
|
tc.action(t)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
2023-06-01 08:33:08 +00:00
|
|
|
_, err := s.InitBroker(&tc.procCfg)
|
2022-10-17 12:17:23 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerNoThreshold() {
|
2022-04-19 17:12:23 +00:00
|
|
|
var alerts []models.Alert
|
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
t := s.T()
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
pb, err := s.InitBroker(nil)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
tomb := tomb.Tomb{}
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// send one item, it should be processed right now
|
2022-04-19 17:12:23 +00:00
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// we expect one now
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
err = json.Unmarshal(content, &alerts)
|
2023-06-01 08:33:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
assert.Len(t, alerts, 1)
|
|
|
|
|
|
|
|
// remove it
|
2022-04-19 17:12:23 +00:00
|
|
|
os.Remove("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// and another one
|
2022-04-19 17:12:23 +00:00
|
|
|
log.Printf("second send")
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// we expect one again, as we cleaned the file
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err = os.ReadFile("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
err = json.Unmarshal(content, &alerts)
|
|
|
|
log.Printf("content-> %s", content)
|
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
assert.Len(t, alerts, 1)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_TimeFirst() {
|
2022-10-17 12:17:23 +00:00
|
|
|
// test grouping by "time"
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2023-06-01 08:33:08 +00:00
|
|
|
|
|
|
|
t := s.T()
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
2023-06-01 08:33:08 +00:00
|
|
|
cfg := s.readconfig()
|
2022-04-19 17:12:23 +00:00
|
|
|
cfg.GroupThreshold = 4
|
|
|
|
cfg.GroupWait = 1 * time.Second
|
2023-06-01 08:33:08 +00:00
|
|
|
s.writeconfig(cfg)
|
|
|
|
|
|
|
|
pb, err := s.InitBroker(nil)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
tomb := tomb.Tomb{}
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
2023-06-01 08:33:08 +00:00
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
// send data
|
2022-04-19 17:12:23 +00:00
|
|
|
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(500 * time.Millisecond)
|
2022-10-17 12:17:23 +00:00
|
|
|
// because of group threshold, we shouldn't have data yet
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoFileExists(t, "./out")
|
|
|
|
time.Sleep(1 * time.Second)
|
2022-10-17 12:17:23 +00:00
|
|
|
// after 1 seconds, we should have data
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile("./out")
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
var alerts []models.Alert
|
|
|
|
err = json.Unmarshal(content, &alerts)
|
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
assert.Len(t, alerts, 3)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_CountFirst() {
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2023-06-01 08:33:08 +00:00
|
|
|
|
|
|
|
t := s.T()
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
2023-06-01 08:33:08 +00:00
|
|
|
cfg := s.readconfig()
|
2022-04-19 17:12:23 +00:00
|
|
|
cfg.GroupThreshold = 4
|
|
|
|
cfg.GroupWait = 4 * time.Second
|
2023-06-01 08:33:08 +00:00
|
|
|
s.writeconfig(cfg)
|
|
|
|
|
|
|
|
pb, err := s.InitBroker(nil)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
tomb := tomb.Tomb{}
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// send data
|
2022-04-19 17:12:23 +00:00
|
|
|
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(100 * time.Millisecond)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// because of group threshold, we shouldn't have data yet
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoFileExists(t, "./out")
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// and now we should
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
var alerts []models.Alert
|
|
|
|
err = json.Unmarshal(content, &alerts)
|
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
assert.Len(t, alerts, 4)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerRunGroupThreshold() {
|
2022-10-17 12:17:23 +00:00
|
|
|
// test grouping by "size"
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2023-06-01 08:33:08 +00:00
|
|
|
|
|
|
|
t := s.T()
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// set groupwait
|
2023-06-01 08:33:08 +00:00
|
|
|
cfg := s.readconfig()
|
2022-04-19 17:12:23 +00:00
|
|
|
cfg.GroupThreshold = 4
|
2023-06-01 08:33:08 +00:00
|
|
|
s.writeconfig(cfg)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
pb, err := s.InitBroker(nil)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
tomb := tomb.Tomb{}
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// send data
|
2022-04-19 17:12:23 +00:00
|
|
|
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{}}
|
2023-06-05 10:55:03 +00:00
|
|
|
time.Sleep(time.Second)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// because of group threshold, we shouldn't have data yet
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoFileExists(t, "./out")
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
2023-06-05 10:55:03 +00:00
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
time.Sleep(time.Second)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// and now we should
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
2023-06-05 10:55:03 +00:00
|
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
var alerts []models.Alert
|
2023-06-05 10:55:03 +00:00
|
|
|
|
|
|
|
// two notifications, one with 4 alerts, one with 2 alerts
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
assert.Len(t, alerts, 4)
|
2023-06-05 10:55:03 +00:00
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, alerts, 2)
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.Equal(t, err, io.EOF)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerRunTimeThreshold() {
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2023-06-01 08:33:08 +00:00
|
|
|
|
|
|
|
t := s.T()
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// set groupwait
|
2023-06-01 08:33:08 +00:00
|
|
|
cfg := s.readconfig()
|
2022-10-17 12:17:23 +00:00
|
|
|
cfg.GroupWait = 1 * time.Second
|
2023-06-01 08:33:08 +00:00
|
|
|
s.writeconfig(cfg)
|
|
|
|
|
|
|
|
pb, err := s.InitBroker(nil)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
tomb := tomb.Tomb{}
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// send data
|
2022-04-19 17:12:23 +00:00
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// we shouldn't have data yet
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoFileExists(t, "./out")
|
|
|
|
time.Sleep(1 * time.Second)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// and now we should
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
var alerts []models.Alert
|
|
|
|
err = json.Unmarshal(content, &alerts)
|
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
assert.Len(t, alerts, 1)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
func (s *PluginSuite) TestBrokerRunSimple() {
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2023-06-01 08:33:08 +00:00
|
|
|
|
|
|
|
t := s.T()
|
2023-06-22 09:31:41 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
pb, err := s.InitBroker(nil)
|
2022-03-29 12:20:26 +00:00
|
|
|
assert.NoError(t, err)
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2023-06-01 08:33:08 +00:00
|
|
|
tomb := tomb.Tomb{}
|
2022-03-29 12:20:26 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
|
|
|
|
assert.NoFileExists(t, "./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
defer os.Remove("./out")
|
|
|
|
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
2023-06-05 10:55:03 +00:00
|
|
|
// make it wait a bit, CI can be slow
|
|
|
|
time.Sleep(time.Second)
|
2022-03-29 12:20:26 +00:00
|
|
|
|
2022-09-06 11:55:03 +00:00
|
|
|
content, err := os.ReadFile("./out")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
2023-06-05 10:55:03 +00:00
|
|
|
decoder := json.NewDecoder(bytes.NewReader(content))
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
var alerts []models.Alert
|
2023-06-05 10:55:03 +00:00
|
|
|
|
|
|
|
// two notifications, one alert each
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
2023-06-05 10:55:03 +00:00
|
|
|
assert.Len(t, alerts, 1)
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, alerts, 1)
|
|
|
|
|
|
|
|
err = decoder.Decode(&alerts)
|
|
|
|
assert.Equal(t, err, io.EOF)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|