Repository: grpc/grpc-go
File: internal/xds/xdsdepmgr/xds_dependency_manager_test.go (Go)

Code before:
	edsCluster := e2e.DefaultCluster("eds-cluster", defaultTestEDSServiceName, e2e.SecurityLevelNone)
	endpoint := e2e.DefaultEndpoint(defaultTestEDSServiceName, "localhost", []uint32{8080})
	resources := e2e.UpdateOptions{
		NodeID:    nodeID,
		Listeners: []*v3listenerpb.Listener{listener},
		Routes:    []*v3routepb.RouteConfiguration{route},
		Clusters:  []*v3clusterpb.Cluster{aggregateCluster, edsCluster},
		Endpoints: []*v3endpointpb.ClusterLoadAssignment{endpoint},
	}
	if err := mgmtServer.Update(ctx, resources); err != nil {
		t.Fatal(err)
	}

	dm := xdsdepmgr.New(defaultTestServiceName, defaultTestServiceName, xdsClient, watcher)
	defer dm.Close()

	// Verify that no configuration is pushed to the child policy yet, because
	// not all clusters making up the aggregate cluster have been resolved yet.
	select {
	case <-time.After(defaultTestShortTimeout):
	case update := <-watcher.updateCh:
		t.Fatalf("received unexpected update from dependency manager: %+v", update)
	case err := <-watcher.errorCh:
		t.Fatalf("received unexpected error from dependency manager: %v", err)
	}
	dnsR := replaceDNSResolver(t)
	dnsR.InitialState(resolver.State{
		Addresses: []resolver.Address{
			{Addr: "127.0.0.1:8081"},
			{Addr: "[::1]:8081"},
		},
	})

	// Now configure the LogicalDNS cluster in the management server. This
	// should result in configuration being pushed down to the child policy.
	resources.Clusters = append(resources.Clusters, makeLogicalDNSClusterResource("dns-cluster", "localhost", 8081))
	if err := mgmtServer.Update(ctx, resources); err != nil {
		t.Fatal(err)
	}

	wantXdsConfig := &xdsresource.XDSConfig{
		Listener: &xdsresource.ListenerUpdate{
			RouteConfigName: resources.Routes[0].Name,
			HTTPFilters:     []xdsresource.HTTPFilter{{Name: "router"}},
		},
		RouteConfig: &xdsresource.RouteConfigUpdate{
			VirtualHosts: []*xdsresource.VirtualHost{
				{
					Domains: []string{defaultTestServiceName},
					Routes: []*xdsresource.Route{{
						Prefix:           newStringP("/"),

Review comment (suggestion):
Can you please move the DNS resolver replacement near the top of the test, before any xDS configuration is sent? This would avoid potential races where the real DNS resolver gets used before replacement.

Code after:

	dnsR := replaceDNSResolver(t)
	dnsR.UpdateState(resolver.State{
		Addresses: []resolver.Address{
			{Addr: "127.0.0.1:8081"},
			{Addr: "[::1]:8081"},
		},
	})

	watcher := newTestWatcher()
	defer watcher.close()

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

	listener := e2e.DefaultClientListener(defaultTestServiceName, defaultTestRouteConfigName)
	route := e2e.DefaultRouteConfig(defaultTestRouteConfigName, defaultTestServiceName, defaultTestClusterName)
	aggregateCluster := makeAggregateClusterResource(defaultTestClusterName, []string{"eds-cluster", "dns-cluster"})
	edsCluster := e2e.DefaultCluster("eds-cluster", defaultTestEDSServiceName, e2e.SecurityLevelNone)
	endpoint := e2e.DefaultEndpoint(defaultTestEDSServiceName, "localhost", []uint32{8080})
	resources := e2e.UpdateOptions{
		NodeID:    nodeID,
		Listeners: []*v3listenerpb.Listener{listener},
		Routes:    []*v3routepb.RouteConfiguration{route},
		Clusters:  []*v3clusterpb.Cluster{aggregateCluster, edsCluster},
		Endpoints: []*v3endpointpb.ClusterLoadAssignment{endpoint},
	}
	if err := mgmtServer.Update(ctx, resources); err != nil {
		t.Fatal(err)
	}

	dm := xdsdepmgr.New(defaultTestServiceName, defaultTestServiceName, xdsClient, watcher)
	defer dm.Close()

	// Verify that no configuration is pushed to the child policy yet, because
	// not all clusters making up the aggregate cluster have been resolved yet.
	select {
	case <-time.After(defaultTestShortTimeout):
	case update := <-watcher.updateCh:
		t.Fatalf("received unexpected update from dependency manager: %+v", update)
	case err := <-watcher.errorCh:
		t.Fatalf("received unexpected error from dependency manager: %v", err)
	}

	// Now configure the LogicalDNS cluster in the management server. This
	// should result in configuration being pushed down to the child policy.
	resources.Clusters = append(resources.Clusters, makeLogicalDNSClusterResource("dns-cluster", "localhost", 8081))
	if err := mgmtServer.Update(ctx, resources); err != nil {
		t.Fatal(err)
	}