-
Notifications
You must be signed in to change notification settings - Fork 5
/
watcher_test.go
150 lines (127 loc) · 3.72 KB
/
watcher_test.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
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
package natswatcher
import (
"fmt"
"testing"
"time"
"github.com/casbin/casbin"
gnatsd "github.com/nats-io/gnatsd/test"
"github.com/nats-io/go-nats"
)
func TestWatcher(t *testing.T) {
// Setup nats server
s := gnatsd.RunDefaultServer()
defer s.Shutdown()
natsEndpoint := fmt.Sprintf("nats://localhost:%d", nats.DefaultPort)
natsSubject := "casbin-policy-updated-subject"
updaterCh := make(chan string, 1)
listenerCh := make(chan string, 1)
// updater represents the Casbin enforcer instance that changes the policy in DB.
// Use the endpoint of nats as parameter.
updater, err := NewWatcher(natsEndpoint, natsSubject)
if err != nil {
t.Fatal("Failed to create updater")
}
defer updater.Close()
updater.SetUpdateCallback(func(msg string) {
updaterCh <- "updater"
})
// listener represents any other Casbin enforcer instance that watches the change of policy in DB.
listener, err := NewWatcher(natsEndpoint, natsSubject)
if err != nil {
t.Fatal("Failed to create listener")
}
// listener should set a callback that gets called when policy changes.
err = listener.SetUpdateCallback(func(msg string) {
listenerCh <- "listener"
})
if err != nil {
t.Fatal("Failed to set listener callback")
}
// updater changes the policy, and sends the notifications.
err = updater.Update()
if err != nil {
t.Fatal("The updater failed to send Update")
}
// Validate that listener received message
var updaterReceived bool
var listenerReceived bool
for {
select {
case res := <-listenerCh:
if res != "listener" {
t.Fatalf("Message from unknown source: %v", res)
break
}
listenerReceived = true
case res := <-updaterCh:
if res != "updater" {
t.Fatalf("Message from unknown source: %v", res)
break
}
updaterReceived = true
case <-time.After(time.Second * 10):
t.Fatal("Updater or listener didn't received message in time")
break
}
if updaterReceived && listenerReceived {
close(listenerCh)
close(updaterCh)
break
}
}
}
func TestWithEnforcer(t *testing.T) {
// Setup nats server
s := gnatsd.RunDefaultServer()
defer s.Shutdown()
natsEndpoint := fmt.Sprintf("nats://localhost:%d", nats.DefaultPort)
natsSubject := "casbin-policy-updated-subject"
cannel := make(chan string, 1)
// Initialize the watcher.
// Use the endpoint of etcd cluster as parameter.
w, err := NewWatcher(natsEndpoint, natsSubject)
if err != nil {
t.Fatal("Failed to create updater")
}
defer w.Close()
// Initialize the enforcer.
e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
// Set the watcher for the enforcer.
e.SetWatcher(w)
// By default, the watcher's callback is automatically set to the
// enforcer's LoadPolicy() in the SetWatcher() call.
// We can change it by explicitly setting a callback.
w.SetUpdateCallback(func(msg string) {
cannel <- "enforcer"
})
// Update the policy to test the effect.
e.SavePolicy()
// Validate that listener received message
select {
case res := <-cannel:
if res != "enforcer" {
t.Fatalf("Got unexpected message :%v", res)
}
case <-time.After(time.Second * 10):
t.Fatal("The enforcer didn't send message in time")
}
close(cannel)
}
func TestClose(t *testing.T) {
// Setup nats server
s := gnatsd.RunDefaultServer()
defer s.Shutdown()
natsEndpoint := fmt.Sprintf("nats://localhost:%d", nats.DefaultPort)
natsSubject := "casbin-policy-updated-subject"
// updater represents the Casbin enforcer instance that changes the policy in DB.
// Use the endpoint of nats as parameter.
updater, err := NewWatcher(natsEndpoint, natsSubject)
if err != nil {
t.Fatal("Failed to create updater")
}
updater.Close()
err = updater.Update()
if err == nil {
t.Fatal("Closed watcher should return error on update")
}
}