mirror of
https://github.com/caddyserver/caddy.git
synced 2025-03-20 12:38:57 +01:00
Add test for UpstreamHost defaults
This commit is contained in:
parent
a7766c9033
commit
1f7d8d8ab0
2 changed files with 81 additions and 32 deletions
|
@ -67,38 +67,8 @@ 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 {
|
||||||
if !strings.HasPrefix(host, "http") &&
|
uh, err := upstream.NewHost(host)
|
||||||
!strings.HasPrefix(host, "unix:") {
|
if err != nil {
|
||||||
host = "http://" + host
|
|
||||||
}
|
|
||||||
uh := &UpstreamHost{
|
|
||||||
Name: host,
|
|
||||||
Conns: 0,
|
|
||||||
Fails: 0,
|
|
||||||
FailTimeout: upstream.FailTimeout,
|
|
||||||
Unhealthy: false,
|
|
||||||
ExtraHeaders: upstream.proxyHeaders,
|
|
||||||
CheckDown: func(upstream *staticUpstream) UpstreamHostDownFunc {
|
|
||||||
return func(uh *UpstreamHost) bool {
|
|
||||||
if uh.Unhealthy {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if uh.Fails >= upstream.MaxFails &&
|
|
||||||
upstream.MaxFails != 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}(upstream),
|
|
||||||
WithoutPathPrefix: upstream.WithoutPathPrefix,
|
|
||||||
MaxConns: upstream.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
|
return upstreams, err
|
||||||
}
|
}
|
||||||
upstream.Hosts[i] = uh
|
upstream.Hosts[i] = uh
|
||||||
|
@ -121,6 +91,46 @@ func (u *staticUpstream) From() string {
|
||||||
return u.from
|
return u.from
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *staticUpstream) NewHost(host string) (*UpstreamHost, error) {
|
||||||
|
if !strings.HasPrefix(host, "http") &&
|
||||||
|
!strings.HasPrefix(host, "unix:") {
|
||||||
|
host = "http://" + host
|
||||||
|
}
|
||||||
|
uh := &UpstreamHost{
|
||||||
|
Name: host,
|
||||||
|
Conns: 0,
|
||||||
|
Fails: 0,
|
||||||
|
FailTimeout: u.FailTimeout,
|
||||||
|
Unhealthy: false,
|
||||||
|
ExtraHeaders: u.proxyHeaders,
|
||||||
|
CheckDown: func(u *staticUpstream) UpstreamHostDownFunc {
|
||||||
|
return func(uh *UpstreamHost) bool {
|
||||||
|
if uh.Unhealthy {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if uh.Fails >= u.MaxFails &&
|
||||||
|
u.MaxFails != 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}(u),
|
||||||
|
WithoutPathPrefix: u.WithoutPathPrefix,
|
||||||
|
MaxConns: u.MaxConns,
|
||||||
|
}
|
||||||
|
|
||||||
|
baseURL, err := url.Parse(uh.Name)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
uh.ReverseProxy = NewSingleHostReverseProxy(baseURL, uh.WithoutPathPrefix)
|
||||||
|
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 {
|
||||||
switch c.Val() {
|
switch c.Val() {
|
||||||
case "policy":
|
case "policy":
|
||||||
|
|
|
@ -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: "",
|
||||||
|
|
Loading…
Reference in a new issue