6
6
"io"
7
7
"net"
8
8
"net/http"
9
- stdlibhttptest "net/http/httptest"
10
9
"os"
11
10
"syscall"
12
11
"testing"
@@ -16,16 +15,11 @@ import (
16
15
17
16
"github.com/zalando/skipper/dataclients/routestring"
18
17
"github.com/zalando/skipper/filters"
19
- flog "github.com/zalando/skipper/filters/accesslog"
20
18
"github.com/zalando/skipper/filters/auth"
21
19
"github.com/zalando/skipper/filters/builtin"
22
- fscheduler "github.com/zalando/skipper/filters/scheduler"
23
- "github.com/zalando/skipper/loadbalancer"
24
- "github.com/zalando/skipper/metrics/metricstest"
25
20
"github.com/zalando/skipper/proxy"
26
21
"github.com/zalando/skipper/ratelimit"
27
22
"github.com/zalando/skipper/routing"
28
- "github.com/zalando/skipper/scheduler"
29
23
"github.com/zalando/skipper/secrets/certregistry"
30
24
"github.com/zalando/skipper/tracing/tracingtest"
31
25
@@ -440,130 +434,75 @@ func createFilterRegistry(specs ...filters.Spec) filters.Registry {
440
434
return fr
441
435
}
442
436
443
- func createRoutesFile (route string ) (string , error ) {
444
- fd , err := os .CreateTemp ("/tmp" , "test_data_clients_" )
445
- if err != nil {
446
- return "" , fmt .Errorf ("Failed to create tempfile: %w" , err )
447
- }
448
- _ , err = fd .WriteString (route )
449
- if err != nil {
450
- return "" , fmt .Errorf ("Failed to write tempfile: %w" , err )
451
- }
452
-
453
- filePath := fd .Name ()
454
- err = fd .Close ()
455
-
456
- return filePath , err
457
- }
458
-
459
437
func TestDataClients (t * testing.T ) {
438
+ tmpDir := t .TempDir ()
439
+
460
440
// routesfile
461
441
routesFileStatus := 201
462
- routeStringFmt := `r%d: Path("/routes-file") -> status(%d) -> inlineContent("Got it") -> <shunt>;`
463
- filePath , err := createRoutesFile (fmt .Sprintf (routeStringFmt , routesFileStatus , routesFileStatus ))
464
- if err != nil {
465
- t .Fatalf ("Failed to create routes file: %v" , err )
466
- }
467
- defer os .Remove (filePath )
442
+ routesFilePath := tmpDir + "/routes-file"
443
+ routesFileContent := fmt .Sprintf (`r201: Path("/routes-file") -> status(%d) -> inlineContent("Got it") -> <shunt>;` , routesFileStatus )
468
444
469
- // application log
470
- fdApp , err := os .CreateTemp ("/tmp" , "app_log_" )
445
+ err := os .WriteFile (routesFilePath , []byte (routesFileContent ), 0644 )
471
446
if err != nil {
472
- t .Fatalf ("Failed to create tempfile : %v" , err )
447
+ t .Fatalf ("Failed to create routes file : %v" , err )
473
448
}
474
- defer fdApp .Close ()
475
449
476
- // access log
477
- fdAccess , err := os .CreateTemp ("/tmp" , "access_log_" )
450
+ address , err := findAddress ()
478
451
if err != nil {
479
- t .Fatalf ("Failed to create tempfile : %v" , err )
452
+ t .Fatalf ("Failed to find address : %v" , err )
480
453
}
481
- defer fdAccess .Close ()
482
454
483
455
// run skipper proxy that we want to test
484
456
o := Options {
485
- Address : ":8090" ,
486
- EnableRatelimiters : true ,
487
- SourcePollTimeout : 1500 * time .Millisecond ,
488
- WaitFirstRouteLoad : true ,
489
- SuppressRouteUpdateLogs : false ,
490
- MetricsListener : ":8091" ,
491
- RoutesFile : filePath ,
492
- InlineRoutes : `healthz: Path("/healthz") -> status(200) -> inlineContent("OK") -> <shunt>;` ,
493
- ApplicationLogOutput : fdApp .Name (),
494
- AccessLogOutput : fdAccess .Name (),
495
- AccessLogDisabled : false ,
496
- MaxTCPListenerConcurrency : 0 ,
497
- ExpectedBytesPerRequest : 1024 ,
498
- ReadHeaderTimeoutServer : 0 ,
499
- ReadTimeoutServer : 1 * time .Second ,
500
- MetricsFlavours : []string {"codahale" },
501
- EnablePrometheusMetrics : true ,
502
- LoadBalancerHealthCheckInterval : 3 * time .Second ,
503
- OAuthTokeninfoURL : "http://127.0.0.1:12345" ,
504
- CredentialsPaths : []string {"/does-not-exist" },
505
- CompressEncodings : []string {"gzip" },
506
- IgnoreTrailingSlash : true ,
507
- EnableBreakers : true ,
508
- DebugListener : ":8092" ,
509
- StatusChecks : []string {"http://127.0.0.1:8091/metrics" , "http://127.0.0.1:8092" },
510
- }
511
-
512
- dcs , err := createDataClients (o , nil )
457
+ Address : address ,
458
+ SourcePollTimeout : 100 * time .Millisecond ,
459
+ WaitFirstRouteLoad : true ,
460
+ RoutesFile : routesFilePath ,
461
+ InlineRoutes : `healthz: Path("/healthz") -> status(200) -> inlineContent("OK") -> <shunt>;` ,
462
+ ApplicationLogOutput : tmpDir + "/application.log" ,
463
+ AccessLogOutput : tmpDir + "/access.log" ,
464
+ }
465
+
466
+ dataclients , err := createDataClients (o , nil )
513
467
if err != nil {
514
468
t .Fatalf ("Failed to createDataclients: %v" , err )
515
469
}
516
470
517
471
fr := createFilterRegistry (
518
- fscheduler .NewFifo (),
519
- flog .NewEnableAccessLog (),
520
472
builtin .NewStatus (),
521
473
builtin .NewInlineContent (),
522
474
)
523
- metrics := & metricstest.MockMetrics {}
524
- reg := scheduler .RegistryWith (scheduler.Options {
525
- Metrics : metrics ,
526
- EnableRouteFIFOMetrics : true ,
527
- })
528
- defer reg .Close ()
529
475
530
- // create LB in front of apiservers to be able to switch the data served by apiserver
531
476
ro := routing.Options {
532
477
SignalFirstLoad : true ,
533
478
FilterRegistry : fr ,
534
- DataClients : dcs , //[]routing.DataClient{dc},
535
- PostProcessors : []routing.PostProcessor {
536
- loadbalancer .NewAlgorithmProvider (),
537
- reg ,
538
- },
539
- SuppressLogs : true ,
479
+ DataClients : dataclients ,
480
+ SuppressLogs : true ,
540
481
}
482
+
541
483
rt := routing .New (ro )
542
484
defer rt .Close ()
543
485
<- rt .FirstLoad ()
544
- tracer := & tracingtest. Tracer {}
486
+
545
487
pr := proxy .WithParams (proxy.Params {
546
- Routing : rt ,
547
- OpenTracing : & proxy.OpenTracingParams {Tracer : tracer },
488
+ Routing : rt ,
548
489
})
549
490
defer pr .Close ()
550
- lb := stdlibhttptest .NewServer (pr )
551
- defer lb .Close ()
552
491
553
492
sigs := make (chan os.Signal , 1 )
554
493
go run (o , sigs , nil )
555
494
556
- for i := 0 ; i < 10 ; i ++ {
495
+ for i := 0 ; i < 20 ; i ++ {
557
496
t .Logf ("Waiting for proxy being ready" )
558
497
559
- rsp , _ := http .DefaultClient .Get ("http://localhost:8090 /healthz" )
498
+ rsp , _ := http .DefaultClient .Get ("http://" + address + " /healthz" )
560
499
if rsp != nil && rsp .StatusCode == 200 {
561
500
break
562
501
}
563
502
time .Sleep (100 * time .Millisecond )
564
503
}
565
504
566
- rsp , err := http .DefaultClient .Get ("http://localhost:8090 /routes-file" )
505
+ rsp , err := http .DefaultClient .Get ("http://" + address + " /routes-file" )
567
506
if err != nil {
568
507
t .Fatalf ("Failed to GET routes file route: %v" , err )
569
508
}
0 commit comments