2020-09-14 17:16:13 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-10-14 20:47:16 +00:00
|
|
|
"crypto/tls"
|
2020-09-11 18:15:02 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2020-10-14 20:47:16 +00:00
|
|
|
"sync/atomic"
|
2020-09-11 18:15:02 +00:00
|
|
|
"testing"
|
2020-09-14 17:16:13 +00:00
|
|
|
"time"
|
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2020-09-08 21:31:47 +00:00
|
|
|
"google.golang.org/grpc"
|
2020-10-14 17:21:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
|
|
|
|
"github.com/hashicorp/consul/agent/metadata"
|
|
|
|
"github.com/hashicorp/consul/agent/pool"
|
2020-10-14 20:47:16 +00:00
|
|
|
"github.com/hashicorp/consul/tlsutil"
|
2020-09-14 17:16:13 +00:00
|
|
|
)
|
|
|
|
|
2020-09-11 18:15:02 +00:00
|
|
|
type testServer struct {
|
|
|
|
addr net.Addr
|
|
|
|
name string
|
|
|
|
dc string
|
|
|
|
shutdown func()
|
2020-10-14 20:47:16 +00:00
|
|
|
rpc *fakeRPCListener
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s testServer) Metadata() *metadata.Server {
|
2020-10-14 20:47:16 +00:00
|
|
|
return &metadata.Server{
|
|
|
|
ID: s.name,
|
|
|
|
Datacenter: s.dc,
|
|
|
|
Addr: s.addr,
|
|
|
|
UseTLS: s.rpc.tlsConf != nil,
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestServer(t *testing.T, name string, dc string) testServer {
|
|
|
|
addr := &net.IPAddr{IP: net.ParseIP("127.0.0.1")}
|
2020-09-08 21:31:47 +00:00
|
|
|
handler := NewHandler(addr, func(server *grpc.Server) {
|
|
|
|
testservice.RegisterSimpleServer(server, &simple{name: name, dc: dc})
|
|
|
|
})
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
rpc := &fakeRPCListener{t: t, handler: handler}
|
|
|
|
|
|
|
|
g := errgroup.Group{}
|
|
|
|
g.Go(func() error {
|
2020-10-14 17:21:51 +00:00
|
|
|
if err := rpc.listen(lis); err != nil {
|
|
|
|
return fmt.Errorf("fake rpc listen error: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-09-11 18:15:02 +00:00
|
|
|
})
|
|
|
|
g.Go(func() error {
|
2020-10-14 17:21:51 +00:00
|
|
|
if err := handler.Run(); err != nil {
|
|
|
|
return fmt.Errorf("grpc server error: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
2020-09-11 18:15:02 +00:00
|
|
|
})
|
|
|
|
return testServer{
|
|
|
|
addr: lis.Addr(),
|
|
|
|
name: name,
|
|
|
|
dc: dc,
|
2020-10-14 20:47:16 +00:00
|
|
|
rpc: rpc,
|
2020-09-11 18:15:02 +00:00
|
|
|
shutdown: func() {
|
2020-10-14 17:21:51 +00:00
|
|
|
rpc.shutdown = true
|
2020-09-11 18:15:02 +00:00
|
|
|
if err := lis.Close(); err != nil {
|
|
|
|
t.Logf("listener closed with error: %v", err)
|
|
|
|
}
|
|
|
|
if err := handler.Shutdown(); err != nil {
|
|
|
|
t.Logf("grpc server shutdown: %v", err)
|
|
|
|
}
|
|
|
|
if err := g.Wait(); err != nil {
|
2020-10-14 17:21:51 +00:00
|
|
|
t.Log(err)
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-14 17:16:13 +00:00
|
|
|
type simple struct {
|
|
|
|
name string
|
|
|
|
dc string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *simple) Flow(_ *testservice.Req, flow testservice.Simple_FlowServer) error {
|
|
|
|
for flow.Context().Err() == nil {
|
|
|
|
resp := &testservice.Resp{ServerName: "one", Datacenter: s.dc}
|
|
|
|
if err := flow.Send(resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *simple) Something(_ context.Context, _ *testservice.Req) (*testservice.Resp, error) {
|
|
|
|
return &testservice.Resp{ServerName: s.name, Datacenter: s.dc}, nil
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
|
|
|
|
// fakeRPCListener mimics agent/consul.Server.listen to handle the RPCType byte.
|
|
|
|
// In the future we should be able to refactor Server and extract this RPC
|
|
|
|
// handling logic so that we don't need to use a fake.
|
|
|
|
// For now, since this logic is in agent/consul, we can't easily use Server.listen
|
|
|
|
// so we fake it.
|
|
|
|
type fakeRPCListener struct {
|
2020-10-14 20:47:16 +00:00
|
|
|
t *testing.T
|
|
|
|
handler *Handler
|
|
|
|
shutdown bool
|
|
|
|
tlsConf *tlsutil.Configurator
|
|
|
|
tlsConnEstablished int32
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeRPCListener) listen(listener net.Listener) error {
|
|
|
|
for {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2020-10-14 17:21:51 +00:00
|
|
|
if f.shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-11 18:15:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go f.handleConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeRPCListener) handleConn(conn net.Conn) {
|
|
|
|
buf := make([]byte, 1)
|
|
|
|
|
|
|
|
if _, err := conn.Read(buf); err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
fmt.Println("ERROR", err.Error())
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
typ := pool.RPCType(buf[0])
|
|
|
|
|
2020-10-14 20:47:16 +00:00
|
|
|
switch typ {
|
|
|
|
|
|
|
|
case pool.RPCGRPC:
|
2020-09-11 18:15:02 +00:00
|
|
|
f.handler.Handle(conn)
|
|
|
|
return
|
2020-10-14 20:47:16 +00:00
|
|
|
|
|
|
|
case pool.RPCTLS:
|
|
|
|
// occasionally we see a test client connecting to an rpc listener that
|
|
|
|
// was created as part of another test, despite none of the tests running
|
|
|
|
// in parallel.
|
|
|
|
// Maybe some strange grpc behaviour? I'm not sure.
|
|
|
|
if f.tlsConf == nil {
|
|
|
|
fmt.Println("ERROR: tls is not configured")
|
|
|
|
conn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic.AddInt32(&f.tlsConnEstablished, 1)
|
|
|
|
conn = tls.Server(conn, f.tlsConf.IncomingRPCConfig())
|
|
|
|
f.handleConn(conn)
|
|
|
|
|
|
|
|
default:
|
|
|
|
fmt.Println("ERROR: unexpected byte", typ)
|
|
|
|
conn.Close()
|
2020-09-11 18:15:02 +00:00
|
|
|
}
|
2020-10-14 20:47:16 +00:00
|
|
|
}
|