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

Code before:
	// We use a channel to synchronize access and avoid race conditions.
	dnsCCCh := make(chan resolver.ClientConn, 1)
	dnsR.BuildCallback = func(_ resolver.Target, cc resolver.ClientConn, _ resolver.BuildOptions) {
		select {
		case dnsCCCh <- cc:
		default:
		}
	}

	// Create an xDS client for use by the cluster_resolver LB policy.
	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":[{"xds_wrr_locality_experimental": {"childPolicy": [{"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))
	if err != nil {
		t.Fatalf("Failed to create new client for local test server: %v", err)
	}
	cc.Connect()
	defer cc.Close()

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

Review comment (suggestion):
Can you please change this test to be more e2e i.e. can you change the test to create a management server and configure a DNS type cluster on the management server rather than creating a cluster resolver with DNS type discovery mechanism. This will make sure the test runs correctly even after the A74 changes, since clusterresolver is going to be removed.

Code after:
	resolver.Register(dnsR)
	t.Cleanup(func() { resolver.Register(originalDNS) })

	// For LOGICAL_DNS, this updates the SINGLE endpoint to have 2 IPs.
	dnsR.InitialState(resolver.State{
		Endpoints: []resolver.Endpoint{{
			Addresses: []resolver.Address{
				{Addr: server1.Address},
				{Addr: server2.Address},
			}}},
	})

	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++ {