|
@@ -5,11 +5,6 @@ package csplugin
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
"os"
|
|
"os"
|
|
- "os/exec"
|
|
|
|
- "path"
|
|
|
|
- "path/filepath"
|
|
|
|
- "reflect"
|
|
|
|
- "runtime"
|
|
|
|
"testing"
|
|
"testing"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
@@ -25,116 +20,39 @@ import (
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
)
|
|
)
|
|
|
|
|
|
-var testPath string
|
|
|
|
|
|
|
|
-func setPluginPermTo744(t *testing.T) {
|
|
|
|
- setPluginPermTo(t, "744")
|
|
|
|
|
|
+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)
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
-func setPluginPermTo722(t *testing.T) {
|
|
|
|
- setPluginPermTo(t, "722")
|
|
|
|
-}
|
|
|
|
|
|
+func (s *PluginSuite) readconfig() (PluginConfig) {
|
|
|
|
+ var config PluginConfig
|
|
|
|
+ t := s.T()
|
|
|
|
|
|
-func setPluginPermTo724(t *testing.T) {
|
|
|
|
- setPluginPermTo(t, "724")
|
|
|
|
-}
|
|
|
|
-func TestGetPluginNameAndTypeFromPath(t *testing.T) {
|
|
|
|
- setUp(t)
|
|
|
|
- defer tearDown(t)
|
|
|
|
- type args struct {
|
|
|
|
- path string
|
|
|
|
- }
|
|
|
|
- tests := []struct {
|
|
|
|
- name string
|
|
|
|
- args args
|
|
|
|
- want string
|
|
|
|
- want1 string
|
|
|
|
- expectedErr string
|
|
|
|
- }{
|
|
|
|
- {
|
|
|
|
- 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",
|
|
|
|
- },
|
|
|
|
- expectedErr: "plugin name ./tests/gitter is invalid. Name should be like {type-name}",
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- name: "valid plugin name, multiple dash",
|
|
|
|
- args: args{
|
|
|
|
- path: "./tests/notification-instant-slack",
|
|
|
|
- },
|
|
|
|
- want: "notification-instant",
|
|
|
|
- want1: "slack",
|
|
|
|
- },
|
|
|
|
- }
|
|
|
|
- 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)
|
|
|
|
|
|
+ orig, err := os.ReadFile(s.pluginConfig)
|
|
|
|
+ require.NoError(t, err,"unable to read config file %s", s.pluginConfig)
|
|
|
|
|
|
- assert.Equal(t, tc.want, got)
|
|
|
|
- assert.Equal(t, tc.want1, got1)
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
|
|
+ err = yaml.Unmarshal(orig, &config)
|
|
|
|
+ require.NoError(t, err,"unable to unmarshal config file")
|
|
|
|
+
|
|
|
|
+ return config
|
|
}
|
|
}
|
|
|
|
|
|
-func TestListFilesAtPath(t *testing.T) {
|
|
|
|
- setUp(t)
|
|
|
|
- defer tearDown(t)
|
|
|
|
- type args struct {
|
|
|
|
- path string
|
|
|
|
- }
|
|
|
|
- tests := []struct {
|
|
|
|
- name string
|
|
|
|
- args args
|
|
|
|
- want []string
|
|
|
|
- expectedErr string
|
|
|
|
- }{
|
|
|
|
- {
|
|
|
|
- name: "valid directory",
|
|
|
|
- args: args{
|
|
|
|
- path: testPath,
|
|
|
|
- },
|
|
|
|
- want: []string{
|
|
|
|
- filepath.Join(testPath, "notification-gitter"),
|
|
|
|
- filepath.Join(testPath, "slack"),
|
|
|
|
- },
|
|
|
|
- },
|
|
|
|
- {
|
|
|
|
- name: "invalid directory",
|
|
|
|
- args: args{
|
|
|
|
- path: "./foo/bar/",
|
|
|
|
- },
|
|
|
|
- expectedErr: "open ./foo/bar/: " + cstest.FileNotFoundMessage,
|
|
|
|
- },
|
|
|
|
- }
|
|
|
|
- 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)
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
|
|
+func (s *PluginSuite) writeconfig(config PluginConfig) {
|
|
|
|
+ t := s.T()
|
|
|
|
+ data, err := yaml.Marshal(&config)
|
|
|
|
+ require.NoError(t, err,"unable to marshal config file")
|
|
|
|
+
|
|
|
|
+ err = os.WriteFile(s.pluginConfig, data, 0644)
|
|
|
|
+ require.NoError(t, err,"unable to write config file %s", s.pluginConfig)
|
|
}
|
|
}
|
|
|
|
|
|
-func TestBrokerInit(t *testing.T) {
|
|
|
|
- if runtime.GOOS == "windows" {
|
|
|
|
- t.Skip("Skipping test on windows")
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerInit() {
|
|
tests := []struct {
|
|
tests := []struct {
|
|
name string
|
|
name string
|
|
action func(*testing.T)
|
|
action func(*testing.T)
|
|
@@ -143,28 +61,30 @@ func TestBrokerInit(t *testing.T) {
|
|
}{
|
|
}{
|
|
{
|
|
{
|
|
name: "valid config",
|
|
name: "valid config",
|
|
- action: setPluginPermTo744,
|
|
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "group writable binary",
|
|
name: "group writable binary",
|
|
expectedErr: "notification-dummy is world writable",
|
|
expectedErr: "notification-dummy is world writable",
|
|
- action: setPluginPermTo722,
|
|
|
|
|
|
+ action: s.permissionSetter(0o722),
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "group writable binary",
|
|
name: "group writable binary",
|
|
expectedErr: "notification-dummy is group writable",
|
|
expectedErr: "notification-dummy is group writable",
|
|
- action: setPluginPermTo724,
|
|
|
|
|
|
+ action: s.permissionSetter(0o724),
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "no plugin dir",
|
|
name: "no plugin dir",
|
|
expectedErr: cstest.FileNotFoundMessage,
|
|
expectedErr: cstest.FileNotFoundMessage,
|
|
- action: tearDown,
|
|
|
|
|
|
+ action: func(t *testing.T) {
|
|
|
|
+ err := os.RemoveAll(s.runDir)
|
|
|
|
+ require.NoError(t, err)
|
|
|
|
+ },
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "no plugin binary",
|
|
name: "no plugin binary",
|
|
expectedErr: "binary for plugin dummy_default not found",
|
|
expectedErr: "binary for plugin dummy_default not found",
|
|
action: func(t *testing.T) {
|
|
action: func(t *testing.T) {
|
|
- err := os.Remove(path.Join(testPath, "notification-dummy"))
|
|
|
|
|
|
+ err := os.Remove(s.pluginBinary)
|
|
require.NoError(t, err)
|
|
require.NoError(t, err)
|
|
},
|
|
},
|
|
},
|
|
},
|
|
@@ -174,7 +94,6 @@ func TestBrokerInit(t *testing.T) {
|
|
procCfg: csconfig.PluginCfg{
|
|
procCfg: csconfig.PluginCfg{
|
|
User: "123445555551122toto",
|
|
User: "123445555551122toto",
|
|
},
|
|
},
|
|
- action: setPluginPermTo744,
|
|
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "only specify group",
|
|
name: "only specify group",
|
|
@@ -182,7 +101,6 @@ func TestBrokerInit(t *testing.T) {
|
|
procCfg: csconfig.PluginCfg{
|
|
procCfg: csconfig.PluginCfg{
|
|
Group: "123445555551122toto",
|
|
Group: "123445555551122toto",
|
|
},
|
|
},
|
|
- action: setPluginPermTo744,
|
|
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "Fails to run as root",
|
|
name: "Fails to run as root",
|
|
@@ -191,7 +109,6 @@ func TestBrokerInit(t *testing.T) {
|
|
User: "root",
|
|
User: "root",
|
|
Group: "root",
|
|
Group: "root",
|
|
},
|
|
},
|
|
- action: setPluginPermTo744,
|
|
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "Invalid user and group",
|
|
name: "Invalid user and group",
|
|
@@ -200,7 +117,6 @@ func TestBrokerInit(t *testing.T) {
|
|
User: "toto1234",
|
|
User: "toto1234",
|
|
Group: "toto1234",
|
|
Group: "toto1234",
|
|
},
|
|
},
|
|
- action: setPluginPermTo744,
|
|
|
|
},
|
|
},
|
|
{
|
|
{
|
|
name: "Valid user and invalid group",
|
|
name: "Valid user and invalid group",
|
|
@@ -209,79 +125,33 @@ func TestBrokerInit(t *testing.T) {
|
|
User: "nobody",
|
|
User: "nobody",
|
|
Group: "toto1234",
|
|
Group: "toto1234",
|
|
},
|
|
},
|
|
- action: setPluginPermTo744,
|
|
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
for _, tc := range tests {
|
|
for _, tc := range tests {
|
|
tc := tc
|
|
tc := tc
|
|
- t.Run(tc.name, func(t *testing.T) {
|
|
|
|
- defer tearDown(t)
|
|
|
|
- buildDummyPlugin(t)
|
|
|
|
|
|
+ s.Run(tc.name, func() {
|
|
|
|
+ t := s.T()
|
|
if tc.action != nil {
|
|
if tc.action != nil {
|
|
tc.action(t)
|
|
tc.action(t)
|
|
}
|
|
}
|
|
- pb := PluginBroker{}
|
|
|
|
- profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
- profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
- Notifications: []string{"dummy_default"},
|
|
|
|
- })
|
|
|
|
- err := pb.Init(&tc.procCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
- defer pb.Kill()
|
|
|
|
|
|
+ _, err := s.InitBroker(&tc.procCfg)
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
cstest.RequireErrorContains(t, err, tc.expectedErr)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func readconfig(t *testing.T, path string) ([]byte, PluginConfig) {
|
|
|
|
- var config PluginConfig
|
|
|
|
- orig, err := os.ReadFile("tests/notifications/dummy.yaml")
|
|
|
|
- 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")
|
|
|
|
-
|
|
|
|
- return orig, config
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func writeconfig(t *testing.T, config PluginConfig, path string) {
|
|
|
|
- data, err := yaml.Marshal(&config)
|
|
|
|
- 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)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func TestBrokerNoThreshold(t *testing.T) {
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerNoThreshold() {
|
|
var alerts []models.Alert
|
|
var alerts []models.Alert
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
|
|
|
|
- buildDummyPlugin(t)
|
|
|
|
- setPluginPermTo744(t)
|
|
|
|
- defer tearDown(t)
|
|
|
|
-
|
|
|
|
- // init
|
|
|
|
- pluginCfg := csconfig.PluginCfg{}
|
|
|
|
- pb := PluginBroker{}
|
|
|
|
- profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
- profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
- Notifications: []string{"dummy_default"},
|
|
|
|
- })
|
|
|
|
-
|
|
|
|
- // default config
|
|
|
|
- err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
|
|
+ t := s.T()
|
|
|
|
|
|
|
|
+ pb, err := s.InitBroker(nil)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
- tomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
|
|
+ tomb := tomb.Tomb{}
|
|
go pb.Run(&tomb)
|
|
go pb.Run(&tomb)
|
|
- defer pb.Kill()
|
|
|
|
|
|
|
|
// send one item, it should be processed right now
|
|
// send one item, it should be processed right now
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
@@ -292,7 +162,7 @@ func TestBrokerNoThreshold(t *testing.T) {
|
|
require.NoError(t, err, "Error reading file")
|
|
require.NoError(t, err, "Error reading file")
|
|
|
|
|
|
err = json.Unmarshal(content, &alerts)
|
|
err = json.Unmarshal(content, &alerts)
|
|
- assert.NoError(t, err)
|
|
|
|
|
|
+ require.NoError(t, err)
|
|
assert.Len(t, alerts, 1)
|
|
assert.Len(t, alerts, 1)
|
|
|
|
|
|
// remove it
|
|
// remove it
|
|
@@ -313,34 +183,24 @@ func TestBrokerNoThreshold(t *testing.T) {
|
|
assert.Len(t, alerts, 1)
|
|
assert.Len(t, alerts, 1)
|
|
}
|
|
}
|
|
|
|
|
|
-func TestBrokerRunGroupAndTimeThreshold_TimeFirst(t *testing.T) {
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_TimeFirst() {
|
|
// test grouping by "time"
|
|
// test grouping by "time"
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
- buildDummyPlugin(t)
|
|
|
|
- setPluginPermTo744(t)
|
|
|
|
- defer tearDown(t)
|
|
|
|
-
|
|
|
|
- // init
|
|
|
|
- pluginCfg := csconfig.PluginCfg{}
|
|
|
|
- pb := PluginBroker{}
|
|
|
|
- profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
- profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
- Notifications: []string{"dummy_default"},
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
|
|
+ t := s.T()
|
|
|
|
+
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
|
- raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
|
|
+ cfg := s.readconfig()
|
|
cfg.GroupThreshold = 4
|
|
cfg.GroupThreshold = 4
|
|
cfg.GroupWait = 1 * time.Second
|
|
cfg.GroupWait = 1 * time.Second
|
|
- writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
- err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
|
|
+ s.writeconfig(cfg)
|
|
|
|
+
|
|
|
|
+ pb, err := s.InitBroker(nil)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
- tomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
|
|
+ tomb := tomb.Tomb{}
|
|
go pb.Run(&tomb)
|
|
go pb.Run(&tomb)
|
|
- defer pb.Kill()
|
|
|
|
|
|
+
|
|
// send data
|
|
// send data
|
|
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{}}
|
|
@@ -357,40 +217,24 @@ func TestBrokerRunGroupAndTimeThreshold_TimeFirst(t *testing.T) {
|
|
err = json.Unmarshal(content, &alerts)
|
|
err = json.Unmarshal(content, &alerts)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, alerts, 3)
|
|
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")
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func TestBrokerRunGroupAndTimeThreshold_CountFirst(t *testing.T) {
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerRunGroupAndTimeThreshold_CountFirst() {
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
- buildDummyPlugin(t)
|
|
|
|
- setPluginPermTo(t, "744")
|
|
|
|
- defer tearDown(t)
|
|
|
|
-
|
|
|
|
- // init
|
|
|
|
- pluginCfg := csconfig.PluginCfg{}
|
|
|
|
- pb := PluginBroker{}
|
|
|
|
- profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
- profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
- Notifications: []string{"dummy_default"},
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
|
|
+ t := s.T()
|
|
|
|
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
|
// set groupwait and groupthreshold, should honor whichever comes first
|
|
- raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
|
|
+ cfg := s.readconfig()
|
|
cfg.GroupThreshold = 4
|
|
cfg.GroupThreshold = 4
|
|
cfg.GroupWait = 4 * time.Second
|
|
cfg.GroupWait = 4 * time.Second
|
|
- writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
- err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
|
|
+ s.writeconfig(cfg)
|
|
|
|
+
|
|
|
|
+ pb, err := s.InitBroker(nil)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
- tomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
|
|
+ tomb := tomb.Tomb{}
|
|
go pb.Run(&tomb)
|
|
go pb.Run(&tomb)
|
|
- defer pb.Kill()
|
|
|
|
|
|
|
|
// send data
|
|
// send data
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
@@ -411,41 +255,24 @@ func TestBrokerRunGroupAndTimeThreshold_CountFirst(t *testing.T) {
|
|
err = json.Unmarshal(content, &alerts)
|
|
err = json.Unmarshal(content, &alerts)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, alerts, 4)
|
|
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")
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func TestBrokerRunGroupThreshold(t *testing.T) {
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerRunGroupThreshold() {
|
|
// test grouping by "size"
|
|
// test grouping by "size"
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
- buildDummyPlugin(t)
|
|
|
|
- setPluginPermTo(t, "744")
|
|
|
|
- defer tearDown(t)
|
|
|
|
-
|
|
|
|
- // init
|
|
|
|
- pluginCfg := csconfig.PluginCfg{}
|
|
|
|
- pb := PluginBroker{}
|
|
|
|
- profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
- profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
- Notifications: []string{"dummy_default"},
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
|
|
+ t := s.T()
|
|
|
|
|
|
// set groupwait
|
|
// set groupwait
|
|
- raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
|
|
+ cfg := s.readconfig()
|
|
cfg.GroupThreshold = 4
|
|
cfg.GroupThreshold = 4
|
|
- writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
- err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
|
|
+ s.writeconfig(cfg)
|
|
|
|
|
|
|
|
+ pb, err := s.InitBroker(nil)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
- tomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
|
|
+ tomb := tomb.Tomb{}
|
|
go pb.Run(&tomb)
|
|
go pb.Run(&tomb)
|
|
- defer pb.Kill()
|
|
|
|
|
|
|
|
// send data
|
|
// send data
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
@@ -466,39 +293,23 @@ func TestBrokerRunGroupThreshold(t *testing.T) {
|
|
err = json.Unmarshal(content, &alerts)
|
|
err = json.Unmarshal(content, &alerts)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, alerts, 4)
|
|
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")
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func TestBrokerRunTimeThreshold(t *testing.T) {
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerRunTimeThreshold() {
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
- buildDummyPlugin(t)
|
|
|
|
- setPluginPermTo(t, "744")
|
|
|
|
- defer tearDown(t)
|
|
|
|
-
|
|
|
|
- // init
|
|
|
|
- pluginCfg := csconfig.PluginCfg{}
|
|
|
|
- pb := PluginBroker{}
|
|
|
|
- profiles := csconfig.NewDefaultConfig().API.Server.Profiles
|
|
|
|
- profiles = append(profiles, &csconfig.ProfileCfg{
|
|
|
|
- Notifications: []string{"dummy_default"},
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
|
|
+ t := s.T()
|
|
|
|
|
|
// set groupwait
|
|
// set groupwait
|
|
- raw, cfg := readconfig(t, "tests/notifications/dummy.yaml")
|
|
|
|
|
|
+ cfg := s.readconfig()
|
|
cfg.GroupWait = 1 * time.Second
|
|
cfg.GroupWait = 1 * time.Second
|
|
- writeconfig(t, cfg, "tests/notifications/dummy.yaml")
|
|
|
|
- err := pb.Init(&pluginCfg, profiles, &csconfig.ConfigurationPaths{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
|
|
+ s.writeconfig(cfg)
|
|
|
|
+
|
|
|
|
+ pb, err := s.InitBroker(nil)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
- tomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
|
|
+ tomb := tomb.Tomb{}
|
|
go pb.Run(&tomb)
|
|
go pb.Run(&tomb)
|
|
- defer pb.Kill()
|
|
|
|
|
|
|
|
// send data
|
|
// send data
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
pb.PluginChannel <- ProfileAlert{ProfileID: uint(0), Alert: &models.Alert{}}
|
|
@@ -516,32 +327,18 @@ func TestBrokerRunTimeThreshold(t *testing.T) {
|
|
err = json.Unmarshal(content, &alerts)
|
|
err = json.Unmarshal(content, &alerts)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, alerts, 1)
|
|
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)
|
|
|
|
}
|
|
}
|
|
|
|
|
|
-func TestBrokerRunSimple(t *testing.T) {
|
|
|
|
|
|
+func (s *PluginSuite) TestBrokerRunSimple() {
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
DefaultEmptyTicker = 50 * time.Millisecond
|
|
- buildDummyPlugin(t)
|
|
|
|
- setPluginPermTo(t, "744")
|
|
|
|
- defer tearDown(t)
|
|
|
|
- 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{
|
|
|
|
- PluginDir: testPath,
|
|
|
|
- NotificationDir: "./tests/notifications",
|
|
|
|
- })
|
|
|
|
|
|
+
|
|
|
|
+ t := s.T()
|
|
|
|
+
|
|
|
|
+ pb, err := s.InitBroker(nil)
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
- tomb := tomb.Tomb{}
|
|
|
|
|
|
|
|
|
|
+ tomb := tomb.Tomb{}
|
|
go pb.Run(&tomb)
|
|
go pb.Run(&tomb)
|
|
- defer pb.Kill()
|
|
|
|
|
|
|
|
assert.NoFileExists(t, "./out")
|
|
assert.NoFileExists(t, "./out")
|
|
|
|
|
|
@@ -559,47 +356,3 @@ func TestBrokerRunSimple(t *testing.T) {
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, alerts, 2)
|
|
assert.Len(t, alerts, 2)
|
|
}
|
|
}
|
|
-
|
|
|
|
-func buildDummyPlugin(t *testing.T) {
|
|
|
|
- dir, err := os.MkdirTemp("./tests", "cs_plugin_test")
|
|
|
|
- require.NoError(t, err)
|
|
|
|
-
|
|
|
|
- cmd := exec.Command("go", "build", "-o", path.Join(dir, "notification-dummy"), "../../plugins/notifications/dummy/")
|
|
|
|
- err = cmd.Run()
|
|
|
|
- require.NoError(t, err, "while building dummy plugin")
|
|
|
|
-
|
|
|
|
- testPath = dir
|
|
|
|
- os.Remove("./out")
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func setPluginPermTo(t *testing.T, perm string) {
|
|
|
|
- if runtime.GOOS != "windows" {
|
|
|
|
- err := exec.Command("chmod", perm, path.Join(testPath, "notification-dummy")).Run()
|
|
|
|
- require.NoError(t, err, "chmod 744 %s", path.Join(testPath, "notification-dummy"))
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func setUp(t *testing.T) {
|
|
|
|
- dir, err := os.MkdirTemp("./", "cs_plugin_test")
|
|
|
|
- require.NoError(t, err)
|
|
|
|
-
|
|
|
|
- f, err := os.Create(path.Join(dir, "slack"))
|
|
|
|
- require.NoError(t, err)
|
|
|
|
-
|
|
|
|
- f.Close()
|
|
|
|
- f, err = os.Create(path.Join(dir, "notification-gitter"))
|
|
|
|
- require.NoError(t, err)
|
|
|
|
-
|
|
|
|
- f.Close()
|
|
|
|
- err = os.Mkdir(path.Join(dir, "dummy_dir"), 0666)
|
|
|
|
- require.NoError(t, err)
|
|
|
|
-
|
|
|
|
- testPath = dir
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func tearDown(t *testing.T) {
|
|
|
|
- err := os.RemoveAll(testPath)
|
|
|
|
- require.NoError(t, err)
|
|
|
|
-
|
|
|
|
- os.Remove("./out")
|
|
|
|
-}
|
|
|