Commit 1f7d8d8a authored by Pieter Raubenheimer's avatar Pieter Raubenheimer

Add test for UpstreamHost defaults

parent a7766c90
...@@ -67,6 +67,31 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) { ...@@ -67,6 +67,31 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
upstream.Hosts = make([]*UpstreamHost, len(to)) upstream.Hosts = make([]*UpstreamHost, len(to))
for i, host := range to { for i, host := range to {
uh, err := upstream.NewHost(host)
if err != nil {
return upstreams, err
}
upstream.Hosts[i] = uh
}
if upstream.HealthCheck.Path != "" {
go upstream.HealthCheckWorker(nil)
}
upstreams = append(upstreams, upstream)
}
return upstreams, nil
}
// RegisterPolicy adds a custom policy to the proxy.
func RegisterPolicy(name string, policy func() Policy) {
supportedPolicies[name] = policy
}
func (u *staticUpstream) From() string {
return u.from
}
func (u *staticUpstream) NewHost(host string) (*UpstreamHost, error) {
if !strings.HasPrefix(host, "http") && if !strings.HasPrefix(host, "http") &&
!strings.HasPrefix(host, "unix:") { !strings.HasPrefix(host, "unix:") {
host = "http://" + host host = "http://" + host
...@@ -75,50 +100,35 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) { ...@@ -75,50 +100,35 @@ func NewStaticUpstreams(c parse.Dispenser) ([]Upstream, error) {
Name: host, Name: host,
Conns: 0, Conns: 0,
Fails: 0, Fails: 0,
FailTimeout: upstream.FailTimeout, FailTimeout: u.FailTimeout,
Unhealthy: false, Unhealthy: false,
ExtraHeaders: upstream.proxyHeaders, ExtraHeaders: u.proxyHeaders,
CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc { CheckDown: func(u *staticUpstream) UpstreamHostDownFunc {
return func(uh *UpstreamHost) bool { return func(uh *UpstreamHost) bool {
if uh.Unhealthy { if uh.Unhealthy {
return true return true
} }
if uh.Fails >= upstream.MaxFails && if uh.Fails >= u.MaxFails &&
upstream.MaxFails != 0 { u.MaxFails != 0 {
return true return true
} }
return false return false
} }
}(upstream), }(u),
WithoutPathPrefix: upstream.WithoutPathPrefix, WithoutPathPrefix: u.WithoutPathPrefix,
MaxConns: upstream.MaxConns, MaxConns: u.MaxConns,
}
if baseURL, err := url.Parse(uh.Name); err == nil {
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix)
if upstream.insecureSkipVerify {
uh.ReverseProxy.Transport = InsecureTransport
}
} else {
return upstreams, err
}
upstream.Hosts[i] = uh
} }
if upstream.HealthCheck.Path != "" { baseURL, err := url.Parse(uh.Name)
go upstream.HealthCheckWorker(nil) if err != nil {
} return nil, err
upstreams = append(upstreams, upstream)
} }
return upstreams, nil
}
// RegisterPolicy adds a custom policy to the proxy.
func RegisterPolicy(name string, policy func() Policy) {
supportedPolicies[name] = policy
}
func (u *staticUpstream) From() string { uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix)
return u.from if u.insecureSkipVerify {
uh.ReverseProxy.Transport = InsecureTransport
}
return uh, nil
} }
func parseBlock(c *parse.Dispenser, u *staticUpstream) error { func parseBlock(c *parse.Dispenser, u *staticUpstream) error {
......
...@@ -5,6 +5,45 @@ import ( ...@@ -5,6 +5,45 @@ import (
"time" "time"
) )
func TestNewHost(t *testing.T) {
upstream := &staticUpstream{
FailTimeout: 10 * time.Second,
MaxConns: 1,
MaxFails: 1,
}
uh, err := upstream.NewHost("example.com")
if err != nil {
t.Error("Expected no error")
}
if uh.Name != "http://example.com" {
t.Error("Expected default schema to be added to Name.")
}
if uh.FailTimeout != upstream.FailTimeout {
t.Error("Expected default FailTimeout to be set.")
}
if uh.MaxConns != upstream.MaxConns {
t.Error("Expected default MaxConns to be set.")
}
if uh.CheckDown == nil {
t.Error("Expected default CheckDown to be set.")
}
if uh.CheckDown(uh) {
t.Error("Expected new host not to be down.")
}
// mark Unhealthy
uh.Unhealthy = true
if !uh.CheckDown(uh) {
t.Error("Expected unhealthy host to be down.")
}
// mark with Fails
uh.Unhealthy = false
uh.Fails = 1
if !uh.CheckDown(uh) {
t.Error("Expected failed host to be down.")
}
}
func TestHealthCheck(t *testing.T) { func TestHealthCheck(t *testing.T) {
upstream := &staticUpstream{ upstream := &staticUpstream{
from: "", from: "",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment