google.golang.org/grpc@v1.72.2/internal/testutils/xds/e2e/setup/setup.go (about)

     1  /*
     2   *
     3   * Copyright 2024 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package setup implements setup helpers for xDS e2e tests.
    20  package setup
    21  
    22  import (
    23  	"testing"
    24  
    25  	"github.com/google/uuid"
    26  	"google.golang.org/grpc/internal"
    27  	"google.golang.org/grpc/internal/testutils/xds/e2e"
    28  	"google.golang.org/grpc/resolver"
    29  	_ "google.golang.org/grpc/xds" // Register the xds_resolver.
    30  )
    31  
    32  // ManagementServerAndResolver sets up an xDS management server, creates
    33  // bootstrap configuration pointing to that server and creates an xDS resolver
    34  // using that configuration.
    35  //
    36  // Registers a cleanup function on t to stop the management server.
    37  //
    38  // Returns the following:
    39  // - the xDS management server
    40  // - the node ID to use when talking to this management server
    41  // - bootstrap configuration to use (if creating an xDS-enabled gRPC server)
    42  // - xDS resolver builder (if creating an xDS-enabled gRPC client)
    43  func ManagementServerAndResolver(t *testing.T) (*e2e.ManagementServer, string, []byte, resolver.Builder) {
    44  	// Start an xDS management server.
    45  	xdsServer := e2e.StartManagementServer(t, e2e.ManagementServerOptions{AllowResourceSubset: true})
    46  
    47  	// Create bootstrap configuration pointing to the above management server.
    48  	nodeID := uuid.New().String()
    49  	bc := e2e.DefaultBootstrapContents(t, nodeID, xdsServer.Address)
    50  
    51  	// Create an xDS resolver with the above bootstrap configuration.
    52  	if internal.NewXDSResolverWithConfigForTesting == nil {
    53  		t.Fatalf("internal.NewXDSResolverWithConfigForTesting is nil")
    54  	}
    55  	r, err := internal.NewXDSResolverWithConfigForTesting.(func([]byte) (resolver.Builder, error))(bc)
    56  	if err != nil {
    57  		t.Fatalf("Failed to create xDS resolver for testing: %v", err)
    58  	}
    59  
    60  	return xdsServer, nodeID, bc, r
    61  }