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

     1  /*
     2   *
     3   * Copyright 2020 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 xdsclient
    20  
    21  import (
    22  	"context"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/xdsresource"
    27  )
    28  
    29  // TestRDSWatch covers the cases:
    30  // - an update is received after a watch()
    31  // - an update for another resource name (which doesn't trigger callback)
    32  // - an update is received after cancel()
    33  func (s) TestRDSWatch(t *testing.T) {
    34  	testWatch(t, xdsresource.RouteConfigResource, xdsresource.RouteConfigUpdate{
    35  		VirtualHosts: []*xdsresource.VirtualHost{
    36  			{
    37  				Domains: []string{testLDSName},
    38  				Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName: {Weight: 1}}}},
    39  			},
    40  		},
    41  	}, testRDSName)
    42  }
    43  
    44  // TestRDSTwoWatchSameResourceName covers the case where an update is received
    45  // after two watch() for the same resource name.
    46  func (s) TestRDSTwoWatchSameResourceName(t *testing.T) {
    47  	testTwoWatchSameResourceName(t, xdsresource.RouteConfigResource, xdsresource.RouteConfigUpdate{
    48  		VirtualHosts: []*xdsresource.VirtualHost{
    49  			{
    50  				Domains: []string{testLDSName},
    51  				Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName: {Weight: 1}}}},
    52  			},
    53  		},
    54  	}, testRDSName)
    55  }
    56  
    57  // TestRDSThreeWatchDifferentResourceName covers the case where an update is
    58  // received after three watch() for different resource names.
    59  func (s) TestRDSThreeWatchDifferentResourceName(t *testing.T) {
    60  	testThreeWatchDifferentResourceName(t, xdsresource.RouteConfigResource,
    61  		xdsresource.RouteConfigUpdate{
    62  			VirtualHosts: []*xdsresource.VirtualHost{
    63  				{
    64  					Domains: []string{testLDSName},
    65  					Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName + "1": {Weight: 1}}}},
    66  				},
    67  			},
    68  		}, testRDSName+"1",
    69  		xdsresource.RouteConfigUpdate{
    70  			VirtualHosts: []*xdsresource.VirtualHost{
    71  				{
    72  					Domains: []string{testLDSName},
    73  					Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName + "2": {Weight: 1}}}},
    74  				},
    75  			},
    76  		}, testRDSName+"2",
    77  	)
    78  }
    79  
    80  // TestRDSWatchAfterCache covers the case where watch is called after the update
    81  // is in cache.
    82  func (s) TestRDSWatchAfterCache(t *testing.T) {
    83  	testWatchAfterCache(t, xdsresource.RouteConfigResource, xdsresource.RouteConfigUpdate{
    84  		VirtualHosts: []*xdsresource.VirtualHost{
    85  			{
    86  				Domains: []string{testLDSName},
    87  				Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName: {Weight: 1}}}},
    88  			},
    89  		},
    90  	}, testRDSName)
    91  }
    92  
    93  // TestRouteWatchNACKError covers the case that an update is NACK'ed, and the
    94  // watcher should also receive the error.
    95  func (s) TestRouteWatchNACKError(t *testing.T) {
    96  	ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
    97  	defer cancel()
    98  	client, ctrlCh := testClientSetup(t, false)
    99  	rdsUpdateCh, _ := newWatch(t, client, xdsresource.RouteConfigResource, testRDSName)
   100  	_, updateHandler := getControllerAndPubsub(ctx, t, client, ctrlCh, xdsresource.RouteConfigResource, testRDSName)
   101  
   102  	wantError := fmt.Errorf("testing error")
   103  	updateHandler.NewRouteConfigs(map[string]xdsresource.RouteConfigUpdateErrTuple{testRDSName: {Err: wantError}}, xdsresource.UpdateMetadata{ErrState: &xdsresource.UpdateErrorMetadata{Err: wantError}})
   104  	if err := verifyRouteConfigUpdate(ctx, rdsUpdateCh, xdsresource.RouteConfigUpdate{}, wantError); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  }
   108  
   109  // TestRouteWatchPartialValid covers the case that a response contains both
   110  // valid and invalid resources. This response will be NACK'ed by the xdsclient.
   111  // But the watchers with valid resources should receive the update, those with
   112  // invalida resources should receive an error.
   113  func (s) TestRouteWatchPartialValid(t *testing.T) {
   114  	testWatchPartialValid(t, xdsresource.RouteConfigResource, xdsresource.RouteConfigUpdate{
   115  		VirtualHosts: []*xdsresource.VirtualHost{
   116  			{
   117  				Domains: []string{testLDSName},
   118  				Routes:  []*xdsresource.Route{{Prefix: newStringP(""), WeightedClusters: map[string]xdsresource.WeightedCluster{testCDSName: {Weight: 1}}}},
   119  			},
   120  		},
   121  	}, testRDSName)
   122  }