1
0
Fork 0

add http headers to healthcheck

This commit is contained in:
Jesse Haka 2018-04-16 11:40:03 +02:00 committed by Traefiker Bot
parent de3aeb9732
commit 1954a49f37
29 changed files with 337 additions and 64 deletions

View file

@ -10,6 +10,8 @@ import (
"time"
"github.com/containous/traefik/testhelpers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vulcand/oxy/roundrobin"
)
@ -21,57 +23,57 @@ type testHandler struct {
}
func TestSetBackendsConfiguration(t *testing.T) {
tests := []struct {
desc string
startHealthy bool
healthSequence []bool
wantNumRemovedServers int
wantNumUpsertedServers int
wantGaugeValue float64
testCases := []struct {
desc string
startHealthy bool
healthSequence []bool
expectedNumRemovedServers int
expectedNumUpsertedServers int
expectedGaugeValue float64
}{
{
desc: "healthy server staying healthy",
startHealthy: true,
healthSequence: []bool{true},
wantNumRemovedServers: 0,
wantNumUpsertedServers: 0,
wantGaugeValue: 1,
desc: "healthy server staying healthy",
startHealthy: true,
healthSequence: []bool{true},
expectedNumRemovedServers: 0,
expectedNumUpsertedServers: 0,
expectedGaugeValue: 1,
},
{
desc: "healthy server becoming sick",
startHealthy: true,
healthSequence: []bool{false},
wantNumRemovedServers: 1,
wantNumUpsertedServers: 0,
wantGaugeValue: 0,
desc: "healthy server becoming sick",
startHealthy: true,
healthSequence: []bool{false},
expectedNumRemovedServers: 1,
expectedNumUpsertedServers: 0,
expectedGaugeValue: 0,
},
{
desc: "sick server becoming healthy",
startHealthy: false,
healthSequence: []bool{true},
wantNumRemovedServers: 0,
wantNumUpsertedServers: 1,
wantGaugeValue: 1,
desc: "sick server becoming healthy",
startHealthy: false,
healthSequence: []bool{true},
expectedNumRemovedServers: 0,
expectedNumUpsertedServers: 1,
expectedGaugeValue: 1,
},
{
desc: "sick server staying sick",
startHealthy: false,
healthSequence: []bool{false},
wantNumRemovedServers: 0,
wantNumUpsertedServers: 0,
wantGaugeValue: 0,
desc: "sick server staying sick",
startHealthy: false,
healthSequence: []bool{false},
expectedNumRemovedServers: 0,
expectedNumUpsertedServers: 0,
expectedGaugeValue: 0,
},
{
desc: "healthy server toggling to sick and back to healthy",
startHealthy: true,
healthSequence: []bool{false, true},
wantNumRemovedServers: 1,
wantNumUpsertedServers: 1,
wantGaugeValue: 1,
desc: "healthy server toggling to sick and back to healthy",
startHealthy: true,
healthSequence: []bool{false, true},
expectedNumRemovedServers: 1,
expectedNumUpsertedServers: 1,
expectedGaugeValue: 1,
},
}
for _, test := range tests {
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
@ -88,6 +90,7 @@ func TestSetBackendsConfiguration(t *testing.T) {
Interval: healthCheckInterval,
LB: lb,
}, "backendName")
serverURL := testhelpers.MustParseURL(ts.URL)
if test.startHealthy {
lb.servers = append(lb.servers, serverURL)
@ -100,8 +103,10 @@ func TestSetBackendsConfiguration(t *testing.T) {
Backends: make(map[string]*BackendHealthCheck),
metrics: collectingMetrics,
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
check.execute(ctx, backend)
wg.Done()
@ -119,23 +124,16 @@ func TestSetBackendsConfiguration(t *testing.T) {
lb.Lock()
defer lb.Unlock()
if lb.numRemovedServers != test.wantNumRemovedServers {
t.Errorf("got %d removed servers, wanted %d", lb.numRemovedServers, test.wantNumRemovedServers)
}
if lb.numUpsertedServers != test.wantNumUpsertedServers {
t.Errorf("got %d upserted servers, wanted %d", lb.numUpsertedServers, test.wantNumUpsertedServers)
}
if collectingMetrics.Gauge.GaugeValue != test.wantGaugeValue {
t.Errorf("got %v ServerUp Gauge, want %v", collectingMetrics.Gauge.GaugeValue, test.wantGaugeValue)
}
assert.Equal(t, test.expectedNumRemovedServers, lb.numRemovedServers, "removed servers")
assert.Equal(t, test.expectedNumUpsertedServers, lb.numUpsertedServers, "upserted servers")
assert.Equal(t, test.expectedGaugeValue, collectingMetrics.Gauge.GaugeValue, "ServerUp Gauge")
})
}
}
func TestNewRequest(t *testing.T) {
tests := []struct {
testCases := []struct {
desc string
host string
port int
@ -172,10 +170,11 @@ func TestNewRequest(t *testing.T) {
},
}
for _, test := range tests {
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
backend := NewBackendHealthCheck(
Options{
Path: test.path,
@ -187,15 +186,94 @@ func TestNewRequest(t *testing.T) {
Host: test.host,
}
req, err := backend.newRequest(u)
require.NoError(t, err, "failed to create new backend request")
assert.Equal(t, test.expected, req.URL.String())
})
}
}
func TestNewRequestWithAddHeaders(t *testing.T) {
testCases := []struct {
desc string
host string
headers map[string]string
hostname string
port int
path string
expectedHostname string
expectedHeader string
}{
{
desc: "override hostname",
host: "backend1:80",
headers: map[string]string{},
hostname: "myhost",
port: 0,
path: "/",
expectedHostname: "myhost",
expectedHeader: "",
},
{
desc: "not override hostname",
host: "backend1:80",
headers: map[string]string{},
hostname: "",
port: 0,
path: "/",
expectedHostname: "backend1:80",
expectedHeader: "",
},
{
desc: "custom header",
host: "backend1:80",
headers: map[string]string{"Custom-Header": "foo"},
hostname: "",
port: 0,
path: "/",
expectedHostname: "backend1:80",
expectedHeader: "foo",
},
{
desc: "custom header with host override",
host: "backend1:80",
headers: map[string]string{"Custom-Header": "foo"},
hostname: "myhost",
port: 0,
path: "/",
expectedHostname: "myhost",
expectedHeader: "foo",
},
}
for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
backend := NewBackendHealthCheck(
Options{
Hostname: test.hostname,
Path: test.path,
Port: test.port,
Headers: test.headers,
}, "backendName")
u := &url.URL{
Scheme: "http",
Host: test.host,
}
req, err := backend.newRequest(u)
if err != nil {
t.Fatalf("failed to create new backend request: %s", err)
}
actual := req.URL.String()
if actual != test.expected {
t.Fatalf("got %s for healthcheck URL, wanted %s", actual, test.expected)
}
req = backend.addHeadersAndHost(req)
assert.Equal(t, test.expectedHostname, req.Host)
assert.Equal(t, test.expectedHeader, req.Header.Get("Custom-Header"))
})
}
}