forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter_test.go
536 lines (422 loc) · 13 KB
/
router_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
package router
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os/exec"
"regexp"
"strings"
"time"
"github.com/cloudfoundry/yagnats"
. "launchpad.net/gocheck"
"github.com/cloudfoundry/gorouter/common"
"github.com/cloudfoundry/gorouter/config"
"github.com/cloudfoundry/gorouter/proxy"
"github.com/cloudfoundry/gorouter/registry"
"github.com/cloudfoundry/gorouter/route"
"github.com/cloudfoundry/gorouter/test"
)
type RouterSuite struct {
Config *config.Config
natsServerCmd *exec.Cmd
mbusClient *yagnats.Client
router *Router
natsPort uint16
}
var _ = Suite(&RouterSuite{})
func (s *RouterSuite) SetUpSuite(c *C) {
s.natsPort = nextAvailPort()
s.natsServerCmd = StartNats(int(s.natsPort))
proxyPort := nextAvailPort()
statusPort := nextAvailPort()
s.Config = SpecConfig(s.natsPort, statusPort, proxyPort)
s.router = NewRouter(s.Config)
go s.router.Run()
<-s.WaitUntilNatsIsUp()
s.mbusClient = s.router.mbusClient
}
func (s *RouterSuite) TearDownSuite(c *C) {
StopNats(s.natsServerCmd)
}
func (s *RouterSuite) TestRouterGreets(c *C) {
response := make(chan []byte)
s.mbusClient.Subscribe("router.greet.test.response", func(msg *yagnats.Message) {
response <- msg.Payload
})
s.mbusClient.PublishWithReplyTo("router.greet", "router.greet.test.response", []byte{})
select {
case msg := <-response:
c.Assert(string(msg), Matches, ".*\"minimumRegisterIntervalInSeconds\":5.*")
case <-time.After(500 * time.Millisecond):
c.Error("Did not see a response to router.greet!")
}
}
func (s *RouterSuite) TestDiscover(c *C) {
// Test if router responses to discover message
sig := make(chan common.VcapComponent)
// Since the form of uptime is xxd:xxh:xxm:xxs, we should make
// sure that router has run at least for one second
time.Sleep(time.Second)
s.mbusClient.Subscribe("vcap.component.discover.test.response", func(msg *yagnats.Message) {
var component common.VcapComponent
_ = json.Unmarshal(msg.Payload, &component)
sig <- component
})
s.mbusClient.PublishWithReplyTo(
"vcap.component.discover",
"vcap.component.discover.test.response",
[]byte{},
)
cc := <-sig
var emptyTime time.Time
var emptyDuration common.Duration
c.Check(cc.Type, Equals, "Router")
c.Check(cc.Index, Equals, uint(2))
c.Check(cc.UUID, Not(Equals), "")
c.Check(cc.Start, Not(Equals), emptyTime)
c.Check(cc.Uptime, Not(Equals), emptyDuration)
verify_var_z(cc.Host, cc.Credentials[0], cc.Credentials[1], c)
verify_health_z(cc.Host, s.router.registry, c)
}
func (s *RouterSuite) waitMsgReceived(a *test.TestApp, r bool, t time.Duration) bool {
i := time.Millisecond * 50
m := int(t / i)
for j := 0; j < m; j++ {
received := true
for _, v := range a.Urls() {
_, ok := s.router.registry.Lookup(v)
if ok != r {
received = false
break
}
}
if received {
return true
}
time.Sleep(i)
}
return false
}
func (s *RouterSuite) waitAppRegistered(app *test.TestApp, timeout time.Duration) bool {
return s.waitMsgReceived(app, true, timeout)
}
func (s *RouterSuite) waitAppUnregistered(app *test.TestApp, timeout time.Duration) bool {
return s.waitMsgReceived(app, false, timeout)
}
func (s *RouterSuite) TestRegisterUnregister(c *C) {
app := test.NewGreetApp([]route.Uri{"test.vcap.me"}, s.Config.Port, s.mbusClient, nil)
app.Listen()
c.Assert(s.waitAppRegistered(app, time.Second*5), Equals, true)
app.VerifyAppStatus(200, c)
app.Unregister()
c.Assert(s.waitAppUnregistered(app, time.Second*5), Equals, true)
app.VerifyAppStatus(404, c)
}
func (s *RouterSuite) TestRegistryLastUpdatedVarz(c *C) {
initialUpdateTime := f(s.readVarz(), "ms_since_last_registry_update").(float64)
app1 := test.NewGreetApp([]route.Uri{"test1.vcap.me"}, s.Config.Port, s.mbusClient, nil)
app1.Listen()
c.Assert(s.waitAppRegistered(app1, time.Second*5), Equals, true)
// varz time should be different
updateTime := f(s.readVarz(), "ms_since_last_registry_update").(float64)
c.Assert(updateTime < initialUpdateTime, Equals, true)
}
func (s *RouterSuite) readVarz() map[string]interface{} {
x, err := s.router.varz.MarshalJSON()
if err != nil {
panic(err)
}
y := make(map[string]interface{})
err = json.Unmarshal(x, &y)
if err != nil {
panic(err)
}
return y
}
func f(x interface{}, s ...string) interface{} {
var ok bool
for _, y := range s {
z := x.(map[string]interface{})
x, ok = z[y]
if !ok {
panic(fmt.Sprintf("no key: %s", s))
}
}
return x
}
func (s *RouterSuite) TestVarz(c *C) {
app := test.NewGreetApp([]route.Uri{"count.vcap.me"}, s.Config.Port, s.mbusClient, map[string]string{"framework": "rails"})
app.Listen()
c.Assert(s.waitAppRegistered(app, time.Millisecond*500), Equals, true)
// Send seed request
sendRequests(c, "count.vcap.me", s.Config.Port, 1)
vA := s.readVarz()
// Send requests
sendRequests(c, "count.vcap.me", s.Config.Port, 100)
vB := s.readVarz()
// Verify varz update
RequestsA := int(f(vA, "requests").(float64))
RequestsB := int(f(vB, "requests").(float64))
allRequests := RequestsB - RequestsA
c.Check(allRequests, Equals, 100)
Responses2xxA := int(f(vA, "responses_2xx").(float64))
Responses2xxB := int(f(vB, "responses_2xx").(float64))
allResponses2xx := Responses2xxB - Responses2xxA
c.Check(allResponses2xx, Equals, 100)
app.Unregister()
}
func (s *RouterSuite) TestStickySession(c *C) {
apps := make([]*test.TestApp, 10)
for i := range apps {
apps[i] = test.NewStickyApp([]route.Uri{"sticky.vcap.me"}, s.Config.Port, s.mbusClient, nil)
apps[i].Listen()
}
for _, app := range apps {
c.Assert(s.waitAppRegistered(app, time.Millisecond*500), Equals, true)
}
sessionCookie, vcapCookie, port1 := getSessionAndAppPort("sticky.vcap.me", s.Config.Port, c)
port2 := getAppPortWithSticky("sticky.vcap.me", s.Config.Port, sessionCookie, vcapCookie, c)
c.Check(port1, Equals, port2)
c.Check(vcapCookie.Path, Equals, "/")
for _, app := range apps {
app.Unregister()
}
}
func timeoutDialler() func(net, addr string) (c net.Conn, err error) {
return func(netw, addr string) (net.Conn, error) {
c, err := net.Dial(netw, addr)
c.SetDeadline(time.Now().Add(2 * time.Second))
return c, err
}
}
func verify_health_z(host string, registry *registry.Registry, c *C) {
var req *http.Request
var resp *http.Response
var err error
path := "/healthz"
req, _ = http.NewRequest("GET", "http://"+host+path, nil)
bytes := verify_success(req, c)
c.Check(err, IsNil)
c.Check(string(bytes), Equals, "ok")
// Check that healthz does not reply during deadlock
registry.Lock()
defer registry.Unlock()
httpClient := http.Client{
Transport: &http.Transport{
Dial: timeoutDialler(),
},
}
req, err = http.NewRequest("GET", "http://"+host+path, nil)
resp, err = httpClient.Do(req)
c.Assert(err, Not(IsNil))
match, _ := regexp.Match("i/o timeout", []byte(err.Error()))
c.Assert(match, Equals, true)
c.Check(resp, IsNil)
}
func verify_var_z(host, user, pass string, c *C) {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
path := "/varz"
// Request without username:password should be rejected
req, _ = http.NewRequest("GET", "http://"+host+path, nil)
resp, err = client.Do(req)
c.Check(err, IsNil)
c.Assert(resp, Not(IsNil))
c.Check(resp.StatusCode, Equals, 401)
// varz Basic auth
req.SetBasicAuth(user, pass)
bytes := verify_success(req, c)
varz := make(map[string]interface{})
json.Unmarshal(bytes, &varz)
c.Assert(varz["num_cores"], Not(Equals), 0)
c.Assert(varz["type"], Equals, "Router")
c.Assert(varz["uuid"], Not(Equals), "")
}
func verify_success(req *http.Request, c *C) []byte {
var client http.Client
resp, err := client.Do(req)
defer resp.Body.Close()
c.Check(err, IsNil)
c.Assert(resp, Not(IsNil))
c.Check(resp.StatusCode, Equals, 200)
bytes, err := ioutil.ReadAll(resp.Body)
c.Check(err, IsNil)
return bytes
}
func (s *RouterSuite) TestRouterRunErrors(c *C) {
c.Assert(func() { s.router.Run() }, PanicMatches, "net.Listen.*")
}
func (s *RouterSuite) TestProxyPutRequest(c *C) {
app := test.NewTestApp([]route.Uri{"greet.vcap.me"}, s.Config.Port, s.mbusClient, nil)
var rr *http.Request
var msg string
app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
rr = r
b, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
msg = string(b)
})
app.Listen()
c.Assert(s.waitAppRegistered(app, time.Second*5), Equals, true)
url := app.Endpoint()
buf := bytes.NewBufferString("foobar")
r, err := http.NewRequest("PUT", url, buf)
c.Assert(err, IsNil)
resp, err := http.DefaultClient.Do(r)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, http.StatusOK)
c.Assert(rr, NotNil)
c.Assert(rr.Method, Equals, "PUT")
c.Assert(rr.Proto, Equals, "HTTP/1.1")
c.Assert(msg, Equals, "foobar")
}
func (s *RouterSuite) TestRouterSendsStartOnConnect(c *C) {
started := make(chan bool)
s.router.mbusClient.Subscribe("router.start", func(*yagnats.Message) {
started <- true
})
StopNats(s.natsServerCmd)
s.natsServerCmd = StartNats(int(s.natsPort))
<-s.WaitUntilNatsIsUp()
select {
case <-started:
case <-time.After(500 * time.Millisecond):
c.Error("Did not receive router.start!")
}
}
func (s *RouterSuite) WaitUntilNatsIsUp() chan bool {
natsConnected := make(chan bool, 1)
go func() {
for {
if s.router.mbusClient.Publish("asdf", []byte("data")) == nil {
break
}
time.Sleep(500 * time.Millisecond)
}
natsConnected <- true
}()
return natsConnected
}
func (s *RouterSuite) Test100ContinueRequest(c *C) {
app := test.NewTestApp([]route.Uri{"foo.vcap.me"}, s.Config.Port, s.mbusClient, nil)
rCh := make(chan *http.Request)
app.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
}
rCh <- r
})
<-s.WaitUntilNatsIsUp()
app.Listen()
c.Assert(s.waitAppRegistered(app, time.Second*5), Equals, true)
host := fmt.Sprintf("foo.vcap.me:%d", s.Config.Port)
conn, err := net.Dial("tcp", host)
c.Assert(err, IsNil)
defer conn.Close()
fmt.Fprintf(conn, "POST / HTTP/1.1\r\n"+
"Host: %s\r\n"+
"Connection: close\r\n"+
"Content-Length: 1\r\n"+
"Expect: 100-continue\r\n"+
"\r\n", host)
fmt.Fprintf(conn, "a")
buf := bufio.NewReader(conn)
line, err := buf.ReadString('\n')
c.Assert(err, IsNil)
c.Assert(strings.Contains(line, "100 Continue"), Equals, true)
rr := <-rCh
c.Assert(rr, NotNil)
c.Assert(rr.Header.Get("Expect"), Equals, "")
}
func sendRequests(c *C, url string, rPort uint16, times int) {
uri := fmt.Sprintf("http://%s:%d", url, rPort)
for i := 0; i < times; i++ {
r, err := http.Get(uri)
if err != nil {
panic(err)
}
c.Check(r.StatusCode, Equals, http.StatusOK)
}
}
func getSessionAndAppPort(url string, rPort uint16, c *C) (*http.Cookie, *http.Cookie, string) {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
var port []byte
uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)
req, err = http.NewRequest("GET", uri, nil)
c.Assert(err, IsNil)
resp, err = client.Do(req)
c.Assert(err, IsNil)
port, err = ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
var sessionCookie, vcapCookie *http.Cookie
for _, cookie := range resp.Cookies() {
if cookie.Name == proxy.StickyCookieKey {
sessionCookie = cookie
} else if cookie.Name == proxy.VcapCookieId {
vcapCookie = cookie
}
}
return sessionCookie, vcapCookie, string(port)
}
func getAppPortWithSticky(url string, rPort uint16, sessionCookie, vcapCookie *http.Cookie, c *C) string {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
var port []byte
uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)
req, err = http.NewRequest("GET", uri, nil)
c.Assert(err, IsNil)
req.AddCookie(sessionCookie)
req.AddCookie(vcapCookie)
resp, err = client.Do(req)
c.Assert(err, IsNil)
port, err = ioutil.ReadAll(resp.Body)
return string(port)
}
func (s *RouterSuite) TestInfoApi(c *C) {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
<-s.WaitUntilNatsIsUp()
s.mbusClient.Publish("router.register", []byte(`{"dea":"dea1","app":"app1","uris":["test.com"],"host":"1.2.3.4","port":1234,"tags":{},"private_instance_id":"private_instance_id"}`))
time.Sleep(250 * time.Millisecond)
host := fmt.Sprintf("http://%s:%d/routes", s.Config.Ip, s.Config.Status.Port)
req, err = http.NewRequest("GET", host, nil)
req.SetBasicAuth("user", "pass")
resp, err = client.Do(req)
c.Assert(err, IsNil)
c.Assert(resp, Not(IsNil))
c.Check(resp.StatusCode, Equals, 200)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
c.Assert(err, IsNil)
c.Check(string(body), Matches, ".*1\\.2\\.3\\.4:1234.*\n")
}
func (s *RouterSuite) TestRouterTerminatesLongRequests(c *C) {
app := test.NewSlowApp(
[]route.Uri{"slow-app.vcap.me"},
s.Config.Port,
s.mbusClient,
10*time.Second,
)
app.Listen()
uri := fmt.Sprintf("http://slow-app.vcap.me:%d", s.Config.Port)
resp, err := http.Get(uri)
c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, 502)
}