gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/test/xds_client_affinity_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 22 package xds_test 23 24 import ( 25 "context" 26 "fmt" 27 "testing" 28 29 grpc "gitee.com/ks-custle/core-gm/grpc" 30 "gitee.com/ks-custle/core-gm/grpc/credentials/insecure" 31 "gitee.com/ks-custle/core-gm/grpc/internal/envconfig" 32 "gitee.com/ks-custle/core-gm/grpc/xds/internal/testutils/e2e" 33 34 v3clusterpb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/cluster/v3" 35 v3corepb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/core/v3" 36 v3routepb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/config/route/v3" 37 testpb "gitee.com/ks-custle/core-gm/grpc/test/grpc_testing" 38 ) 39 40 const hashHeaderName = "session_id" 41 42 // hashRouteConfig returns a RouteConfig resource with hash policy set to 43 // header "session_id". 44 func hashRouteConfig(routeName, ldsTarget, clusterName string) *v3routepb.RouteConfiguration { 45 return &v3routepb.RouteConfiguration{ 46 Name: routeName, 47 VirtualHosts: []*v3routepb.VirtualHost{{ 48 Domains: []string{ldsTarget}, 49 Routes: []*v3routepb.Route{{ 50 Match: &v3routepb.RouteMatch{PathSpecifier: &v3routepb.RouteMatch_Prefix{Prefix: "/"}}, 51 Action: &v3routepb.Route_Route{Route: &v3routepb.RouteAction{ 52 ClusterSpecifier: &v3routepb.RouteAction_Cluster{Cluster: clusterName}, 53 HashPolicy: []*v3routepb.RouteAction_HashPolicy{{ 54 PolicySpecifier: &v3routepb.RouteAction_HashPolicy_Header_{ 55 Header: &v3routepb.RouteAction_HashPolicy_Header{ 56 HeaderName: hashHeaderName, 57 }, 58 }, 59 Terminal: true, 60 }}, 61 }}, 62 }}, 63 }}, 64 } 65 } 66 67 // ringhashCluster returns a Cluster resource that picks ringhash as the lb 68 // policy. 69 func ringhashCluster(clusterName, edsServiceName string) *v3clusterpb.Cluster { 70 return &v3clusterpb.Cluster{ 71 Name: clusterName, 72 ClusterDiscoveryType: &v3clusterpb.Cluster_Type{Type: v3clusterpb.Cluster_EDS}, 73 EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{ 74 EdsConfig: &v3corepb.ConfigSource{ 75 ConfigSourceSpecifier: &v3corepb.ConfigSource_Ads{ 76 Ads: &v3corepb.AggregatedConfigSource{}, 77 }, 78 }, 79 ServiceName: edsServiceName, 80 }, 81 LbPolicy: v3clusterpb.Cluster_RING_HASH, 82 } 83 } 84 85 // TestClientSideAffinitySanityCheck tests that the affinity config can be 86 // propagated to pick the ring_hash policy. It doesn't test the affinity 87 // behavior in ring_hash policy. 88 func (s) TestClientSideAffinitySanityCheck(t *testing.T) { 89 defer func() func() { 90 old := envconfig.XDSRingHash 91 envconfig.XDSRingHash = true 92 return func() { envconfig.XDSRingHash = old } 93 }()() 94 95 managementServer, nodeID, _, resolver, cleanup1 := setupManagementServer(t) 96 defer cleanup1() 97 98 port, cleanup2 := clientSetup(t, &testService{}) 99 defer cleanup2() 100 101 const serviceName = "my-service-client-side-xds" 102 resources := e2e.DefaultClientResources(e2e.ResourceParams{ 103 DialTarget: serviceName, 104 NodeID: nodeID, 105 Host: "localhost", 106 Port: port, 107 SecLevel: e2e.SecurityLevelNone, 108 }) 109 // Replace RDS and CDS resources with ringhash config, but keep the resource 110 // names. 111 resources.Routes = []*v3routepb.RouteConfiguration{hashRouteConfig( 112 resources.Routes[0].Name, 113 resources.Listeners[0].Name, 114 resources.Clusters[0].Name, 115 )} 116 resources.Clusters = []*v3clusterpb.Cluster{ringhashCluster( 117 resources.Clusters[0].Name, 118 resources.Clusters[0].EdsClusterConfig.ServiceName, 119 )} 120 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 121 defer cancel() 122 if err := managementServer.Update(ctx, resources); err != nil { 123 t.Fatal(err) 124 } 125 126 // Create a ClientConn and make a successful RPC. 127 cc, err := grpc.Dial(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolver)) 128 if err != nil { 129 t.Fatalf("failed to dial local test server: %v", err) 130 } 131 defer cc.Close() 132 133 client := testpb.NewTestServiceClient(cc) 134 if _, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil { 135 t.Fatalf("rpc EmptyCall() failed: %v", err) 136 } 137 }