2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2022-07-13 15:33:48 +00:00
|
|
|
package internal
|
2021-10-16 17:02:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-28 10:18:38 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2021-12-07 21:30:41 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
|
|
|
|
2021-10-16 17:02:03 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2023-02-28 10:18:38 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-internal/balancer"
|
2022-07-13 15:33:48 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-internal/resolver"
|
2022-10-11 22:00:32 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-middleware/testutil/testservice"
|
2021-10-16 17:02:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandler_PanicRecoveryInterceptor(t *testing.T) {
|
|
|
|
// Prepare a logger with output to a buffer
|
|
|
|
// so we can check what it writes.
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
Output: &buf,
|
|
|
|
})
|
|
|
|
|
2023-05-11 16:08:57 +00:00
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t, "dc1", "server"))
|
2023-02-28 10:18:38 +00:00
|
|
|
bb := balancer.NewBuilder(res.Authority(), testutil.Logger(t))
|
|
|
|
registerWithGRPC(t, res, bb)
|
2021-10-16 17:02:03 +00:00
|
|
|
|
|
|
|
srv := newPanicTestServer(t, logger, "server-1", "dc1", nil)
|
2023-05-11 16:08:57 +00:00
|
|
|
res.AddServer(types.AreaLAN, srv.Metadata())
|
2021-10-16 17:02:03 +00:00
|
|
|
t.Cleanup(srv.shutdown)
|
|
|
|
|
|
|
|
pool := NewClientConnPool(ClientConnPoolConfig{
|
|
|
|
Servers: res,
|
|
|
|
UseTLSForDC: useTLSForDcAlwaysTrue,
|
|
|
|
DialingFromServer: true,
|
|
|
|
DialingFromDatacenter: "dc1",
|
|
|
|
})
|
|
|
|
|
|
|
|
conn, err := pool.ClientConn("dc1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
client := testservice.NewSimpleClient(conn)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
resp, err := client.Something(ctx, &testservice.Req{})
|
|
|
|
expectedErr := status.Errorf(codes.Internal, "grpc: panic serving request")
|
2022-03-30 16:51:56 +00:00
|
|
|
require.Equal(t, expectedErr.Error(), err.Error())
|
2021-10-16 17:02:03 +00:00
|
|
|
require.Nil(t, resp)
|
|
|
|
|
|
|
|
// Read the log
|
|
|
|
strLog := buf.String()
|
|
|
|
// Checking the entire stack trace is not possible, let's
|
|
|
|
// make sure that it contains a couple of expected strings.
|
|
|
|
require.Contains(t, strLog, `[ERROR] panic serving grpc request: panic="panic from Something`)
|
2022-10-11 22:00:32 +00:00
|
|
|
require.Contains(t, strLog, `github.com/hashicorp/consul/agent/grpc-middleware/testutil/testservice.(*SimplePanic).Something`)
|
|
|
|
|
2021-10-16 17:02:03 +00:00
|
|
|
}
|