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-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"
|
2023-02-06 17:14:35 +00:00
|
|
|
"github.com/hashicorp/consul/envoyextensions/xdscommon"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2018-10-03 18:18:55 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
2023-02-06 17:14:35 +00:00
|
|
|
"google.golang.org/genproto/googleapis/rpc/status"
|
2021-04-29 18:54:05 +00:00
|
|
|
"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
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-01-25 17:24:27 +00:00
|
|
|
EnvoyVersion string
|
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
deltaStream *TestADSDeltaStream // Incremental v3
|
2023-02-17 19:24:01 +00:00
|
|
|
|
|
|
|
closed bool
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-15 20:21:07 +00:00
|
|
|
func (e *TestEnvoy) SetSendErr(err error) {
|
|
|
|
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()
|
|
|
|
|
2022-01-25 17:24:27 +00:00
|
|
|
stringVersion := e.EnvoyVersion
|
|
|
|
if stringVersion == "" {
|
2023-02-06 17:14:35 +00:00
|
|
|
stringVersion = xdscommon.EnvoyVersions[0]
|
2022-01-25 17:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ev, valid := stringToEnvoyVersion(stringVersion)
|
2021-04-29 18:54:05 +00:00
|
|
|
if !valid {
|
2022-01-25 17:24:27 +00:00
|
|
|
t.Fatal("envoy version is not valid: %s", stringVersion)
|
2021-04-29 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-02-17 19:24:01 +00:00
|
|
|
if !e.closed && e.deltaStream.recvCh != nil {
|
2021-04-29 18:54:05 +00:00
|
|
|
close(e.deltaStream.recvCh)
|
2023-02-17 19:24:01 +00:00
|
|
|
e.closed = true
|
2021-04-29 18:54:05 +00:00
|
|
|
}
|
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
|
|
|
|
}
|