File: watcher_test.go

package info (click to toggle)
crowdsec 1.4.6-10.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,500 kB
  • sloc: sh: 2,870; makefile: 386; python: 74
file content (117 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package csplugin

import (
	"context"
	"log"
	"runtime"
	"testing"
	"time"

	"github.com/crowdsecurity/crowdsec/pkg/models"
	"gopkg.in/tomb.v2"
	"gotest.tools/v3/assert"
)

var ctx = context.Background()

func resetTestTomb(testTomb *tomb.Tomb, pw *PluginWatcher) {
	testTomb.Kill(nil)
	<-pw.PluginEvents
	if err := testTomb.Wait(); err != nil {
		log.Fatal(err)
	}
}

func resetWatcherAlertCounter(pw *PluginWatcher) {
	pw.AlertCountByPluginName.Lock()
	for k := range pw.AlertCountByPluginName.data {
		pw.AlertCountByPluginName.data[k] = 0
	}
	pw.AlertCountByPluginName.Unlock()
}

func insertNAlertsToPlugin(pw *PluginWatcher, n int, pluginName string) {
	for i := 0; i < n; i++ {
		pw.Inserts <- pluginName
	}
}

func listenChannelWithTimeout(ctx context.Context, channel chan string) error {
	select {
	case x := <-channel:
		log.Printf("received -> %v", x)
	case <-ctx.Done():
		return ctx.Err()
	}
	return nil
}

func TestPluginWatcherInterval(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("Skipping test on windows because timing is not reliable")
	}
	pw := PluginWatcher{}
	alertsByPluginName := make(map[string][]*models.Alert)
	testTomb := tomb.Tomb{}
	configs := map[string]PluginConfig{
		"testPlugin": {
			GroupWait: time.Millisecond,
		},
	}
	pw.Init(configs, alertsByPluginName)
	pw.Start(&testTomb)

	ct, cancel := context.WithTimeout(ctx, time.Microsecond)
	defer cancel()
	err := listenChannelWithTimeout(ct, pw.PluginEvents)
	assert.ErrorContains(t, err, "context deadline exceeded")
	resetTestTomb(&testTomb, &pw)
	testTomb = tomb.Tomb{}
	pw.Start(&testTomb)

	ct, cancel = context.WithTimeout(ctx, time.Millisecond*5)
	defer cancel()
	err = listenChannelWithTimeout(ct, pw.PluginEvents)
	assert.NilError(t, err)
	resetTestTomb(&testTomb, &pw)
	// This is to avoid the int complaining
}

func TestPluginAlertCountWatcher(t *testing.T) {
	if runtime.GOOS == "windows" {
		t.Skip("Skipping test on windows because timing is not reliable")
	}
	pw := PluginWatcher{}
	alertsByPluginName := make(map[string][]*models.Alert)
	configs := map[string]PluginConfig{
		"testPlugin": {
			GroupThreshold: 5,
		},
	}
	testTomb := tomb.Tomb{}
	pw.Init(configs, alertsByPluginName)
	pw.Start(&testTomb)

	// Channel won't contain any events since threshold is not crossed.
	ct, cancel := context.WithTimeout(ctx, time.Second)
	defer cancel()
	err := listenChannelWithTimeout(ct, pw.PluginEvents)
	assert.ErrorContains(t, err, "context deadline exceeded")

	// Channel won't contain any events since threshold is not crossed.
	resetWatcherAlertCounter(&pw)
	insertNAlertsToPlugin(&pw, 4, "testPlugin")
	ct, cancel = context.WithTimeout(ctx, time.Second)
	defer cancel()
	err = listenChannelWithTimeout(ct, pw.PluginEvents)
	assert.ErrorContains(t, err, "context deadline exceeded")

	// Channel will contain an event since threshold is crossed.
	resetWatcherAlertCounter(&pw)
	insertNAlertsToPlugin(&pw, 5, "testPlugin")
	ct, cancel = context.WithTimeout(ctx, time.Second)
	defer cancel()
	err = listenChannelWithTimeout(ct, pw.PluginEvents)
	assert.NilError(t, err)
	resetTestTomb(&testTomb, &pw)
}