Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xds/internal/resolver: final bit of test cleanup #6725

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
xds/internal/resolver: final bit of test cleanup
  • Loading branch information
easwars committed Oct 13, 2023
commit 4fdb8468e5468c56961ad1a2d37e241f9346df64
16 changes: 16 additions & 0 deletions xds/internal/resolver/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package resolver_test
import (
"context"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -161,6 +162,21 @@ func verifyNoUpdateFromResolver(ctx context.Context, t *testing.T, stateCh chan
}
}

// verifyErrorFromResolver waits for the resolver to push an error and verifies
// that it matches the expected error.
func verifyErrorFromResolver(ctx context.Context, t *testing.T, errCh chan error, wantErr string) {
t.Helper()

select {
case <-ctx.Done():
t.Fatal("Timeout when waiting for error to be propagated to the ClientConn")
case gotErr := <-errCh:
if gotErr == nil || !strings.Contains(gotErr.Error(), wantErr) {
t.Fatalf("Received error from resolver %q, want %q", gotErr, wantErr)
}
}
}

// Spins up an xDS management server and sets up an xDS bootstrap configuration
// file that points to it.
//
Expand Down
30 changes: 30 additions & 0 deletions xds/internal/resolver/internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
*
* Copyright 2023 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Package internal contains functionality internal to the xDS resolver.
package internal

// The following variables are overridden in tests.
var (
// NewWRR is the function used to create a new weighted round robin
// implementation.
NewWRR any // func() wrr.WRR
zasweq marked this conversation as resolved.
Show resolved Hide resolved

// NewXDSClient is the function used to create a new xDS client.
NewXDSClient any // func() (xdsclient.XDSClient, func(), error)
)
6 changes: 2 additions & 4 deletions xds/internal/resolver/serviceconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"google.golang.org/grpc/xds/internal/balancer/clustermanager"
"google.golang.org/grpc/xds/internal/balancer/ringhash"
"google.golang.org/grpc/xds/internal/httpfilter"
rinternal "google.golang.org/grpc/xds/internal/resolver/internal"
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
)

Expand Down Expand Up @@ -348,9 +349,6 @@ func (cs *configSelector) stop() {
}
}

// A global for testing.
var newWRR = wrr.NewRandom

// newConfigSelector creates the config selector for su; may add entries to
// r.activeClusters for previously-unseen clusters.
func (r *xdsResolver) newConfigSelector(su serviceUpdate) (*configSelector, error) {
Expand All @@ -366,7 +364,7 @@ func (r *xdsResolver) newConfigSelector(su serviceUpdate) (*configSelector, erro
}

for i, rt := range su.virtualHost.Routes {
clusters := newWRR()
clusters := rinternal.NewWRR.(func() wrr.WRR)()
if rt.ClusterSpecifierPlugin != "" {
clusterName := clusterSpecifierPluginPrefix + rt.ClusterSpecifierPlugin
clusters.Add(&routeCluster{
Expand Down
12 changes: 11 additions & 1 deletion xds/internal/resolver/serviceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,23 @@ import (

xxhash "github.com/cespare/xxhash/v2"
"github.com/google/go-cmp/cmp"
"google.golang.org/grpc/internal/grpctest"
"google.golang.org/grpc/internal/grpcutil"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/internal/testutils"
"google.golang.org/grpc/metadata"
_ "google.golang.org/grpc/xds/internal/balancer/cdsbalancer" // To parse LB config
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
)

type s struct {
grpctest.Tester
}

func Test(t *testing.T) {
grpctest.RunSubTests(t, s{})
}

func (s) TestPruneActiveClusters(t *testing.T) {
r := &xdsResolver{activeClusters: map[string]*clusterInfo{
"zero": {refCount: 0},
Expand All @@ -53,7 +63,7 @@ func (s) TestGenerateRequestHash(t *testing.T) {
const channelID = 12378921
cs := &configSelector{
r: &xdsResolver{
cc: &testClientConn{},
cc: &testutils.ResolverClientConn{Logger: t},
channelID: channelID,
},
}
Expand Down
12 changes: 7 additions & 5 deletions xds/internal/resolver/xds_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import (
"google.golang.org/grpc/internal/grpcsync"
"google.golang.org/grpc/internal/pretty"
iresolver "google.golang.org/grpc/internal/resolver"
"google.golang.org/grpc/internal/wrr"
"google.golang.org/grpc/resolver"
rinternal "google.golang.org/grpc/xds/internal/resolver/internal"
"google.golang.org/grpc/xds/internal/xdsclient"
"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
Expand All @@ -54,12 +56,12 @@ func newBuilderForTesting(config []byte) (resolver.Builder, error) {
}, nil
}

// For overriding in unittests.
var newXDSClient = func() (xdsclient.XDSClient, func(), error) { return xdsclient.New() }

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

rinternal.NewWRR = wrr.NewRandom
rinternal.NewXDSClient = xdsclient.New
}

type xdsResolverBuilder struct {
Expand All @@ -86,7 +88,7 @@ func (b *xdsResolverBuilder) Build(target resolver.Target, cc resolver.ClientCon
r.logger = prefixLogger(r)
r.logger.Infof("Creating resolver for target: %+v", target)

newXDSClient := newXDSClient
newXDSClient := rinternal.NewXDSClient.(func() (xdsclient.XDSClient, func(), error))
if b.newXDSClient != nil {
newXDSClient = b.newXDSClient
}
Expand Down Expand Up @@ -115,7 +117,7 @@ func (b *xdsResolverBuilder) Build(target resolver.Target, cc resolver.ClientCon
}
if xc, ok := creds.(interface{ UsesXDS() bool }); ok && xc.UsesXDS() {
if len(bootstrapConfig.CertProviderConfigs) == 0 {
return nil, errors.New("xds: xdsCreds specified but certificate_providers config missing in bootstrap file")
return nil, fmt.Errorf("xds: use of xDS credentials is specified, but certificate_providers config missing in bootstrap file")
}
}

Expand Down
Loading