mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-08 17:16:36 +01:00
Migrate some selection policy tests over to v2
This commit is contained in:
parent
50e62d06bc
commit
b4f4fcd437
4 changed files with 246 additions and 334 deletions
|
@ -55,7 +55,7 @@ type Transport struct {
|
||||||
// PATH_INFO for the CGI script to use.
|
// PATH_INFO for the CGI script to use.
|
||||||
SplitPath string `json:"split_path,omitempty"`
|
SplitPath string `json:"split_path,omitempty"`
|
||||||
|
|
||||||
// Environment Variables
|
// Environment variables (TODO: make a map of string to value...?)
|
||||||
EnvVars [][2]string `json:"env,omitempty"`
|
EnvVars [][2]string `json:"env,omitempty"`
|
||||||
|
|
||||||
// The duration used to set a deadline when connecting to an upstream.
|
// The duration used to set a deadline when connecting to an upstream.
|
||||||
|
|
|
@ -34,19 +34,21 @@ type Host interface {
|
||||||
// Unhealthy returns true if the backend is unhealthy.
|
// Unhealthy returns true if the backend is unhealthy.
|
||||||
Unhealthy() bool
|
Unhealthy() bool
|
||||||
|
|
||||||
// CountRequest counts the given number of requests
|
// CountRequest atomically counts the given number of
|
||||||
// as currently in process with the host. The count
|
// requests as currently in process with the host. The
|
||||||
// should not go below 0.
|
// count should not go below 0.
|
||||||
CountRequest(int) error
|
CountRequest(int) error
|
||||||
|
|
||||||
// CountFail counts the given number of failures
|
// CountFail atomically counts the given number of
|
||||||
// with the host. The count should not go below 0.
|
// failures with the host. The count should not go
|
||||||
|
// below 0.
|
||||||
CountFail(int) error
|
CountFail(int) error
|
||||||
|
|
||||||
// SetHealthy marks the host as either healthy (true)
|
// SetHealthy atomically marks the host as either
|
||||||
// or unhealthy (false). If the given status is the
|
// healthy (true) or unhealthy (false). If the given
|
||||||
// same, this should be a no-op. It returns true if
|
// status is the same, this should be a no-op and
|
||||||
// the given status was different, false otherwise.
|
// return false. It returns true if the status was
|
||||||
|
// changed; i.e. if it is now different from before.
|
||||||
SetHealthy(bool) (bool, error)
|
SetHealthy(bool) (bool, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ type RandomChoiceSelection struct {
|
||||||
// CaddyModule returns the Caddy module information.
|
// CaddyModule returns the Caddy module information.
|
||||||
func (RandomChoiceSelection) CaddyModule() caddy.ModuleInfo {
|
func (RandomChoiceSelection) CaddyModule() caddy.ModuleInfo {
|
||||||
return caddy.ModuleInfo{
|
return caddy.ModuleInfo{
|
||||||
Name: "http.handlers.reverse_proxy.selection_policies.random_choice",
|
Name: "http.handlers.reverse_proxy.selection_policies.random_choose",
|
||||||
New: func() caddy.Module { return new(RandomChoiceSelection) },
|
New: func() caddy.Module { return new(RandomChoiceSelection) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -147,14 +147,14 @@ func (LeastConnSelection) CaddyModule() caddy.ModuleInfo {
|
||||||
func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request) *Upstream {
|
func (LeastConnSelection) Select(pool UpstreamPool, _ *http.Request) *Upstream {
|
||||||
var bestHost *Upstream
|
var bestHost *Upstream
|
||||||
var count int
|
var count int
|
||||||
var leastReqs int
|
leastReqs := -1
|
||||||
|
|
||||||
for _, host := range pool {
|
for _, host := range pool {
|
||||||
if !host.Available() {
|
if !host.Available() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
numReqs := host.NumRequests()
|
numReqs := host.NumRequests()
|
||||||
if numReqs < leastReqs {
|
if leastReqs == -1 || numReqs < leastReqs {
|
||||||
leastReqs = numReqs
|
leastReqs = numReqs
|
||||||
count = 0
|
count = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,350 +14,260 @@
|
||||||
|
|
||||||
package reverseproxy
|
package reverseproxy
|
||||||
|
|
||||||
// TODO: finish migrating these
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
// import (
|
func testPool() UpstreamPool {
|
||||||
// "net/http"
|
return UpstreamPool{
|
||||||
// "net/http/httptest"
|
{Host: new(upstreamHost)},
|
||||||
// "os"
|
{Host: new(upstreamHost)},
|
||||||
// "testing"
|
{Host: new(upstreamHost)},
|
||||||
// )
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// var workableServer *httptest.Server
|
func TestRoundRobinPolicy(t *testing.T) {
|
||||||
|
pool := testPool()
|
||||||
|
rrPolicy := new(RoundRobinSelection)
|
||||||
|
req, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
|
||||||
// func TestMain(m *testing.M) {
|
h := rrPolicy.Select(pool, req)
|
||||||
// workableServer = httptest.NewServer(http.HandlerFunc(
|
// First selected host is 1, because counter starts at 0
|
||||||
// func(w http.ResponseWriter, r *http.Request) {
|
// and increments before host is selected
|
||||||
// // do nothing
|
if h != pool[1] {
|
||||||
// }))
|
t.Error("Expected first round robin host to be second host in the pool.")
|
||||||
// r := m.Run()
|
}
|
||||||
// workableServer.Close()
|
h = rrPolicy.Select(pool, req)
|
||||||
// os.Exit(r)
|
if h != pool[2] {
|
||||||
// }
|
t.Error("Expected second round robin host to be third host in the pool.")
|
||||||
|
}
|
||||||
|
h = rrPolicy.Select(pool, req)
|
||||||
|
if h != pool[0] {
|
||||||
|
t.Error("Expected third round robin host to be first host in the pool.")
|
||||||
|
}
|
||||||
|
// mark host as down
|
||||||
|
pool[1].SetHealthy(false)
|
||||||
|
h = rrPolicy.Select(pool, req)
|
||||||
|
if h != pool[2] {
|
||||||
|
t.Error("Expected to skip down host.")
|
||||||
|
}
|
||||||
|
// mark host as up
|
||||||
|
pool[1].SetHealthy(true)
|
||||||
|
|
||||||
// type customPolicy struct{}
|
h = rrPolicy.Select(pool, req)
|
||||||
|
if h == pool[2] {
|
||||||
|
t.Error("Expected to balance evenly among healthy hosts")
|
||||||
|
}
|
||||||
|
// mark host as full
|
||||||
|
pool[1].CountRequest(1)
|
||||||
|
pool[1].MaxRequests = 1
|
||||||
|
h = rrPolicy.Select(pool, req)
|
||||||
|
if h != pool[2] {
|
||||||
|
t.Error("Expected to skip full host.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// func (customPolicy) Select(pool HostPool, _ *http.Request) Host {
|
func TestLeastConnPolicy(t *testing.T) {
|
||||||
// return pool[0]
|
pool := testPool()
|
||||||
// }
|
lcPolicy := new(LeastConnSelection)
|
||||||
|
req, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
|
||||||
// func testPool() HostPool {
|
pool[0].CountRequest(10)
|
||||||
// pool := []*UpstreamHost{
|
pool[1].CountRequest(10)
|
||||||
// {
|
h := lcPolicy.Select(pool, req)
|
||||||
// Name: workableServer.URL, // this should resolve (healthcheck test)
|
if h != pool[2] {
|
||||||
// },
|
t.Error("Expected least connection host to be third host.")
|
||||||
// {
|
}
|
||||||
// Name: "http://localhost:99998", // this shouldn't
|
pool[2].CountRequest(100)
|
||||||
// },
|
h = lcPolicy.Select(pool, req)
|
||||||
// {
|
if h != pool[0] && h != pool[1] {
|
||||||
// Name: "http://C",
|
t.Error("Expected least connection host to be first or second host.")
|
||||||
// },
|
}
|
||||||
// }
|
}
|
||||||
// return HostPool(pool)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestRoundRobinPolicy(t *testing.T) {
|
func TestIPHashPolicy(t *testing.T) {
|
||||||
// pool := testPool()
|
pool := testPool()
|
||||||
// rrPolicy := &RoundRobin{}
|
ipHash := new(IPHashSelection)
|
||||||
// request, _ := http.NewRequest("GET", "/", nil)
|
req, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
|
||||||
// h := rrPolicy.Select(pool, request)
|
// We should be able to predict where every request is routed.
|
||||||
// // First selected host is 1, because counter starts at 0
|
req.RemoteAddr = "172.0.0.1:80"
|
||||||
// // and increments before host is selected
|
h := ipHash.Select(pool, req)
|
||||||
// if h != pool[1] {
|
if h != pool[1] {
|
||||||
// t.Error("Expected first round robin host to be second host in the pool.")
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
// }
|
}
|
||||||
// h = rrPolicy.Select(pool, request)
|
req.RemoteAddr = "172.0.0.2:80"
|
||||||
// if h != pool[2] {
|
h = ipHash.Select(pool, req)
|
||||||
// t.Error("Expected second round robin host to be third host in the pool.")
|
if h != pool[1] {
|
||||||
// }
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
// h = rrPolicy.Select(pool, request)
|
}
|
||||||
// if h != pool[0] {
|
req.RemoteAddr = "172.0.0.3:80"
|
||||||
// t.Error("Expected third round robin host to be first host in the pool.")
|
h = ipHash.Select(pool, req)
|
||||||
// }
|
if h != pool[2] {
|
||||||
// // mark host as down
|
t.Error("Expected ip hash policy host to be the third host.")
|
||||||
// pool[1].Unhealthy = 1
|
}
|
||||||
// h = rrPolicy.Select(pool, request)
|
req.RemoteAddr = "172.0.0.4:80"
|
||||||
// if h != pool[2] {
|
h = ipHash.Select(pool, req)
|
||||||
// t.Error("Expected to skip down host.")
|
if h != pool[1] {
|
||||||
// }
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
// // mark host as up
|
}
|
||||||
// pool[1].Unhealthy = 0
|
|
||||||
|
|
||||||
// h = rrPolicy.Select(pool, request)
|
// we should get the same results without a port
|
||||||
// if h == pool[2] {
|
req.RemoteAddr = "172.0.0.1"
|
||||||
// t.Error("Expected to balance evenly among healthy hosts")
|
h = ipHash.Select(pool, req)
|
||||||
// }
|
if h != pool[1] {
|
||||||
// // mark host as full
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
// pool[1].Conns = 1
|
}
|
||||||
// pool[1].MaxConns = 1
|
req.RemoteAddr = "172.0.0.2"
|
||||||
// h = rrPolicy.Select(pool, request)
|
h = ipHash.Select(pool, req)
|
||||||
// if h != pool[2] {
|
if h != pool[1] {
|
||||||
// t.Error("Expected to skip full host.")
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
// }
|
}
|
||||||
// }
|
req.RemoteAddr = "172.0.0.3"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[2] {
|
||||||
|
t.Error("Expected ip hash policy host to be the third host.")
|
||||||
|
}
|
||||||
|
req.RemoteAddr = "172.0.0.4"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[1] {
|
||||||
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
|
}
|
||||||
|
|
||||||
// func TestLeastConnPolicy(t *testing.T) {
|
// we should get a healthy host if the original host is unhealthy and a
|
||||||
// pool := testPool()
|
// healthy host is available
|
||||||
// lcPolicy := &LeastConn{}
|
req.RemoteAddr = "172.0.0.1"
|
||||||
// request, _ := http.NewRequest("GET", "/", nil)
|
pool[1].SetHealthy(false)
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[2] {
|
||||||
|
t.Error("Expected ip hash policy host to be the third host.")
|
||||||
|
}
|
||||||
|
|
||||||
// pool[0].Conns = 10
|
req.RemoteAddr = "172.0.0.2"
|
||||||
// pool[1].Conns = 10
|
h = ipHash.Select(pool, req)
|
||||||
// h := lcPolicy.Select(pool, request)
|
if h != pool[2] {
|
||||||
// if h != pool[2] {
|
t.Error("Expected ip hash policy host to be the third host.")
|
||||||
// t.Error("Expected least connection host to be third host.")
|
}
|
||||||
// }
|
pool[1].SetHealthy(true)
|
||||||
// pool[2].Conns = 100
|
|
||||||
// h = lcPolicy.Select(pool, request)
|
|
||||||
// if h != pool[0] && h != pool[1] {
|
|
||||||
// t.Error("Expected least connection host to be first or second host.")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestCustomPolicy(t *testing.T) {
|
req.RemoteAddr = "172.0.0.3"
|
||||||
// pool := testPool()
|
pool[2].SetHealthy(false)
|
||||||
// customPolicy := &customPolicy{}
|
h = ipHash.Select(pool, req)
|
||||||
// request, _ := http.NewRequest("GET", "/", nil)
|
if h != pool[0] {
|
||||||
|
t.Error("Expected ip hash policy host to be the first host.")
|
||||||
|
}
|
||||||
|
req.RemoteAddr = "172.0.0.4"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[1] {
|
||||||
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
|
}
|
||||||
|
|
||||||
// h := customPolicy.Select(pool, request)
|
// We should be able to resize the host pool and still be able to predict
|
||||||
// if h != pool[0] {
|
// where a req will be routed with the same IP's used above
|
||||||
// t.Error("Expected custom policy host to be the first host.")
|
pool = UpstreamPool{
|
||||||
// }
|
{Host: new(upstreamHost)},
|
||||||
// }
|
{Host: new(upstreamHost)},
|
||||||
|
}
|
||||||
|
req.RemoteAddr = "172.0.0.1:80"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[0] {
|
||||||
|
t.Error("Expected ip hash policy host to be the first host.")
|
||||||
|
}
|
||||||
|
req.RemoteAddr = "172.0.0.2:80"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[1] {
|
||||||
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
|
}
|
||||||
|
req.RemoteAddr = "172.0.0.3:80"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[0] {
|
||||||
|
t.Error("Expected ip hash policy host to be the first host.")
|
||||||
|
}
|
||||||
|
req.RemoteAddr = "172.0.0.4:80"
|
||||||
|
h = ipHash.Select(pool, req)
|
||||||
|
if h != pool[1] {
|
||||||
|
t.Error("Expected ip hash policy host to be the second host.")
|
||||||
|
}
|
||||||
|
|
||||||
// func TestIPHashPolicy(t *testing.T) {
|
// We should get nil when there are no healthy hosts
|
||||||
// pool := testPool()
|
pool[0].SetHealthy(false)
|
||||||
// ipHash := &IPHash{}
|
pool[1].SetHealthy(false)
|
||||||
// request, _ := http.NewRequest("GET", "/", nil)
|
h = ipHash.Select(pool, req)
|
||||||
// // We should be able to predict where every request is routed.
|
if h != nil {
|
||||||
// request.RemoteAddr = "172.0.0.1:80"
|
t.Error("Expected ip hash policy host to be nil.")
|
||||||
// h := ipHash.Select(pool, request)
|
}
|
||||||
// if h != pool[1] {
|
}
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.2:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.3:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[2] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the third host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.4:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // we should get the same results without a port
|
func TestFirstPolicy(t *testing.T) {
|
||||||
// request.RemoteAddr = "172.0.0.1"
|
pool := testPool()
|
||||||
// h = ipHash.Select(pool, request)
|
firstPolicy := new(FirstSelection)
|
||||||
// if h != pool[1] {
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.2"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.3"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[2] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the third host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.4"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // we should get a healthy host if the original host is unhealthy and a
|
h := firstPolicy.Select(pool, req)
|
||||||
// // healthy host is available
|
if h != pool[0] {
|
||||||
// request.RemoteAddr = "172.0.0.1"
|
t.Error("Expected first policy host to be the first host.")
|
||||||
// pool[1].Unhealthy = 1
|
}
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[2] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the third host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// request.RemoteAddr = "172.0.0.2"
|
pool[0].SetHealthy(false)
|
||||||
// h = ipHash.Select(pool, request)
|
h = firstPolicy.Select(pool, req)
|
||||||
// if h != pool[2] {
|
if h != pool[1] {
|
||||||
// t.Error("Expected ip hash policy host to be the third host.")
|
t.Error("Expected first policy host to be the second host.")
|
||||||
// }
|
}
|
||||||
// pool[1].Unhealthy = 0
|
}
|
||||||
|
|
||||||
// request.RemoteAddr = "172.0.0.3"
|
func TestURIHashPolicy(t *testing.T) {
|
||||||
// pool[2].Unhealthy = 1
|
pool := testPool()
|
||||||
// h = ipHash.Select(pool, request)
|
uriPolicy := new(URIHashSelection)
|
||||||
// if h != pool[0] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the first host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.4"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // We should be able to resize the host pool and still be able to predict
|
request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
// // where a request will be routed with the same IP's used above
|
h := uriPolicy.Select(pool, request)
|
||||||
// pool = []*UpstreamHost{
|
if h != pool[0] {
|
||||||
// {
|
t.Error("Expected uri policy host to be the first host.")
|
||||||
// Name: workableServer.URL, // this should resolve (healthcheck test)
|
}
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Name: "http://localhost:99998", // this shouldn't
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
// pool = HostPool(pool)
|
|
||||||
// request.RemoteAddr = "172.0.0.1:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[0] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the first host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.2:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.3:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[0] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the first host.")
|
|
||||||
// }
|
|
||||||
// request.RemoteAddr = "172.0.0.4:80"
|
|
||||||
// h = ipHash.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected ip hash policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // We should get nil when there are no healthy hosts
|
pool[0].SetHealthy(false)
|
||||||
// pool[0].Unhealthy = 1
|
h = uriPolicy.Select(pool, request)
|
||||||
// pool[1].Unhealthy = 1
|
if h != pool[1] {
|
||||||
// h = ipHash.Select(pool, request)
|
t.Error("Expected uri policy host to be the first host.")
|
||||||
// if h != nil {
|
}
|
||||||
// t.Error("Expected ip hash policy host to be nil.")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestFirstPolicy(t *testing.T) {
|
request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
|
||||||
// pool := testPool()
|
h = uriPolicy.Select(pool, request)
|
||||||
// firstPolicy := &First{}
|
if h != pool[1] {
|
||||||
// req := httptest.NewRequest(http.MethodGet, "/", nil)
|
t.Error("Expected uri policy host to be the second host.")
|
||||||
|
}
|
||||||
|
|
||||||
// h := firstPolicy.Select(pool, req)
|
// We should be able to resize the host pool and still be able to predict
|
||||||
// if h != pool[0] {
|
// where a request will be routed with the same URI's used above
|
||||||
// t.Error("Expected first policy host to be the first host.")
|
pool = UpstreamPool{
|
||||||
// }
|
{Host: new(upstreamHost)},
|
||||||
|
{Host: new(upstreamHost)},
|
||||||
|
}
|
||||||
|
|
||||||
// pool[0].Unhealthy = 1
|
request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
||||||
// h = firstPolicy.Select(pool, req)
|
h = uriPolicy.Select(pool, request)
|
||||||
// if h != pool[1] {
|
if h != pool[0] {
|
||||||
// t.Error("Expected first policy host to be the second host.")
|
t.Error("Expected uri policy host to be the first host.")
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestUriPolicy(t *testing.T) {
|
pool[0].SetHealthy(false)
|
||||||
// pool := testPool()
|
h = uriPolicy.Select(pool, request)
|
||||||
// uriPolicy := &URIHash{}
|
if h != pool[1] {
|
||||||
|
t.Error("Expected uri policy host to be the first host.")
|
||||||
|
}
|
||||||
|
|
||||||
// request := httptest.NewRequest(http.MethodGet, "/test", nil)
|
request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
|
||||||
// h := uriPolicy.Select(pool, request)
|
h = uriPolicy.Select(pool, request)
|
||||||
// if h != pool[0] {
|
if h != pool[1] {
|
||||||
// t.Error("Expected uri policy host to be the first host.")
|
t.Error("Expected uri policy host to be the second host.")
|
||||||
// }
|
}
|
||||||
|
|
||||||
// pool[0].Unhealthy = 1
|
pool[0].SetHealthy(false)
|
||||||
// h = uriPolicy.Select(pool, request)
|
pool[1].SetHealthy(false)
|
||||||
// if h != pool[1] {
|
h = uriPolicy.Select(pool, request)
|
||||||
// t.Error("Expected uri policy host to be the first host.")
|
if h != nil {
|
||||||
// }
|
t.Error("Expected uri policy policy host to be nil.")
|
||||||
|
}
|
||||||
// request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
|
}
|
||||||
// h = uriPolicy.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected uri policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // We should be able to resize the host pool and still be able to predict
|
|
||||||
// // where a request will be routed with the same URI's used above
|
|
||||||
// pool = []*UpstreamHost{
|
|
||||||
// {
|
|
||||||
// Name: workableServer.URL, // this should resolve (healthcheck test)
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// Name: "http://localhost:99998", // this shouldn't
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
|
|
||||||
// request = httptest.NewRequest(http.MethodGet, "/test", nil)
|
|
||||||
// h = uriPolicy.Select(pool, request)
|
|
||||||
// if h != pool[0] {
|
|
||||||
// t.Error("Expected uri policy host to be the first host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pool[0].Unhealthy = 1
|
|
||||||
// h = uriPolicy.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected uri policy host to be the first host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// request = httptest.NewRequest(http.MethodGet, "/test_2", nil)
|
|
||||||
// h = uriPolicy.Select(pool, request)
|
|
||||||
// if h != pool[1] {
|
|
||||||
// t.Error("Expected uri policy host to be the second host.")
|
|
||||||
// }
|
|
||||||
|
|
||||||
// pool[0].Unhealthy = 1
|
|
||||||
// pool[1].Unhealthy = 1
|
|
||||||
// h = uriPolicy.Select(pool, request)
|
|
||||||
// if h != nil {
|
|
||||||
// t.Error("Expected uri policy policy host to be nil.")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func TestHeaderPolicy(t *testing.T) {
|
|
||||||
// pool := testPool()
|
|
||||||
// tests := []struct {
|
|
||||||
// Name string
|
|
||||||
// Policy *Header
|
|
||||||
// RequestHeaderName string
|
|
||||||
// RequestHeaderValue string
|
|
||||||
// NilHost bool
|
|
||||||
// HostIndex int
|
|
||||||
// }{
|
|
||||||
// {"empty config", &Header{""}, "", "", true, 0},
|
|
||||||
// {"empty config+header+value", &Header{""}, "Affinity", "somevalue", true, 0},
|
|
||||||
// {"empty config+header", &Header{""}, "Affinity", "", true, 0},
|
|
||||||
|
|
||||||
// {"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 1},
|
|
||||||
// {"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 2},
|
|
||||||
// {"no header(fallback to roundrobin)", &Header{"Affinity"}, "", "", false, 0},
|
|
||||||
|
|
||||||
// {"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue", false, 1},
|
|
||||||
// {"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue2", false, 0},
|
|
||||||
// {"hash route to host", &Header{"Affinity"}, "Affinity", "somevalue3", false, 2},
|
|
||||||
// {"hash route with empty value", &Header{"Affinity"}, "Affinity", "", false, 1},
|
|
||||||
// }
|
|
||||||
|
|
||||||
// for idx, test := range tests {
|
|
||||||
// request, _ := http.NewRequest("GET", "/", nil)
|
|
||||||
// if test.RequestHeaderName != "" {
|
|
||||||
// request.Header.Add(test.RequestHeaderName, test.RequestHeaderValue)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// host := test.Policy.Select(pool, request)
|
|
||||||
// if test.NilHost && host != nil {
|
|
||||||
// t.Errorf("%d: Expected host to be nil", idx)
|
|
||||||
// }
|
|
||||||
// if !test.NilHost && host == nil {
|
|
||||||
// t.Errorf("%d: Did not expect host to be nil", idx)
|
|
||||||
// }
|
|
||||||
// if !test.NilHost && host != pool[test.HostIndex] {
|
|
||||||
// t.Errorf("%d: Expected Header policy to be host %d", idx, test.HostIndex)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Reference in a new issue