-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul_watcher_test.go
326 lines (289 loc) · 6.63 KB
/
consul_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright Piero de Salvia.
// All Rights Reserved
package pluginator
import (
"fmt"
"io/ioutil"
"os"
"testing"
"time"
"github.com/google/uuid"
"github.com/hashicorp/consul/api"
)
func TestConsulWatcher(t *testing.T) {
if !*runConsulTests {
t.SkipNow()
}
config := api.DefaultConfig()
(*config).Address = "localhost:8500"
client, err := api.NewClient(config)
if err != nil {
t.Fatal(err)
}
kv := client.KV()
uuid := uuid.New().String()
cw, err := newConsulWatcher("localhost", 8500, uuid)
if err != nil {
t.Fatal(err)
}
p := &api.KVPair{
Key: uuid + ".key1",
Value: []byte("key 1 bytes"),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
select {
case event := <-cw.Events:
if event.Action != consulAddAction {
t.Fatal("Should be able to detect an added key/value pair")
}
if event.Key != uuid+".key1" {
t.Fatal("Should be able to read an added key")
}
if event.Value != "key 1 bytes" {
t.Fatal("Should be able to read an added value")
}
}
p = &api.KVPair{
Key: uuid + ".key2",
Value: []byte("key 2 bytes"),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
select {
case event := <-cw.Events:
if event.Action != consulAddAction {
t.Fatal("Should be able to detect an added key/value pair")
}
if event.Key != uuid+".key2" {
t.Fatal("Should be able to read an added key")
}
if event.Value != "key 2 bytes" {
t.Fatal("Should be able to read an added value")
}
}
p = &api.KVPair{
Key: uuid + ".key1",
Value: []byte("key 1 bytes-updated"),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
select {
case event := <-cw.Events:
if event.Action != consulUpdateAction {
t.Fatal("Should be able to detect an updated key/value pair")
}
if event.Key != uuid+".key1" {
t.Fatal("Should be able to read an updated key")
}
if event.Value != "key 1 bytes-updated" {
t.Fatal("Should be able to read an updated value")
}
}
_, err = kv.Delete(uuid+".key2", nil)
if err != nil {
t.Fatal(err)
}
select {
case event := <-cw.Events:
if event.Action != consulRemoveAction {
t.Fatal("Should be able to detect a removed key/value pair")
}
if event.Key != uuid+".key2" {
t.Fatal("Should be able to read a removed key")
}
if event.Value != "key 2 bytes" {
t.Fatal("Should be able to read a deleted value")
}
}
}
func TestConsulMode(t *testing.T) {
if !*runConsulTests {
t.SkipNow()
}
var err error
tempPluginDir, err := ioutil.TempDir("", "testconsulplugindir")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
t.Log("tmpPluginDir=", tempPluginDir)
config := api.DefaultConfig()
(*config).Address = "localhost:8500"
client, err := api.NewClient(config)
if err != nil {
t.Fatal(err)
}
uuid := uuid.New().String()
pluginator, err := NewPluginatorC("localhost", 8500, uuid)
if err != nil {
t.Fatal(err)
}
plugin1, err := ioutil.ReadFile(testDataDir + "/plugin1.go")
if err != nil {
t.Fatal(err)
}
plugin2, err := ioutil.ReadFile(testDataDir + "/plugin2.go")
if err != nil {
t.Fatal(err)
}
plugin3, err := ioutil.ReadFile(testDataDir + "/plugin3.go")
if err != nil {
t.Fatal(err)
}
kv := client.KV()
p := &api.KVPair{
Key: uuid + ".plugin1.go",
Value: []byte(plugin1),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
p = &api.KVPair{
Key: uuid + ".plugin2.go",
Value: []byte(plugin2),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
es := EventSubscriber{
ScanDone: make(chan bool),
RemoveDone: make(chan bool),
UpdateDone: make(chan bool),
AddDone: make(chan bool),
}
pluginator.SubscribeScan(es.ScanSubscriber)
err = pluginator.Start()
if err != nil {
t.Fatal(err)
}
// give consul watcher time to poll consull
time.Sleep(10 * time.Second)
select {
case _ = <-es.ScanDone:
if len(es.ScannedPlugins) != 2 {
t.Fatal("Should be able to scan plugins")
}
p1, exists := es.ScannedPlugins["plugin1"]
if !exists {
t.Fatal("Should be able to load a plugin")
}
p2, exists := es.ScannedPlugins["plugin2"]
if !exists {
t.Fatal("Should be able to load a plugin")
}
addPtr, err := p1.Lib.Lookup("Add")
if err != nil {
t.Fatal("Should be able to lookup a symbol")
}
subPtr, err := p2.Lib.Lookup("Sub")
if err != nil {
t.Fatal("Should be able to lookup a symbol")
}
add, ok := addPtr.(func(int, int) int)
if !ok {
t.Fatal("Should be able to convert to function type")
}
sub, ok := subPtr.(func(int, int) int)
if !ok {
t.Fatal("Should be able to convert to function type")
}
sum := add(1, 2)
if sum != 3 {
t.Fatal("Should be able to invoke loaded function")
}
diff := sub(1, 2)
if diff != -1 {
t.Fatal("Should be able to invoke loaded function")
}
}
pluginator.SubscribeRemove(es.RemoveSubscriber)
_, err = kv.Delete(uuid+".plugin1.go", nil)
if err != nil {
t.Fatal("Should be able to remove a test key")
}
select {
case _ = <-es.RemoveDone:
if es.RemovedName != "plugin1" {
t.Fatal("Should be able to remove a plugin")
}
addPtr, err := es.RemovedLib.Lib.Lookup("Add")
if err != nil {
t.Fatal("Should be able to lookup a symbol")
}
add, ok := addPtr.(func(int, int) int)
if !ok {
t.Fatal("Should be able to convert to function type")
}
sum := add(1, 2)
if sum != 3 {
t.Fatal("Should be able to invoke loaded function")
}
}
pluginator.SubscribeUpdate(es.UpdateSubscriber)
p = &api.KVPair{
Key: uuid + ".plugin2.go",
Value: []byte(plugin1),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
// plugin2 now contains Add
select {
case _ = <-es.UpdateDone:
if es.UpdatedName != "plugin2" {
t.Fatal("Should be able to update a plugin")
}
addPtr, err := es.UpdatedLib.Lib.Lookup("Add")
if err != nil {
t.Log(err)
t.Fatal("Should be able to lookup a symbol")
}
add, ok := addPtr.(func(int, int) int)
if !ok {
t.Fatal("Should be able to convert to function type")
}
sum := add(1, 2)
if sum != 3 {
t.Fatal("Should be able to invoke loaded function")
}
}
pluginator.SubscribeAdd(es.AddSubscriber)
p = &api.KVPair{
Key: uuid + ".plugin3.go",
Value: []byte(plugin3),
}
_, err = kv.Put(p, nil)
if err != nil {
t.Fatal(err)
}
select {
case _ = <-es.AddDone:
if es.AddedName != "plugin3" {
t.Fatal("Should be able to load an added file")
}
mulPtr, err := es.AddedLib.Lib.Lookup("Mul")
if err != nil {
t.Log(err)
t.Fatal("Should be able to lookup a symbol")
}
mul, ok := mulPtr.(func(int, int) int)
if !ok {
t.Fatal("Should be able to convert to function type")
}
prod := mul(3, 2)
if prod != 6 {
t.Fatal("Should be able to invoke loaded function")
}
}
pluginator.Terminate()
}