2017-03-09 15:27:31 +00:00
|
|
|
package healthcheck
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-04-16 09:40:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-11-16 10:38:07 +00:00
|
|
|
ptypes "github.com/traefik/paerser/types"
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/config/runtime"
|
|
|
|
"github.com/traefik/traefik/v3/pkg/testhelpers"
|
2022-09-20 14:54:08 +00:00
|
|
|
healthpb "google.golang.org/grpc/health/grpc_health_v1"
|
2017-03-09 15:27:31 +00:00
|
|
|
)
|
|
|
|
|
2023-11-17 00:50:06 +00:00
|
|
|
const delta float64 = 1e-10
|
|
|
|
|
2023-08-08 14:40:05 +00:00
|
|
|
func TestNewServiceHealthChecker_durations(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
config *dynamic.ServerHealthCheck
|
|
|
|
expInterval time.Duration
|
|
|
|
expTimeout time.Duration
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "default values",
|
|
|
|
config: &dynamic.ServerHealthCheck{},
|
|
|
|
expInterval: time.Duration(dynamic.DefaultHealthCheckInterval),
|
|
|
|
expTimeout: time.Duration(dynamic.DefaultHealthCheckTimeout),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "out of range values",
|
|
|
|
config: &dynamic.ServerHealthCheck{
|
|
|
|
Interval: ptypes.Duration(-time.Second),
|
|
|
|
Timeout: ptypes.Duration(-time.Second),
|
|
|
|
},
|
|
|
|
expInterval: time.Duration(dynamic.DefaultHealthCheckInterval),
|
|
|
|
expTimeout: time.Duration(dynamic.DefaultHealthCheckTimeout),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "custom durations",
|
|
|
|
config: &dynamic.ServerHealthCheck{
|
|
|
|
Interval: ptypes.Duration(time.Second * 10),
|
|
|
|
Timeout: ptypes.Duration(time.Second * 5),
|
|
|
|
},
|
|
|
|
expInterval: time.Second * 10,
|
|
|
|
expTimeout: time.Second * 5,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "interval shorter than timeout",
|
|
|
|
config: &dynamic.ServerHealthCheck{
|
|
|
|
Interval: ptypes.Duration(time.Second),
|
|
|
|
Timeout: ptypes.Duration(time.Second * 5),
|
|
|
|
},
|
|
|
|
expInterval: time.Second,
|
|
|
|
expTimeout: time.Second * 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
healthChecker := NewServiceHealthChecker(context.Background(), nil, test.config, nil, nil, http.DefaultTransport, nil)
|
|
|
|
assert.Equal(t, test.expInterval, healthChecker.interval)
|
|
|
|
assert.Equal(t, test.expTimeout, healthChecker.timeout)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func TestServiceHealthChecker_newRequest(t *testing.T) {
|
2018-04-16 09:40:03 +00:00
|
|
|
testCases := []struct {
|
2022-11-16 10:38:07 +00:00
|
|
|
desc string
|
|
|
|
targetURL string
|
|
|
|
config dynamic.ServerHealthCheck
|
|
|
|
expTarget string
|
|
|
|
expError bool
|
|
|
|
expHostname string
|
|
|
|
expHeader string
|
|
|
|
expMethod string
|
2017-05-10 18:28:57 +00:00
|
|
|
}{
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "no port override",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Path: "/test",
|
|
|
|
Port: 0,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend1:80/test",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expMethod: http.MethodGet,
|
2017-05-10 18:28:57 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "port override",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend2:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Path: "/test",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend2:8080/test",
|
|
|
|
expHostname: "backend2:8080",
|
|
|
|
expMethod: http.MethodGet,
|
2017-05-10 18:28:57 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "no port override with no port in server URL",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Path: "/health",
|
|
|
|
Port: 0,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend1/health",
|
|
|
|
expHostname: "backend1",
|
|
|
|
expMethod: http.MethodGet,
|
2017-05-10 18:28:57 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "port override with no port in server URL",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend2",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Path: "/health",
|
|
|
|
Port: 8080,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend2:8080/health",
|
|
|
|
expHostname: "backend2:8080",
|
|
|
|
expMethod: http.MethodGet,
|
2017-05-10 18:28:57 +00:00
|
|
|
},
|
2018-05-14 10:08:03 +00:00
|
|
|
{
|
|
|
|
desc: "scheme override",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "https://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Scheme: "http",
|
|
|
|
Path: "/test",
|
|
|
|
Port: 0,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend1:80/test",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expMethod: http.MethodGet,
|
2018-11-15 14:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path with param",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-11-15 14:50:03 +00:00
|
|
|
Path: "/health?powpow=do",
|
|
|
|
Port: 0,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend1:80/health?powpow=do",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expMethod: http.MethodGet,
|
2018-11-15 14:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path with params",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-11-15 14:50:03 +00:00
|
|
|
Path: "/health?powpow=do&do=powpow",
|
|
|
|
Port: 0,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: false,
|
|
|
|
expTarget: "http://backend1:80/health?powpow=do&do=powpow",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expMethod: http.MethodGet,
|
2018-11-15 14:50:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "path with invalid path",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-11-15 14:50:03 +00:00
|
|
|
Path: ":",
|
|
|
|
Port: 0,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expError: true,
|
|
|
|
expTarget: "",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expMethod: http.MethodGet,
|
2018-05-14 10:08:03 +00:00
|
|
|
},
|
2018-04-16 09:40:03 +00:00
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "override hostname",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Hostname: "myhost",
|
|
|
|
Path: "/",
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expTarget: "http://backend1:80/",
|
|
|
|
expHostname: "myhost",
|
|
|
|
expHeader: "",
|
|
|
|
expMethod: http.MethodGet,
|
2018-04-16 09:40:03 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "not override hostname",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Hostname: "",
|
|
|
|
Path: "/",
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expTarget: "http://backend1:80/",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expHeader: "",
|
|
|
|
expMethod: http.MethodGet,
|
2018-04-16 09:40:03 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "custom header",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Headers: map[string]string{"Custom-Header": "foo"},
|
|
|
|
Hostname: "",
|
|
|
|
Path: "/",
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expTarget: "http://backend1:80/",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expHeader: "foo",
|
|
|
|
expMethod: http.MethodGet,
|
2018-04-16 09:40:03 +00:00
|
|
|
},
|
|
|
|
{
|
2018-05-14 10:08:03 +00:00
|
|
|
desc: "custom header with hostname override",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2018-05-14 10:08:03 +00:00
|
|
|
Headers: map[string]string{"Custom-Header": "foo"},
|
|
|
|
Hostname: "myhost",
|
|
|
|
Path: "/",
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expTarget: "http://backend1:80/",
|
|
|
|
expHostname: "myhost",
|
|
|
|
expHeader: "foo",
|
|
|
|
expMethod: http.MethodGet,
|
2022-08-08 13:22:07 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "custom method",
|
2022-11-16 10:38:07 +00:00
|
|
|
targetURL: "http://backend1:80",
|
|
|
|
config: dynamic.ServerHealthCheck{
|
2022-08-08 13:22:07 +00:00
|
|
|
Path: "/",
|
|
|
|
Method: http.MethodHead,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
expTarget: "http://backend1:80/",
|
|
|
|
expHostname: "backend1:80",
|
|
|
|
expMethod: http.MethodHead,
|
2018-04-16 09:40:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
shc := ServiceHealthChecker{config: &test.config}
|
2018-04-16 09:40:03 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
u := testhelpers.MustParseURL(test.targetURL)
|
|
|
|
req, err := shc.newRequest(context.Background(), u)
|
2018-04-16 09:40:03 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
if test.expError {
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Nil(t, req)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err, "failed to create new request")
|
|
|
|
require.NotNil(t, req)
|
2018-04-16 09:40:03 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
assert.Equal(t, test.expTarget, req.URL.String())
|
|
|
|
assert.Equal(t, test.expHeader, req.Header.Get("Custom-Header"))
|
|
|
|
assert.Equal(t, test.expHostname, req.Host)
|
|
|
|
assert.Equal(t, test.expMethod, req.Method)
|
|
|
|
}
|
2017-05-10 18:28:57 +00:00
|
|
|
})
|
|
|
|
}
|
2017-06-03 13:02:28 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func TestServiceHealthChecker_checkHealthHTTP_NotFollowingRedirects(t *testing.T) {
|
2020-02-26 16:28:04 +00:00
|
|
|
redirectServerCalled := false
|
|
|
|
redirectTestServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
redirectServerCalled = true
|
|
|
|
}))
|
|
|
|
defer redirectTestServer.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(dynamic.DefaultHealthCheckTimeout))
|
2020-02-26 16:28:04 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
rw.Header().Add("location", redirectTestServer.URL)
|
|
|
|
rw.WriteHeader(http.StatusSeeOther)
|
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
config := &dynamic.ServerHealthCheck{
|
|
|
|
Path: "/path",
|
|
|
|
FollowRedirects: Bool(false),
|
|
|
|
Interval: dynamic.DefaultHealthCheckInterval,
|
|
|
|
Timeout: dynamic.DefaultHealthCheckTimeout,
|
2020-02-26 16:28:04 +00:00
|
|
|
}
|
2022-11-16 10:38:07 +00:00
|
|
|
healthChecker := NewServiceHealthChecker(ctx, nil, config, nil, nil, http.DefaultTransport, nil)
|
2020-02-26 16:28:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
err := healthChecker.checkHealthHTTP(ctx, testhelpers.MustParseURL(server.URL))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.False(t, redirectServerCalled, "HTTP redirect must not be followed")
|
|
|
|
}
|
2020-02-26 16:28:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func TestServiceHealthChecker_Launch(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
desc string
|
|
|
|
mode string
|
2022-11-24 10:40:05 +00:00
|
|
|
status int
|
2022-11-16 10:38:07 +00:00
|
|
|
server StartTestServer
|
|
|
|
expNumRemovedServers int
|
|
|
|
expNumUpsertedServers int
|
|
|
|
expGaugeValue float64
|
|
|
|
targetStatus string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
desc: "healthy server staying healthy",
|
|
|
|
server: newHTTPServer(http.StatusOK),
|
|
|
|
expNumRemovedServers: 0,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
2022-11-24 10:40:05 +00:00
|
|
|
{
|
|
|
|
desc: "healthy server staying healthy, with custom code status check",
|
|
|
|
server: newHTTPServer(http.StatusNotFound),
|
|
|
|
status: http.StatusNotFound,
|
|
|
|
expNumRemovedServers: 0,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
{
|
|
|
|
desc: "healthy server staying healthy (StatusNoContent)",
|
|
|
|
server: newHTTPServer(http.StatusNoContent),
|
|
|
|
expNumRemovedServers: 0,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy server staying healthy (StatusPermanentRedirect)",
|
|
|
|
server: newHTTPServer(http.StatusPermanentRedirect),
|
|
|
|
expNumRemovedServers: 0,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy server becoming sick",
|
|
|
|
server: newHTTPServer(http.StatusServiceUnavailable),
|
|
|
|
expNumRemovedServers: 1,
|
|
|
|
expNumUpsertedServers: 0,
|
|
|
|
expGaugeValue: 0,
|
|
|
|
targetStatus: runtime.StatusDown,
|
|
|
|
},
|
2022-11-24 10:40:05 +00:00
|
|
|
{
|
|
|
|
desc: "healthy server becoming sick, with custom code status check",
|
|
|
|
server: newHTTPServer(http.StatusOK),
|
|
|
|
status: http.StatusServiceUnavailable,
|
|
|
|
expNumRemovedServers: 1,
|
|
|
|
expNumUpsertedServers: 0,
|
|
|
|
expGaugeValue: 0,
|
|
|
|
targetStatus: runtime.StatusDown,
|
|
|
|
},
|
2022-11-16 10:38:07 +00:00
|
|
|
{
|
|
|
|
desc: "healthy server toggling to sick and back to healthy",
|
|
|
|
server: newHTTPServer(http.StatusServiceUnavailable, http.StatusOK),
|
|
|
|
expNumRemovedServers: 1,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy server toggling to healthy and go to sick",
|
|
|
|
server: newHTTPServer(http.StatusOK, http.StatusServiceUnavailable),
|
|
|
|
expNumRemovedServers: 1,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 0,
|
|
|
|
targetStatus: runtime.StatusDown,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy grpc server staying healthy",
|
|
|
|
mode: "grpc",
|
|
|
|
server: newGRPCServer(healthpb.HealthCheckResponse_SERVING),
|
|
|
|
expNumRemovedServers: 0,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy grpc server becoming sick",
|
|
|
|
mode: "grpc",
|
|
|
|
server: newGRPCServer(healthpb.HealthCheckResponse_NOT_SERVING),
|
|
|
|
expNumRemovedServers: 1,
|
|
|
|
expNumUpsertedServers: 0,
|
|
|
|
expGaugeValue: 0,
|
|
|
|
targetStatus: runtime.StatusDown,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: "healthy grpc server toggling to sick and back to healthy",
|
|
|
|
mode: "grpc",
|
|
|
|
server: newGRPCServer(healthpb.HealthCheckResponse_NOT_SERVING, healthpb.HealthCheckResponse_SERVING),
|
|
|
|
expNumRemovedServers: 1,
|
|
|
|
expNumUpsertedServers: 1,
|
|
|
|
expGaugeValue: 1,
|
|
|
|
targetStatus: runtime.StatusUp,
|
|
|
|
},
|
2020-02-26 16:28:04 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
for _, test := range testCases {
|
|
|
|
test := test
|
|
|
|
t.Run(test.desc, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// The context is passed to the health check and
|
|
|
|
// canonically canceled by the test server once all expected requests have been received.
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
targetURL, timeout := test.server.Start(t, cancel)
|
|
|
|
|
|
|
|
lb := &testLoadBalancer{RWMutex: &sync.RWMutex{}}
|
|
|
|
|
|
|
|
config := &dynamic.ServerHealthCheck{
|
|
|
|
Mode: test.mode,
|
2022-11-24 10:40:05 +00:00
|
|
|
Status: test.status,
|
2022-11-16 10:38:07 +00:00
|
|
|
Path: "/path",
|
|
|
|
Interval: ptypes.Duration(500 * time.Millisecond),
|
|
|
|
Timeout: ptypes.Duration(499 * time.Millisecond),
|
|
|
|
}
|
|
|
|
|
|
|
|
gauge := &testhelpers.CollectingGauge{}
|
|
|
|
serviceInfo := &runtime.ServiceInfo{}
|
|
|
|
hc := NewServiceHealthChecker(ctx, &MetricsMock{gauge}, config, lb, serviceInfo, http.DefaultTransport, map[string]*url.URL{"test": targetURL})
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2020-02-26 16:28:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
go func() {
|
|
|
|
hc.Launch(ctx)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2020-02-26 16:28:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(timeout):
|
|
|
|
t.Fatal("test did not complete in time")
|
|
|
|
case <-ctx.Done():
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
lb.Lock()
|
|
|
|
defer lb.Unlock()
|
|
|
|
|
|
|
|
assert.Equal(t, test.expNumRemovedServers, lb.numRemovedServers, "removed servers")
|
|
|
|
assert.Equal(t, test.expNumUpsertedServers, lb.numUpsertedServers, "upserted servers")
|
2023-11-29 11:20:57 +00:00
|
|
|
assert.InDelta(t, test.expGaugeValue, gauge.GaugeValue, delta, "ServerUp Gauge")
|
|
|
|
assert.Equal(t, map[string]string{targetURL.String(): test.targetStatus}, serviceInfo.GetAllStatus())
|
2022-11-16 10:38:07 +00:00
|
|
|
})
|
2020-02-26 16:28:04 +00:00
|
|
|
}
|
2022-11-16 10:38:07 +00:00
|
|
|
}
|
2020-02-26 16:28:04 +00:00
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
func Bool(b bool) *bool {
|
|
|
|
return &b
|
2020-02-26 16:28:04 +00:00
|
|
|
}
|