watcher_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package csplugin
  2. import (
  3. "context"
  4. "log"
  5. "testing"
  6. "time"
  7. "github.com/crowdsecurity/crowdsec/pkg/models"
  8. "gopkg.in/tomb.v2"
  9. "gotest.tools/v3/assert"
  10. )
  11. var ctx = context.Background()
  12. func resetTestTomb(testTomb *tomb.Tomb) {
  13. testTomb.Kill(nil)
  14. if err := testTomb.Wait(); err != nil {
  15. log.Fatal(err)
  16. }
  17. }
  18. func resetWatcherAlertCounter(pw *PluginWatcher) {
  19. for k := range pw.AlertCountByPluginName {
  20. pw.AlertCountByPluginName[k] = 0
  21. }
  22. }
  23. func insertNAlertsToPlugin(pw *PluginWatcher, n int, pluginName string) {
  24. for i := 0; i < n; i++ {
  25. pw.Inserts <- pluginName
  26. }
  27. }
  28. func listenChannelWithTimeout(ctx context.Context, channel chan string) error {
  29. select {
  30. case <-channel:
  31. case <-ctx.Done():
  32. return ctx.Err()
  33. }
  34. return nil
  35. }
  36. func TestPluginWatcherInterval(t *testing.T) {
  37. pw := PluginWatcher{}
  38. alertsByPluginName := make(map[string][]*models.Alert)
  39. testTomb := tomb.Tomb{}
  40. configs := map[string]PluginConfig{
  41. "testPlugin": {
  42. GroupWait: time.Millisecond,
  43. },
  44. }
  45. pw.Init(configs, alertsByPluginName)
  46. pw.Start(&testTomb)
  47. ct, cancel := context.WithTimeout(ctx, time.Microsecond)
  48. defer cancel()
  49. err := listenChannelWithTimeout(ct, pw.PluginEvents)
  50. assert.ErrorContains(t, err, "context deadline exceeded")
  51. resetTestTomb(&testTomb)
  52. testTomb = tomb.Tomb{}
  53. pw.Start(&testTomb)
  54. ct, cancel = context.WithTimeout(ctx, time.Millisecond*5)
  55. defer cancel()
  56. err = listenChannelWithTimeout(ct, pw.PluginEvents)
  57. assert.NilError(t, err)
  58. resetTestTomb(&testTomb)
  59. // This is to avoid the int complaining
  60. }
  61. func TestPluginAlertCountWatcher(t *testing.T) {
  62. pw := PluginWatcher{}
  63. alertsByPluginName := make(map[string][]*models.Alert)
  64. configs := map[string]PluginConfig{
  65. "testPlugin": {
  66. GroupThreshold: 5,
  67. },
  68. }
  69. testTomb := tomb.Tomb{}
  70. pw.Init(configs, alertsByPluginName)
  71. pw.Start(&testTomb)
  72. // Channel won't contain any events since threshold is not crossed.
  73. ct, cancel := context.WithTimeout(ctx, time.Second)
  74. defer cancel()
  75. err := listenChannelWithTimeout(ct, pw.PluginEvents)
  76. assert.ErrorContains(t, err, "context deadline exceeded")
  77. // Channel won't contain any events since threshold is not crossed.
  78. resetWatcherAlertCounter(&pw)
  79. insertNAlertsToPlugin(&pw, 4, "testPlugin")
  80. ct, cancel = context.WithTimeout(ctx, time.Second)
  81. defer cancel()
  82. err = listenChannelWithTimeout(ct, pw.PluginEvents)
  83. assert.ErrorContains(t, err, "context deadline exceeded")
  84. // Channel will contain an event since threshold is crossed.
  85. resetWatcherAlertCounter(&pw)
  86. insertNAlertsToPlugin(&pw, 5, "testPlugin")
  87. ct, cancel = context.WithTimeout(ctx, time.Second)
  88. defer cancel()
  89. err = listenChannelWithTimeout(ct, pw.PluginEvents)
  90. assert.NilError(t, err)
  91. resetTestTomb(&testTomb)
  92. }