gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/xdsclient/controller/controller_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 controller
    19  
    20  import (
    21  	"testing"
    22  
    23  	grpc "gitee.com/ks-custle/core-gm/grpc"
    24  	"gitee.com/ks-custle/core-gm/grpc/credentials/insecure"
    25  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/testutils"
    26  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/bootstrap"
    27  	"gitee.com/ks-custle/core-gm/grpc/xds/internal/xdsclient/xdsresource/version"
    28  )
    29  
    30  const testXDSServer = "xds-server"
    31  
    32  // TestNew covers that New() returns an error if the input *ServerConfig
    33  // contains invalid content.
    34  func (s) TestNew(t *testing.T) {
    35  	tests := []struct {
    36  		name    string
    37  		config  *bootstrap.ServerConfig
    38  		wantErr bool
    39  	}{
    40  		{
    41  			name:    "empty-opts",
    42  			config:  &bootstrap.ServerConfig{},
    43  			wantErr: true,
    44  		},
    45  		{
    46  			name: "empty-balancer-name",
    47  			config: &bootstrap.ServerConfig{
    48  				Creds:     grpc.WithTransportCredentials(insecure.NewCredentials()),
    49  				NodeProto: testutils.EmptyNodeProtoV2,
    50  			},
    51  			wantErr: true,
    52  		},
    53  		{
    54  			name: "empty-dial-creds",
    55  			config: &bootstrap.ServerConfig{
    56  				ServerURI: testXDSServer,
    57  				NodeProto: testutils.EmptyNodeProtoV2,
    58  			},
    59  			wantErr: true,
    60  		},
    61  		{
    62  			name: "empty-node-proto",
    63  			config: &bootstrap.ServerConfig{
    64  				ServerURI: testXDSServer,
    65  				Creds:     grpc.WithTransportCredentials(insecure.NewCredentials()),
    66  			},
    67  			wantErr: true,
    68  		},
    69  		{
    70  			name: "node-proto-version-mismatch",
    71  			config: &bootstrap.ServerConfig{
    72  				ServerURI:    testXDSServer,
    73  				Creds:        grpc.WithTransportCredentials(insecure.NewCredentials()),
    74  				TransportAPI: version.TransportV2,
    75  				NodeProto:    testutils.EmptyNodeProtoV3,
    76  			},
    77  			wantErr: true,
    78  		},
    79  		{
    80  			name: "happy-case",
    81  			config: &bootstrap.ServerConfig{
    82  				ServerURI: testXDSServer,
    83  				Creds:     grpc.WithInsecure(),
    84  				NodeProto: testutils.EmptyNodeProtoV2,
    85  			},
    86  		},
    87  	}
    88  
    89  	for _, test := range tests {
    90  		t.Run(test.name, func(t *testing.T) {
    91  			c, err := New(test.config, nil, nil, nil) // Only testing the config, other inputs are left as nil.
    92  			defer func() {
    93  				if c != nil {
    94  					c.Close()
    95  				}
    96  			}()
    97  			if (err != nil) != test.wantErr {
    98  				t.Fatalf("New(%+v) = %v, wantErr: %v", test.config, err, test.wantErr)
    99  			}
   100  		})
   101  	}
   102  }