2018-11-14 09:18:03 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2019-06-12 22:42:06 +00:00
|
|
|
"strings"
|
2018-11-14 09:18:03 +00:00
|
|
|
"testing"
|
|
|
|
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
|
|
|
"github.com/containous/traefik/v2/pkg/config/runtime"
|
|
|
|
"github.com/containous/traefik/v2/pkg/server/internal"
|
|
|
|
"github.com/containous/traefik/v2/pkg/testhelpers"
|
2018-11-14 09:18:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockForwarder struct{}
|
|
|
|
|
|
|
|
func (MockForwarder) ServeHTTP(http.ResponseWriter, *http.Request) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLoadBalancer(t *testing.T) {
|
|
|
|
sm := Manager{}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
serviceName string
|
2019-08-26 08:30:05 +00:00
|
|
|
service *dynamic.ServersLoadBalancer
|
2018-11-14 09:18:03 +00:00
|
|
|
fwd http.Handler
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Fails when provided an invalid URL",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &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: ":",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fwd: &MockForwarder{},
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Succeeds when there are no servers",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{},
|
2018-11-14 09:18:03 +00:00
|
|
|
fwd: &MockForwarder{},
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
{
|
2019-08-26 08:30:05 +00:00
|
|
|
desc: "Succeeds when sticky.cookie is set",
|
2018-11-14 09:18:03 +00:00
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{
|
|
|
|
Sticky: &dynamic.Sticky{Cookie: &dynamic.Cookie{}},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
fwd: &MockForwarder{},
|
|
|
|
expectError: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
handler, err := sm.getLoadBalancer(context.Background(), test.serviceName, test.service, test.fwd)
|
2018-11-14 09:18:03 +00:00
|
|
|
if test.expectError {
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Nil(t, handler)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, handler)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLoadBalancerServiceHandler(t *testing.T) {
|
2019-09-06 13:08:04 +00:00
|
|
|
sm := NewManager(nil, http.DefaultTransport, nil, nil, nil, nil)
|
2018-11-14 09:18:03 +00:00
|
|
|
|
|
|
|
server1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-From", "first")
|
|
|
|
}))
|
|
|
|
defer server1.Close()
|
|
|
|
|
|
|
|
server2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-From", "second")
|
|
|
|
}))
|
|
|
|
defer server2.Close()
|
|
|
|
|
|
|
|
serverPassHost := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-From", "passhost")
|
|
|
|
assert.Equal(t, "callme", r.Host)
|
|
|
|
}))
|
|
|
|
defer serverPassHost.Close()
|
|
|
|
|
|
|
|
serverPassHostFalse := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-From", "passhostfalse")
|
|
|
|
assert.NotEqual(t, "callme", r.Host)
|
|
|
|
}))
|
|
|
|
defer serverPassHostFalse.Close()
|
|
|
|
|
|
|
|
type ExpectedResult struct {
|
2019-06-12 22:42:06 +00:00
|
|
|
StatusCode int
|
|
|
|
XFrom string
|
|
|
|
SecureCookie bool
|
|
|
|
HTTPOnlyCookie bool
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
serviceName string
|
2019-08-26 08:30:05 +00:00
|
|
|
service *dynamic.ServersLoadBalancer
|
2018-11-14 09:18:03 +00:00
|
|
|
responseModifier func(*http.Response) error
|
|
|
|
|
|
|
|
expected []ExpectedResult
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Load balances between the two servers",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &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: server1.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server2.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "first",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "second",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "StatusBadGateway when the server is not reachable",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &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: "http://foo",
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusBadGateway,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "ServiceUnavailable when no servers are available",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{},
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusServiceUnavailable,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-08-26 08:30:05 +00:00
|
|
|
desc: "Always call the same server when sticky.cookie is true",
|
2018-11-14 09:18:03 +00:00
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{
|
|
|
|
Sticky: &dynamic.Sticky{Cookie: &dynamic.Cookie{}},
|
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: server1.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
{
|
2019-06-05 20:18:06 +00:00
|
|
|
URL: server2.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "first",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "first",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-06-12 22:42:06 +00:00
|
|
|
{
|
|
|
|
desc: "Sticky Cookie's options set correctly",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{
|
|
|
|
Sticky: &dynamic.Sticky{Cookie: &dynamic.Cookie{HTTPOnly: true, Secure: true}},
|
2019-07-10 07:26:04 +00:00
|
|
|
Servers: []dynamic.Server{
|
2019-06-12 22:42:06 +00:00
|
|
|
{
|
|
|
|
URL: server1.URL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "first",
|
|
|
|
SecureCookie: true,
|
|
|
|
HTTPOnlyCookie: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 09:18:03 +00:00
|
|
|
{
|
|
|
|
desc: "PassHost passes the host instead of the IP",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{
|
|
|
|
Sticky: &dynamic.Sticky{Cookie: &dynamic.Cookie{}},
|
2018-11-14 09:18:03 +00:00
|
|
|
PassHostHeader: true,
|
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: serverPassHost.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "passhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "PassHost doesn't passe the host instead of the IP",
|
|
|
|
serviceName: "test",
|
2019-08-26 08:30:05 +00:00
|
|
|
service: &dynamic.ServersLoadBalancer{
|
|
|
|
Sticky: &dynamic.Sticky{Cookie: &dynamic.Cookie{}},
|
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: serverPassHostFalse.URL,
|
2018-11-14 09:18:03 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: []ExpectedResult{
|
|
|
|
{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
XFrom: "passhostfalse",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
handler, err := sm.getLoadBalancerServiceHandler(context.Background(), test.serviceName, test.service, test.responseModifier)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, handler)
|
|
|
|
|
|
|
|
req := testhelpers.MustNewRequest(http.MethodGet, "http://callme", nil)
|
|
|
|
for _, expected := range test.expected {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
|
|
|
|
handler.ServeHTTP(recorder, req)
|
|
|
|
|
|
|
|
assert.Equal(t, expected.StatusCode, recorder.Code)
|
|
|
|
assert.Equal(t, expected.XFrom, recorder.Header().Get("X-From"))
|
|
|
|
|
2019-06-12 22:42:06 +00:00
|
|
|
cookieHeader := recorder.Header().Get("Set-Cookie")
|
|
|
|
if len(cookieHeader) > 0 {
|
|
|
|
req.Header.Set("Cookie", cookieHeader)
|
|
|
|
assert.Equal(t, expected.SecureCookie, strings.Contains(cookieHeader, "Secure"))
|
|
|
|
assert.Equal(t, expected.HTTPOnlyCookie, strings.Contains(cookieHeader, "HttpOnly"))
|
2018-11-14 09:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:28:04 +00:00
|
|
|
func TestManager_Build(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
serviceName string
|
2019-07-15 15:04:04 +00:00
|
|
|
configs map[string]*runtime.ServiceInfo
|
2019-01-15 13:28:04 +00:00
|
|
|
providerName string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "Simple service name",
|
|
|
|
serviceName: "serviceName",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.ServiceInfo{
|
2019-01-15 13:28:04 +00:00
|
|
|
"serviceName": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Service: &dynamic.Service{
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Service name with provider",
|
2019-06-21 07:54:04 +00:00
|
|
|
serviceName: "serviceName@provider-1",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Service: &dynamic.Service{
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "Service name with provider in context",
|
|
|
|
serviceName: "serviceName",
|
2019-07-15 15:04:04 +00:00
|
|
|
configs: map[string]*runtime.ServiceInfo{
|
2019-06-21 07:54:04 +00:00
|
|
|
"serviceName@provider-1": {
|
2019-07-10 07:26:04 +00:00
|
|
|
Service: &dynamic.Service{
|
2019-08-26 08:30:05 +00:00
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{},
|
2019-05-16 08:58:06 +00:00
|
|
|
},
|
2019-01-15 13:28:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
providerName: "provider-1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-09-06 13:08:04 +00:00
|
|
|
manager := NewManager(test.configs, http.DefaultTransport, nil, nil, nil, nil)
|
2019-01-15 13:28:04 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
if len(test.providerName) > 0 {
|
2019-06-21 07:54:04 +00:00
|
|
|
ctx = internal.AddProviderInContext(ctx, "foobar@"+test.providerName)
|
2019-01-15 13:28:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
_, err := manager.BuildHTTP(ctx, test.serviceName, nil)
|
2019-01-15 13:28:04 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-26 17:00:04 +00:00
|
|
|
func TestMultipleTypeOnBuildHTTP(t *testing.T) {
|
|
|
|
manager := NewManager(map[string]*runtime.ServiceInfo{
|
|
|
|
"test@file": {
|
|
|
|
Service: &dynamic.Service{
|
|
|
|
LoadBalancer: &dynamic.ServersLoadBalancer{},
|
|
|
|
Weighted: &dynamic.WeightedRoundRobin{},
|
|
|
|
},
|
|
|
|
},
|
2019-09-06 13:08:04 +00:00
|
|
|
}, http.DefaultTransport, nil, nil, nil, nil)
|
2019-08-26 17:00:04 +00:00
|
|
|
|
|
|
|
_, err := manager.BuildHTTP(context.Background(), "test@file", nil)
|
|
|
|
assert.Error(t, err, "cannot create service: multi-types service not supported, consider declaring two different pieces of service instead")
|
|
|
|
}
|
|
|
|
|
2018-11-14 09:18:03 +00:00
|
|
|
// FIXME Add healthcheck tests
|