2020-08-05 09:39:54 +00:00
|
|
|
package leakybucket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-02-25 10:26:46 +00:00
|
|
|
"gopkg.in/tomb.v2"
|
2020-08-05 09:39:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func expectBucketCount(buckets *Buckets, expected int) error {
|
|
|
|
count := 0
|
|
|
|
buckets.Bucket_map.Range(func(rkey, rvalue interface{}) bool {
|
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != expected {
|
|
|
|
return fmt.Errorf("expected %d live buckets, got %d", expected, count)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCandDump(t *testing.T) {
|
2021-02-25 10:26:46 +00:00
|
|
|
var (
|
2023-03-09 10:56:02 +00:00
|
|
|
buckets = NewBuckets()
|
|
|
|
tomb = &tomb.Tomb{}
|
2021-02-25 10:26:46 +00:00
|
|
|
)
|
2020-08-05 09:39:54 +00:00
|
|
|
|
|
|
|
var Holders = []BucketFactory{
|
|
|
|
//one overflowing soon + bh
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2021-02-25 10:26:46 +00:00
|
|
|
Name: "test_counter_fast",
|
|
|
|
Description: "test_counter_fast",
|
|
|
|
Debug: true,
|
|
|
|
Type: "counter",
|
|
|
|
Capacity: -1,
|
|
|
|
Duration: "0.5s",
|
|
|
|
Blackhole: "1m",
|
|
|
|
Filter: "true",
|
|
|
|
wgDumpState: buckets.wgDumpState,
|
|
|
|
wgPour: buckets.wgPour,
|
|
|
|
},
|
2020-08-05 09:39:54 +00:00
|
|
|
//one long counter
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2021-02-25 10:26:46 +00:00
|
|
|
Name: "test_counter_slow",
|
|
|
|
Description: "test_counter_slow",
|
|
|
|
Debug: true,
|
|
|
|
Type: "counter",
|
|
|
|
Capacity: -1,
|
|
|
|
Duration: "10m",
|
|
|
|
Filter: "true",
|
|
|
|
wgDumpState: buckets.wgDumpState,
|
|
|
|
wgPour: buckets.wgPour,
|
|
|
|
},
|
2020-08-05 09:39:54 +00:00
|
|
|
//slow leaky
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2021-02-25 10:26:46 +00:00
|
|
|
Name: "test_leaky_slow",
|
|
|
|
Description: "test_leaky_slow",
|
|
|
|
Debug: true,
|
|
|
|
Type: "leaky",
|
|
|
|
Capacity: 5,
|
|
|
|
LeakSpeed: "10m",
|
|
|
|
Filter: "true",
|
|
|
|
wgDumpState: buckets.wgDumpState,
|
|
|
|
wgPour: buckets.wgPour,
|
|
|
|
},
|
2020-08-05 09:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range Holders {
|
2021-02-25 10:26:46 +00:00
|
|
|
if err := LoadBucket(&Holders[idx], tomb); err != nil {
|
2020-08-05 09:39:54 +00:00
|
|
|
t.Fatalf("while loading (%d/%d): %s", idx, len(Holders), err)
|
|
|
|
}
|
|
|
|
if err := ValidateFactory(&Holders[idx]); err != nil {
|
|
|
|
t.Fatalf("while validating (%d/%d): %s", idx, len(Holders), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Pouring to bucket")
|
|
|
|
|
|
|
|
var in = types.Event{Parsed: map[string]string{"something": "something"}}
|
|
|
|
//pour an item that will go to leaky + counter
|
|
|
|
ok, err := PourItemToHolders(in, Holders, buckets)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("while pouring item : %s", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("didn't pour item")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
if err := expectBucketCount(buckets, 3); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
log.Printf("Bucket GC")
|
|
|
|
|
|
|
|
//call garbage collector
|
2022-01-19 13:56:05 +00:00
|
|
|
if err := GarbageCollectBuckets(time.Now().UTC(), buckets); err != nil {
|
2020-08-05 09:39:54 +00:00
|
|
|
t.Fatalf("failed to garbage collect buckets : %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := expectBucketCount(buckets, 1); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Dumping buckets state")
|
|
|
|
//dump remaining buckets
|
2022-01-19 13:56:05 +00:00
|
|
|
if _, err := DumpBucketsStateAt(time.Now().UTC(), ".", buckets); err != nil {
|
2020-08-05 09:39:54 +00:00
|
|
|
t.Fatalf("failed to dump buckets : %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:37:17 +00:00
|
|
|
func TestShutdownBuckets(t *testing.T) {
|
2021-02-25 10:26:46 +00:00
|
|
|
var (
|
2023-03-09 10:56:02 +00:00
|
|
|
buckets = NewBuckets()
|
|
|
|
Holders = []BucketFactory{
|
2021-02-25 10:26:46 +00:00
|
|
|
//one long counter
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2021-02-25 10:26:46 +00:00
|
|
|
Name: "test_counter_slow",
|
|
|
|
Description: "test_counter_slow",
|
|
|
|
Debug: true,
|
|
|
|
Type: "counter",
|
|
|
|
Capacity: -1,
|
|
|
|
Duration: "10m",
|
|
|
|
Filter: "true",
|
|
|
|
wgDumpState: buckets.wgDumpState,
|
|
|
|
wgPour: buckets.wgPour,
|
|
|
|
},
|
|
|
|
//slow leaky
|
2023-03-09 10:56:02 +00:00
|
|
|
{
|
2021-02-25 10:26:46 +00:00
|
|
|
Name: "test_leaky_slow",
|
|
|
|
Description: "test_leaky_slow",
|
|
|
|
Debug: true,
|
|
|
|
Type: "leaky",
|
|
|
|
Capacity: 5,
|
|
|
|
LeakSpeed: "10m",
|
|
|
|
Filter: "true",
|
|
|
|
wgDumpState: buckets.wgDumpState,
|
|
|
|
wgPour: buckets.wgPour,
|
|
|
|
},
|
|
|
|
}
|
2023-03-09 10:56:02 +00:00
|
|
|
tomb = &tomb.Tomb{}
|
2021-02-25 10:26:46 +00:00
|
|
|
)
|
2020-08-05 09:39:54 +00:00
|
|
|
|
|
|
|
for idx := range Holders {
|
2021-02-25 10:26:46 +00:00
|
|
|
if err := LoadBucket(&Holders[idx], tomb); err != nil {
|
2020-08-05 09:39:54 +00:00
|
|
|
t.Fatalf("while loading (%d/%d): %s", idx, len(Holders), err)
|
|
|
|
}
|
|
|
|
if err := ValidateFactory(&Holders[idx]); err != nil {
|
|
|
|
t.Fatalf("while validating (%d/%d): %s", idx, len(Holders), err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Pouring to bucket")
|
|
|
|
|
|
|
|
var in = types.Event{Parsed: map[string]string{"something": "something"}}
|
|
|
|
//pour an item that will go to leaky + counter
|
|
|
|
ok, err := PourItemToHolders(in, Holders, buckets)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("while pouring item : %s", err)
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("didn't pour item")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
if err := expectBucketCount(buckets, 2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ShutdownAllBuckets(buckets); err != nil {
|
2022-04-27 09:04:12 +00:00
|
|
|
t.Fatalf("while shutting down buckets : %s", err)
|
2020-08-05 09:39:54 +00:00
|
|
|
}
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
if err := expectBucketCount(buckets, 2); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|