-
Notifications
You must be signed in to change notification settings - Fork 459
/
notifier.go
58 lines (48 loc) · 1.84 KB
/
notifier.go
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
package fakenotifier
import (
"context"
"testing"
notifierv1 "github.com/spiffe/spire-plugin-sdk/proto/spire/plugin/server/notifier/v1"
"github.com/spiffe/spire/pkg/common/catalog"
"github.com/spiffe/spire/pkg/common/coretypes/bundle"
"github.com/spiffe/spire/pkg/server/plugin/notifier"
"github.com/spiffe/spire/proto/spire/common"
"github.com/spiffe/spire/test/plugintest"
)
type Config struct {
OnNotifyBundleUpdated func(*common.Bundle) error
OnNotifyAndAdviseBundleLoaded func(*common.Bundle) error
}
func New(t *testing.T, config Config) notifier.Notifier {
server := notifierv1.NotifierPluginServer(&fakeNotifer{config: config})
v1 := new(notifier.V1)
plugintest.Load(t, catalog.MakeBuiltIn("fake", server), v1)
return v1
}
type fakeNotifer struct {
notifierv1.UnimplementedNotifierServer
config Config
}
func (n *fakeNotifer) Notify(ctx context.Context, req *notifierv1.NotifyRequest) (*notifierv1.NotifyResponse, error) {
var err error
if event := req.GetBundleUpdated(); event != nil && n.config.OnNotifyBundleUpdated != nil {
err = n.config.OnNotifyBundleUpdated(bundle.RequireToCommonFromPluginProto(event.Bundle))
}
return ¬ifierv1.NotifyResponse{}, err
}
func (n *fakeNotifer) NotifyAndAdvise(ctx context.Context, req *notifierv1.NotifyAndAdviseRequest) (*notifierv1.NotifyAndAdviseResponse, error) {
var err error
if event := req.GetBundleLoaded(); event != nil && n.config.OnNotifyAndAdviseBundleLoaded != nil {
err = n.config.OnNotifyAndAdviseBundleLoaded(bundle.RequireToCommonFromPluginProto(event.Bundle))
}
return ¬ifierv1.NotifyAndAdviseResponse{}, err
}
func NotifyBundleUpdatedWaiter(t *testing.T) (notifier.Notifier, <-chan *common.Bundle) {
ch := make(chan *common.Bundle)
return New(t, Config{
OnNotifyBundleUpdated: func(bundle *common.Bundle) error {
ch <- bundle
return nil
},
}), ch
}