gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/xdsclient/watchers_federation_test.go (about)

     1  /*
     2   *
     3   * Copyright 2021 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  package xdsclient
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  
    24  	"gitee.com/ks-custle/core-gm/grpc/internal/envconfig"
    25  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/testutils"
    26  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource"
    27  )
    28  
    29  func overrideFedEnvVar(t *testing.T) {
    30  	oldFed := envconfig.XDSFederation
    31  	envconfig.XDSFederation = true
    32  	t.Cleanup(func() { envconfig.XDSFederation = oldFed })
    33  }
    34  
    35  func testFedTwoWatchDifferentContextParameterOrder(t *testing.T, typ xdsresource.ResourceType, update interface{}) {
    36  	overrideFedEnvVar(t)
    37  	var (
    38  		// Two resource names only differ in context parameter __order__.
    39  		resourceName1 = testutils.BuildResourceName(typ, testAuthority, "test-resource-name", nil) + "?a=1&b=2"
    40  		resourceName2 = testutils.BuildResourceName(typ, testAuthority, "test-resource-name", nil) + "?b=2&a=1"
    41  	)
    42  
    43  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    44  	defer cancel()
    45  	client, ctrlCh := testClientSetup(t, false)
    46  	updateCh, _ := newWatch(t, client, typ, resourceName1)
    47  	_, updateHandler := getControllerAndPubsub(ctx, t, client, ctrlCh, typ, resourceName1)
    48  	newWatchF, newUpdateF, verifyUpdateF := typeToTestFuncs(typ)
    49  
    50  	// Start a watch on the second resource name.
    51  	updateCh2, _ := newWatchF(client, resourceName2)
    52  
    53  	// Send an update on the first resoruce, both watchers should be updated.
    54  	newUpdateF(updateHandler, map[string]interface{}{resourceName1: update})
    55  	verifyUpdateF(ctx, t, updateCh, update, nil)
    56  	verifyUpdateF(ctx, t, updateCh2, update, nil)
    57  }
    58  
    59  // TestLDSFedTwoWatchDifferentContextParameterOrder covers the case with new style resource name
    60  //   - Two watches with the same query string, but in different order. The two
    61  //     watches should watch the same resource.
    62  //   - The response has the same query string, but in different order. The watch
    63  //     should still be notified.
    64  func (s) TestLDSFedTwoWatchDifferentContextParameterOrder(t *testing.T) {
    65  	testFedTwoWatchDifferentContextParameterOrder(t, xdsresource.ListenerResource, xdsresource.ListenerUpdate{RouteConfigName: testRDSName})
    66  }
    67  
    68  // TestRDSFedTwoWatchDifferentContextParameterOrder covers the case with new style resource name
    69  //   - Two watches with the same query string, but in different order. The two
    70  //     watches should watch the same resource.
    71  //   - The response has the same query string, but in different order. The watch
    72  //     should still be notified.
    73  func (s) TestRDSFedTwoWatchDifferentContextParameterOrder(t *testing.T) {
    74  	testFedTwoWatchDifferentContextParameterOrder(t, xdsresource.RouteConfigResource, xdsresource.RouteConfigUpdate{
    75  		VirtualHosts: []*xdsresource.VirtualHost{
    76  			{
    77  				Domains: []string{testLDSName},
    78  				Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName: {Weight: 1}}}},
    79  			},
    80  		},
    81  	})
    82  }
    83  
    84  // TestClusterFedTwoWatchDifferentContextParameterOrder covers the case with new style resource name
    85  //   - Two watches with the same query string, but in different order. The two
    86  //     watches should watch the same resource.
    87  //   - The response has the same query string, but in different order. The watch
    88  //     should still be notified.
    89  func (s) TestClusterFedTwoWatchDifferentContextParameterOrder(t *testing.T) {
    90  	testFedTwoWatchDifferentContextParameterOrder(t, xdsresource.ClusterResource, xdsresource.ClusterUpdate{ClusterName: testEDSName})
    91  }
    92  
    93  // TestEndpointsFedTwoWatchDifferentContextParameterOrder covers the case with new style resource name
    94  //   - Two watches with the same query string, but in different order. The two
    95  //     watches should watch the same resource.
    96  //   - The response has the same query string, but in different order. The watch
    97  //     should still be notified.
    98  func (s) TestEndpointsFedTwoWatchDifferentContextParameterOrder(t *testing.T) {
    99  	testFedTwoWatchDifferentContextParameterOrder(t, xdsresource.EndpointsResource, xdsresource.EndpointsUpdate{Localities: []xdsresource.Locality{testLocalities[0]}})
   100  }