1
0
Fork 0
mirror of https://github.com/kyverno/policy-reporter.git synced 2024-12-14 11:57:32 +00:00
policy-reporter/pkg/kubernetes/debouncer_test.go
Frank Jogeleit f3b980bfd3
Implement MetadataClient (#257)
* Implement MetadataClient

Signed-off-by: Frank Jogeleit <frank.jogeleit@web.de>
2023-02-08 15:53:59 +01:00

119 lines
2.6 KiB
Go

package kubernetes_test
import (
"sync"
"testing"
"time"
"github.com/kyverno/policy-reporter/pkg/fixtures"
"github.com/kyverno/policy-reporter/pkg/kubernetes"
"github.com/kyverno/policy-reporter/pkg/report"
)
func Test_Debouncer(t *testing.T) {
t.Run("Skip Empty Update", func(t *testing.T) {
counter := 0
wg := sync.WaitGroup{}
wg.Add(2)
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(event report.LifecycleEvent) {
counter++
wg.Done()
})
debouncer := kubernetes.NewDebouncer(200*time.Millisecond, publisher)
debouncer.Add(report.LifecycleEvent{
Type: report.Added,
PolicyReport: fixtures.DefaultPolicyReport,
})
debouncer.Add(report.LifecycleEvent{
Type: report.Updated,
PolicyReport: fixtures.MinPolicyReport,
})
time.Sleep(10 * time.Millisecond)
debouncer.Add(report.LifecycleEvent{
Type: report.Updated,
PolicyReport: fixtures.DefaultPolicyReport,
})
wg.Wait()
if counter != 2 {
t.Error("Expected to skip the empty modify event")
}
})
t.Run("Execute Empty Updates after waiting time", func(t *testing.T) {
counter := 0
wg := sync.WaitGroup{}
wg.Add(2)
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(event report.LifecycleEvent) {
counter++
wg.Done()
})
debouncer := kubernetes.NewDebouncer(10*time.Millisecond, publisher)
debouncer.Add(report.LifecycleEvent{
Type: report.Added,
PolicyReport: fixtures.DefaultPolicyReport,
})
debouncer.Add(report.LifecycleEvent{
Type: report.Updated,
PolicyReport: fixtures.MinPolicyReport,
})
time.Sleep(5 * time.Millisecond)
wg.Wait()
if counter != 2 {
t.Error("Expected to execute empty update event after wait time")
}
})
t.Run("Skip Empty Update when delete event follows directly", func(t *testing.T) {
counter := 0
wg := sync.WaitGroup{}
wg.Add(2)
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(event report.LifecycleEvent) {
counter++
wg.Done()
})
debouncer := kubernetes.NewDebouncer(200*time.Millisecond, publisher)
debouncer.Add(report.LifecycleEvent{
Type: report.Added,
PolicyReport: fixtures.DefaultPolicyReport,
})
debouncer.Add(report.LifecycleEvent{
Type: report.Updated,
PolicyReport: fixtures.MinPolicyReport,
})
time.Sleep(10 * time.Millisecond)
debouncer.Add(report.LifecycleEvent{
Type: report.Deleted,
PolicyReport: fixtures.DefaultPolicyReport,
})
wg.Wait()
if counter != 2 {
t.Error("Expected to skip the empty modify event")
}
})
}