forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
754 lines (606 loc) · 22.5 KB
/
main_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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
package main_test
import (
"crypto/tls"
"errors"
"path"
"strconv"
"strings"
"github.com/cloudfoundry-incubator/candiedyaml"
"github.com/cloudfoundry/gorouter/config"
"github.com/cloudfoundry/gorouter/route"
"github.com/cloudfoundry/gorouter/test"
"github.com/cloudfoundry/gorouter/test/common"
"github.com/cloudfoundry/gorouter/test_util"
"github.com/nats-io/nats"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gbytes"
. "github.com/onsi/gomega/gexec"
"github.com/pivotal-golang/localip"
"net"
"net/http/httptest"
"net/url"
"syscall"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
)
const defaultPruneInterval = 1
const defaultPruneThreshold = 2
var _ = Describe("Router Integration", func() {
var tmpdir string
var natsPort uint16
var natsRunner *test_util.NATSRunner
var gorouterSession *Session
writeConfig := func(config *config.Config, cfgFile string) {
cfgBytes, err := candiedyaml.Marshal(config)
Expect(err).ToNot(HaveOccurred())
ioutil.WriteFile(cfgFile, cfgBytes, os.ModePerm)
}
configDrainSetup := func(cfg *config.Config, pruneInterval, pruneThreshold int) {
// ensure the threshold is longer than the interval that we check,
// because we set the route's timestamp to time.Now() on the interval
// as part of pausing
cfg.PruneStaleDropletsIntervalInSeconds = pruneInterval
cfg.DropletStaleThresholdInSeconds = pruneThreshold
cfg.StartResponseDelayIntervalInSeconds = 1
cfg.EndpointTimeoutInSeconds = 5
cfg.DrainTimeoutInSeconds = 1
}
createConfig := func(cfgFile string, statusPort, proxyPort uint16, pruneInterval, pruneThreshold int, natsPorts ...uint16) *config.Config {
cfg := test_util.SpecConfig(statusPort, proxyPort, natsPorts...)
configDrainSetup(cfg, pruneInterval, pruneThreshold)
caCertsPath := filepath.Join(lastGoPath(), "src", "github.com", "cloudfoundry", "gorouter", "test", "assets", "certs", "uaa-ca.pem")
cfg.OAuth = config.OAuthConfig{
TokenEndpoint: "127.0.0.1",
Port: 8443,
ClientName: "client-id",
ClientSecret: "client-secret",
SkipSSLValidation: false,
CACerts: caCertsPath,
}
writeConfig(cfg, cfgFile)
return cfg
}
createSSLConfig := func(cfgFile string, statusPort, proxyPort, SSLPort uint16, natsPorts ...uint16) *config.Config {
config := test_util.SpecSSLConfig(statusPort, proxyPort, SSLPort, natsPorts...)
configDrainSetup(config, defaultPruneInterval, defaultPruneThreshold)
writeConfig(config, cfgFile)
return config
}
startGorouterSession := func(cfgFile string) *Session {
gorouterCmd := exec.Command(gorouterPath, "-c", cfgFile)
session, err := Start(gorouterCmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session, 30).Should(Say("starting"))
Eventually(session, 15).Should(Say("gorouter.started"))
gorouterSession = session
return session
}
stopGorouter := func(gorouterSession *Session) {
err := gorouterSession.Command.Process.Signal(syscall.SIGTERM)
Expect(err).ToNot(HaveOccurred())
Eventually(gorouterSession, 5).Should(Exit(0))
}
BeforeEach(func() {
var err error
tmpdir, err = ioutil.TempDir("", "gorouter")
Expect(err).ToNot(HaveOccurred())
natsPort = test_util.NextAvailPort()
natsRunner = test_util.NewNATSRunner(int(natsPort))
natsRunner.Start()
})
AfterEach(func() {
if natsRunner != nil {
natsRunner.Stop()
}
os.RemoveAll(tmpdir)
if gorouterSession != nil && gorouterSession.ExitCode() == -1 {
stopGorouter(gorouterSession)
}
})
Context("Drain", func() {
var config *config.Config
var localIP string
var statusPort uint16
var proxyPort uint16
var cfgFile string
BeforeEach(func() {
var err error
localIP, err = localip.LocalIP()
Expect(err).ToNot(HaveOccurred())
statusPort = test_util.NextAvailPort()
proxyPort = test_util.NextAvailPort()
cfgFile = filepath.Join(tmpdir, "config.yml")
config = createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
})
JustBeforeEach(func() {
gorouterSession = startGorouterSession(cfgFile)
})
It("waits for all requests to finish", func() {
mbusClient, err := newMessageBus(config)
Expect(err).ToNot(HaveOccurred())
requestMade := make(chan bool)
requestProcessing := make(chan bool)
responseRead := make(chan bool)
longApp := common.NewTestApp([]route.Uri{"longapp.vcap.me"}, proxyPort, mbusClient, nil, "")
longApp.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
requestMade <- true
<-requestProcessing
_, ioErr := ioutil.ReadAll(r.Body)
defer r.Body.Close()
Expect(ioErr).ToNot(HaveOccurred())
w.WriteHeader(http.StatusOK)
w.Write([]byte{'b'})
})
longApp.Listen()
routesUri := fmt.Sprintf("http://%s:%s@%s:%d/routes", config.Status.User, config.Status.Pass, localIP, statusPort)
Eventually(func() bool {
return appRegistered(routesUri, longApp)
}).Should(BeTrue())
go func() {
defer GinkgoRecover()
//Open a connection that never goes active
Eventually(func() bool {
conn, dialErr := net.DialTimeout("tcp",
fmt.Sprintf("%s:%d", localIP, proxyPort), 30*time.Second)
if dialErr == nil {
return conn.Close() == nil
}
return false
}).Should(BeTrue())
//Open a connection that goes active
resp, httpErr := http.Get(longApp.Endpoint())
Expect(httpErr).ShouldNot(HaveOccurred())
Expect(resp.StatusCode).Should(Equal(http.StatusOK))
bytes, httpErr := ioutil.ReadAll(resp.Body)
resp.Body.Close()
Expect(httpErr).ShouldNot(HaveOccurred())
Expect(bytes).Should(Equal([]byte{'b'}))
responseRead <- true
}()
grouter := gorouterSession
gorouterSession = nil
<-requestMade
err = grouter.Command.Process.Signal(syscall.SIGUSR1)
requestProcessing <- true
Expect(err).ToNot(HaveOccurred())
Eventually(grouter, 5).Should(Exit(0))
Eventually(responseRead).Should(Receive(BeTrue()))
})
It("returns error when the gorouter terminates before a request completes", func() {
mbusClient, err := newMessageBus(config)
Expect(err).ToNot(HaveOccurred())
blocker := make(chan bool)
resultCh := make(chan error, 1)
timeoutApp := common.NewTestApp([]route.Uri{"timeout.vcap.me"}, proxyPort, mbusClient, nil, "")
timeoutApp.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
blocker <- true
<-blocker
})
timeoutApp.Listen()
routesUri := fmt.Sprintf("http://%s:%s@%s:%d/routes", config.Status.User, config.Status.Pass, localIP, statusPort)
Eventually(func() bool { return appRegistered(routesUri, timeoutApp) }).Should(BeTrue())
go func() {
defer GinkgoRecover()
_, httpErr := http.Get(timeoutApp.Endpoint())
resultCh <- httpErr
}()
<-blocker
defer func() {
blocker <- true
}()
grouter := gorouterSession
gorouterSession = nil
err = grouter.Command.Process.Signal(syscall.SIGUSR1)
Expect(err).ToNot(HaveOccurred())
Eventually(grouter, 5).Should(Exit(0))
var result error
Eventually(resultCh, 5).Should(Receive(&result))
Expect(result).ToNot(BeNil())
})
It("prevents new connections", func() {
mbusClient, err := newMessageBus(config)
blocker := make(chan bool)
timeoutApp := common.NewTestApp([]route.Uri{"timeout.vcap.me"}, proxyPort, mbusClient, nil, "")
timeoutApp.AddHandler("/", func(w http.ResponseWriter, r *http.Request) {
blocker <- true
<-blocker
})
timeoutApp.Listen()
routesUri := fmt.Sprintf("http://%s:%s@%s:%d/routes", config.Status.User, config.Status.Pass, localIP, statusPort)
Eventually(func() bool { return appRegistered(routesUri, timeoutApp) }).Should(BeTrue())
go func() {
http.Get(timeoutApp.Endpoint())
}()
<-blocker
defer func() {
blocker <- true
}()
grouter := gorouterSession
gorouterSession = nil
err = grouter.Command.Process.Signal(syscall.SIGUSR1)
Expect(err).ToNot(HaveOccurred())
Eventually(grouter, 5).Should(Exit(0))
_, err = http.Get(timeoutApp.Endpoint())
Expect(err).To(HaveOccurred())
urlErr := err.(*url.Error)
opErr := urlErr.Err.(*net.OpError)
Expect(opErr.Op).To(Equal("dial"))
})
Context("when ssl is enabled", func() {
BeforeEach(func() {
createSSLConfig(cfgFile, statusPort, proxyPort, test_util.NextAvailPort(), defaultPruneInterval, defaultPruneThreshold, natsPort)
})
It("drains properly", func() {
grouter := gorouterSession
gorouterSession = nil
err := grouter.Command.Process.Signal(syscall.SIGUSR1)
Expect(err).ToNot(HaveOccurred())
Eventually(grouter, 5).Should(Exit(0))
})
})
})
Context("When Dropsonde is misconfigured", func() {
It("fails to start", func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile := filepath.Join(tmpdir, "config.yml")
config := createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
config.Logging.MetronAddress = ""
writeConfig(config, cfgFile)
gorouterCmd := exec.Command(gorouterPath, "-c", cfgFile)
gorouterSession, _ = Start(gorouterCmd, GinkgoWriter, GinkgoWriter)
Eventually(gorouterSession, 5*time.Second).Should(Exit(2))
})
})
It("logs component logs", func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile := filepath.Join(tmpdir, "config.yml")
createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
gorouterSession = startGorouterSession(cfgFile)
Expect(gorouterSession.Out.Contents()).To(ContainSubstring("Component Router registered successfully"))
})
It("has Nats connectivity", func() {
localIP, err := localip.LocalIP()
Expect(err).ToNot(HaveOccurred())
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile := filepath.Join(tmpdir, "config.yml")
config := createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
gorouterSession = startGorouterSession(cfgFile)
mbusClient, err := newMessageBus(config)
zombieApp := test.NewGreetApp([]route.Uri{"zombie.vcap.me"}, proxyPort, mbusClient, nil)
zombieApp.Listen()
runningApp := test.NewGreetApp([]route.Uri{"innocent.bystander.vcap.me"}, proxyPort, mbusClient, nil)
runningApp.Listen()
routesUri := fmt.Sprintf("http://%s:%s@%s:%d/routes", config.Status.User, config.Status.Pass, localIP, statusPort)
Eventually(func() bool { return appRegistered(routesUri, zombieApp) }).Should(BeTrue())
Eventually(func() bool { return appRegistered(routesUri, runningApp) }).Should(BeTrue())
heartbeatInterval := 200 * time.Millisecond
zombieTicker := time.NewTicker(heartbeatInterval)
runningTicker := time.NewTicker(heartbeatInterval)
go func() {
for {
select {
case <-zombieTicker.C:
zombieApp.Register()
case <-runningTicker.C:
runningApp.Register()
}
}
}()
zombieApp.VerifyAppStatus(200)
runningApp.VerifyAppStatus(200)
// Give enough time to register multiple times
time.Sleep(heartbeatInterval * 3)
// kill registration ticker => kill app (must be before stopping NATS since app.Register is fake and queues messages in memory)
zombieTicker.Stop()
natsRunner.Stop()
staleCheckInterval := config.PruneStaleDropletsInterval
staleThreshold := config.DropletStaleThreshold
// Give router time to make a bad decision (i.e. prune routes)
time.Sleep(10 * (staleCheckInterval + staleThreshold))
// While NATS is down all routes should go down
zombieApp.VerifyAppStatus(404)
runningApp.VerifyAppStatus(404)
natsRunner.Start()
// After NATS starts up the zombie should stay gone
zombieApp.VerifyAppStatus(404)
runningApp.VerifyAppStatus(200)
})
Context("multiple nats server", func() {
var (
config *config.Config
cfgFile string
natsPort2 uint16
proxyPort uint16
statusPort uint16
natsRunner2 *test_util.NATSRunner
pruneInterval int
pruneThreshold int
)
BeforeEach(func() {
natsPort2 = test_util.NextAvailPort()
natsRunner2 = test_util.NewNATSRunner(int(natsPort2))
statusPort = test_util.NextAvailPort()
proxyPort = test_util.NextAvailPort()
cfgFile = filepath.Join(tmpdir, "config.yml")
pruneInterval = 2
pruneThreshold = 10
config = createConfig(cfgFile, statusPort, proxyPort, pruneInterval, pruneThreshold, natsPort, natsPort2)
})
AfterEach(func() {
natsRunner2.Stop()
})
JustBeforeEach(func() {
gorouterSession = startGorouterSession(cfgFile)
})
It("fails over to second nats server before pruning", func() {
localIP, err := localip.LocalIP()
Expect(err).ToNot(HaveOccurred())
mbusClient, err := newMessageBus(config)
runningApp := test.NewGreetApp([]route.Uri{"demo.vcap.me"}, proxyPort, mbusClient, nil)
runningApp.Listen()
routesUri := fmt.Sprintf("http://%s:%s@%s:%d/routes", config.Status.User, config.Status.Pass, localIP, statusPort)
Eventually(func() bool { return appRegistered(routesUri, runningApp) }).Should(BeTrue())
heartbeatInterval := 200 * time.Millisecond
runningTicker := time.NewTicker(heartbeatInterval)
go func() {
for {
select {
case <-runningTicker.C:
runningApp.Register()
}
}
}()
runningApp.VerifyAppStatus(200)
// Give enough time to register multiple times
time.Sleep(heartbeatInterval * 3)
natsRunner.Stop()
natsRunner2.Start()
staleCheckInterval := config.PruneStaleDropletsIntervalInSeconds
staleThreshold := config.DropletStaleThresholdInSeconds
// Give router time to make a bad decision (i.e. prune routes)
sleepTime := time.Duration((2*staleCheckInterval)+(2*staleThreshold)) * time.Second
time.Sleep(sleepTime)
// Expect not to have pruned the routes as it fails over to next NAT server
runningApp.VerifyAppStatus(200)
natsRunner.Start()
})
})
Context("when the route_services_secret and the route_services_secret_decrypt_only are valid", func() {
It("starts fine", func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile := filepath.Join(tmpdir, "config.yml")
config := createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
config.RouteServiceSecret = "route-service-secret"
config.RouteServiceSecretPrev = "my-previous-route-service-secret"
writeConfig(config, cfgFile)
// The process should not have any error.
session := startGorouterSession(cfgFile)
stopGorouter(session)
})
})
Context("when no oauth config is specified", func() {
Context("and routing api is disabled", func() {
It("is able to start up", func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile := filepath.Join(tmpdir, "config.yml")
cfg := createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
cfg.OAuth = config.OAuthConfig{}
writeConfig(cfg, cfgFile)
// The process should not have any error.
session := startGorouterSession(cfgFile)
stopGorouter(session)
})
})
})
Context("when routing api is disabled", func() {
var (
cfgFile string
cfg *config.Config
)
BeforeEach(func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile = filepath.Join(tmpdir, "config.yml")
cfg = createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
writeConfig(cfg, cfgFile)
})
It("doesn't start the route fetcher", func() {
session := startGorouterSession(cfgFile)
Eventually(session).ShouldNot(Say("setting-up-routing-api"))
stopGorouter(session)
})
})
Context("when the routing api is enabled", func() {
var (
config *config.Config
uaaTlsListener net.Listener
routingApi *httptest.Server
cfgFile string
)
BeforeEach(func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile = filepath.Join(tmpdir, "config.yml")
config = createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
routingApi = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
jsonBytes := []byte(`[{"route":"foo.com","port":65340,"ip":"1.2.3.4","ttl":60,"log_guid":"foo-guid"}]`)
w.Write(jsonBytes)
}))
config.RoutingApi.Uri, config.RoutingApi.Port = uriAndPort(routingApi.URL)
})
Context("when the routing api auth is disabled ", func() {
It("uses the no-op token fetcher", func() {
config.RoutingApi.AuthDisabled = true
writeConfig(config, cfgFile)
// note, this will start with routing api, but will not be able to connect
session := startGorouterSession(cfgFile)
Expect(gorouterSession.Out.Contents()).To(ContainSubstring("using-noop-token-fetcher"))
stopGorouter(session)
})
})
Context("when the routing api auth is enabled (default)", func() {
Context("when uaa is available on tls port", func() {
BeforeEach(func() {
uaaTlsListener = setupTlsServer()
config.OAuth.TokenEndpoint, config.OAuth.Port = hostnameAndPort(uaaTlsListener.Addr().String())
})
It("fetches a token from uaa", func() {
writeConfig(config, cfgFile)
// note, this will start with routing api, but will not be able to connect
session := startGorouterSession(cfgFile)
Expect(gorouterSession.Out.Contents()).To(ContainSubstring("started-fetching-token"))
stopGorouter(session)
})
})
Context("when the uaa is not available", func() {
It("gorouter exits with non-zero code", func() {
writeConfig(config, cfgFile)
gorouterCmd := exec.Command(gorouterPath, "-c", cfgFile)
session, err := Start(gorouterCmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session, 30*time.Second).Should(Say("unable-to-fetch-token"))
Eventually(session, 5*time.Second).Should(Exit(2))
})
})
Context("when routing api is not available", func() {
BeforeEach(func() {
uaaTlsListener = setupTlsServer()
config.OAuth.TokenEndpoint, config.OAuth.Port = hostnameAndPort(uaaTlsListener.Addr().String())
})
It("gorouter exits with non-zero code", func() {
routingApi.Close()
writeConfig(config, cfgFile)
gorouterCmd := exec.Command(gorouterPath, "-c", cfgFile)
session, err := Start(gorouterCmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session, 30*time.Second).Should(Say("routing-api-connection-failed"))
Eventually(session, 5*time.Second).Should(Exit(2))
})
})
})
Context("when tls for uaa is disabled", func() {
It("fails fast", func() {
config.OAuth.Port = -1
writeConfig(config, cfgFile)
gorouterCmd := exec.Command(gorouterPath, "-c", cfgFile)
session, err := Start(gorouterCmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session, 30*time.Second).Should(Say("tls-not-enabled"))
Eventually(session, 5*time.Second).Should(Exit(2))
})
})
})
Context("when failing to open configured logging file", func() {
var cfgFile string
BeforeEach(func() {
statusPort := test_util.NextAvailPort()
proxyPort := test_util.NextAvailPort()
cfgFile = filepath.Join(tmpdir, "config.yml")
config := createConfig(cfgFile, statusPort, proxyPort, defaultPruneInterval, defaultPruneThreshold, natsPort)
config.Logging.File = "nonExistentDir/file"
writeConfig(config, cfgFile)
})
It("exits with non-zero code", func() {
gorouterCmd := exec.Command(gorouterPath, "-c", cfgFile)
session, err := Start(gorouterCmd, GinkgoWriter, GinkgoWriter)
Expect(err).ToNot(HaveOccurred())
Eventually(session, 30*time.Second).Should(Say("error-opening-log-file"))
Eventually(session, 5*time.Second).Should(Exit(2))
})
})
})
func uriAndPort(url string) (string, int) {
parts := strings.Split(url, ":")
uri := strings.Join(parts[0:2], ":")
port, _ := strconv.Atoi(parts[2])
return uri, port
}
func hostnameAndPort(url string) (string, int) {
parts := strings.Split(url, ":")
hostname := parts[0]
port, _ := strconv.Atoi(parts[1])
return hostname, port
}
func newMessageBus(c *config.Config) (*nats.Conn, error) {
natsMembers := make([]string, len(c.Nats))
options := nats.DefaultOptions
for _, info := range c.Nats {
uri := url.URL{
Scheme: "nats",
User: url.UserPassword(info.User, info.Pass),
Host: fmt.Sprintf("%s:%d", info.Host, info.Port),
}
natsMembers = append(natsMembers, uri.String())
}
options.Servers = natsMembers
return options.Connect()
}
func appRegistered(routesUri string, app *common.TestApp) bool {
routeFound, err := routeExists(routesUri, string(app.Urls()[0]))
return err == nil && routeFound
}
func appUnregistered(routesUri string, app *common.TestApp) bool {
routeFound, err := routeExists(routesUri, string(app.Urls()[0]))
return err == nil && !routeFound
}
func routeExists(routesEndpoint, routeName string) (bool, error) {
resp, err := http.Get(routesEndpoint)
if err != nil {
return false, err
}
switch resp.StatusCode {
case http.StatusOK:
bytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
Expect(err).ToNot(HaveOccurred())
routes := make(map[string]interface{})
err = json.Unmarshal(bytes, &routes)
Expect(err).ToNot(HaveOccurred())
_, found := routes[routeName]
return found, nil
default:
return false, errors.New("Didn't get an OK response")
}
}
func setupTlsServer() net.Listener {
listener, err := net.Listen("tcp", "127.0.0.1:0")
Expect(err).ToNot(HaveOccurred())
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf("{\"alg\":\"alg\", \"value\": \"%s\" }", "fake-public-key")))
})
tlsListener := newTlsListener(listener)
tlsServer := &http.Server{Handler: handler}
go func() {
err := tlsServer.Serve(tlsListener)
Expect(err).ToNot(HaveOccurred())
}()
return tlsListener
}
func lastGoPath() string {
goPaths := strings.Split(os.Getenv("GOPATH"), ":")
return goPaths[len(goPaths)-1]
}
func newTlsListener(listener net.Listener) net.Listener {
caCertsPath := path.Join(lastGoPath(), "src", "github.com", "cloudfoundry", "gorouter", "test", "assets", "certs")
public := filepath.Join(caCertsPath, "server.pem")
private := filepath.Join(caCertsPath, "server.key")
cert, err := tls.LoadX509KeyPair(public, private)
Expect(err).ToNot(HaveOccurred())
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
CipherSuites: []uint16{tls.TLS_RSA_WITH_AES_256_CBC_SHA},
}
return tls.NewListener(listener, tlsConfig)
}