...

Source file src/google.golang.org/grpc/xds/internal/balancer/clusterimpl/config_test.go

Documentation: google.golang.org/grpc/xds/internal/balancer/clusterimpl

     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 clusterimpl
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"github.com/google/go-cmp/cmp/cmpopts"
    26  	"google.golang.org/grpc/balancer"
    27  	_ "google.golang.org/grpc/balancer/roundrobin"
    28  	_ "google.golang.org/grpc/balancer/weightedtarget"
    29  	internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
    30  	"google.golang.org/grpc/internal/xds/bootstrap"
    31  )
    32  
    33  const (
    34  	testJSONConfig = `{
    35    "cluster": "test_cluster",
    36    "edsServiceName": "test-eds",
    37    "lrsLoadReportingServer": {
    38      "server_uri": "trafficdirector.googleapis.com:443",
    39      "channel_creds": [ { "type": "google_default" } ]
    40    },
    41    "maxConcurrentRequests": 123,
    42    "dropCategories": [
    43      {
    44        "category": "drop-1",
    45        "requestsPerMillion": 314
    46      },
    47      {
    48        "category": "drop-2",
    49        "requestsPerMillion": 159
    50      }
    51    ],
    52    "childPolicy": [
    53      {
    54        "weighted_target_experimental": {
    55          "targets": {
    56            "wt-child-1": {
    57              "weight": 75,
    58              "childPolicy":[{"round_robin":{}}]
    59            },
    60            "wt-child-2": {
    61              "weight": 25,
    62              "childPolicy":[{"round_robin":{}}]
    63            }
    64          }
    65        }
    66      }
    67    ]
    68  }`
    69  
    70  	wtName = "weighted_target_experimental"
    71  )
    72  
    73  var (
    74  	wtConfigParser = balancer.Get(wtName).(balancer.ConfigParser)
    75  	wtConfigJSON   = `{
    76    "targets": {
    77      "wt-child-1": {
    78        "weight": 75,
    79        "childPolicy":[{"round_robin":{}}]
    80      },
    81      "wt-child-2": {
    82        "weight": 25,
    83        "childPolicy":[{"round_robin":{}}]
    84      }
    85    }
    86  }`
    87  
    88  	wtConfig, _ = wtConfigParser.ParseConfig([]byte(wtConfigJSON))
    89  )
    90  
    91  func TestParseConfig(t *testing.T) {
    92  	tests := []struct {
    93  		name    string
    94  		js      string
    95  		want    *LBConfig
    96  		wantErr bool
    97  	}{
    98  		{
    99  			name:    "empty json",
   100  			js:      "",
   101  			want:    nil,
   102  			wantErr: true,
   103  		},
   104  		{
   105  			name:    "bad json",
   106  			js:      "{",
   107  			want:    nil,
   108  			wantErr: true,
   109  		},
   110  		{
   111  			name: "OK",
   112  			js:   testJSONConfig,
   113  			want: &LBConfig{
   114  				Cluster:               "test_cluster",
   115  				EDSServiceName:        "test-eds",
   116  				LoadReportingServer:   testLRSServerConfig,
   117  				MaxConcurrentRequests: newUint32(123),
   118  				DropCategories: []DropConfig{
   119  					{Category: "drop-1", RequestsPerMillion: 314},
   120  					{Category: "drop-2", RequestsPerMillion: 159},
   121  				},
   122  				ChildPolicy: &internalserviceconfig.BalancerConfig{
   123  					Name:   wtName,
   124  					Config: wtConfig,
   125  				},
   126  			},
   127  			wantErr: false,
   128  		},
   129  	}
   130  	for _, tt := range tests {
   131  		t.Run(tt.name, func(t *testing.T) {
   132  			got, err := parseConfig([]byte(tt.js))
   133  			if (err != nil) != tt.wantErr {
   134  				t.Fatalf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
   135  			}
   136  			if !cmp.Equal(got, tt.want, cmpopts.IgnoreFields(bootstrap.ServerConfig{}, "Creds")) {
   137  				t.Errorf("parseConfig() got unexpected result, diff: %v", cmp.Diff(got, tt.want))
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func newUint32(i uint32) *uint32 {
   144  	return &i
   145  }
   146  

View as plain text