Skip to content

Commit d5a446f

Browse files
Fix flaky TestDataClients
* simplify setup boilerplate by using test t.TempDir() * remove unrelated options and unused code * increase startup wait interval Fixes #2556 Signed-off-by: Alexander Yastrebov <[email protected]>
1 parent 430106d commit d5a446f

File tree

1 file changed

+26
-87
lines changed

1 file changed

+26
-87
lines changed

skipper_test.go

+26-87
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"net"
88
"net/http"
9-
stdlibhttptest "net/http/httptest"
109
"os"
1110
"syscall"
1211
"testing"
@@ -16,16 +15,11 @@ import (
1615

1716
"github.com/zalando/skipper/dataclients/routestring"
1817
"github.com/zalando/skipper/filters"
19-
flog "github.com/zalando/skipper/filters/accesslog"
2018
"github.com/zalando/skipper/filters/auth"
2119
"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"
2520
"github.com/zalando/skipper/proxy"
2621
"github.com/zalando/skipper/ratelimit"
2722
"github.com/zalando/skipper/routing"
28-
"github.com/zalando/skipper/scheduler"
2923
"github.com/zalando/skipper/secrets/certregistry"
3024
"github.com/zalando/skipper/tracing/tracingtest"
3125

@@ -440,130 +434,75 @@ func createFilterRegistry(specs ...filters.Spec) filters.Registry {
440434
return fr
441435
}
442436

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-
459437
func TestDataClients(t *testing.T) {
438+
tmpDir := t.TempDir()
439+
460440
// routesfile
461441
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)
468444

469-
// application log
470-
fdApp, err := os.CreateTemp("/tmp", "app_log_")
445+
err := os.WriteFile(routesFilePath, []byte(routesFileContent), 0644)
471446
if err != nil {
472-
t.Fatalf("Failed to create tempfile: %v", err)
447+
t.Fatalf("Failed to create routes file: %v", err)
473448
}
474-
defer fdApp.Close()
475449

476-
// access log
477-
fdAccess, err := os.CreateTemp("/tmp", "access_log_")
450+
address, err := findAddress()
478451
if err != nil {
479-
t.Fatalf("Failed to create tempfile: %v", err)
452+
t.Fatalf("Failed to find address: %v", err)
480453
}
481-
defer fdAccess.Close()
482454

483455
// run skipper proxy that we want to test
484456
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)
513467
if err != nil {
514468
t.Fatalf("Failed to createDataclients: %v", err)
515469
}
516470

517471
fr := createFilterRegistry(
518-
fscheduler.NewFifo(),
519-
flog.NewEnableAccessLog(),
520472
builtin.NewStatus(),
521473
builtin.NewInlineContent(),
522474
)
523-
metrics := &metricstest.MockMetrics{}
524-
reg := scheduler.RegistryWith(scheduler.Options{
525-
Metrics: metrics,
526-
EnableRouteFIFOMetrics: true,
527-
})
528-
defer reg.Close()
529475

530-
// create LB in front of apiservers to be able to switch the data served by apiserver
531476
ro := routing.Options{
532477
SignalFirstLoad: true,
533478
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,
540481
}
482+
541483
rt := routing.New(ro)
542484
defer rt.Close()
543485
<-rt.FirstLoad()
544-
tracer := &tracingtest.Tracer{}
486+
545487
pr := proxy.WithParams(proxy.Params{
546-
Routing: rt,
547-
OpenTracing: &proxy.OpenTracingParams{Tracer: tracer},
488+
Routing: rt,
548489
})
549490
defer pr.Close()
550-
lb := stdlibhttptest.NewServer(pr)
551-
defer lb.Close()
552491

553492
sigs := make(chan os.Signal, 1)
554493
go run(o, sigs, nil)
555494

556-
for i := 0; i < 10; i++ {
495+
for i := 0; i < 20; i++ {
557496
t.Logf("Waiting for proxy being ready")
558497

559-
rsp, _ := http.DefaultClient.Get("http://localhost:8090/healthz")
498+
rsp, _ := http.DefaultClient.Get("http://" + address + "/healthz")
560499
if rsp != nil && rsp.StatusCode == 200 {
561500
break
562501
}
563502
time.Sleep(100 * time.Millisecond)
564503
}
565504

566-
rsp, err := http.DefaultClient.Get("http://localhost:8090/routes-file")
505+
rsp, err := http.DefaultClient.Get("http://" + address + "/routes-file")
567506
if err != nil {
568507
t.Fatalf("Failed to GET routes file route: %v", err)
569508
}

0 commit comments

Comments
 (0)