github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/test/xds_client_federation_test.go (about)

     1  //go:build !386
     2  // +build !386
     3  
     4  /*
     5   *
     6   * Copyright 2021 gRPC authors.
     7   *
     8   * Licensed under the Apache License, Version 2.0 (the "License");
     9   * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   *     http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   */
    20  
    21  package xds_test
    22  
    23  import (
    24  	"context"
    25  	"fmt"
    26  	"testing"
    27  
    28  	"github.com/google/uuid"
    29  	v3clusterpb "github.com/hxx258456/ccgo/go-control-plane/envoy/config/cluster/v3"
    30  	v3endpointpb "github.com/hxx258456/ccgo/go-control-plane/envoy/config/endpoint/v3"
    31  	v3listenerpb "github.com/hxx258456/ccgo/go-control-plane/envoy/config/listener/v3"
    32  	v3routepb "github.com/hxx258456/ccgo/go-control-plane/envoy/config/route/v3"
    33  	grpc "github.com/hxx258456/ccgo/grpc"
    34  	"github.com/hxx258456/ccgo/grpc/credentials/insecure"
    35  	"github.com/hxx258456/ccgo/grpc/internal/envconfig"
    36  	xdsinternal "github.com/hxx258456/ccgo/grpc/internal/xds"
    37  	testpb "github.com/hxx258456/ccgo/grpc/test/grpc_testing"
    38  	"github.com/hxx258456/ccgo/grpc/xds"
    39  	"github.com/hxx258456/ccgo/grpc/xds/internal/testutils"
    40  	"github.com/hxx258456/ccgo/grpc/xds/internal/testutils/e2e"
    41  	"github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/xdsresource"
    42  )
    43  
    44  // TestClientSideFederation tests that federation is supported.
    45  //
    46  // In this test, some xDS responses contain resource names in another authority
    47  // (in the new resource name style):
    48  // - LDS: old style, no authority (default authority)
    49  // - RDS: new style, in a different authority
    50  // - CDS: old style, no authority (default authority)
    51  // - EDS: new style, in a different authority
    52  func (s) TestClientSideFederation(t *testing.T) {
    53  	oldXDSFederation := envconfig.XDSFederation
    54  	envconfig.XDSFederation = true
    55  	defer func() { envconfig.XDSFederation = oldXDSFederation }()
    56  
    57  	// Start a management server as the default authority.
    58  	serverDefaultAuth, err := e2e.StartManagementServer()
    59  	if err != nil {
    60  		t.Fatalf("Failed to spin up the xDS management server: %v", err)
    61  	}
    62  	t.Cleanup(serverDefaultAuth.Stop)
    63  
    64  	// Start another management server as the other authority.
    65  	const nonDefaultAuth = "non-default-auth"
    66  	serverAnotherAuth, err := e2e.StartManagementServer()
    67  	if err != nil {
    68  		t.Fatalf("Failed to spin up the xDS management server: %v", err)
    69  	}
    70  	t.Cleanup(serverAnotherAuth.Stop)
    71  
    72  	// Create a bootstrap file in a temporary directory.
    73  	nodeID := uuid.New().String()
    74  	bootstrapContents, err := xdsinternal.BootstrapContents(xdsinternal.BootstrapOptions{
    75  		Version:                            xdsinternal.TransportV3,
    76  		NodeID:                             nodeID,
    77  		ServerURI:                          serverDefaultAuth.Address,
    78  		ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
    79  		// Specify the address of the non-default authority.
    80  		Authorities: map[string]string{nonDefaultAuth: serverAnotherAuth.Address},
    81  	})
    82  	if err != nil {
    83  		t.Fatalf("Failed to create bootstrap file: %v", err)
    84  	}
    85  
    86  	resolver, err := xds.NewXDSResolverWithConfigForTesting(bootstrapContents)
    87  	if err != nil {
    88  		t.Fatalf("Failed to create xDS resolver for testing: %v", err)
    89  	}
    90  	port, cleanup := clientSetup(t, &testService{})
    91  	defer cleanup()
    92  
    93  	const serviceName = "my-service-client-side-xds"
    94  	// LDS is old style name.
    95  	ldsName := serviceName
    96  	// RDS is new style, with the non default authority.
    97  	rdsName := testutils.BuildResourceName(xdsresource.RouteConfigResource, nonDefaultAuth, "route-"+serviceName, nil)
    98  	// CDS is old style name.
    99  	cdsName := "cluster-" + serviceName
   100  	// EDS is new style, with the non default authority.
   101  	edsName := testutils.BuildResourceName(xdsresource.EndpointsResource, nonDefaultAuth, "endpoints-"+serviceName, nil)
   102  
   103  	// Split resources, put LDS/CDS in the default authority, and put RDS/EDS in
   104  	// the other authority.
   105  	resourcesDefault := e2e.UpdateOptions{
   106  		NodeID: nodeID,
   107  		// This has only LDS and CDS.
   108  		Listeners:      []*v3listenerpb.Listener{e2e.DefaultClientListener(ldsName, rdsName)},
   109  		Clusters:       []*v3clusterpb.Cluster{e2e.DefaultCluster(cdsName, edsName, e2e.SecurityLevelNone)},
   110  		SkipValidation: true,
   111  	}
   112  	resourcesAnother := e2e.UpdateOptions{
   113  		NodeID: nodeID,
   114  		// This has only RDS and EDS.
   115  		Routes:         []*v3routepb.RouteConfiguration{e2e.DefaultRouteConfig(rdsName, ldsName, cdsName)},
   116  		Endpoints:      []*v3endpointpb.ClusterLoadAssignment{e2e.DefaultEndpoint(edsName, "localhost", []uint32{port})},
   117  		SkipValidation: true,
   118  	}
   119  
   120  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
   121  	defer cancel()
   122  	// This has only LDS and CDS.
   123  	if err := serverDefaultAuth.Update(ctx, resourcesDefault); err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	// This has only RDS and EDS.
   127  	if err := serverAnotherAuth.Update(ctx, resourcesAnother); err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	// Create a ClientConn and make a successful RPC.
   132  	cc, err := grpc.Dial(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolver))
   133  	if err != nil {
   134  		t.Fatalf("failed to dial local test server: %v", err)
   135  	}
   136  	defer cc.Close()
   137  
   138  	client := testpb.NewTestServiceClient(cc)
   139  	if _, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
   140  		t.Fatalf("rpc EmptyCall() failed: %v", err)
   141  	}
   142  }