Repository navigation
Expand file tree
/
Copy pathwatcher_test.go
More file actions
165 lines (128 loc) · 4.1 KB
/
Copy pathwatcher_test.go
File metadata and controls
165 lines (128 loc) · 4.1 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package csplugin
import (
"context"
"log"
"testing"
"time"
"github.com/stretchr/testify/require"
"gopkg.in/tomb.v2"
"github.com/crowdsecurity/go-cs-lib/cstest"
"github.com/crowdsecurity/crowdsec/pkg/models"
)
func resetTestTomb(t *testing.T, testTomb *tomb.Tomb, pw *PluginWatcher) {
testTomb.Kill(nil)
<-pw.PluginEvents
err := testTomb.Wait()
require.NoError(t, 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 range n {
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) {
cstest.SkipOnWindowsBecause(t, "timing is not reliable")
ctx := t.Context()
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)
cstest.RequireErrorContains(t, err, "context deadline exceeded")
resetTestTomb(t, &testTomb, &pw)
testTomb = tomb.Tomb{}
pw.Start(&testTomb)
insertNAlertsToPlugin(&pw, 1, "testPlugin")
ct, cancel = context.WithTimeout(ctx, time.Millisecond*5)
defer cancel()
err = listenChannelWithTimeout(ct, pw.PluginEvents)
require.NoError(t, err)
resetTestTomb(t, &testTomb, &pw)
// This is to avoid the int complaining
}
func TestPluginAlertCountWatcher(t *testing.T) {
cstest.SkipOnWindowsBecause(t, "timing is not reliable")
ctx := t.Context()
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)
cstest.RequireErrorContains(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)
cstest.RequireErrorContains(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)
require.NoError(t, err)
resetTestTomb(t, &testTomb, &pw)
}
func TestPluginAlertCountWatcherFlushesImmediately(t *testing.T) {
cstest.SkipOnWindowsBecause(t, "timing is not reliable")
// only an immediate flush can get the event out before the first tick
oldTicker := DefaultEmptyTicker
DefaultEmptyTicker = time.Hour
t.Cleanup(func() { DefaultEmptyTicker = oldTicker })
tests := []struct {
name string
config PluginConfig
}{
{"threshold only", PluginConfig{GroupThreshold: 1}},
{"threshold and long interval", PluginConfig{GroupThreshold: 1, GroupWait: time.Hour}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
pw := PluginWatcher{}
testTomb := tomb.Tomb{}
pw.Init(map[string]PluginConfig{"testPlugin": tc.config}, make(map[string][]*models.Alert))
pw.Start(&testTomb)
insertNAlertsToPlugin(&pw, 1, "testPlugin")
ct, cancel := context.WithTimeout(t.Context(), 200*time.Millisecond)
defer cancel()
err := listenChannelWithTimeout(ct, pw.PluginEvents)
require.NoError(t, err)
resetTestTomb(t, &testTomb, &pw)
})
}
}