Repository: grpc/grpc-go
File: internal/xds/balancer/cdsbalancer/e2e_test/dns_impl_test.go (Go)

Code before:
	config, err := bootstrap.NewConfigFromContents(bootstrapContents)
	if err != nil {
		t.Fatalf("Failed to parse bootstrap: %v", err)
	}
	pool := xdsclient.NewPool(config)
	xdsC, closeClient, err := pool.NewClientForTesting(xdsclient.OptionsForTesting{
		Name: t.Name(),
	})
	if err != nil {
		t.Fatalf("Failed to create xDS client: %v", err)
	}
	defer closeClient()

	// Create a manual resolver and push service config specifying the use of
	// the cluster_resolver LB policy with LOGICAL_DNS discovery mechanism.
	r := manual.NewBuilderWithScheme("whatever")
	jsonSC := fmt.Sprintf(`{
            "loadBalancingConfig":[{
                "cluster_resolver_experimental":{
                    "discoveryMechanisms": [{
                        "cluster": "test-cluster",
                        "type": "LOGICAL_DNS",
                        "dnsHostname": "%s:///target-name",
                        "outlierDetection": {}
                    }],
                    "xdsLbPolicy":[{"round_robin":{}}]
                }
            }]
        }`, dnsScheme)

	scpr := internal.ParseServiceConfig.(func(string) *serviceconfig.ParseResult)(jsonSC)
	r.InitialState(xdsclient.SetClient(resolver.State{ServiceConfig: scpr}, xdsC))

	// Create a ClientConn and make a successful RPC.
	cc, err := grpc.NewClient(r.Scheme()+":///test.service",
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithResolvers(r),
	)
	cc.Connect()
	if err != nil {
		t.Fatalf("failed to create new client for local test server: %v", err)
	}
	defer cc.Close()

	testClient := testpb.NewTestServiceClient(cc)
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	var dnsClientConn resolver.ClientConn
	select {
	case dnsClientConn = <-dnsCCCh:

Review comment (suggestion):
Can we make this use the `wrr_locality` LB policy to include more of the xDS LB policy tree in this test: `[{"xds_wrr_locality_experimental": {"childPolicy": [{"round_robin": {}}]}}]`

Code after:
			}}},
	})

	const (
		serviceName   = "test-xds-service"
		clusterName   = "cluster-test-xds-service"
		endpointsName = "endpoints-test-xds-service"
		rdsName       = "route-test-xds-service"
	)

	resources := e2e.UpdateOptions{
		NodeID:    nodeID,
		Listeners: []*v3listenerpb.Listener{e2e.DefaultClientListener(serviceName, rdsName)},
		Routes:    []*v3routepb.RouteConfiguration{e2e.DefaultRouteConfig(rdsName, serviceName, clusterName)},
		Clusters: []*v3clusterpb.Cluster{e2e.ClusterResourceWithOptions(e2e.ClusterOptions{
			ClusterName: clusterName,
			ServiceName: endpointsName,
			Type:        e2e.ClusterTypeLogicalDNS,
			DNSHostName: "dns",
			DNSPort:     uint32(8080),
			Policy:      e2e.LoadBalancingPolicyRoundRobin,
		})},
		Endpoints: nil,
	}

	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
	defer cancel()

	if err := managementServer.Update(ctx, resources); err != nil {
		t.Fatalf("Failed to update management server: %v", err)
	}

	// Create a ClientConn and make a successful RPC.
	cc, err := grpc.NewClient(fmt.Sprintf("xds:///"+serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolverBuilder))
	if err != nil {
		t.Fatalf("Failed to create new client for local test server: %v", err)
	}
	defer cc.Close()

	// Ensure the RPC is routed to the first backend.
	testClient := testpb.NewTestServiceClient(cc)
	for i := 0; i < 10; i++ {
		var peer peer.Peer
		if _, err := testClient.EmptyCall(ctx, &testpb.Empty{}, grpc.Peer(&peer)); err != nil {
			t.Fatalf("RPC failed: %v", err)
		}

		if got, want := peer.Addr.String(), server1.Address; got != want {
			t.Errorf("peer.Addr = %q, want = %q", got, want)
		}
	}