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"
|
2021-02-22 21:00:15 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
"github.com/hashicorp/consul/agent/xds/proxysupport"
|
2018-10-03 18:18:55 +00:00
|
|
|
"github.com/mitchellh/go-testing-interface"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestADSStream mocks
|
|
|
|
// discovery.AggregatedDiscoveryService_StreamAggregatedResourcesServer to allow
|
|
|
|
// testing ADS handler.
|
|
|
|
type TestADSStream struct {
|
|
|
|
ctx context.Context
|
2021-02-26 22:23:15 +00:00
|
|
|
sendCh chan *envoy_discovery_v3.DiscoveryResponse
|
|
|
|
recvCh chan *envoy_discovery_v3.DiscoveryRequest
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTestADSStream makes a new TestADSStream
|
|
|
|
func NewTestADSStream(t testing.T, ctx context.Context) *TestADSStream {
|
|
|
|
return &TestADSStream{
|
|
|
|
ctx: ctx,
|
2021-02-26 22:23:15 +00:00
|
|
|
sendCh: make(chan *envoy_discovery_v3.DiscoveryResponse, 1),
|
|
|
|
recvCh: make(chan *envoy_discovery_v3.DiscoveryRequest, 1),
|
2018-10-03 18:18:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send implements ADSStream
|
2021-02-26 22:23:15 +00:00
|
|
|
func (s *TestADSStream) Send(r *envoy_discovery_v3.DiscoveryResponse) error {
|
2018-10-03 18:18:55 +00:00
|
|
|
s.sendCh <- r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recv implements ADSStream
|
2021-02-26 22:23:15 +00:00
|
|
|
func (s *TestADSStream) Recv() (*envoy_discovery_v3.DiscoveryRequest, error) {
|
2018-10-03 18:18:55 +00:00
|
|
|
r := <-s.recvCh
|
|
|
|
if r == nil {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeader implements ADSStream
|
|
|
|
func (s *TestADSStream) SetHeader(metadata.MD) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendHeader implements ADSStream
|
|
|
|
func (s *TestADSStream) SendHeader(metadata.MD) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetTrailer implements ADSStream
|
|
|
|
func (s *TestADSStream) SetTrailer(metadata.MD) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Context implements ADSStream
|
|
|
|
func (s *TestADSStream) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendMsg implements ADSStream
|
|
|
|
func (s *TestADSStream) SendMsg(m interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecvMsg implements ADSStream
|
|
|
|
func (s *TestADSStream) RecvMsg(m interface{}) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type configState struct {
|
|
|
|
lastNonce, lastVersion, acceptedVersion string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestEnvoy is a helper to simulate Envoy ADS requests.
|
|
|
|
type TestEnvoy struct {
|
|
|
|
sync.Mutex
|
|
|
|
stream *TestADSStream
|
|
|
|
proxyID string
|
|
|
|
token string
|
|
|
|
state map[string]configState
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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{
|
|
|
|
stream: NewTestADSStream(t, ctx),
|
|
|
|
state: make(map[string]configState),
|
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
proxyID: proxyID,
|
|
|
|
token: token,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
|
|
|
|
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-02-26 22:23:15 +00:00
|
|
|
req := &envoy_discovery_v3.DiscoveryRequest{
|
2018-10-03 18:18:55 +00:00
|
|
|
VersionInfo: hexString(version),
|
2021-02-26 22:23:15 +00:00
|
|
|
Node: &envoy_core_v3.Node{
|
2020-08-27 17:20:58 +00:00
|
|
|
Id: e.proxyID,
|
|
|
|
Cluster: e.proxyID,
|
|
|
|
UserAgentName: "envoy",
|
2021-02-26 22:23:15 +00:00
|
|
|
UserAgentVersionType: &envoy_core_v3.Node_UserAgentBuildVersion{
|
|
|
|
UserAgentBuildVersion: &envoy_core_v3.BuildVersion{
|
2020-08-27 17:20:58 +00:00
|
|
|
Version: ev,
|
|
|
|
},
|
|
|
|
},
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the client and cancels it's request context.
|
|
|
|
func (e *TestEnvoy) Close() error {
|
|
|
|
e.Lock()
|
|
|
|
defer e.Unlock()
|
|
|
|
|
|
|
|
// unblock the recv chan to simulate recv error when client disconnects
|
|
|
|
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
|
|
|
}
|
|
|
|
if e.cancel != nil {
|
|
|
|
e.cancel()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|