Skip to content

Commit 4fdb846

Browse files
committed
xds/internal/resolver: final bit of test cleanup
1 parent c76442c commit 4fdb846

File tree

6 files changed

+835
-1325
lines changed

6 files changed

+835
-1325
lines changed

xds/internal/resolver/helpers_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package resolver_test
2121
import (
2222
"context"
2323
"fmt"
24+
"strings"
2425
"testing"
2526
"time"
2627

@@ -161,6 +162,21 @@ func verifyNoUpdateFromResolver(ctx context.Context, t *testing.T, stateCh chan
161162
}
162163
}
163164

165+
// verifyErrorFromResolver waits for the resolver to push an error and verifies
166+
// that it matches the expected error.
167+
func verifyErrorFromResolver(ctx context.Context, t *testing.T, errCh chan error, wantErr string) {
168+
t.Helper()
169+
170+
select {
171+
case <-ctx.Done():
172+
t.Fatal("Timeout when waiting for error to be propagated to the ClientConn")
173+
case gotErr := <-errCh:
174+
if gotErr == nil || !strings.Contains(gotErr.Error(), wantErr) {
175+
t.Fatalf("Received error from resolver %q, want %q", gotErr, wantErr)
176+
}
177+
}
178+
}
179+
164180
// Spins up an xDS management server and sets up an xDS bootstrap configuration
165181
// file that points to it.
166182
//
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
*
3+
* Copyright 2023 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
// Package internal contains functionality internal to the xDS resolver.
20+
package internal
21+
22+
// The following variables are overridden in tests.
23+
var (
24+
// NewWRR is the function used to create a new weighted round robin
25+
// implementation.
26+
NewWRR any // func() wrr.WRR
27+
28+
// NewXDSClient is the function used to create a new xDS client.
29+
NewXDSClient any // func() (xdsclient.XDSClient, func(), error)
30+
)

xds/internal/resolver/serviceconfig.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"google.golang.org/grpc/xds/internal/balancer/clustermanager"
4141
"google.golang.org/grpc/xds/internal/balancer/ringhash"
4242
"google.golang.org/grpc/xds/internal/httpfilter"
43+
rinternal "google.golang.org/grpc/xds/internal/resolver/internal"
4344
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
4445
)
4546

@@ -348,9 +349,6 @@ func (cs *configSelector) stop() {
348349
}
349350
}
350351

351-
// A global for testing.
352-
var newWRR = wrr.NewRandom
353-
354352
// newConfigSelector creates the config selector for su; may add entries to
355353
// r.activeClusters for previously-unseen clusters.
356354
func (r *xdsResolver) newConfigSelector(su serviceUpdate) (*configSelector, error) {
@@ -366,7 +364,7 @@ func (r *xdsResolver) newConfigSelector(su serviceUpdate) (*configSelector, erro
366364
}
367365

368366
for i, rt := range su.virtualHost.Routes {
369-
clusters := newWRR()
367+
clusters := rinternal.NewWRR.(func() wrr.WRR)()
370368
if rt.ClusterSpecifierPlugin != "" {
371369
clusterName := clusterSpecifierPluginPrefix + rt.ClusterSpecifierPlugin
372370
clusters.Add(&routeCluster{

xds/internal/resolver/serviceconfig_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ import (
2525

2626
xxhash "github.com/cespare/xxhash/v2"
2727
"github.com/google/go-cmp/cmp"
28+
"google.golang.org/grpc/internal/grpctest"
2829
"google.golang.org/grpc/internal/grpcutil"
2930
iresolver "google.golang.org/grpc/internal/resolver"
31+
"google.golang.org/grpc/internal/testutils"
3032
"google.golang.org/grpc/metadata"
3133
_ "google.golang.org/grpc/xds/internal/balancer/cdsbalancer" // To parse LB config
3234
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
3335
)
3436

37+
type s struct {
38+
grpctest.Tester
39+
}
40+
41+
func Test(t *testing.T) {
42+
grpctest.RunSubTests(t, s{})
43+
}
44+
3545
func (s) TestPruneActiveClusters(t *testing.T) {
3646
r := &xdsResolver{activeClusters: map[string]*clusterInfo{
3747
"zero": {refCount: 0},
@@ -53,7 +63,7 @@ func (s) TestGenerateRequestHash(t *testing.T) {
5363
const channelID = 12378921
5464
cs := &configSelector{
5565
r: &xdsResolver{
56-
cc: &testClientConn{},
66+
cc: &testutils.ResolverClientConn{Logger: t},
5767
channelID: channelID,
5868
},
5969
}

xds/internal/resolver/xds_resolver.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import (
3131
"google.golang.org/grpc/internal/grpcsync"
3232
"google.golang.org/grpc/internal/pretty"
3333
iresolver "google.golang.org/grpc/internal/resolver"
34+
"google.golang.org/grpc/internal/wrr"
3435
"google.golang.org/grpc/resolver"
36+
rinternal "google.golang.org/grpc/xds/internal/resolver/internal"
3537
"google.golang.org/grpc/xds/internal/xdsclient"
3638
"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
3739
"google.golang.org/grpc/xds/internal/xdsclient/xdsresource"
@@ -54,12 +56,12 @@ func newBuilderForTesting(config []byte) (resolver.Builder, error) {
5456
}, nil
5557
}
5658

57-
// For overriding in unittests.
58-
var newXDSClient = func() (xdsclient.XDSClient, func(), error) { return xdsclient.New() }
59-
6059
func init() {
6160
resolver.Register(&xdsResolverBuilder{})
6261
internal.NewXDSResolverWithConfigForTesting = newBuilderForTesting
62+
63+
rinternal.NewWRR = wrr.NewRandom
64+
rinternal.NewXDSClient = xdsclient.New
6365
}
6466

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

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

0 commit comments

Comments
 (0)