2017-12-27 04:35:22 +00:00
|
|
|
package checks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-11-10 16:26:01 +00:00
|
|
|
"io"
|
2017-12-27 04:35:22 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-10-14 20:49:49 +00:00
|
|
|
"github.com/hashicorp/consul/agent/mock"
|
2019-12-10 02:26:41 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-10-14 20:49:49 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2019-10-14 20:49:49 +00:00
|
|
|
|
2017-12-27 04:35:22 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/health"
|
|
|
|
hv1 "google.golang.org/grpc/health/grpc_health_v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
port int
|
|
|
|
server string
|
|
|
|
svcHealthy string
|
|
|
|
svcUnhealthy string
|
|
|
|
svcMissing string
|
|
|
|
)
|
|
|
|
|
|
|
|
func startServer() (*health.Server, *grpc.Server) {
|
|
|
|
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to listen: %v", err)
|
|
|
|
}
|
|
|
|
grpcServer := grpc.NewServer()
|
|
|
|
server := health.NewServer()
|
|
|
|
hv1.RegisterHealthServer(grpcServer, server)
|
|
|
|
go grpcServer.Serve(listener)
|
|
|
|
return server, grpcServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.IntVar(&port, "grpc-stub-port", 54321, "port for the gRPC stub server")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
healthy := "healthy"
|
|
|
|
unhealthy := "unhealthy"
|
|
|
|
missing := "missing"
|
|
|
|
|
|
|
|
srv, grpcStubApp := startServer()
|
|
|
|
srv.SetServingStatus(healthy, hv1.HealthCheckResponse_SERVING)
|
|
|
|
srv.SetServingStatus(unhealthy, hv1.HealthCheckResponse_NOT_SERVING)
|
|
|
|
|
|
|
|
server = fmt.Sprintf("%s:%d", "localhost", port)
|
|
|
|
svcHealthy = fmt.Sprintf("%s/%s", server, healthy)
|
|
|
|
svcUnhealthy = fmt.Sprintf("%s/%s", server, unhealthy)
|
|
|
|
svcMissing = fmt.Sprintf("%s/%s", server, missing)
|
|
|
|
|
|
|
|
result := 1
|
|
|
|
defer func() {
|
|
|
|
grpcStubApp.Stop()
|
|
|
|
os.Exit(result)
|
|
|
|
}()
|
|
|
|
|
|
|
|
result = m.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheck(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
target string
|
|
|
|
timeout time.Duration
|
|
|
|
tlsConfig *tls.Config
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
healthy bool
|
|
|
|
}{
|
|
|
|
// successes
|
|
|
|
{"should pass for healthy server", args{server, time.Second, nil}, true},
|
|
|
|
{"should pass for healthy service", args{svcHealthy, time.Second, nil}, true},
|
|
|
|
|
|
|
|
// failures
|
|
|
|
{"should fail for unhealthy service", args{svcUnhealthy, time.Second, nil}, false},
|
|
|
|
{"should fail for missing service", args{svcMissing, time.Second, nil}, false},
|
|
|
|
{"should timeout for healthy service", args{server, time.Nanosecond, nil}, false},
|
|
|
|
{"should fail if probe is secure, but server is not", args{server, time.Second, &tls.Config{}}, false},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
probe := NewGrpcHealthProbe(tt.args.target, tt.args.timeout, tt.args.tlsConfig)
|
2019-09-26 02:55:52 +00:00
|
|
|
actualError := probe.Check(tt.args.target)
|
2017-12-27 04:35:22 +00:00
|
|
|
actuallyHealthy := actualError == nil
|
|
|
|
if tt.healthy != actuallyHealthy {
|
|
|
|
t.Errorf("FAIL: %s. Expected healthy %t, but err == %v", tt.name, tt.healthy, actualError)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-09-26 02:55:52 +00:00
|
|
|
|
|
|
|
func TestGRPC_Proxied(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
notif := mock.NewNotify()
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Name: uniqueID(),
|
2022-11-10 16:26:01 +00:00
|
|
|
Output: io.Discard,
|
2020-01-28 23:50:41 +00:00
|
|
|
})
|
|
|
|
|
2021-09-14 16:47:52 +00:00
|
|
|
statusHandler := NewStatusHandler(notif, logger, 0, 0, 0)
|
2019-12-10 02:26:41 +00:00
|
|
|
cid := structs.NewCheckID("foo", nil)
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
check := &CheckGRPC{
|
2019-12-10 02:26:41 +00:00
|
|
|
CheckID: cid,
|
2019-10-14 20:49:49 +00:00
|
|
|
GRPC: "",
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: logger,
|
|
|
|
ProxyGRPC: server,
|
|
|
|
StatusHandler: statusHandler,
|
2019-09-26 02:55:52 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
// If ProxyGRPC is set, check() reqs should go to that address
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2019-12-10 02:26:41 +00:00
|
|
|
if got, want := notif.Updates(cid), 2; got < want {
|
2019-09-26 02:55:52 +00:00
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
if got, want := notif.State(cid), api.HealthPassing; got != want {
|
2019-09-26 02:55:52 +00:00
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGRPC_NotProxied(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
notif := mock.NewNotify()
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Name: uniqueID(),
|
2022-11-10 16:26:01 +00:00
|
|
|
Output: io.Discard,
|
2020-01-28 23:50:41 +00:00
|
|
|
})
|
|
|
|
|
2021-09-14 16:47:52 +00:00
|
|
|
statusHandler := NewStatusHandler(notif, logger, 0, 0, 0)
|
2019-12-10 02:26:41 +00:00
|
|
|
cid := structs.NewCheckID("foo", nil)
|
2020-01-28 23:50:41 +00:00
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
check := &CheckGRPC{
|
2019-12-10 02:26:41 +00:00
|
|
|
CheckID: cid,
|
2019-10-14 20:49:49 +00:00
|
|
|
GRPC: server,
|
|
|
|
Interval: 10 * time.Millisecond,
|
|
|
|
Logger: logger,
|
|
|
|
ProxyGRPC: "",
|
|
|
|
StatusHandler: statusHandler,
|
2019-09-26 02:55:52 +00:00
|
|
|
}
|
|
|
|
check.Start()
|
|
|
|
defer check.Stop()
|
|
|
|
|
|
|
|
// If ProxyGRPC is not set, check() reqs should go to check.GRPC
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
2019-12-10 02:26:41 +00:00
|
|
|
if got, want := notif.Updates(cid), 2; got < want {
|
2019-09-26 02:55:52 +00:00
|
|
|
r.Fatalf("got %d updates want at least %d", got, want)
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
if got, want := notif.State(cid), api.HealthPassing; got != want {
|
2019-09-26 02:55:52 +00:00
|
|
|
r.Fatalf("got state %q want %q", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|