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 (
|
2022-04-19 17:12:23 +00:00
|
|
|
"encoding/json"
|
2021-08-25 09:43:29 +00:00
|
|
|
"os"
|
2022-03-29 12:20:26 +00:00
|
|
|
"os/exec"
|
2021-08-25 09:43:29 +00:00
|
|
|
"path"
|
2022-05-17 10:14:59 +00:00
|
|
|
"path/filepath"
|
2021-08-25 09:43:29 +00:00
|
|
|
"reflect"
|
2022-05-17 10:14:59 +00:00
|
|
|
"runtime"
|
2021-08-25 09:43:29 +00:00
|
|
|
"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-05-25 13:37:44 +00:00
|
|
|
"github.com/crowdsecurity/go-cs-lib/pkg/cstest"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
var testPath string
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func setPluginPermTo744(t *testing.T) {
|
|
|
|
setPluginPermTo(t, "744")
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func setPluginPermTo722(t *testing.T) {
|
|
|
|
setPluginPermTo(t, "722")
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func setPluginPermTo724(t *testing.T) {
|
|
|
|
setPluginPermTo(t, "724")
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
func TestGetPluginNameAndTypeFromPath(t *testing.T) {
|
2022-10-17 12:17:23 +00:00
|
|
|
setUp(t)
|
|
|
|
defer tearDown(t)
|
2021-08-25 09:43:29 +00:00
|
|
|
type args struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
want1 string
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr string
|
2021-08-25 09:43:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid plugin name, single dash",
|
|
|
|
args: args{
|
|
|
|
path: path.Join(testPath, "notification-gitter"),
|
|
|
|
},
|
|
|
|
want: "notification",
|
|
|
|
want1: "gitter",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid plugin name",
|
|
|
|
args: args{
|
|
|
|
path: "./tests/gitter",
|
|
|
|
},
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "plugin name ./tests/gitter is invalid. Name should be like {type-name}",
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid plugin name, multiple dash",
|
|
|
|
args: args{
|
|
|
|
path: "./tests/notification-instant-slack",
|
|
|
|
},
|
|
|
|
want: "notification-instant",
|
|
|
|
want1: "slack",
|
|
|
|
},
|
|
|
|
}
|
2022-10-17 12:17:23 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got, got1, err := getPluginTypeAndSubtypeFromPath(tc.args.path)
|
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
assert.Equal(t, tc.want1, got1)
|
2021-08-25 09:43:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
func TestListFilesAtPath(t *testing.T) {
|
2022-10-17 12:17:23 +00:00
|
|
|
setUp(t)
|
|
|
|
defer tearDown(t)
|
2021-08-25 09:43:29 +00:00
|
|
|
type args struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want []string
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr string
|
2021-08-25 09:43:29 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid directory",
|
|
|
|
args: args{
|
|
|
|
path: testPath,
|
|
|
|
},
|
|
|
|
want: []string{
|
2022-05-17 10:14:59 +00:00
|
|
|
filepath.Join(testPath, "notification-gitter"),
|
|
|
|
filepath.Join(testPath, "slack"),
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid directory",
|
|
|
|
args: args{
|
|
|
|
path: "./foo/bar/",
|
|
|
|
},
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "open ./foo/bar/: " + cstest.FileNotFoundMessage,
|
2021-08-25 09:43:29 +00:00
|
|
|
},
|
|
|
|
}
|
2022-10-17 12:17:23 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got, err := listFilesAtPath(tc.args.path)
|
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(got, tc.want) {
|
|
|
|
t.Errorf("listFilesAtPath() = %v, want %v", got, tc.want)
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
func TestBrokerInit(t *testing.T) {
|
2022-05-17 10:14:59 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("Skipping test on windows")
|
|
|
|
}
|
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
|
|
|
}{
|
|
|
|
{
|
2022-10-17 12:17:23 +00:00
|
|
|
name: "valid config",
|
|
|
|
action: setPluginPermTo744,
|
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",
|
2022-03-29 12:20:26 +00:00
|
|
|
action: setPluginPermTo722,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "group writable binary",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: "notification-dummy is group writable",
|
2022-03-29 12:20:26 +00:00
|
|
|
action: setPluginPermTo724,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no plugin dir",
|
2022-10-17 12:17:23 +00:00
|
|
|
expectedErr: cstest.FileNotFoundMessage,
|
2022-03-29 12:20:26 +00:00
|
|
|
action: tearDown,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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) {
|
2022-03-29 12:20:26 +00:00
|
|
|
err := os.Remove(path.Join(testPath, "notification-dummy"))
|
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",
|
|
|
|
},
|
|
|
|
action: setPluginPermTo744,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
action: setPluginPermTo744,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
action: setPluginPermTo744,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
action: setPluginPermTo744,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
action: setPluginPermTo744,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
for _, tc := range tests {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
defer tearDown(t)
|
|
|
|
buildDummyPlugin(t)
|
|
|
|
if tc.action != nil {
|
|
|
|
tc.action(t)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
err := pb.Init(&tc.procCfg, profiles, &csconfig.ConfigurationPaths{
|
2022-03-29 12:20:26 +00:00
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
|
|
|
defer pb.Kill()
|
2022-10-17 12:17:23 +00:00
|
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
2022-03-29 12:20:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
func readconfig(t *testing.T, path string) ([]byte, PluginConfig) {
|
|
|
|
var config PluginConfig
|
2022-09-06 11:55:03 +00:00
|
|
|
orig, err := os.ReadFile("tests/notifications/dummy.yaml")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err,"unable to read config file %s", path)
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(orig, &config)
|
|
|
|
require.NoError(t, err,"unable to unmarshal config file")
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
return orig, config
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeconfig(t *testing.T, config PluginConfig, path string) {
|
|
|
|
data, err := yaml.Marshal(&config)
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err,"unable to marshal config file")
|
|
|
|
|
|
|
|
err = os.WriteFile(path, data, 0644)
|
|
|
|
require.NoError(t, err,"unable to write config file %s", path)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrokerNoThreshold(t *testing.T) {
|
|
|
|
var alerts []models.Alert
|
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
buildDummyPlugin(t)
|
|
|
|
setPluginPermTo744(t)
|
|
|
|
defer tearDown(t)
|
|
|
|
|
|
|
|
// init
|
2022-04-19 17:12:23 +00:00
|
|
|
pluginCfg := csconfig.PluginCfg{}
|
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// default config
|
2022-04-19 17:12:23 +00:00
|
|
|
err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
tomb := tomb.Tomb{}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
defer pb.Kill()
|
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)
|
|
|
|
assert.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
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrokerRunGroupAndTimeThreshold_TimeFirst(t *testing.T) {
|
2022-10-17 12:17:23 +00:00
|
|
|
// test grouping by "time"
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2022-10-17 12:17:23 +00:00
|
|
|
buildDummyPlugin(t)
|
|
|
|
setPluginPermTo744(t)
|
|
|
|
defer tearDown(t)
|
2022-04-19 17:12:23 +00:00
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
// init
|
2022-04-19 17:12:23 +00:00
|
|
|
pluginCfg := csconfig.PluginCfg{}
|
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
2022-04-19 17:12:23 +00:00
|
|
|
raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
cfg.GroupThreshold = 4
|
|
|
|
cfg.GroupWait = 1 * time.Second
|
|
|
|
writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tomb := tomb.Tomb{}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
defer pb.Kill()
|
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)
|
|
|
|
|
|
|
|
// restore config
|
|
|
|
err = os.WriteFile("tests/notifications/dummy.yaml", raw, 0644)
|
|
|
|
require.NoError(t, err,"unable to write config file")
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrokerRunGroupAndTimeThreshold_CountFirst(t *testing.T) {
|
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2022-10-17 12:17:23 +00:00
|
|
|
buildDummyPlugin(t)
|
|
|
|
setPluginPermTo(t, "744")
|
|
|
|
defer tearDown(t)
|
|
|
|
|
|
|
|
// init
|
2022-04-19 17:12:23 +00:00
|
|
|
pluginCfg := csconfig.PluginCfg{}
|
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
2022-04-19 17:12:23 +00:00
|
|
|
raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
cfg.GroupThreshold = 4
|
|
|
|
cfg.GroupWait = 4 * time.Second
|
|
|
|
writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tomb := tomb.Tomb{}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
defer pb.Kill()
|
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)
|
|
|
|
|
|
|
|
// restore config
|
|
|
|
err = os.WriteFile("tests/notifications/dummy.yaml", raw, 0644)
|
|
|
|
require.NoError(t, err,"unable to write config file")
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrokerRunGroupThreshold(t *testing.T) {
|
2022-10-17 12:17:23 +00:00
|
|
|
// test grouping by "size"
|
2022-04-19 17:12:23 +00:00
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2022-10-17 12:17:23 +00:00
|
|
|
buildDummyPlugin(t)
|
|
|
|
setPluginPermTo(t, "744")
|
|
|
|
defer tearDown(t)
|
|
|
|
|
|
|
|
// init
|
2022-04-19 17:12:23 +00:00
|
|
|
pluginCfg := csconfig.PluginCfg{}
|
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// set groupwait
|
2022-04-19 17:12:23 +00:00
|
|
|
raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
cfg.GroupThreshold = 4
|
|
|
|
writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
tomb := tomb.Tomb{}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
defer pb.Kill()
|
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)
|
|
|
|
|
|
|
|
// restore config
|
|
|
|
err = os.WriteFile("tests/notifications/dummy.yaml", raw, 0644)
|
|
|
|
require.NoError(t, err, "unable to write config file")
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrokerRunTimeThreshold(t *testing.T) {
|
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2022-10-17 12:17:23 +00:00
|
|
|
buildDummyPlugin(t)
|
|
|
|
setPluginPermTo(t, "744")
|
|
|
|
defer tearDown(t)
|
|
|
|
|
|
|
|
// init
|
2022-04-19 17:12:23 +00:00
|
|
|
pluginCfg := csconfig.PluginCfg{}
|
2022-03-29 12:20:26 +00:00
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
2022-10-17 12:17:23 +00:00
|
|
|
|
|
|
|
// set groupwait
|
2022-04-19 17:12:23 +00:00
|
|
|
raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
2022-10-17 12:17:23 +00:00
|
|
|
cfg.GroupWait = 1 * time.Second
|
2022-04-19 17:12:23 +00:00
|
|
|
writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tomb := tomb.Tomb{}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
defer pb.Kill()
|
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)
|
|
|
|
|
|
|
|
// restore config
|
|
|
|
err = os.WriteFile("tests/notifications/dummy.yaml", raw, 0644)
|
|
|
|
require.NoError(t, err, "unable to write config file %s", err)
|
2022-04-19 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBrokerRunSimple(t *testing.T) {
|
|
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
2022-10-17 12:17:23 +00:00
|
|
|
buildDummyPlugin(t)
|
|
|
|
setPluginPermTo(t, "744")
|
|
|
|
defer tearDown(t)
|
2022-04-19 17:12:23 +00:00
|
|
|
pluginCfg := csconfig.PluginCfg{}
|
|
|
|
pb := PluginBroker{}
|
|
|
|
profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
Notifications: []string{"dummy_default"},
|
|
|
|
})
|
|
|
|
err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
2022-03-29 12:20:26 +00:00
|
|
|
PluginDir: testPath,
|
|
|
|
NotificationDir: "./tests/notifications",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tomb := tomb.Tomb{}
|
2022-10-17 12:17:23 +00:00
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
go pb.Run(&tomb)
|
|
|
|
defer pb.Kill()
|
|
|
|
|
|
|
|
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{}}
|
2022-04-19 17:12:23 +00:00
|
|
|
time.Sleep(time.Millisecond * 200)
|
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")
|
|
|
|
|
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, 2)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func buildDummyPlugin(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
dir, err := os.MkdirTemp("./tests", "cs_plugin_test")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
cmd := exec.Command("go", "build", "-o", path.Join(dir, "notification-dummy"), "../../plugins/notifications/dummy/")
|
2022-10-17 12:17:23 +00:00
|
|
|
err = cmd.Run()
|
|
|
|
require.NoError(t, err, "while building dummy plugin")
|
|
|
|
|
2022-03-29 12:20:26 +00:00
|
|
|
testPath = dir
|
2022-04-19 17:12:23 +00:00
|
|
|
os.Remove("./out")
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func setPluginPermTo(t *testing.T, perm string) {
|
2022-05-17 10:14:59 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
2022-10-17 12:17:23 +00:00
|
|
|
err := exec.Command("chmod", perm, path.Join(testPath, "notification-dummy")).Run()
|
|
|
|
require.NoError(t, err, "chmod 744 %s", path.Join(testPath, "notification-dummy"))
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func setUp(t *testing.T) {
|
2022-03-29 12:20:26 +00:00
|
|
|
dir, err := os.MkdirTemp("./", "cs_plugin_test")
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
f, err := os.Create(path.Join(dir, "slack"))
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
f.Close()
|
|
|
|
f, err = os.Create(path.Join(dir, "notification-gitter"))
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-17 10:14:59 +00:00
|
|
|
f.Close()
|
2021-08-25 09:43:29 +00:00
|
|
|
err = os.Mkdir(path.Join(dir, "dummy_dir"), 0666)
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
testPath = dir
|
|
|
|
}
|
|
|
|
|
2022-10-17 12:17:23 +00:00
|
|
|
func tearDown(t *testing.T) {
|
2021-08-25 09:43:29 +00:00
|
|
|
err := os.RemoveAll(testPath)
|
2022-10-17 12:17:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-19 17:12:23 +00:00
|
|
|
os.Remove("./out")
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|