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

     1  /*
     2   *
     3   * Copyright 2019 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 controller
    20  
    21  import (
    22  	"testing"
    23  	"time"
    24  
    25  	v2xdspb "gitee.com/ks-custle/core-gm/go-control-plane/envoy/api/v2"
    26  	"gitee.com/ks-custle/core-gm/grpc/internal/testutils"
    27  	"gitee.com/ks-custle/core-gm/grpc/xds/internal"
    28  	xtestutils "gitee.com/ks-custle/core-gm/grpc/xds/internal/testutils"
    29  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource"
    30  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource/version"
    31  	anypb "github.com/golang/protobuf/ptypes/any"
    32  	"github.com/google/go-cmp/cmp/cmpopts"
    33  )
    34  
    35  var (
    36  	badlyMarshaledEDSResponse = &v2xdspb.DiscoveryResponse{
    37  		Resources: []*anypb.Any{
    38  			{
    39  				TypeUrl: version.V2EndpointsURL,
    40  				Value:   []byte{1, 2, 3, 4},
    41  			},
    42  		},
    43  		TypeUrl: version.V2EndpointsURL,
    44  	}
    45  	badResourceTypeInEDSResponse = &v2xdspb.DiscoveryResponse{
    46  		Resources: []*anypb.Any{marshaledConnMgr1},
    47  		TypeUrl:   version.V2EndpointsURL,
    48  	}
    49  	marshaledGoodCLA1 = func() *anypb.Any {
    50  		clab0 := xtestutils.NewClusterLoadAssignmentBuilder(goodEDSName, nil)
    51  		clab0.AddLocality("locality-1", 1, 1, []string{"addr1:314"}, nil)
    52  		clab0.AddLocality("locality-2", 1, 0, []string{"addr2:159"}, nil)
    53  		return testutils.MarshalAny(clab0.Build())
    54  	}()
    55  	goodEDSResponse1 = &v2xdspb.DiscoveryResponse{
    56  		Resources: []*anypb.Any{
    57  			marshaledGoodCLA1,
    58  		},
    59  		TypeUrl: version.V2EndpointsURL,
    60  	}
    61  	marshaledGoodCLA2 = func() *anypb.Any {
    62  		clab0 := xtestutils.NewClusterLoadAssignmentBuilder("not-goodEDSName", nil)
    63  		clab0.AddLocality("locality-1", 1, 0, []string{"addr1:314"}, nil)
    64  		return testutils.MarshalAny(clab0.Build())
    65  	}()
    66  	goodEDSResponse2 = &v2xdspb.DiscoveryResponse{
    67  		Resources: []*anypb.Any{
    68  			marshaledGoodCLA2,
    69  		},
    70  		TypeUrl: version.V2EndpointsURL,
    71  	}
    72  )
    73  
    74  func (s) TestEDSHandleResponse(t *testing.T) {
    75  	tests := []struct {
    76  		name          string
    77  		edsResponse   *v2xdspb.DiscoveryResponse
    78  		wantErr       bool
    79  		wantUpdate    map[string]xdsresource.EndpointsUpdateErrTuple
    80  		wantUpdateMD  xdsresource.UpdateMetadata
    81  		wantUpdateErr bool
    82  	}{
    83  		// Any in resource is badly marshaled.
    84  		{
    85  			name:        "badly-marshaled_response",
    86  			edsResponse: badlyMarshaledEDSResponse,
    87  			wantErr:     true,
    88  			wantUpdate:  nil,
    89  			wantUpdateMD: xdsresource.UpdateMetadata{
    90  				Status: xdsresource.ServiceStatusNACKed,
    91  				ErrState: &xdsresource.UpdateErrorMetadata{
    92  					Err: cmpopts.AnyError,
    93  				},
    94  			},
    95  			wantUpdateErr: false,
    96  		},
    97  		// Response doesn't contain resource with the right type.
    98  		{
    99  			name:        "no-config-in-response",
   100  			edsResponse: badResourceTypeInEDSResponse,
   101  			wantErr:     true,
   102  			wantUpdate:  nil,
   103  			wantUpdateMD: xdsresource.UpdateMetadata{
   104  				Status: xdsresource.ServiceStatusNACKed,
   105  				ErrState: &xdsresource.UpdateErrorMetadata{
   106  					Err: cmpopts.AnyError,
   107  				},
   108  			},
   109  			wantUpdateErr: false,
   110  		},
   111  		// Response contains one uninteresting ClusterLoadAssignment.
   112  		{
   113  			name:        "one-uninterestring-assignment",
   114  			edsResponse: goodEDSResponse2,
   115  			wantErr:     false,
   116  			wantUpdate: map[string]xdsresource.EndpointsUpdateErrTuple{
   117  				"not-goodEDSName": {Update: xdsresource.EndpointsUpdate{
   118  					Localities: []xdsresource.Locality{
   119  						{
   120  							Endpoints: []xdsresource.Endpoint{{Address: "addr1:314"}},
   121  							ID:        internal.LocalityID{SubZone: "locality-1"},
   122  							Priority:  0,
   123  							Weight:    1,
   124  						},
   125  					},
   126  					Raw: marshaledGoodCLA2,
   127  				}},
   128  			},
   129  			wantUpdateMD: xdsresource.UpdateMetadata{
   130  				Status: xdsresource.ServiceStatusACKed,
   131  			},
   132  			wantUpdateErr: false,
   133  		},
   134  		// Response contains one good ClusterLoadAssignment.
   135  		{
   136  			name:        "one-good-assignment",
   137  			edsResponse: goodEDSResponse1,
   138  			wantErr:     false,
   139  			wantUpdate: map[string]xdsresource.EndpointsUpdateErrTuple{
   140  				goodEDSName: {Update: xdsresource.EndpointsUpdate{
   141  					Localities: []xdsresource.Locality{
   142  						{
   143  							Endpoints: []xdsresource.Endpoint{{Address: "addr1:314"}},
   144  							ID:        internal.LocalityID{SubZone: "locality-1"},
   145  							Priority:  1,
   146  							Weight:    1,
   147  						},
   148  						{
   149  							Endpoints: []xdsresource.Endpoint{{Address: "addr2:159"}},
   150  							ID:        internal.LocalityID{SubZone: "locality-2"},
   151  							Priority:  0,
   152  							Weight:    1,
   153  						},
   154  					},
   155  					Raw: marshaledGoodCLA1,
   156  				}},
   157  			},
   158  			wantUpdateMD: xdsresource.UpdateMetadata{
   159  				Status: xdsresource.ServiceStatusACKed,
   160  			},
   161  			wantUpdateErr: false,
   162  		},
   163  	}
   164  	for _, test := range tests {
   165  		t.Run(test.name, func(t *testing.T) {
   166  			testWatchHandle(t, &watchHandleTestcase{
   167  				rType:            xdsresource.EndpointsResource,
   168  				resourceName:     goodEDSName,
   169  				responseToHandle: test.edsResponse,
   170  				wantHandleErr:    test.wantErr,
   171  				wantUpdate:       test.wantUpdate,
   172  				wantUpdateMD:     test.wantUpdateMD,
   173  				wantUpdateErr:    test.wantUpdateErr,
   174  			})
   175  		})
   176  	}
   177  }
   178  
   179  // TestEDSHandleResponseWithoutWatch tests the case where the v2Client
   180  // receives an EDS response without a registered EDS watcher.
   181  func (s) TestEDSHandleResponseWithoutWatch(t *testing.T) {
   182  	fakeServer, cleanup := startServer(t)
   183  	defer cleanup()
   184  
   185  	v2c, err := newTestController(&testUpdateReceiver{
   186  		f: func(xdsresource.ResourceType, map[string]interface{}, xdsresource.UpdateMetadata) {},
   187  	}, fakeServer.Address, goodNodeProto, func(int) time.Duration { return 0 }, nil)
   188  	if err != nil {
   189  		t.Fatal(err)
   190  	}
   191  	defer v2c.Close()
   192  
   193  	if _, _, _, err := v2c.handleResponse(badResourceTypeInEDSResponse); err == nil {
   194  		t.Fatal("v2c.handleEDSResponse() succeeded, should have failed")
   195  	}
   196  
   197  	if _, _, _, err := v2c.handleResponse(goodEDSResponse1); err != nil {
   198  		t.Fatal("v2c.handleEDSResponse() succeeded, should have failed")
   199  	}
   200  }