forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_test.go
417 lines (365 loc) · 9.75 KB
/
setup_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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main
import (
"errors"
"flag"
"net"
"path"
"strings"
"sync"
"testing"
"time"
"github.com/flynn/flynn/Godeps/_workspace/src/github.com/coreos/go-etcd/etcd"
. "github.com/flynn/flynn/Godeps/_workspace/src/gopkg.in/check.v1"
"github.com/flynn/flynn/discoverd/agent"
"github.com/flynn/flynn/discoverd/client"
"github.com/flynn/flynn/discoverd/testutil"
"github.com/flynn/flynn/discoverd/testutil/etcdrunner"
"github.com/flynn/flynn/router/types"
)
var fake = flag.Bool("fake", true, "stub out discoverd/etcd")
func init() {
flag.Parse()
}
type discoverdClient interface {
DiscoverdClient
Register(string, string) error
Unregister(string, string) error
UnregisterAll() error
Close() error
}
func newEtcd(t etcdrunner.TestingT) (EtcdClient, func()) {
if *fake {
return newFakeEtcd(), func() {}
}
cleanup := etcdrunner.RunEtcdServer(t)
return etcd.NewClient(nil), cleanup
}
func newDiscoverd(t etcdrunner.TestingT) (discoverdClient, func()) {
if *fake {
return newFakeDiscoverd(), func() {}
}
discoverd, killDiscoverd := testutil.BootDiscoverd(t, "")
return discoverd, func() {
discoverd.Close()
killDiscoverd()
}
}
func setup(t etcdrunner.TestingT, ec EtcdClient, dc discoverdClient) (discoverdClient, EtcdClient, func()) {
if *fake {
if ec == nil {
ec = newFakeEtcd()
}
if dc == nil {
dc = newFakeDiscoverd()
}
return dc, ec, nil
}
var killEtcd, killDiscoverd func()
if ec == nil {
killEtcd = etcdrunner.RunEtcdServer(t)
ec = etcd.NewClient(nil)
}
if dc == nil {
dc, killDiscoverd = testutil.BootDiscoverd(t, "")
}
return dc, ec, func() {
if killDiscoverd != nil {
dc.Close()
killDiscoverd()
}
if killEtcd != nil {
killEtcd()
}
}
}
func newFakeEtcd() EtcdClient {
e := &fakeEtcd{
index: make(map[string]*etcd.Node),
root: &etcd.Node{Key: "/", Dir: true},
watches: make(map[chan *etcd.Response]watchConfig),
ch: make(chan *etcd.Response),
}
e.index["/"] = e.root
go e.watcher()
return e
}
type fakeEtcd struct {
mtx sync.RWMutex
root *etcd.Node
index map[string]*etcd.Node
ch chan *etcd.Response
watchesMtx sync.RWMutex
watches map[chan *etcd.Response]watchConfig
}
type watchConfig struct {
prefix string
recursive bool
stop chan bool
}
func (e *fakeEtcd) watcher() {
for r := range e.ch {
go func(r *etcd.Response) {
e.watchesMtx.RLock()
defer e.watchesMtx.RUnlock()
for ch, conf := range e.watches {
if r.Node.Key == conf.prefix || conf.recursive && strings.HasPrefix(r.Node.Key, appendSlash(conf.prefix)) {
select {
case <-conf.stop:
go func() {
e.watchesMtx.Lock()
delete(e.watches, ch)
e.watchesMtx.Unlock()
}()
case ch <- &etcd.Response{Action: r.Action, Node: deepCopyNode(r.Node)}:
}
}
}
}(r)
}
}
func appendSlash(s string) string {
if !strings.HasSuffix(s, "/") {
return s + "/"
}
return s
}
func deepCopyNode(n *etcd.Node) *etcd.Node {
newNode := *n
newNode.Nodes = make(etcd.Nodes, len(n.Nodes))
for i, n := range n.Nodes {
newNode.Nodes[i] = deepCopyNode(n)
}
return &newNode
}
func (e *fakeEtcd) Watch(prefix string, waitIndex uint64, recursive bool, receiver chan *etcd.Response, stop chan bool) (*etcd.Response, error) {
if prefix != "/" {
prefix = strings.TrimSuffix(prefix, "/")
}
e.watchesMtx.Lock()
e.watches[receiver] = watchConfig{prefix: prefix, recursive: recursive, stop: stop}
e.watchesMtx.Unlock()
return &etcd.Response{Action: "watch"}, nil
}
func (e *fakeEtcd) Set(key string, value string, ttl uint64) (*etcd.Response, error) {
return e.set(key, value, ttl, true)
}
func (e *fakeEtcd) Create(key string, value string, ttl uint64) (*etcd.Response, error) {
return e.set(key, value, ttl, false)
}
func (e *fakeEtcd) set(key string, value string, ttl uint64, allowExist bool) (*etcd.Response, error) {
if key == "" || key[0] != '/' {
return nil, errors.New("etcd: key must start with /")
}
key = strings.TrimSuffix(key, "/")
e.mtx.Lock()
defer e.mtx.Unlock()
if _, ok := e.index[key]; ok && !allowExist {
return nil, &etcd.EtcdError{ErrorCode: 105, Message: "Key already exists"}
}
components := strings.Split(key, "/")[1:]
components[0] = "/" + components[0]
n := e.root
for i := range components {
path := strings.Join(components[:i+1], "/")
if tmp, ok := e.index[path]; ok {
n = tmp
continue
}
last := i == len(components)-1
newNode := &etcd.Node{Key: path, Dir: !last}
if last {
newNode.Value = value
}
n.Nodes = append(n.Nodes, newNode)
n = newNode
e.index[path] = n
}
e.ch <- &etcd.Response{Action: "create", Node: deepCopyNode(n)}
return &etcd.Response{Action: "create", Node: deepCopyNode(n)}, nil
}
func (e *fakeEtcd) Get(key string, sort, recursive bool) (*etcd.Response, error) {
key = strings.TrimSuffix(key, "/")
e.mtx.RLock()
defer e.mtx.RUnlock()
node, ok := e.index[key]
if !ok {
return nil, &etcd.EtcdError{ErrorCode: 100, Message: "Key not found"}
}
var res *etcd.Node
if recursive {
res = deepCopyNode(node)
} else {
n := *node
n.Nodes = nil
res = &n
}
return &etcd.Response{Action: "get", Node: res}, nil
}
func (e *fakeEtcd) Delete(key string, recursive bool) (*etcd.Response, error) {
key = strings.TrimSuffix(key, "/")
e.mtx.Lock()
defer e.mtx.Unlock()
n, ok := e.index[key]
if !ok {
return nil, &etcd.EtcdError{ErrorCode: 100, Message: "Key not found"}
}
if !recursive && len(n.Nodes) > 0 {
return nil, &etcd.EtcdError{ErrorCode: 108, Message: "Directory not empty"}
}
delete(e.index, key)
parent := e.index[path.Dir(key)]
var idx int
for i, node := range parent.Nodes {
if node.Key == key {
idx = i
break
}
}
parent.Nodes = append(parent.Nodes[:idx], parent.Nodes[idx+1:]...)
if recursive {
key = key + "/"
for k := range e.index {
if strings.HasPrefix(k, key) {
delete(e.index, k)
}
}
}
e.ch <- &etcd.Response{Action: "delete", Node: deepCopyNode(n)}
return &etcd.Response{Action: "delete", Node: n}, nil
}
func newFakeDiscoverd() *fakeDiscoverd {
return &fakeDiscoverd{services: make(map[string]map[string]*discoverd.Service)}
}
type fakeDiscoverd struct {
mtx sync.RWMutex
services map[string]map[string]*discoverd.Service
}
func (d *fakeDiscoverd) Register(name, addr string) error {
return d.RegisterWithAttributes(name, addr, nil)
}
func (d *fakeDiscoverd) Close() error { return nil }
func (d *fakeDiscoverd) RegisterWithAttributes(name, addr string, attrs map[string]string) error {
d.mtx.Lock()
defer d.mtx.Unlock()
if d.services[name] == nil {
d.services[name] = make(map[string]*discoverd.Service)
}
if addr[0] == ':' {
addr = "127.0.0.1" + addr
}
host, port, _ := net.SplitHostPort(addr)
d.services[name][addr] = &discoverd.Service{
Name: name,
Host: host,
Port: port,
Addr: addr,
Attrs: attrs,
}
return nil
}
func (d *fakeDiscoverd) Unregister(name, addr string) error {
d.mtx.Lock()
defer d.mtx.Unlock()
if addr[0] == ':' {
addr = "127.0.0.1" + addr
}
if s, ok := d.services[name]; ok {
delete(s, addr)
}
return nil
}
func (d *fakeDiscoverd) UnregisterAll() error {
d.mtx.Lock()
d.services = make(map[string]map[string]*discoverd.Service)
d.mtx.Unlock()
return nil
}
func (d *fakeDiscoverd) NewServiceSet(name string) (discoverd.ServiceSet, error) {
return &fakeServiceSet{d: d, name: name}, nil
}
type fakeServiceSet struct {
d *fakeDiscoverd
name string
}
func (s *fakeServiceSet) SelfAddr() string { return "" }
func (s *fakeServiceSet) Leader() *discoverd.Service { return nil }
func (s *fakeServiceSet) Leaders() chan *discoverd.Service { return nil }
func (s *fakeServiceSet) Services() []*discoverd.Service {
s.d.mtx.RLock()
defer s.d.mtx.RUnlock()
services := s.d.services[s.name]
res := make([]*discoverd.Service, 0, len(services))
for _, s := range services {
res = append(res, s)
}
return res
}
func (s *fakeServiceSet) Addrs() []string {
s.d.mtx.RLock()
defer s.d.mtx.RUnlock()
services := s.d.services[s.name]
res := make([]string, 0, len(services))
for _, s := range services {
res = append(res, s.Addr)
}
return res
}
func (s *fakeServiceSet) Select(attrs map[string]string) []*discoverd.Service { return nil }
func (s *fakeServiceSet) Filter(attrs map[string]string) {}
func (s *fakeServiceSet) Watch(bringCurrent bool) chan *agent.ServiceUpdate { return nil }
func (s *fakeServiceSet) Unwatch(chan *agent.ServiceUpdate) {}
func (s *fakeServiceSet) Close() error { return nil }
// Hook gocheck up to the "go test" runner
func Test(t *testing.T) { TestingT(t) }
type S struct{}
var _ = Suite(&S{})
const waitTimeout = time.Second
func waitForEvent(c *C, w Watcher, event string, id string) func() *router.Event {
ch := make(chan *router.Event)
w.Watch(ch)
return func() *router.Event {
defer w.Unwatch(ch)
start := time.Now()
for {
timeout := waitTimeout - time.Now().Sub(start)
if timeout <= 0 {
break
}
select {
case e := <-ch:
if e.Event == event && (id == "" || e.ID == id) {
return e
}
case <-time.After(timeout):
break
}
}
c.Fatalf("timeout exceeded waiting for %s %s", event, id)
return nil
}
}
func discoverdRegisterTCP(c *C, l *tcpListener, port int, addr string) {
dc := l.TCPListener.discoverd.(discoverdClient)
ss := l.TCPListener.services[port].ss
discoverdRegister(c, dc, ss, addr)
}
func discoverdRegisterHTTP(c *C, l *httpListener, addr string) {
dc := l.HTTPListener.discoverd.(discoverdClient)
ss := l.HTTPListener.services["test"].ss
discoverdRegister(c, dc, ss, addr)
}
func discoverdRegister(c *C, dc discoverdClient, ss discoverd.ServiceSet, addr string) {
var ch chan *agent.ServiceUpdate
if !*fake {
ch = ss.Watch(false)
defer ss.Unwatch(ch)
}
dc.Register("test", addr)
if ch != nil {
select {
case <-ch:
case <-time.After(10 * time.Second):
c.Fatal("timed out waiting for discoverd registration")
}
}
}