traefik/server/server_test.go

330 lines
9.7 KiB
Go
Raw Normal View History

package server
import (
2018-03-21 21:43:15 +00:00
"context"
"net/http"
2017-07-10 10:11:44 +00:00
"net/http/httptest"
"testing"
"time"
2018-07-31 17:28:03 +00:00
"github.com/containous/flaeg/parse"
"github.com/containous/mux"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/config"
2017-04-18 06:22:06 +00:00
"github.com/containous/traefik/middlewares"
2018-11-14 09:18:03 +00:00
"github.com/containous/traefik/old/configuration"
2018-06-05 10:32:03 +00:00
th "github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrepareServerTimeouts(t *testing.T) {
testCases := []struct {
desc string
globalConfig configuration.GlobalConfiguration
expectedIdleTimeout time.Duration
expectedReadTimeout time.Duration
expectedWriteTimeout time.Duration
}{
{
desc: "full configuration",
globalConfig: configuration.GlobalConfiguration{
RespondingTimeouts: &configuration.RespondingTimeouts{
2018-07-31 17:28:03 +00:00
IdleTimeout: parse.Duration(10 * time.Second),
ReadTimeout: parse.Duration(12 * time.Second),
WriteTimeout: parse.Duration(14 * time.Second),
},
},
2018-08-06 18:00:03 +00:00
expectedIdleTimeout: 10 * time.Second,
expectedReadTimeout: 12 * time.Second,
expectedWriteTimeout: 14 * time.Second,
},
{
desc: "using defaults",
globalConfig: configuration.GlobalConfiguration{},
2018-08-06 18:00:03 +00:00
expectedIdleTimeout: 180 * time.Second,
expectedReadTimeout: 0 * time.Second,
expectedWriteTimeout: 0 * time.Second,
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
entryPointName := "http"
2017-10-16 10:46:03 +00:00
entryPoint := &configuration.EntryPoint{
Address: "localhost:0",
ForwardedHeaders: &configuration.ForwardedHeaders{Insecure: true},
}
router := middlewares.NewHandlerSwitcher(mux.NewRouter())
srv := NewServer(test.globalConfig, nil, nil)
2018-11-14 09:18:03 +00:00
httpServer, _, err := srv.prepareServer(context.Background(), entryPointName, entryPoint, router)
require.NoError(t, err, "Unexpected error when preparing srv")
assert.Equal(t, test.expectedIdleTimeout, httpServer.IdleTimeout, "IdleTimeout")
assert.Equal(t, test.expectedReadTimeout, httpServer.ReadTimeout, "ReadTimeout")
assert.Equal(t, test.expectedWriteTimeout, httpServer.WriteTimeout, "WriteTimeout")
})
}
}
2017-11-17 09:26:03 +00:00
func TestListenProvidersSkipsEmptyConfigs(t *testing.T) {
server, stop, invokeStopChan := setupListenProvider(10 * time.Millisecond)
defer invokeStopChan()
go func() {
for {
select {
case <-stop:
return
case <-server.configurationValidatedChan:
t.Error("An empty configuration was published but it should not")
}
}
}()
2018-11-14 09:18:03 +00:00
server.configurationChan <- config.Message{ProviderName: "kubernetes"}
2017-11-17 09:26:03 +00:00
// give some time so that the configuration can be processed
time.Sleep(100 * time.Millisecond)
}
func TestListenProvidersSkipsSameConfigurationForProvider(t *testing.T) {
server, stop, invokeStopChan := setupListenProvider(10 * time.Millisecond)
defer invokeStopChan()
publishedConfigCount := 0
go func() {
for {
select {
case <-stop:
return
2018-11-14 09:18:03 +00:00
case conf := <-server.configurationValidatedChan:
2017-11-17 09:26:03 +00:00
// set the current configuration
// this is usually done in the processing part of the published configuration
2018-08-06 18:00:03 +00:00
// so we have to emulate the behavior here
2018-11-14 09:18:03 +00:00
currentConfigurations := server.currentConfigurations.Get().(config.Configurations)
currentConfigurations[conf.ProviderName] = conf.Configuration
2017-11-17 09:26:03 +00:00
server.currentConfigurations.Set(currentConfigurations)
publishedConfigCount++
if publishedConfigCount > 1 {
t.Error("Same configuration should not be published multiple times")
}
}
}
}()
2018-11-14 09:18:03 +00:00
conf := th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo")),
th.WithLoadBalancerServices(th.WithService("bar")),
2017-11-17 09:26:03 +00:00
)
// provide a configuration
2018-11-14 09:18:03 +00:00
server.configurationChan <- config.Message{ProviderName: "kubernetes", Configuration: conf}
2017-11-17 09:26:03 +00:00
// give some time so that the configuration can be processed
time.Sleep(20 * time.Millisecond)
// provide the same configuration a second time
2018-11-14 09:18:03 +00:00
server.configurationChan <- config.Message{ProviderName: "kubernetes", Configuration: conf}
2017-11-17 09:26:03 +00:00
// give some time so that the configuration can be processed
time.Sleep(100 * time.Millisecond)
}
func TestListenProvidersPublishesConfigForEachProvider(t *testing.T) {
server, stop, invokeStopChan := setupListenProvider(10 * time.Millisecond)
defer invokeStopChan()
publishedProviderConfigCount := map[string]int{}
publishedConfigCount := 0
consumePublishedConfigsDone := make(chan bool)
go func() {
for {
select {
case <-stop:
return
case newConfig := <-server.configurationValidatedChan:
publishedProviderConfigCount[newConfig.ProviderName]++
publishedConfigCount++
if publishedConfigCount == 2 {
consumePublishedConfigsDone <- true
return
}
}
}
}()
2018-11-14 09:18:03 +00:00
conf := th.BuildConfiguration(
th.WithRouters(th.WithRouter("foo")),
th.WithLoadBalancerServices(th.WithService("bar")),
2017-11-17 09:26:03 +00:00
)
2018-11-14 09:18:03 +00:00
server.configurationChan <- config.Message{ProviderName: "kubernetes", Configuration: conf}
server.configurationChan <- config.Message{ProviderName: "marathon", Configuration: conf}
2017-11-17 09:26:03 +00:00
select {
case <-consumePublishedConfigsDone:
if val := publishedProviderConfigCount["kubernetes"]; val != 1 {
t.Errorf("Got %d configuration publication(s) for provider %q, want 1", val, "kubernetes")
}
if val := publishedProviderConfigCount["marathon"]; val != 1 {
t.Errorf("Got %d configuration publication(s) for provider %q, want 1", val, "marathon")
}
case <-time.After(100 * time.Millisecond):
t.Errorf("Published configurations were not consumed in time")
}
}
// setupListenProvider configures the Server and starts listenProviders
func setupListenProvider(throttleDuration time.Duration) (server *Server, stop chan bool, invokeStopChan func()) {
stop = make(chan bool)
invokeStopChan = func() {
stop <- true
}
globalConfig := configuration.GlobalConfiguration{
EntryPoints: configuration.EntryPoints{
"http": &configuration.EntryPoint{},
},
2018-07-31 17:28:03 +00:00
ProvidersThrottleDuration: parse.Duration(throttleDuration),
2017-11-17 09:26:03 +00:00
}
server = NewServer(globalConfig, nil, nil)
2017-11-17 09:26:03 +00:00
go server.listenProviders(stop)
return server, stop, invokeStopChan
}
2017-07-10 10:11:44 +00:00
func TestServerResponseEmptyBackend(t *testing.T) {
const requestPath = "/path"
const routeRule = "Path:" + requestPath
testCases := []struct {
desc string
2018-11-14 09:18:03 +00:00
config func(testServerURL string) *config.Configuration
expectedStatusCode int
2017-07-10 10:11:44 +00:00
}{
{
desc: "Ok",
2018-11-14 09:18:03 +00:00
config: func(testServerURL string) *config.Configuration {
2018-06-05 10:32:03 +00:00
return th.BuildConfiguration(
2018-11-14 09:18:03 +00:00
th.WithRouters(th.WithRouter("foo",
2018-06-05 10:32:03 +00:00
th.WithEntryPoints("http"),
2018-11-14 09:18:03 +00:00
th.WithServiceName("bar"),
th.WithRule(routeRule)),
2018-06-05 10:32:03 +00:00
),
2018-11-14 09:18:03 +00:00
th.WithLoadBalancerServices(th.WithService("bar",
2018-06-05 10:32:03 +00:00
th.WithLBMethod("wrr"),
2018-11-14 09:18:03 +00:00
th.WithServers(th.WithServer(testServerURL))),
2018-06-05 10:32:03 +00:00
),
2017-07-10 10:11:44 +00:00
)
},
expectedStatusCode: http.StatusOK,
2017-07-10 10:11:44 +00:00
},
{
desc: "No Frontend",
2018-11-14 09:18:03 +00:00
config: func(testServerURL string) *config.Configuration {
2018-06-05 10:32:03 +00:00
return th.BuildConfiguration()
2017-07-10 10:11:44 +00:00
},
expectedStatusCode: http.StatusNotFound,
2017-07-10 10:11:44 +00:00
},
{
desc: "Empty Backend LB-Drr",
2018-11-14 09:18:03 +00:00
config: func(testServerURL string) *config.Configuration {
2018-06-05 10:32:03 +00:00
return th.BuildConfiguration(
2018-11-14 09:18:03 +00:00
th.WithRouters(th.WithRouter("foo",
2018-06-05 10:32:03 +00:00
th.WithEntryPoints("http"),
2018-11-14 09:18:03 +00:00
th.WithServiceName("bar"),
th.WithRule(routeRule)),
2018-06-05 10:32:03 +00:00
),
2018-11-14 09:18:03 +00:00
th.WithLoadBalancerServices(th.WithService("bar",
2018-06-05 10:32:03 +00:00
th.WithLBMethod("drr")),
),
2017-07-10 10:11:44 +00:00
)
},
expectedStatusCode: http.StatusServiceUnavailable,
2017-07-10 10:11:44 +00:00
},
{
desc: "Empty Backend LB-Drr Sticky",
2018-11-14 09:18:03 +00:00
config: func(testServerURL string) *config.Configuration {
2018-06-05 10:32:03 +00:00
return th.BuildConfiguration(
2018-11-14 09:18:03 +00:00
th.WithRouters(th.WithRouter("foo",
2018-06-05 10:32:03 +00:00
th.WithEntryPoints("http"),
2018-11-14 09:18:03 +00:00
th.WithServiceName("bar"),
th.WithRule(routeRule)),
2018-06-05 10:32:03 +00:00
),
2018-11-14 09:18:03 +00:00
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("drr"), th.WithStickiness("test")),
2018-06-05 10:32:03 +00:00
),
2017-07-10 10:11:44 +00:00
)
},
expectedStatusCode: http.StatusServiceUnavailable,
2017-07-10 10:11:44 +00:00
},
{
desc: "Empty Backend LB-Wrr",
2018-11-14 09:18:03 +00:00
config: func(testServerURL string) *config.Configuration {
2018-06-05 10:32:03 +00:00
return th.BuildConfiguration(
2018-11-14 09:18:03 +00:00
th.WithRouters(th.WithRouter("foo",
2018-06-05 10:32:03 +00:00
th.WithEntryPoints("http"),
2018-11-14 09:18:03 +00:00
th.WithServiceName("bar"),
th.WithRule(routeRule)),
2018-06-05 10:32:03 +00:00
),
2018-11-14 09:18:03 +00:00
th.WithLoadBalancerServices(th.WithService("bar",
2018-06-05 10:32:03 +00:00
th.WithLBMethod("wrr")),
),
2017-07-10 10:11:44 +00:00
)
},
expectedStatusCode: http.StatusServiceUnavailable,
2017-07-10 10:11:44 +00:00
},
{
desc: "Empty Backend LB-Wrr Sticky",
2018-11-14 09:18:03 +00:00
config: func(testServerURL string) *config.Configuration {
2018-06-05 10:32:03 +00:00
return th.BuildConfiguration(
2018-11-14 09:18:03 +00:00
th.WithRouters(th.WithRouter("foo",
2018-06-05 10:32:03 +00:00
th.WithEntryPoints("http"),
2018-11-14 09:18:03 +00:00
th.WithServiceName("bar"),
th.WithRule(routeRule)),
2018-06-05 10:32:03 +00:00
),
2018-11-14 09:18:03 +00:00
th.WithLoadBalancerServices(th.WithService("bar",
th.WithLBMethod("wrr"), th.WithStickiness("test")),
2018-06-05 10:32:03 +00:00
),
2017-07-10 10:11:44 +00:00
)
},
expectedStatusCode: http.StatusServiceUnavailable,
2017-07-10 10:11:44 +00:00
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
testServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
}))
defer testServer.Close()
globalConfig := configuration.GlobalConfiguration{}
entryPointsConfig := map[string]EntryPoint{
"http": {Configuration: &configuration.EntryPoint{ForwardedHeaders: &configuration.ForwardedHeaders{Insecure: true}}},
2017-07-10 10:11:44 +00:00
}
2018-11-14 09:18:03 +00:00
dynamicConfigs := config.Configurations{"config": test.config(testServer.URL)}
2017-07-10 10:11:44 +00:00
srv := NewServer(globalConfig, nil, entryPointsConfig)
2018-11-14 09:18:03 +00:00
entryPoints, _ := srv.loadConfig(dynamicConfigs, globalConfig)
2017-07-10 10:11:44 +00:00
responseRecorder := &httptest.ResponseRecorder{}
request := httptest.NewRequest(http.MethodGet, testServer.URL+requestPath, nil)
2018-11-14 09:18:03 +00:00
entryPoints["http"].ServeHTTP(responseRecorder, request)
2017-07-10 10:11:44 +00:00
assert.Equal(t, test.expectedStatusCode, responseRecorder.Result().StatusCode, "status code")
2017-07-10 10:11:44 +00:00
})
}
}