2018-10-03 18:18:55 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-08-27 17:20:58 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2018-10-03 18:18:55 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
envoy_api_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2"
|
|
|
|
envoy_core_v2 "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
|
2021-02-26 22:23:15 +00:00
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
|
|
|
|
envoy_type_v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2021-04-29 18:54:05 +00:00
|
|
|
status "google.golang.org/genproto/googleapis/rpc/status"
|
|
|
|
"google.golang.org/grpc"
|
2018-10-03 18:18:55 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2021-04-29 18:54:05 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/xds/proxysupport"
|
2018-10-03 18:18:55 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// TestADSDeltaStream mocks
|
|
|
|
// discovery.AggregatedDiscoveryService_DeltaAggregatedResourcesServer to allow
|
|
|
|
// testing the ADS handler.
|
|
|
|
type TestADSDeltaStream struct {
|
|
|
|
stubGrpcServerStream
|
|
|
|
sendCh chan *envoy_discovery_v3.DeltaDiscoveryResponse
|
|
|
|
recvCh chan *envoy_discovery_v3.DeltaDiscoveryRequest
|
2021-06-15 20:21:07 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
sendErr error
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
var _ ADSDeltaStream = (*TestADSDeltaStream)(nil)
|
|
|
|
|
|
|
|
func NewTestADSDeltaStream(t testing.T, ctx context.Context) *TestADSDeltaStream {
|
|
|
|
s := &TestADSDeltaStream{
|
|
|
|
sendCh: make(chan *envoy_discovery_v3.DeltaDiscoveryResponse, 1),
|
|
|
|
recvCh: make(chan *envoy_discovery_v3.DeltaDiscoveryRequest, 1),
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2021-04-29 18:54:05 +00:00
|
|
|
s.stubGrpcServerStream.ctx = ctx
|
|
|
|
return s
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// Send implements ADSDeltaStream
|
|
|
|
func (s *TestADSDeltaStream) Send(r *envoy_discovery_v3.DeltaDiscoveryResponse) error {
|
2021-06-15 20:21:07 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
err := s.sendErr
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
s.sendCh <- r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:21:07 +00:00
|
|
|
func (s *TestADSDeltaStream) SetSendErr(err error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.sendErr = err
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// Recv implements ADSDeltaStream
|
|
|
|
func (s *TestADSDeltaStream) Recv() (*envoy_discovery_v3.DeltaDiscoveryRequest, error) {
|
2018-10-03 18:18:55 +00:00
|
|
|
r := <-s.recvCh
|
|
|
|
if r == nil {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// TestADSStream mocks
|
|
|
|
// discovery.AggregatedDiscoveryService_StreamAggregatedResourcesServer to allow
|
|
|
|
// testing ADS handler.
|
|
|
|
type TestADSStream struct {
|
|
|
|
stubGrpcServerStream
|
|
|
|
sendCh chan *envoy_api_v2.DiscoveryResponse
|
|
|
|
recvCh chan *envoy_api_v2.DiscoveryRequest
|
2021-06-15 20:21:07 +00:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
sendErr error
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// NewTestADSStream makes a new TestADSStream
|
|
|
|
func NewTestADSStream(t testing.T, ctx context.Context) *TestADSStream {
|
|
|
|
s := &TestADSStream{
|
|
|
|
sendCh: make(chan *envoy_api_v2.DiscoveryResponse, 1),
|
|
|
|
recvCh: make(chan *envoy_api_v2.DiscoveryRequest, 1),
|
|
|
|
}
|
|
|
|
s.stubGrpcServerStream.ctx = ctx
|
|
|
|
return s
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// Send implements ADSStream
|
|
|
|
func (s *TestADSStream) Send(r *envoy_api_v2.DiscoveryResponse) error {
|
2021-06-15 20:21:07 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
err := s.sendErr
|
|
|
|
s.mu.Unlock()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
s.sendCh <- r
|
2018-10-03 18:18:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:21:07 +00:00
|
|
|
func (s *TestADSStream) SetSendErr(err error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
s.sendErr = err
|
|
|
|
s.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// Recv implements ADSStream
|
|
|
|
func (s *TestADSStream) Recv() (*envoy_api_v2.DiscoveryRequest, error) {
|
|
|
|
r := <-s.recvCh
|
|
|
|
if r == nil {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return r, nil
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestEnvoy is a helper to simulate Envoy ADS requests.
|
|
|
|
type TestEnvoy struct {
|
2021-04-29 18:54:05 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
proxyID string
|
|
|
|
token string
|
2021-04-29 18:54:05 +00:00
|
|
|
|
|
|
|
stream *TestADSStream // SoTW v2
|
|
|
|
deltaStream *TestADSDeltaStream // Incremental v3
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestEnvoy creates a TestEnvoy instance.
|
|
|
|
func NewTestEnvoy(t testing.T, proxyID, token string) *TestEnvoy {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// If a token is given, attach it to the context in the same way gRPC attaches
|
|
|
|
// metadata in calls and stream contexts.
|
|
|
|
if token != "" {
|
|
|
|
ctx = metadata.NewIncomingContext(ctx,
|
|
|
|
metadata.Pairs("x-consul-token", token))
|
|
|
|
}
|
|
|
|
return &TestEnvoy{
|
2021-04-29 18:54:05 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
proxyID: proxyID,
|
|
|
|
token: token,
|
2021-04-29 18:54:05 +00:00
|
|
|
|
|
|
|
stream: NewTestADSStream(t, ctx),
|
|
|
|
deltaStream: NewTestADSDeltaStream(t, ctx),
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func hexString(v uint64) string {
|
|
|
|
if v == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%08x", v)
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
func stringToEnvoyVersion(vs string) (*envoy_type_v3.SemanticVersion, bool) {
|
2020-08-27 17:20:58 +00:00
|
|
|
parts := strings.Split(vs, ".")
|
|
|
|
if len(parts) != 3 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
major, err := strconv.Atoi(parts[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
minor, err := strconv.Atoi(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
patch, err := strconv.Atoi(parts[2])
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
return &envoy_type_v3.SemanticVersion{
|
2020-08-27 17:20:58 +00:00
|
|
|
MajorNumber: uint32(major),
|
|
|
|
MinorNumber: uint32(minor),
|
|
|
|
Patch: uint32(patch),
|
|
|
|
}, true
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
// SendReq sends a request from the test server.
|
|
|
|
func (e *TestEnvoy) SendReq(t testing.T, typeURL string, version, nonce uint64) {
|
2021-04-29 18:54:05 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
2018-10-03 18:18:55 +00:00
|
|
|
|
2020-08-27 17:20:58 +00:00
|
|
|
ev, valid := stringToEnvoyVersion(proxysupport.EnvoyVersions[0])
|
|
|
|
if !valid {
|
|
|
|
t.Fatal("envoy version is not valid: %s", proxysupport.EnvoyVersions[0])
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
evV2, err := convertSemanticVersionToV2(ev)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &envoy_api_v2.DiscoveryRequest{
|
2018-10-03 18:18:55 +00:00
|
|
|
VersionInfo: hexString(version),
|
2021-04-29 18:54:05 +00:00
|
|
|
Node: &envoy_core_v2.Node{
|
2020-08-27 17:20:58 +00:00
|
|
|
Id: e.proxyID,
|
|
|
|
Cluster: e.proxyID,
|
|
|
|
UserAgentName: "envoy",
|
2021-04-29 18:54:05 +00:00
|
|
|
UserAgentVersionType: &envoy_core_v2.Node_UserAgentBuildVersion{
|
|
|
|
UserAgentBuildVersion: &envoy_core_v2.BuildVersion{
|
|
|
|
Version: evV2,
|
2020-08-27 17:20:58 +00:00
|
|
|
},
|
|
|
|
},
|
2018-10-03 18:18:55 +00:00
|
|
|
},
|
|
|
|
ResponseNonce: hexString(nonce),
|
|
|
|
TypeUrl: typeURL,
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case e.stream.recvCh <- req:
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
t.Fatalf("send to stream blocked for too long")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:21:07 +00:00
|
|
|
func (e *TestEnvoy) SetSendErr(err error) {
|
|
|
|
e.stream.SetSendErr(err)
|
|
|
|
e.deltaStream.SetSendErr(err)
|
|
|
|
}
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// SendDeltaReq sends a delta request from the test server.
|
|
|
|
//
|
|
|
|
// NOTE: the input request is mutated before sending by injecting the node.
|
|
|
|
func (e *TestEnvoy) SendDeltaReq(
|
|
|
|
t testing.T,
|
|
|
|
typeURL string,
|
|
|
|
req *envoy_discovery_v3.DeltaDiscoveryRequest, // optional
|
|
|
|
) {
|
|
|
|
e.sendDeltaReq(t, typeURL, nil, req)
|
|
|
|
}
|
2021-06-15 20:21:07 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
func (e *TestEnvoy) SendDeltaReqACK(
|
|
|
|
t testing.T,
|
|
|
|
typeURL string,
|
|
|
|
nonce uint64,
|
2021-06-15 20:21:07 +00:00
|
|
|
) {
|
|
|
|
e.sendDeltaReq(t, typeURL, &nonce, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *TestEnvoy) SendDeltaReqNACK(
|
|
|
|
t testing.T,
|
|
|
|
typeURL string,
|
|
|
|
nonce uint64,
|
2021-04-29 18:54:05 +00:00
|
|
|
errorDetail *status.Status,
|
|
|
|
) {
|
2021-06-15 20:21:07 +00:00
|
|
|
e.sendDeltaReq(t, typeURL, &nonce, &envoy_discovery_v3.DeltaDiscoveryRequest{
|
|
|
|
ErrorDetail: errorDetail,
|
|
|
|
})
|
2021-04-29 18:54:05 +00:00
|
|
|
}
|
2021-06-15 20:21:07 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
func (e *TestEnvoy) sendDeltaReq(
|
|
|
|
t testing.T,
|
|
|
|
typeURL string,
|
|
|
|
nonce *uint64,
|
|
|
|
req *envoy_discovery_v3.DeltaDiscoveryRequest, // optional
|
|
|
|
) {
|
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
|
|
|
|
|
|
|
ev, valid := stringToEnvoyVersion(proxysupport.EnvoyVersions[0])
|
|
|
|
if !valid {
|
|
|
|
t.Fatal("envoy version is not valid: %s", proxysupport.EnvoyVersions[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
if req == nil {
|
|
|
|
req = &envoy_discovery_v3.DeltaDiscoveryRequest{}
|
|
|
|
}
|
|
|
|
if nonce != nil {
|
|
|
|
req.ResponseNonce = hexString(*nonce)
|
|
|
|
}
|
|
|
|
req.TypeUrl = typeURL
|
|
|
|
|
|
|
|
req.Node = &envoy_core_v3.Node{
|
|
|
|
Id: e.proxyID,
|
|
|
|
Cluster: e.proxyID,
|
|
|
|
UserAgentName: "envoy",
|
|
|
|
UserAgentVersionType: &envoy_core_v3.Node_UserAgentBuildVersion{
|
|
|
|
UserAgentBuildVersion: &envoy_core_v3.BuildVersion{
|
|
|
|
Version: ev,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case e.deltaStream.recvCh <- req:
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
t.Fatalf("send to delta stream blocked for too long")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
// Close closes the client and cancels it's request context.
|
|
|
|
func (e *TestEnvoy) Close() error {
|
2021-04-29 18:54:05 +00:00
|
|
|
e.mu.Lock()
|
|
|
|
defer e.mu.Unlock()
|
2018-10-03 18:18:55 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
// unblock the recv chans to simulate recv errors when client disconnects
|
2018-10-03 18:18:55 +00:00
|
|
|
if e.stream != nil && e.stream.recvCh != nil {
|
|
|
|
close(e.stream.recvCh)
|
2020-12-23 17:59:05 +00:00
|
|
|
e.stream = nil
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
2021-04-29 18:54:05 +00:00
|
|
|
if e.deltaStream != nil && e.deltaStream.recvCh != nil {
|
|
|
|
close(e.deltaStream.recvCh)
|
|
|
|
e.deltaStream = nil
|
|
|
|
}
|
2018-10-03 18:18:55 +00:00
|
|
|
if e.cancel != nil {
|
|
|
|
e.cancel()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-29 18:54:05 +00:00
|
|
|
|
|
|
|
type stubGrpcServerStream struct {
|
|
|
|
ctx context.Context
|
|
|
|
grpc.ServerStream
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ grpc.ServerStream = (*stubGrpcServerStream)(nil)
|
|
|
|
|
|
|
|
// SetHeader implements grpc.ServerStream as part of ADSDeltaStream
|
|
|
|
func (s *stubGrpcServerStream) SetHeader(metadata.MD) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendHeader implements grpc.ServerStream as part of ADSDeltaStream
|
|
|
|
func (s *stubGrpcServerStream) SendHeader(metadata.MD) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTrailer implements grpc.ServerStream as part of ADSDeltaStream
|
|
|
|
func (s *stubGrpcServerStream) SetTrailer(metadata.MD) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context implements grpc.ServerStream as part of ADSDeltaStream
|
|
|
|
func (s *stubGrpcServerStream) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMsg implements grpc.ServerStream as part of ADSDeltaStream
|
|
|
|
func (s *stubGrpcServerStream) SendMsg(m interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecvMsg implements grpc.ServerStream as part of ADSDeltaStream
|
|
|
|
func (s *stubGrpcServerStream) RecvMsg(m interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|