2018-11-14 09:18:03 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-04 19:08:03 +00:00
|
|
|
"io"
|
2018-11-14 09:18:03 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-03-14 08:30:04 +00:00
|
|
|
"strings"
|
2018-11-14 09:18:03 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/runtime"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/config/static"
|
2021-04-30 08:22:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/metrics"
|
2020-09-16 13:46:04 +00:00
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/accesslog"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/middlewares/requestdecorator"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/server/middleware"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/server/service"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/testhelpers"
|
|
|
|
"github.com/traefik/traefik/v2/pkg/types"
|
2018-11-14 09:18:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouterManager_Get(t *testing.T) {
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
|
|
|
2020-07-08 10:18:03 +00:00
|
|
|
t.Cleanup(func() { server.Close() })
|
|
|
|
|
2019-11-14 15:40:05 +00:00
|
|
|
type expectedResult struct {
|
2018-11-14 09:18:03 +00:00
|
|
|
StatusCode int
|
|
|
|
RequestHeaders map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2018-11-27 16:42:04 +00:00
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig map[string]*dynamic.Router
|
|
|
|
serviceConfig map[string]*dynamic.Service
|
|
|
|
middlewaresConfig map[string]*dynamic.Middleware
|
2018-11-27 16:42:04 +00:00
|
|
|
entryPoints []string
|
2019-11-14 15:40:05 +00:00
|
|
|
expected expectedResult
|
2018-11-14 09:18:03 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "no middleware",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{StatusCode: http.StatusOK},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
2020-12-17 09:06:03 +00:00
|
|
|
{
|
|
|
|
desc: "empty host",
|
|
|
|
routersConfig: map[string]*dynamic.Router{
|
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(``)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
|
|
|
"foo-service": {
|
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
|
|
|
Servers: []dynamic.Server{
|
|
|
|
{
|
|
|
|
URL: server.URL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
|
|
|
expected: expectedResult{StatusCode: http.StatusNotFound},
|
|
|
|
},
|
2018-12-03 10:32:05 +00:00
|
|
|
{
|
|
|
|
desc: "no load balancer",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-12-03 10:32:05 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2018-12-03 10:32:05 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-12-03 10:32:05 +00:00
|
|
|
"foo-service": {},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{StatusCode: http.StatusNotFound},
|
2018-12-03 10:32:05 +00:00
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
|
|
|
desc: "no middleware, no matching",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`bar.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{StatusCode: http.StatusNotFound},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "middleware: headers > auth",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Middlewares: []string{"headers-middle", "auth-middle"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
middlewaresConfig: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"auth-middle": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2018-11-14 09:18:03 +00:00
|
|
|
Users: []string{"toto:titi"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"headers-middle": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
CustomRequestHeaders: map[string]string{"X-Apero": "beer"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{
|
2018-11-14 09:18:03 +00:00
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
RequestHeaders: map[string]string{
|
|
|
|
"X-Apero": "beer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "middleware: auth > header",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Middlewares: []string{"auth-middle", "headers-middle"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
middlewaresConfig: map[string]*dynamic.Middleware{
|
2018-11-14 09:18:03 +00:00
|
|
|
"auth-middle": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2018-11-14 09:18:03 +00:00
|
|
|
Users: []string{"toto:titi"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"headers-middle": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2018-11-14 09:18:03 +00:00
|
|
|
CustomRequestHeaders: map[string]string{"X-Apero": "beer"},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{
|
2019-01-15 13:28:04 +00:00
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
RequestHeaders: map[string]string{
|
|
|
|
"X-Apero": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no middleware with provider name",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo@provider-1": {
|
2019-01-15 13:28:04 +00:00
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo-service@provider-1": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-01-15 13:28:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{StatusCode: http.StatusOK},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "no middleware with specified provider name",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo@provider-1": {
|
2019-01-15 13:28:04 +00:00
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Service: "foo-service@provider-2",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo-service@provider-2": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-01-15 13:28:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{StatusCode: http.StatusOK},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "middleware: chain with provider name",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo@provider-1": {
|
2019-01-15 13:28:04 +00:00
|
|
|
EntryPoints: []string{"web"},
|
2019-06-21 07:54:04 +00:00
|
|
|
Middlewares: []string{"chain-middle@provider-2", "headers-middle"},
|
2019-01-15 13:28:04 +00:00
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-06-21 07:54:04 +00:00
|
|
|
"foo-service@provider-1": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-01-15 13:28:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
middlewaresConfig: map[string]*dynamic.Middleware{
|
2019-06-21 07:54:04 +00:00
|
|
|
"chain-middle@provider-2": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Chain: &dynamic.Chain{Middlewares: []string{"auth-middle"}},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"auth-middle@provider-2": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2019-01-15 13:28:04 +00:00
|
|
|
Users: []string{"toto:titi"},
|
|
|
|
},
|
|
|
|
},
|
2019-06-21 07:54:04 +00:00
|
|
|
"headers-middle@provider-1": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Headers: &dynamic.Headers{
|
2019-01-15 13:28:04 +00:00
|
|
|
CustomRequestHeaders: map[string]string{"X-Apero": "beer"},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
2019-11-14 15:40:05 +00:00
|
|
|
expected: expectedResult{
|
2018-11-14 09:18:03 +00:00
|
|
|
StatusCode: http.StatusUnauthorized,
|
|
|
|
RequestHeaders: map[string]string{
|
|
|
|
"X-Apero": "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2019-05-16 08:58:06 +00:00
|
|
|
Services: test.serviceConfig,
|
|
|
|
Routers: test.routersConfig,
|
|
|
|
Middlewares: test.middlewaresConfig,
|
|
|
|
},
|
|
|
|
})
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
roundTripperManager := service.NewRoundTripperManager()
|
|
|
|
roundTripperManager.Update(map[string]*dynamic.ServersTransport{"default@internal": {}})
|
|
|
|
serviceManager := service.NewManager(rtConf.Services, nil, nil, roundTripperManager)
|
2020-04-20 16:36:34 +00:00
|
|
|
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, nil)
|
2019-11-14 15:40:05 +00:00
|
|
|
chainBuilder := middleware.NewChainBuilder(static.Configuration{}, nil, nil)
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
routerManager := NewManager(rtConf, serviceManager, middlewaresBuilder, chainBuilder, metrics.NewVoidRegistry())
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
handlers := routerManager.BuildHandlers(context.Background(), test.entryPoints, false)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/", nil)
|
|
|
|
|
|
|
|
reqHost := requestdecorator.New(nil)
|
|
|
|
reqHost.ServeHTTP(w, req, handlers["web"].ServeHTTP)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected.StatusCode, w.Code)
|
|
|
|
|
|
|
|
for key, value := range test.expected.RequestHeaders {
|
|
|
|
assert.Equal(t, value, req.Header.Get(key))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccessLog(t *testing.T) {
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
|
|
|
2020-07-08 10:18:03 +00:00
|
|
|
t.Cleanup(func() { server.Close() })
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
testCases := []struct {
|
2019-01-30 15:24:07 +00:00
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig map[string]*dynamic.Router
|
|
|
|
serviceConfig map[string]*dynamic.Service
|
|
|
|
middlewaresConfig map[string]*dynamic.Middleware
|
2019-01-30 15:24:07 +00:00
|
|
|
entryPoints []string
|
|
|
|
expected string
|
2018-11-14 09:18:03 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "apply routerName in accesslog (first match)",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`bar.foo`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
|
|
|
expected: "foo",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "apply routerName in accesslog (second match)",
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig: map[string]*dynamic.Router{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`bar.foo`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
2019-01-30 15:24:07 +00:00
|
|
|
Rule: "Host(`foo.bar`)",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2018-11-14 09:18:03 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
entryPoints: []string{"web"},
|
|
|
|
expected: "bar",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
2019-07-15 15:04:04 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2019-05-16 08:58:06 +00:00
|
|
|
Services: test.serviceConfig,
|
|
|
|
Routers: test.routersConfig,
|
|
|
|
Middlewares: test.middlewaresConfig,
|
|
|
|
},
|
|
|
|
})
|
2019-09-26 09:00:06 +00:00
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
roundTripperManager := service.NewRoundTripperManager()
|
|
|
|
roundTripperManager.Update(map[string]*dynamic.ServersTransport{"default@internal": {}})
|
|
|
|
serviceManager := service.NewManager(rtConf.Services, nil, nil, roundTripperManager)
|
2020-04-20 16:36:34 +00:00
|
|
|
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, nil)
|
2019-11-14 15:40:05 +00:00
|
|
|
chainBuilder := middleware.NewChainBuilder(static.Configuration{}, nil, nil)
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
routerManager := NewManager(rtConf, serviceManager, middlewaresBuilder, chainBuilder, metrics.NewVoidRegistry())
|
2018-11-14 09:18:03 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
handlers := routerManager.BuildHandlers(context.Background(), test.entryPoints, false)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/", nil)
|
|
|
|
|
|
|
|
accesslogger, err := accesslog.NewHandler(&types.AccessLog{
|
|
|
|
Format: "json",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
reqHost := requestdecorator.New(nil)
|
|
|
|
|
|
|
|
accesslogger.ServeHTTP(w, req, http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
reqHost.ServeHTTP(w, req, handlers["web"].ServeHTTP)
|
|
|
|
|
|
|
|
data := accesslog.GetLogData(req)
|
|
|
|
require.NotNil(t, data)
|
|
|
|
|
|
|
|
assert.Equal(t, test.expected, data.Core[accesslog.RouterName])
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 08:30:04 +00:00
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
func TestRuntimeConfiguration(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig map[string]*dynamic.Service
|
|
|
|
routerConfig map[string]*dynamic.Router
|
|
|
|
middlewareConfig map[string]*dynamic.Middleware
|
2019-05-16 08:58:06 +00:00
|
|
|
expectedError int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "No error",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:8085",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1:8086",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
HealthCheck: &dynamic.HealthCheck{
|
2019-05-16 08:58:06 +00:00
|
|
|
Interval: "500ms",
|
|
|
|
Path: "/health",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`bar.foo`)",
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "One router with wrong rule",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "WrongRule(`bar.foo`)",
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "All router with wrong rule",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "WrongRule(`bar.foo`)",
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "WrongRule(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Router with unknown service",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "wrong-service",
|
|
|
|
Rule: "Host(`bar.foo`)",
|
|
|
|
},
|
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Router with broken service",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
|
|
|
LoadBalancer: nil,
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Router with middleware",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
middlewareConfig: map[string]*dynamic.Middleware{
|
2019-05-16 08:58:06 +00:00
|
|
|
"auth": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2019-05-16 08:58:06 +00:00
|
|
|
Users: []string{"admin:admin"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"addPrefixTest": {
|
2019-07-10 07:26:04 +00:00
|
|
|
AddPrefix: &dynamic.AddPrefix{
|
2019-05-16 08:58:06 +00:00
|
|
|
Prefix: "/toto",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
Middlewares: []string{"auth", "addPrefixTest"},
|
|
|
|
},
|
|
|
|
"test": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar.other`)",
|
|
|
|
Middlewares: []string{"addPrefixTest", "auth"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Router with unknown middleware",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
middlewareConfig: map[string]*dynamic.Middleware{
|
2019-05-16 08:58:06 +00:00
|
|
|
"auth": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2019-05-16 08:58:06 +00:00
|
|
|
Users: []string{"admin:admin"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
Middlewares: []string{"unknown"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 1,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
desc: "Router with broken middleware",
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig: map[string]*dynamic.Service{
|
2019-05-16 08:58:06 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-05-16 08:58:06 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "http://127.0.0.1",
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
middlewareConfig: map[string]*dynamic.Middleware{
|
2019-05-16 08:58:06 +00:00
|
|
|
"auth": {
|
2019-07-10 07:26:04 +00:00
|
|
|
BasicAuth: &dynamic.BasicAuth{
|
2019-05-16 08:58:06 +00:00
|
|
|
Users: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-10 07:26:04 +00:00
|
|
|
routerConfig: map[string]*dynamic.Router{
|
2019-05-16 08:58:06 +00:00
|
|
|
"bar": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`)",
|
|
|
|
Middlewares: []string{"auth"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
entryPoints := []string{"web"}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2019-05-16 08:58:06 +00:00
|
|
|
Services: test.serviceConfig,
|
|
|
|
Routers: test.routerConfig,
|
|
|
|
Middlewares: test.middlewareConfig,
|
|
|
|
},
|
|
|
|
})
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
roundTripperManager := service.NewRoundTripperManager()
|
|
|
|
roundTripperManager.Update(map[string]*dynamic.ServersTransport{"default@internal": {}})
|
|
|
|
serviceManager := service.NewManager(rtConf.Services, nil, nil, roundTripperManager)
|
2020-04-20 16:36:34 +00:00
|
|
|
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, nil)
|
2019-11-14 15:40:05 +00:00
|
|
|
chainBuilder := middleware.NewChainBuilder(static.Configuration{}, nil, nil)
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
routerManager := NewManager(rtConf, serviceManager, middlewaresBuilder, chainBuilder, metrics.NewVoidRegistry())
|
2020-07-13 15:58:03 +00:00
|
|
|
|
2019-05-16 08:58:06 +00:00
|
|
|
_ = routerManager.BuildHandlers(context.Background(), entryPoints, false)
|
|
|
|
|
|
|
|
// even though rtConf was passed by argument to the manager builders above,
|
|
|
|
// it's ok to use it as the result we check, because everything worth checking
|
|
|
|
// can be accessed by pointers in it.
|
|
|
|
var allErrors int
|
|
|
|
for _, v := range rtConf.Services {
|
|
|
|
if v.Err != nil {
|
|
|
|
allErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range rtConf.Routers {
|
2019-07-15 15:04:04 +00:00
|
|
|
if len(v.Err) > 0 {
|
2019-05-16 08:58:06 +00:00
|
|
|
allErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, v := range rtConf.Middlewares {
|
|
|
|
if v.Err != nil {
|
|
|
|
allErrors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, test.expectedError, allErrors)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:12:05 +00:00
|
|
|
func TestProviderOnMiddlewares(t *testing.T) {
|
|
|
|
entryPoints := []string{"web"}
|
|
|
|
|
2020-03-05 11:46:05 +00:00
|
|
|
staticCfg := static.Configuration{
|
|
|
|
EntryPoints: map[string]*static.EntryPoint{
|
|
|
|
"web": {
|
|
|
|
Address: ":80",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-09-10 14:12:05 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
|
|
|
Services: map[string]*dynamic.Service{
|
|
|
|
"test@file": {
|
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
|
|
|
Servers: []dynamic.Server{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Routers: map[string]*dynamic.Router{
|
|
|
|
"router@file": {
|
2020-03-05 11:46:05 +00:00
|
|
|
EntryPoints: []string{"web"},
|
2019-09-10 14:12:05 +00:00
|
|
|
Rule: "Host(`test`)",
|
|
|
|
Service: "test@file",
|
|
|
|
Middlewares: []string{"chain@file", "m1"},
|
|
|
|
},
|
|
|
|
"router@docker": {
|
2020-03-05 11:46:05 +00:00
|
|
|
EntryPoints: []string{"web"},
|
2019-09-10 14:12:05 +00:00
|
|
|
Rule: "Host(`test`)",
|
|
|
|
Service: "test@file",
|
|
|
|
Middlewares: []string{"chain", "m1@file"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Middlewares: map[string]*dynamic.Middleware{
|
|
|
|
"chain@file": {
|
|
|
|
Chain: &dynamic.Chain{Middlewares: []string{"m1", "m2", "m1@file"}},
|
|
|
|
},
|
|
|
|
"chain@docker": {
|
|
|
|
Chain: &dynamic.Chain{Middlewares: []string{"m1", "m2", "m1@file"}},
|
|
|
|
},
|
|
|
|
"m1@file": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m1"}},
|
|
|
|
"m2@file": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m2"}},
|
|
|
|
"m1@docker": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m1"}},
|
|
|
|
"m2@docker": {AddPrefix: &dynamic.AddPrefix{Prefix: "/m2"}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
roundTripperManager := service.NewRoundTripperManager()
|
|
|
|
roundTripperManager.Update(map[string]*dynamic.ServersTransport{"default@internal": {}})
|
|
|
|
serviceManager := service.NewManager(rtConf.Services, nil, nil, roundTripperManager)
|
2020-04-20 16:36:34 +00:00
|
|
|
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, nil)
|
2020-03-05 11:46:05 +00:00
|
|
|
chainBuilder := middleware.NewChainBuilder(staticCfg, nil, nil)
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
routerManager := NewManager(rtConf, serviceManager, middlewaresBuilder, chainBuilder, metrics.NewVoidRegistry())
|
2019-09-10 14:12:05 +00:00
|
|
|
|
|
|
|
_ = routerManager.BuildHandlers(context.Background(), entryPoints, false)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"chain@file", "m1@file"}, rtConf.Routers["router@file"].Middlewares)
|
|
|
|
assert.Equal(t, []string{"m1@file", "m2@file", "m1@file"}, rtConf.Middlewares["chain@file"].Chain.Middlewares)
|
|
|
|
assert.Equal(t, []string{"chain@docker", "m1@file"}, rtConf.Routers["router@docker"].Middlewares)
|
|
|
|
assert.Equal(t, []string{"m1@docker", "m2@docker", "m1@file"}, rtConf.Middlewares["chain@docker"].Chain.Middlewares)
|
|
|
|
}
|
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
type staticRoundTripperGetter struct {
|
|
|
|
res *http.Response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s staticRoundTripperGetter) Get(name string) (http.RoundTripper, error) {
|
|
|
|
return &staticTransport{res: s.res}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
type staticTransport struct {
|
|
|
|
res *http.Response
|
|
|
|
}
|
|
|
|
|
2020-07-08 10:18:03 +00:00
|
|
|
func (t *staticTransport) RoundTrip(_ *http.Request) (*http.Response, error) {
|
2019-03-14 08:30:04 +00:00
|
|
|
return t.res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkRouterServe(b *testing.B) {
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
|
|
|
|
2020-07-08 10:18:03 +00:00
|
|
|
b.Cleanup(func() { server.Close() })
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
res := &http.Response{
|
|
|
|
StatusCode: 200,
|
2021-03-04 19:08:03 +00:00
|
|
|
Body: io.NopCloser(strings.NewReader("")),
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
routersConfig := map[string]*dynamic.Router{
|
2019-03-14 08:30:04 +00:00
|
|
|
"foo": {
|
|
|
|
EntryPoints: []string{"web"},
|
|
|
|
Service: "foo-service",
|
|
|
|
Rule: "Host(`foo.bar`) && Path(`/`)",
|
|
|
|
},
|
|
|
|
}
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig := map[string]*dynamic.Service{
|
2019-03-14 08:30:04 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-03-14 08:30:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server.URL,
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
entryPoints := []string{"web"}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2019-05-16 08:58:06 +00:00
|
|
|
Services: serviceConfig,
|
|
|
|
Routers: routersConfig,
|
2019-07-10 07:26:04 +00:00
|
|
|
Middlewares: map[string]*dynamic.Middleware{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
|
|
|
})
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
serviceManager := service.NewManager(rtConf.Services, nil, nil, staticRoundTripperGetter{res})
|
2020-04-20 16:36:34 +00:00
|
|
|
middlewaresBuilder := middleware.NewBuilder(rtConf.Middlewares, serviceManager, nil)
|
2019-11-14 15:40:05 +00:00
|
|
|
chainBuilder := middleware.NewChainBuilder(static.Configuration{}, nil, nil)
|
|
|
|
|
2021-04-30 08:22:04 +00:00
|
|
|
routerManager := NewManager(rtConf, serviceManager, middlewaresBuilder, chainBuilder, metrics.NewVoidRegistry())
|
2019-03-14 08:30:04 +00:00
|
|
|
|
|
|
|
handlers := routerManager.BuildHandlers(context.Background(), entryPoints, false)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/", nil)
|
|
|
|
|
|
|
|
reqHost := requestdecorator.New(nil)
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
reqHost.ServeHTTP(w, req, handlers["web"].ServeHTTP)
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 08:58:06 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
func BenchmarkService(b *testing.B) {
|
|
|
|
res := &http.Response{
|
|
|
|
StatusCode: 200,
|
2021-03-04 19:08:03 +00:00
|
|
|
Body: io.NopCloser(strings.NewReader("")),
|
2019-03-14 08:30:04 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 07:26:04 +00:00
|
|
|
serviceConfig := map[string]*dynamic.Service{
|
2019-03-14 08:30:04 +00:00
|
|
|
"foo-service": {
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-03-14 08:30:04 +00:00
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: "tchouck",
|
2019-03-14 08:30:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:04:04 +00:00
|
|
|
rtConf := runtime.NewConfig(dynamic.Configuration{
|
2019-07-10 07:26:04 +00:00
|
|
|
HTTP: &dynamic.HTTPConfiguration{
|
2019-05-16 08:58:06 +00:00
|
|
|
Services: serviceConfig,
|
|
|
|
},
|
|
|
|
})
|
2019-11-14 15:40:05 +00:00
|
|
|
|
2020-09-11 13:40:03 +00:00
|
|
|
serviceManager := service.NewManager(rtConf.Services, nil, nil, staticRoundTripperGetter{res})
|
2019-03-14 08:30:04 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://foo.bar/", nil)
|
|
|
|
|
2020-09-01 16:16:04 +00:00
|
|
|
handler, _ := serviceManager.BuildHTTP(context.Background(), "foo-service")
|
2019-03-14 08:30:04 +00:00
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
handler.ServeHTTP(w, req)
|
|
|
|
}
|
|
|
|
}
|