Skip to content

Commit 55d820d

Browse files
authored
clusterresolver/e2e_test: Avoid making DNS requests (#7561)
* Avoid making a DNS request in aggregated_cluster_test * Mock DNS resolver
1 parent 52961f7 commit 55d820d

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

resolver/manual/manual.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ func (r *Resolver) InitialState(s resolver.State) {
7676

7777
// Build returns itself for Resolver, because it's both a builder and a resolver.
7878
func (r *Resolver) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
79-
r.BuildCallback(target, cc, opts)
8079
r.mu.Lock()
8180
defer r.mu.Unlock()
81+
// Call BuildCallback after locking to avoid a race when UpdateState
82+
// or ReportError is called before Build returns.
83+
r.BuildCallback(target, cc, opts)
8284
r.CC = cc
8385
if r.lastSeenState != nil {
8486
err := r.CC.UpdateState(*r.lastSeenState)

xds/internal/balancer/clusterresolver/e2e_test/aggregate_cluster_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ func (s) TestAggregateCluster_BadEDSFromError_GoodToBadDNS(t *testing.T) {
840840
// good update, this test verifies the cluster_resolver balancer correctly falls
841841
// back from the LOGICAL_DNS cluster to the EDS cluster.
842842
func (s) TestAggregateCluster_BadDNS_GoodEDS(t *testing.T) {
843+
dnsTargetCh, dnsR := setupDNS(t)
843844
// Start an xDS management server.
844845
managementServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true})
845846

@@ -857,12 +858,14 @@ func (s) TestAggregateCluster_BadDNS_GoodEDS(t *testing.T) {
857858
const (
858859
edsClusterName = clusterName + "-eds"
859860
dnsClusterName = clusterName + "-dns"
861+
dnsHostName = "bad.ip.v4.address"
862+
dnsPort = 8080
860863
)
861864
resources := e2e.UpdateOptions{
862865
NodeID: nodeID,
863866
Clusters: []*v3clusterpb.Cluster{
864867
makeAggregateClusterResource(clusterName, []string{dnsClusterName, edsClusterName}),
865-
makeLogicalDNSClusterResource(dnsClusterName, "bad.ip.v4.address", 8080),
868+
makeLogicalDNSClusterResource(dnsClusterName, dnsHostName, dnsPort),
866869
e2e.DefaultCluster(edsClusterName, edsServiceName, e2e.SecurityLevelNone),
867870
},
868871
Endpoints: []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint(edsServiceName, "localhost", []uint32{uint32(edsPort)})},
@@ -879,6 +882,21 @@ func (s) TestAggregateCluster_BadDNS_GoodEDS(t *testing.T) {
879882
cc, cleanup := setupAndDial(t, bootstrapContents)
880883
defer cleanup()
881884

885+
// Ensure that the DNS resolver is started for the expected target.
886+
select {
887+
case <-ctx.Done():
888+
t.Fatal("Timeout when waiting for DNS resolver to be started")
889+
case target := <-dnsTargetCh:
890+
got, want := target.Endpoint(), fmt.Sprintf("%s:%d", dnsHostName, dnsPort)
891+
if got != want {
892+
t.Fatalf("DNS resolution started for target %q, want %q", got, want)
893+
}
894+
}
895+
896+
// Produce a bad resolver update from the DNS resolver.
897+
dnsErr := fmt.Errorf("DNS error")
898+
dnsR.ReportError(dnsErr)
899+
882900
// RPCs should work, higher level DNS cluster errors so should fallback to
883901
// EDS cluster.
884902
client := testgrpc.NewTestServiceClient(cc)

0 commit comments

Comments
 (0)