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/report/publisher_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

95 lines
2.2 KiB
Go

package report_test
import (
"sync"
"testing"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/report"
)
func Test_PublishLifecycleEvents(t *testing.T) {
var event report.LifecycleEvent
wg := sync.WaitGroup{}
wg.Add(2)
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(le report.LifecycleEvent) {
event = le
wg.Done()
})
publisher.Publish(report.LifecycleEvent{Type: report.Added, PolicyReport: &v1alpha2.PolicyReport{
ObjectMeta: v1.ObjectMeta{
Name: "polr-test",
Namespace: "test",
},
}})
publisher.Publish(report.LifecycleEvent{Type: report.Updated, PolicyReport: &v1alpha2.PolicyReport{
ObjectMeta: v1.ObjectMeta{
Name: "polr-test",
Namespace: "test",
},
}})
wg.Wait()
if event.Type != report.Updated {
t.Error("Expected Event to be published to the listener")
}
}
func Test_PublishDeleteLifecycleEvents(t *testing.T) {
var event report.LifecycleEvent
wg := sync.WaitGroup{}
wg.Add(2)
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(le report.LifecycleEvent) {
event = le
wg.Done()
})
publisher.Publish(report.LifecycleEvent{Type: report.Added, PolicyReport: &v1alpha2.PolicyReport{
ObjectMeta: v1.ObjectMeta{
Name: "polr-test",
Namespace: "test",
},
}})
publisher.Publish(report.LifecycleEvent{Type: report.Deleted, PolicyReport: &v1alpha2.PolicyReport{
ObjectMeta: v1.ObjectMeta{
Name: "polr-test",
Namespace: "test",
},
}})
wg.Wait()
if event.Type != report.Deleted {
t.Error("Expected Event to be published to the listener")
}
}
func Test_GetReisteredListeners(t *testing.T) {
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(le report.LifecycleEvent) {})
if len(publisher.GetListener()) != 1 {
t.Error("Expected to get one registered listener back")
}
}
func Test_UnreisteredListeners(t *testing.T) {
publisher := report.NewEventPublisher()
publisher.RegisterListener("test", func(le report.LifecycleEvent) {})
publisher.UnregisterListener("test")
if len(publisher.GetListener()) != 0 {
t.Error("Expected to get 0 listeners back after unregistration")
}
}