Repository: grpc/grpc-go
File: internal/xds/resolver/xds_resolver_test.go (Go)

Code before:
			listeners := []*v3listenerpb.Listener{e2e.DefaultClientListener(defaultTestServiceName, defaultTestRouteConfigName)}
			clusters := []*v3clusterpb.Cluster{e2e.DefaultCluster(defaultTestClusterName, defaultTestEndpointName, e2e.SecurityLevelNone)}
			endpoints := []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint(defaultTestEndpointName, defaultTestHostname, defaultTestPort)}
			routes := []*v3routepb.RouteConfiguration{{
				Name: defaultTestRouteConfigName,
				VirtualHosts: []*v3routepb.VirtualHost{{
					Domains: []string{defaultTestServiceName},
					Routes: []*v3routepb.Route{{
						Match: &v3routepb.RouteMatch{PathSpecifier: &v3routepb.RouteMatch_Prefix{Prefix: "/"}},
						Action: &v3routepb.Route_Route{Route: &v3routepb.RouteAction{
							ClusterSpecifier: &v3routepb.RouteAction_WeightedClusters{WeightedClusters: &v3routepb.WeightedCluster{
								Clusters: []*v3routepb.WeightedCluster_ClusterWeight{
									{
										Name:   defaultTestClusterName,
										Weight: &wrapperspb.UInt32Value{Value: 100},
									},
								},
							}},
							HostRewriteSpecifier: &v3routepb.RouteAction_AutoHostRewrite{
								AutoHostRewrite: &wrapperspb.BoolValue{Value: tt.autoHostRewrite},
							},
						}},
					}},
				}},
			}}
			configureAllResourcesOnManagementServer(ctx, t, mgmtServer, nodeID, listeners, routes, clusters, endpoints)

			trustedXdsServer := "[]"
			if tt.serverfeature == serverFeature.ServerFeatureTrustedXDSServer {
				trustedXdsServer = `["trusted_xds_server"]`
			}

			opts := bootstrap.ConfigOptionsForTesting{
				Servers: []byte(fmt.Sprintf(`[{
					"server_uri": %q,
					"channel_creds": [{"type": "insecure"}],
					"server_features": %s
				}]`, mgmtServer.Address, trustedXdsServer)),
				Node: []byte(fmt.Sprintf(`{"id": "%s"}`, nodeID)),
			}

			contents, err := bootstrap.NewContentsForTesting(opts)
			if err != nil {
				t.Fatalf("Failed to create bootstrap configuration: %v", err)
			}

			// Build the resolver and read the config selector out of it.
			stateCh, _, _ := buildResolverForTarget(t, resolver.Target{URL: *testutils.MustParseURL("xds:///" + defaultTestServiceName)}, contents)
			cs := verifyUpdateFromResolver(ctx, t, stateCh, "")

			res, err := cs.SelectConfig(iresolver.RPCInfo{

Review comment (suggestion):
Clusters and endpoints are not required for the purposes of this test. So, you should directly call the `Update` method on the management server and just give it the listener and the route (you could also inline the route configuration), and set the `SkipValidation` field in the `UpdateOptions` field to `true`. That will make it possible to not specify all resources.

Code after:
			autoHostRewrite:     false,
			envconfig:           true,
			serverfeature:       serverFeature.ServerFeatureTrustedXDSServer,
			wantAutoHostRewrite: false,
		},
		{
			name:                "EnvVarEnabled_TrustedServer_AutoHostRewriteOn",
			autoHostRewrite:     true,
			envconfig:           true,
			serverfeature:       serverFeature.ServerFeatureTrustedXDSServer,
			wantAutoHostRewrite: true,
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			testutils.SetEnvConfig(t, &envconfig.XDSAuthorityRewrite, tt.envconfig)

			// Spin up an xDS management server for the test.
			ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
			defer cancel()
			nodeID := uuid.New().String()
			mgmtServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true})
			defer mgmtServer.Stop()

			// Configure the management server with a good listener resource and a
			// route configuration resource, as specified by the test case.
			resources := e2e.UpdateOptions{
				NodeID:    nodeID,
				Listeners: []*v3listenerpb.Listener{e2e.DefaultClientListener(defaultTestServiceName, defaultTestRouteConfigName)},
				Routes: []*v3routepb.RouteConfiguration{{
					Name: defaultTestRouteConfigName,
					VirtualHosts: []*v3routepb.VirtualHost{{
						Domains: []string{defaultTestServiceName},
						Routes: []*v3routepb.Route{{
							Match: &v3routepb.RouteMatch{PathSpecifier: &v3routepb.RouteMatch_Prefix{Prefix: "/"}},
							Action: &v3routepb.Route_Route{Route: &v3routepb.RouteAction{
								ClusterSpecifier: &v3routepb.RouteAction_WeightedClusters{WeightedClusters: &v3routepb.WeightedCluster{
									Clusters: []*v3routepb.WeightedCluster_ClusterWeight{
										{
											Name:   defaultTestClusterName,
											Weight: &wrapperspb.UInt32Value{Value: 100},
										},
									},
								}},
								HostRewriteSpecifier: &v3routepb.RouteAction_AutoHostRewrite{
									AutoHostRewrite: &wrapperspb.BoolValue{Value: tt.autoHostRewrite},
								},
							}},
						}},
					}},
				}},
				SkipValidation: true,