2019-08-26 08:30:05 +00:00
|
|
|
package wrr
|
|
|
|
|
|
|
|
import (
|
2021-06-25 19:08:11 +00:00
|
|
|
"context"
|
2019-08-26 08:30:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2019-08-26 08:30:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBalancer(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2019-08-26 08:30:05 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-08-26 08:30:05 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(3))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-08-26 08:30:05 +00:00
|
|
|
rw.Header().Set("server", "second")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 3, recorder.save["first"])
|
|
|
|
assert.Equal(t, 1, recorder.save["second"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBalancerNoService(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2019-08-26 08:30:05 +00:00
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, recorder.Result().StatusCode)
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBalancerOneServerZeroWeight(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2019-08-26 08:30:05 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-08-26 08:30:05 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}), Int(0))
|
2019-08-26 08:30:05 +00:00
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 3, recorder.save["first"])
|
|
|
|
}
|
|
|
|
|
2021-06-25 19:08:11 +00:00
|
|
|
type key string
|
|
|
|
|
|
|
|
const serviceName key = "serviceName"
|
|
|
|
|
|
|
|
func TestBalancerNoServiceUp(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2021-06-25 19:08:11 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}), Int(1))
|
|
|
|
|
|
|
|
balancer.SetStatus(context.WithValue(context.Background(), serviceName, "parent"), "first", false)
|
|
|
|
balancer.SetStatus(context.WithValue(context.Background(), serviceName, "parent"), "second", false)
|
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, recorder.Result().StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBalancerOneServerDown(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2021-06-25 19:08:11 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}), Int(1))
|
|
|
|
balancer.SetStatus(context.WithValue(context.Background(), serviceName, "parent"), "second", false)
|
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 3, recorder.save["first"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBalancerDownThenUp(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2021-06-25 19:08:11 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "second")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
balancer.SetStatus(context.WithValue(context.Background(), serviceName, "parent"), "second", false)
|
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
assert.Equal(t, 3, recorder.save["first"])
|
|
|
|
|
|
|
|
balancer.SetStatus(context.WithValue(context.Background(), serviceName, "parent"), "second", true)
|
|
|
|
recorder = &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
assert.Equal(t, 1, recorder.save["first"])
|
|
|
|
assert.Equal(t, 1, recorder.save["second"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBalancerPropagate(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer1 := New(nil, true)
|
2021-06-25 19:08:11 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer1.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer1.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "second")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer2 := New(nil, true)
|
|
|
|
balancer2.Add("third", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "third")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer2.Add("fourth", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2021-06-25 19:08:11 +00:00
|
|
|
rw.Header().Set("server", "fourth")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
topBalancer := New(nil, true)
|
|
|
|
topBalancer.Add("balancer1", balancer1, Int(1))
|
2021-06-25 19:08:11 +00:00
|
|
|
_ = balancer1.RegisterStatusUpdater(func(up bool) {
|
|
|
|
topBalancer.SetStatus(context.WithValue(context.Background(), serviceName, "top"), "balancer1", up)
|
|
|
|
// TODO(mpl): if test gets flaky, add channel or something here to signal that
|
|
|
|
// propagation is done, and wait on it before sending request.
|
|
|
|
})
|
2022-11-16 10:38:07 +00:00
|
|
|
topBalancer.Add("balancer2", balancer2, Int(1))
|
2021-06-25 19:08:11 +00:00
|
|
|
_ = balancer2.RegisterStatusUpdater(func(up bool) {
|
|
|
|
topBalancer.SetStatus(context.WithValue(context.Background(), serviceName, "top"), "balancer2", up)
|
|
|
|
})
|
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
topBalancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
assert.Equal(t, 2, recorder.save["first"])
|
|
|
|
assert.Equal(t, 2, recorder.save["second"])
|
|
|
|
assert.Equal(t, 2, recorder.save["third"])
|
|
|
|
assert.Equal(t, 2, recorder.save["fourth"])
|
|
|
|
wantStatus := []int{200, 200, 200, 200, 200, 200, 200, 200}
|
|
|
|
assert.Equal(t, wantStatus, recorder.status)
|
|
|
|
|
|
|
|
// fourth gets downed, but balancer2 still up since third is still up.
|
|
|
|
balancer2.SetStatus(context.WithValue(context.Background(), serviceName, "top"), "fourth", false)
|
|
|
|
recorder = &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
topBalancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
assert.Equal(t, 2, recorder.save["first"])
|
|
|
|
assert.Equal(t, 2, recorder.save["second"])
|
|
|
|
assert.Equal(t, 4, recorder.save["third"])
|
|
|
|
assert.Equal(t, 0, recorder.save["fourth"])
|
|
|
|
wantStatus = []int{200, 200, 200, 200, 200, 200, 200, 200}
|
|
|
|
assert.Equal(t, wantStatus, recorder.status)
|
|
|
|
|
|
|
|
// third gets downed, and the propagation triggers balancer2 to be marked as
|
|
|
|
// down as well for topBalancer.
|
|
|
|
balancer2.SetStatus(context.WithValue(context.Background(), serviceName, "top"), "third", false)
|
|
|
|
recorder = &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
for i := 0; i < 8; i++ {
|
|
|
|
topBalancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
assert.Equal(t, 4, recorder.save["first"])
|
|
|
|
assert.Equal(t, 4, recorder.save["second"])
|
|
|
|
assert.Equal(t, 0, recorder.save["third"])
|
|
|
|
assert.Equal(t, 0, recorder.save["fourth"])
|
|
|
|
wantStatus = []int{200, 200, 200, 200, 200, 200, 200, 200}
|
|
|
|
assert.Equal(t, wantStatus, recorder.status)
|
|
|
|
}
|
|
|
|
|
2019-08-26 08:30:05 +00:00
|
|
|
func TestBalancerAllServersZeroWeight(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2019-08-26 08:30:05 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("test", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}), Int(0))
|
|
|
|
balancer.Add("test2", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {}), Int(0))
|
2019-08-26 08:30:05 +00:00
|
|
|
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, recorder.Result().StatusCode)
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSticky(t *testing.T) {
|
|
|
|
balancer := New(&dynamic.Sticky{
|
2023-08-08 13:12:06 +00:00
|
|
|
Cookie: &dynamic.Cookie{
|
|
|
|
Name: "test",
|
|
|
|
Secure: true,
|
|
|
|
HTTPOnly: true,
|
|
|
|
SameSite: "none",
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
}, false)
|
2019-08-26 08:30:05 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-08-26 08:30:05 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2019-08-26 08:30:05 +00:00
|
|
|
rw.Header().Set("server", "second")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(2))
|
|
|
|
|
2023-08-08 13:12:06 +00:00
|
|
|
recorder := &responseRecorder{
|
|
|
|
ResponseRecorder: httptest.NewRecorder(),
|
|
|
|
save: map[string]int{},
|
|
|
|
cookies: make(map[string]*http.Cookie),
|
|
|
|
}
|
2019-08-26 08:30:05 +00:00
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
for _, cookie := range recorder.Result().Cookies() {
|
2024-01-02 14:52:05 +00:00
|
|
|
assert.NotContains(t, "test=first", cookie.Value)
|
|
|
|
assert.NotContains(t, "test=second", cookie.Value)
|
2019-08-26 08:30:05 +00:00
|
|
|
req.AddCookie(cookie)
|
|
|
|
}
|
|
|
|
recorder.ResponseRecorder = httptest.NewRecorder()
|
|
|
|
|
|
|
|
balancer.ServeHTTP(recorder, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 0, recorder.save["first"])
|
|
|
|
assert.Equal(t, 3, recorder.save["second"])
|
2023-11-29 11:20:57 +00:00
|
|
|
assert.True(t, recorder.cookies["test"].HttpOnly)
|
|
|
|
assert.True(t, recorder.cookies["test"].Secure)
|
2023-08-08 13:12:06 +00:00
|
|
|
assert.Equal(t, http.SameSiteNoneMode, recorder.cookies["test"].SameSite)
|
2019-08-26 08:30:05 +00:00
|
|
|
}
|
2020-02-26 10:56:05 +00:00
|
|
|
|
2024-01-02 14:52:05 +00:00
|
|
|
func TestSticky_FallBack(t *testing.T) {
|
|
|
|
balancer := New(&dynamic.Sticky{
|
|
|
|
Cookie: &dynamic.Cookie{Name: "test"},
|
2024-01-03 15:45:47 +00:00
|
|
|
}, false)
|
2024-01-02 14:52:05 +00:00
|
|
|
|
2024-01-03 15:45:47 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2024-01-02 14:52:05 +00:00
|
|
|
rw.Header().Set("server", "first")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(1))
|
|
|
|
|
2024-01-03 15:45:47 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2024-01-02 14:52:05 +00:00
|
|
|
rw.Header().Set("server", "second")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(2))
|
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
req.AddCookie(&http.Cookie{Name: "test", Value: "second"})
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
recorder.ResponseRecorder = httptest.NewRecorder()
|
|
|
|
|
|
|
|
balancer.ServeHTTP(recorder, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, 0, recorder.save["first"])
|
2019-08-26 08:30:05 +00:00
|
|
|
assert.Equal(t, 3, recorder.save["second"])
|
|
|
|
}
|
2020-02-26 10:56:05 +00:00
|
|
|
|
|
|
|
// TestBalancerBias makes sure that the WRR algorithm spreads elements evenly right from the start,
|
|
|
|
// and that it does not "over-favor" the high-weighted ones with a biased start-up regime.
|
|
|
|
func TestBalancerBias(t *testing.T) {
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer := New(nil, false)
|
2020-02-26 10:56:05 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("first", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2020-02-26 10:56:05 +00:00
|
|
|
rw.Header().Set("server", "A")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(11))
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
balancer.Add("second", http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
2020-02-26 10:56:05 +00:00
|
|
|
rw.Header().Set("server", "B")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
}), Int(3))
|
|
|
|
|
|
|
|
recorder := &responseRecorder{ResponseRecorder: httptest.NewRecorder(), save: map[string]int{}}
|
|
|
|
|
|
|
|
for i := 0; i < 14; i++ {
|
|
|
|
balancer.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
wantSequence := []string{"A", "A", "A", "B", "A", "A", "A", "A", "B", "A", "A", "A", "B", "A"}
|
|
|
|
|
|
|
|
assert.Equal(t, wantSequence, recorder.sequence)
|
|
|
|
}
|
2022-11-16 10:38:07 +00:00
|
|
|
|
|
|
|
func Int(v int) *int { return &v }
|
|
|
|
|
|
|
|
type responseRecorder struct {
|
|
|
|
*httptest.ResponseRecorder
|
|
|
|
save map[string]int
|
|
|
|
sequence []string
|
|
|
|
status []int
|
2023-08-08 13:12:06 +00:00
|
|
|
cookies map[string]*http.Cookie
|
2022-11-16 10:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *responseRecorder) WriteHeader(statusCode int) {
|
|
|
|
r.save[r.Header().Get("server")]++
|
|
|
|
r.sequence = append(r.sequence, r.Header().Get("server"))
|
|
|
|
r.status = append(r.status, statusCode)
|
2023-08-08 13:12:06 +00:00
|
|
|
for _, cookie := range r.Result().Cookies() {
|
|
|
|
r.cookies[cookie.Name] = cookie
|
|
|
|
}
|
2022-11-16 10:38:07 +00:00
|
|
|
r.ResponseRecorder.WriteHeader(statusCode)
|
|
|
|
}
|