Skip to content

Commit

Permalink
xds/resolver: add a way to specify the xDS client to use for testing …
Browse files Browse the repository at this point in the history
…purposes (#7771)
  • Loading branch information
easwars authored Oct 24, 2024
1 parent 8212cf0 commit c4c8b11
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
14 changes: 14 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ var (
// other features, including the CSDS service.
NewXDSResolverWithConfigForTesting any // func([]byte) (resolver.Builder, error)

// NewXDSResolverWithClientForTesting creates a new xDS resolver builder
// using the provided xDS client instead of creating a new one using the
// bootstrap configuration specified by the supported environment variables.
// The resolver.Builder is meant to be used in conjunction with the
// grpc.WithResolvers DialOption. The resolver.Builder does not take
// ownership of the provided xDS client and it is the responsibility of the
// caller to close the client when no longer required.
//
// Testing Only
//
// This function should ONLY be used for testing and may not work with some
// other features, including the CSDS service.
NewXDSResolverWithClientForTesting any // func(xdsclient.XDSClient) (resolver.Builder, error)

// RegisterRLSClusterSpecifierPluginForTesting registers the RLS Cluster
// Specifier Plugin for testing purposes, regardless of the XDSRLS environment
// variable.
Expand Down
24 changes: 19 additions & 5 deletions xds/internal/resolver/xds_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,34 @@ import (
// xdsresolver.Scheme
const Scheme = "xds"

// newBuilderForTesting creates a new xds resolver builder using a specific xds
// bootstrap config, so tests can use multiple xds clients in different
// ClientConns at the same time.
func newBuilderForTesting(config []byte) (resolver.Builder, error) {
// newBuilderWithConfigForTesting creates a new xds resolver builder using a
// specific xds bootstrap config, so tests can use multiple xds clients in
// different ClientConns at the same time.
func newBuilderWithConfigForTesting(config []byte) (resolver.Builder, error) {
return &xdsResolverBuilder{
newXDSClient: func(name string) (xdsclient.XDSClient, func(), error) {
return xdsclient.NewForTesting(xdsclient.OptionsForTesting{Name: name, Contents: config})
},
}, nil
}

// newBuilderWithClientForTesting creates a new xds resolver builder using the
// specific xDS client, so that tests have complete control over the exact
// specific xDS client being used.
func newBuilderWithClientForTesting(client xdsclient.XDSClient) (resolver.Builder, error) {
return &xdsResolverBuilder{
newXDSClient: func(string) (xdsclient.XDSClient, func(), error) {
// Returning an empty close func here means that the responsibility
// of closing the client lies with the caller.
return client, func() {}, nil
},
}, nil
}

func init() {
resolver.Register(&xdsResolverBuilder{})
internal.NewXDSResolverWithConfigForTesting = newBuilderForTesting
internal.NewXDSResolverWithConfigForTesting = newBuilderWithConfigForTesting
internal.NewXDSResolverWithClientForTesting = newBuilderWithClientForTesting

rinternal.NewWRR = wrr.NewRandom
rinternal.NewXDSClient = xdsclient.New
Expand Down

0 comments on commit c4c8b11

Please sign in to comment.