2022-07-13 15:33:48 +00:00
|
|
|
package internal
|
2021-10-16 17:02:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
|
|
|
res := resolver.NewServerResolverBuilder(newConfig(t))
|
|
|
|
registerWithGRPC(t, res)
|
|
|
|
|
|
|
|
srv := newPanicTestServer(t, logger, "server-1", "dc1", nil)
|
2021-12-07 21:30:41 +00:00
|
|
|
res.AddServer(types.AreaWAN, 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",
|
2023-01-05 10:21:27 +00:00
|
|
|
BalancerBuilder: balancerBuilder(t, res.Authority()),
|
2021-10-16 17:02:03 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
}
|