-
Notifications
You must be signed in to change notification settings - Fork 25
/
shuttle_test.go
663 lines (538 loc) · 14.9 KB
/
shuttle_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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package main
import (
"fmt"
"io"
"io/ioutil"
"net"
"os"
"sync"
"testing"
"time"
"github.com/litl/shuttle/client"
"github.com/litl/shuttle/log"
. "gopkg.in/check.v1"
)
func init() {
debug = os.Getenv("SHUTTLE_DEBUG") == "1"
if debug {
log.DefaultLogger.Level = log.DEBUG
} else {
log.DefaultLogger = log.New(ioutil.Discard, "", 0)
}
}
// something that can wrap a gocheck.C testing.T or testing.B
// Just add more methods as we need them.
type Tester interface {
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Assert(interface{}, Checker, ...interface{})
}
func Test(t *testing.T) { TestingT(t) }
type BasicSuite struct {
servers []*testServer
service *Service
}
var _ = Suite(&BasicSuite{})
// Make Setup and TearDown more generic, so we can bypass the gocheck Suite if
// needed.
func mySetup(s *BasicSuite, t Tester) {
// start 4 possible backend servers
for i := 0; i < 4; i++ {
server, err := NewTestServer("127.0.0.1:0", t)
if err != nil {
t.Fatal(err)
}
s.servers = append(s.servers, server)
}
svcCfg := client.ServiceConfig{
Name: "testService",
Addr: "127.0.0.1:2000",
ClientTimeout: 1000,
ServerTimeout: 1000,
}
if err := Registry.AddService(svcCfg); err != nil {
t.Fatal(err)
}
s.service = Registry.GetService(svcCfg.Name)
}
// shutdown our backend servers
func myTearDown(s *BasicSuite, t Tester) {
for _, s := range s.servers {
s.Stop()
}
// get rid of the servers refs too!
s.servers = nil
// clear global defaults in Registry
Registry.cfg.Balance = ""
Registry.cfg.CheckInterval = 0
Registry.cfg.Fall = 0
Registry.cfg.Rise = 0
Registry.cfg.ClientTimeout = 0
Registry.cfg.ServerTimeout = 0
Registry.cfg.DialTimeout = 0
err := Registry.RemoveService(s.service.Name)
if err != nil {
t.Fatalf("could not remove service '%s': %s", s.service.Name, err)
}
}
func (s *BasicSuite) SetUpTest(c *C) {
mySetup(s, c)
}
func (s *BasicSuite) TearDownTest(c *C) {
myTearDown(s, c)
}
// Add a default backend for the next server we have running
func (s *BasicSuite) AddBackend(c Tester) {
// get the backends via Config to use the Service's locking.
svcCfg := s.service.Config()
next := len(svcCfg.Backends)
if next >= len(s.servers) {
c.Fatal("no more servers")
}
name := fmt.Sprintf("backend_%d", next)
cfg := client.BackendConfig{
Name: name,
Addr: s.servers[next].addr,
CheckAddr: s.servers[next].addr,
}
s.service.add(NewBackend(cfg))
}
// Connect to address, and check response after write.
func checkResp(addr, expected string, c Tester) {
conn, err := net.Dial("tcp", addr)
if err != nil {
c.Fatal(err)
}
defer conn.Close()
if _, err := io.WriteString(conn, "testing\n"); err != nil {
c.Fatal(err)
}
buff := make([]byte, 1024)
n, err := conn.Read(buff)
if err != nil {
c.Fatal(err)
}
resp := string(buff[:n])
if resp == "" {
c.Fatal("No response")
}
if expected != "" && resp != expected {
c.Fatal("Expected ", expected, ", got ", resp)
}
}
func (s *BasicSuite) TestSingleBackend(c *C) {
s.AddBackend(c)
checkResp(s.service.Addr, s.servers[0].addr, c)
}
func (s *BasicSuite) TestRoundRobin(c *C) {
s.AddBackend(c)
s.AddBackend(c)
checkResp(s.service.Addr, s.servers[0].addr, c)
checkResp(s.service.Addr, s.servers[1].addr, c)
checkResp(s.service.Addr, s.servers[0].addr, c)
checkResp(s.service.Addr, s.servers[1].addr, c)
}
func (s *BasicSuite) TestWeightedRoundRobin(c *C) {
s.AddBackend(c)
s.AddBackend(c)
s.AddBackend(c)
s.service.Backends[0].Weight = 1
s.service.Backends[1].Weight = 2
s.service.Backends[2].Weight = 3
// we already checked that we connect to the correct backends,
// so skip the tcp connection this time.
// one from the first server
c.Assert(s.service.next()[0].Name, Equals, "backend_0")
// A weight of 2 should return twice
c.Assert(s.service.next()[0].Name, Equals, "backend_1")
c.Assert(s.service.next()[0].Name, Equals, "backend_1")
// And a weight of 3 should return thrice
c.Assert(s.service.next()[0].Name, Equals, "backend_2")
c.Assert(s.service.next()[0].Name, Equals, "backend_2")
c.Assert(s.service.next()[0].Name, Equals, "backend_2")
// and once around or good measure
c.Assert(s.service.next()[0].Name, Equals, "backend_0")
}
func (s *BasicSuite) TestLeastConn(c *C) {
// replace out default service with one using LeastConn balancing
Registry.RemoveService("testService")
svcCfg := client.ServiceConfig{
Name: "testService",
Addr: "127.0.0.1:2223",
Balance: "LC",
}
if err := Registry.AddService(svcCfg); err != nil {
c.Fatal(err)
}
s.service = Registry.GetService("testService")
s.AddBackend(c)
s.AddBackend(c)
// tie up 4 connections to the backends
buff := make([]byte, 64)
for i := 0; i < 4; i++ {
conn, e := net.Dial("tcp", s.service.Addr)
if e != nil {
c.Fatal(e)
}
// we need to make a call on this proxy to ensure the backend
// connection is complete.
if _, err := io.WriteString(conn, "connect\n"); err != nil {
c.Fatal(err)
}
n, err := conn.Read(buff)
if err != nil || n == 0 {
c.Fatal("no response from backend")
}
defer conn.Close()
}
s.AddBackend(c)
checkResp(s.service.Addr, s.servers[2].addr, c)
checkResp(s.service.Addr, s.servers[2].addr, c)
}
// Test health check by taking down a server from a configured backend
func (s *BasicSuite) TestFailedCheck(c *C) {
s.service.CheckInterval = 500
s.service.Fall = 1
s.AddBackend(c)
stats := s.service.Stats()
c.Assert(stats.Backends[0].Up, Equals, true)
// Stop the server, and see if the backend shows Down after our check
// interval.
s.servers[0].Stop()
time.Sleep(800 * time.Millisecond)
stats = s.service.Stats()
c.Assert(stats.Backends[0].Up, Equals, false)
c.Assert(stats.Backends[0].CheckFail, Equals, 1)
// now try and connect to the service
conn, err := net.Dial("tcp", s.service.Addr)
if err != nil {
// we should still get an initial connection
c.Fatal(err)
}
b := make([]byte, 1024)
n, err := conn.Read(b)
if n != 0 || err != io.EOF {
c.Fatal("connection should have been closed")
}
// now bring that server back up
server, err := NewTestServer(s.servers[0].addr, c)
if err != nil {
c.Fatal(err)
}
s.servers[0] = server
time.Sleep(800 * time.Millisecond)
stats = s.service.Stats()
c.Assert(stats.Backends[0].Up, Equals, true)
}
// Make sure the connection is re-dispatched when Dialing a backend fails
func (s *BasicSuite) TestConnectAny(c *C) {
s.service.CheckInterval = 2000
s.service.Fall = 2
s.AddBackend(c)
s.AddBackend(c)
// kill the first server
s.servers[0].Stop()
stats := s.service.Stats()
c.Assert(stats.Backends[0].Up, Equals, true)
// Backend 0 still shows up, but we should get connected to backend 1
checkResp(s.service.Addr, s.servers[1].addr, c)
}
// Update a backend in place
func (s *BasicSuite) TestUpdateBackend(c *C) {
s.service.CheckInterval = 500
s.service.Fall = 1
s.AddBackend(c)
cfg := s.service.Config()
backendCfg := cfg.Backends[0]
c.Assert(backendCfg.CheckAddr, Equals, backendCfg.Addr)
backendCfg.CheckAddr = ""
s.service.add(NewBackend(backendCfg))
// see if the config reflects the new backend
cfg = s.service.Config()
c.Assert(len(cfg.Backends), Equals, 1)
c.Assert(cfg.Backends[0].CheckAddr, Equals, "")
// Stopping the server should not take down the backend
// since there is no longer a Check address.
s.servers[0].Stop()
time.Sleep(800 * time.Millisecond)
stats := s.service.Stats()
c.Assert(stats.Backends[0].Up, Equals, true)
// should have been no check failures
c.Assert(stats.Backends[0].CheckFail, Equals, 0)
}
// Test removal of a single Backend from a service with multiple.
func (s *BasicSuite) TestRemoveBackend(c *C) {
s.AddBackend(c)
s.AddBackend(c)
stats, err := Registry.ServiceStats("testService")
if err != nil {
c.Fatal(err)
}
c.Assert(len(stats.Backends), Equals, 2)
backend1 := stats.Backends[0].Name
err = Registry.RemoveBackend("testService", backend1)
if err != nil {
c.Fatal(err)
}
stats, err = Registry.ServiceStats("testService")
if err != nil {
c.Fatal(err)
}
c.Assert(len(stats.Backends), Equals, 1)
_, err = Registry.BackendStats("testService", backend1)
c.Assert(err, Equals, ErrNoBackend)
}
func (s *BasicSuite) TestInvalidUpdateService(c *C) {
svcCfg := client.ServiceConfig{
Name: "Update",
Addr: "127.0.0.1:9324",
}
if err := Registry.AddService(svcCfg); err != nil {
c.Fatal(err)
}
svc := Registry.GetService("Update")
if svc == nil {
c.Fatal(ErrNoService)
}
svcCfg.Addr = "127.0.0.1:9425"
// Make sure we can't add the same service again
if err := Registry.AddService(svcCfg); err == nil {
c.Fatal(err)
}
// the update should fail, because it would require a new listener
if err := Registry.UpdateService(svcCfg); err == nil {
c.Fatal(err)
}
// change the addres back, and try to update ClientTimeout
svcCfg.Addr = "127.0.0.1:9324"
svcCfg.ClientTimeout = 1234
// the update should fail, because it would require a new listener
if err := Registry.UpdateService(svcCfg); err == nil {
c.Fatal(err)
}
if err := Registry.RemoveService("Update"); err != nil {
c.Fatal(err)
}
}
// check valid service updates
func (s *BasicSuite) TestUpdateService(c *C) {
svcCfg := client.ServiceConfig{
Name: "Update2",
Addr: "127.0.0.1:9324",
}
if err := Registry.AddService(svcCfg); err != nil {
c.Fatal(err)
}
svc := Registry.GetService("Update2")
if svc == nil {
c.Fatal(ErrNoService)
}
svcCfg.ServerTimeout = 1234
svcCfg.HTTPSRedirect = true
svcCfg.Fall = 5
svcCfg.Rise = 6
svcCfg.Balance = "LC"
// Now update the service for real
if err := Registry.UpdateService(svcCfg); err != nil {
c.Fatal(err)
}
svc = Registry.GetService("Update2")
if svc == nil {
c.Fatal(ErrNoService)
}
c.Assert(svc.ServerTimeout, Equals, 1234*time.Millisecond)
c.Assert(svc.HTTPSRedirect, Equals, true)
c.Assert(svc.Fall, Equals, 5)
c.Assert(svc.Rise, Equals, 6)
c.Assert(svc.Balance, Equals, "LC")
if err := Registry.RemoveService("Update2"); err != nil {
c.Fatal(err)
}
}
// Add backends and run response tests in parallel
func (s *BasicSuite) TestParallel(c *C) {
var wg sync.WaitGroup
client := func(i int) {
s.AddBackend(c)
// do a bunch of new connections in unison
for i := 0; i < 100; i++ {
checkResp(s.service.Addr, "", c)
}
conn, err := net.Dial("tcp", s.service.Addr)
if err != nil {
// we should still get an initial connection
c.Fatal(err)
}
defer conn.Close()
// now do some more continuous ping-pongs with the server
buff := make([]byte, 1024)
for i := 0; i < 1000; i++ {
n, err := io.WriteString(conn, "Testing testing\n")
if err != nil || n == 0 {
c.Fatal("couldn't write:", err)
}
n, err = conn.Read(buff)
if err != nil || n == 0 {
c.Fatal("no response:", err)
}
}
wg.Done()
}
for i := 0; i < 4; i++ {
wg.Add(1)
go client(i)
}
wg.Wait()
}
type UDPSuite struct {
servers []*udpTestServer
service *Service
}
var _ = Suite(&UDPSuite{})
func (s *UDPSuite) SetUpTest(c *C) {
svcCfg := client.ServiceConfig{
Name: "testService",
Addr: "127.0.0.1:11110",
Network: "udp",
}
if err := Registry.AddService(svcCfg); err != nil {
c.Fatal(err)
}
s.service = Registry.GetService(svcCfg.Name)
}
func (s *UDPSuite) TearDownTest(c *C) {
for _, s := range s.servers {
s.Stop()
}
// get rid of the servers refs too!
s.servers = nil
// clear global defaults in Registry
Registry.cfg.Balance = ""
Registry.cfg.CheckInterval = 0
Registry.cfg.Fall = 0
Registry.cfg.Rise = 0
Registry.cfg.ClientTimeout = 0
Registry.cfg.ServerTimeout = 0
Registry.cfg.DialTimeout = 0
err := Registry.RemoveService(s.service.Name)
if err != nil {
c.Fatalf("could not remove service '%s': %s", s.service.Name, err)
}
}
// Add a UDP service, make sure it works, and remove it
func (s *UDPSuite) TestAddRemove(c *C) {
bckCfg := client.BackendConfig{
Name: "UDPServer",
Addr: "127.0.0.1:11111",
Network: "udp",
}
s.service.add(NewBackend(bckCfg))
lAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:0")
rAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:11110")
conn, err := net.ListenUDP("udp", lAddr)
if err != nil {
c.Fatal(err)
}
n, err := conn.WriteToUDP([]byte("TEST"), rAddr)
if err != nil {
c.Fatal(err)
}
// try to make sure packets were delivered and read
time.Sleep(100 * time.Millisecond)
stats := s.service.Stats()
c.Assert(stats.Rcvd, Equals, int64(n))
ok := s.service.remove("UDPServer")
c.Assert(ok, Equals, true)
stats = s.service.Stats()
c.Assert(len(stats.Backends), Equals, 0)
}
// Make sure UDP Services work, and check our WeightedRoundRobin since we're
// already testing it.
func (s *UDPSuite) TestWeightedRoundRobin(c *C) {
servers := make([]*udpTestServer, 3)
var err error
for i, _ := range servers {
servers[i], err = NewUDPTestServer(fmt.Sprintf("127.0.0.1:1111%d", i+1), c)
if err != nil {
c.Fatal(err)
}
bckCfg := client.BackendConfig{
Name: fmt.Sprintf("UDPServer%d", i+1),
Addr: servers[i].addr,
Weight: i + 1,
Network: "udp",
}
s.service.add(NewBackend(bckCfg))
}
defer func() {
for _, s := range servers {
s.Stop()
}
}()
lAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:0")
rAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:11110")
conn, err := net.ListenUDP("udp", lAddr)
if err != nil {
c.Fatal(err)
}
for i := 0; i < 12; i++ {
msg := fmt.Sprintf("TEST_%d", i)
_, err := conn.WriteToUDP([]byte(msg), rAddr)
if err != nil {
c.Fatal(err)
}
}
// The order that packets are delivered to the 3 servers
time.Sleep(100 * time.Millisecond)
rcvOrder := []int{
0, 6, // servers[0]
1, 2, 7, 8, // servers[1]
3, 4, 5, 9, 10, 11, // servers[2]
}
packetNum := 0
for _, srv := range servers {
srv.Lock()
for _, p := range srv.packets {
c.Assert(string(p), Equals, fmt.Sprintf("TEST_%d", rcvOrder[packetNum]))
packetNum++
}
srv.Unlock()
}
}
// Throw a lot of packets at the proxy then count what went through
// This doesn't pass or fail, just logs how much made it to the backend.
func (s *UDPSuite) TestSpew(c *C) {
server, err := NewUDPTestServer("127.0.0.1:11111", c)
if err != nil {
c.Fatal(err)
}
defer server.Stop()
bckCfg := client.BackendConfig{
Name: "UDPServer",
Addr: server.addr,
Network: "udp",
}
s.service.add(NewBackend(bckCfg))
lAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:0")
rAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:11110")
conn, err := net.ListenUDP("udp", lAddr)
if err != nil {
c.Fatal(err)
}
msg := []byte("10 BYTES")
toSend := 10000
for i := 0; i < toSend; i++ {
n, err := conn.WriteToUDP(msg, rAddr)
if err != nil || n != len(msg) {
c.Fatal(fmt.Sprintf("%d %s", n, err))
}
}
// make sure everything the service received made it to the backend.
time.Sleep(100 * time.Millisecond)
stats := s.service.Stats()
c.Logf("Sent %d packets", toSend)
c.Logf("Proxied %d packets", stats.Rcvd/10)
c.Logf("Received %d packets", server.count)
}