...

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

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

     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 clustermanager
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	"google.golang.org/grpc/balancer"
    26  	_ "google.golang.org/grpc/balancer/weightedtarget"
    27  	internalserviceconfig "google.golang.org/grpc/internal/serviceconfig"
    28  	_ "google.golang.org/grpc/xds/internal/balancer/cdsbalancer"
    29  )
    30  
    31  const (
    32  	testJSONConfig = `{
    33        "children":{
    34          "cds:cluster_1":{
    35            "childPolicy":[{
    36              "cds_experimental":{"cluster":"cluster_1"}
    37            }]
    38          },
    39          "weighted:cluster_1_cluster_2_1":{
    40            "childPolicy":[{
    41              "weighted_target_experimental":{
    42                "targets": {
    43                  "cluster_1" : {
    44                    "weight":75,
    45                    "childPolicy":[{"cds_experimental":{"cluster":"cluster_1"}}]
    46                  },
    47                  "cluster_2" : {
    48                    "weight":25,
    49                    "childPolicy":[{"cds_experimental":{"cluster":"cluster_2"}}]
    50                  }
    51                }
    52              }
    53            }]
    54          },
    55          "weighted:cluster_1_cluster_3_1":{
    56            "childPolicy":[{
    57              "weighted_target_experimental":{
    58                "targets": {
    59                  "cluster_1": {
    60                    "weight":99,
    61                    "childPolicy":[{"cds_experimental":{"cluster":"cluster_1"}}]
    62                  },
    63                  "cluster_3": {
    64                    "weight":1,
    65                    "childPolicy":[{"cds_experimental":{"cluster":"cluster_3"}}]
    66                  }
    67                }
    68              }
    69            }]
    70          }
    71        }
    72  }
    73  `
    74  
    75  	cdsName = "cds_experimental"
    76  	wtName  = "weighted_target_experimental"
    77  )
    78  
    79  var (
    80  	cdsConfigParser = balancer.Get(cdsName).(balancer.ConfigParser)
    81  	cdsConfigJSON1  = `{"cluster":"cluster_1"}`
    82  	cdsConfig1, _   = cdsConfigParser.ParseConfig([]byte(cdsConfigJSON1))
    83  
    84  	wtConfigParser = balancer.Get(wtName).(balancer.ConfigParser)
    85  	wtConfigJSON1  = `{
    86  	"targets": {
    87  	  "cluster_1" : { "weight":75, "childPolicy":[{"cds_experimental":{"cluster":"cluster_1"}}] },
    88  	  "cluster_2" : { "weight":25, "childPolicy":[{"cds_experimental":{"cluster":"cluster_2"}}] }
    89  	} }`
    90  	wtConfig1, _  = wtConfigParser.ParseConfig([]byte(wtConfigJSON1))
    91  	wtConfigJSON2 = `{
    92      "targets": {
    93        "cluster_1": { "weight":99, "childPolicy":[{"cds_experimental":{"cluster":"cluster_1"}}] },
    94        "cluster_3": { "weight":1, "childPolicy":[{"cds_experimental":{"cluster":"cluster_3"}}] }
    95      } }`
    96  	wtConfig2, _ = wtConfigParser.ParseConfig([]byte(wtConfigJSON2))
    97  )
    98  
    99  func Test_parseConfig(t *testing.T) {
   100  	tests := []struct {
   101  		name    string
   102  		js      string
   103  		want    *lbConfig
   104  		wantErr bool
   105  	}{
   106  		{
   107  			name:    "empty json",
   108  			js:      "",
   109  			want:    nil,
   110  			wantErr: true,
   111  		},
   112  		{
   113  			name: "OK",
   114  			js:   testJSONConfig,
   115  			want: &lbConfig{
   116  				Children: map[string]childConfig{
   117  					"cds:cluster_1": {ChildPolicy: &internalserviceconfig.BalancerConfig{
   118  						Name: cdsName, Config: cdsConfig1},
   119  					},
   120  					"weighted:cluster_1_cluster_2_1": {ChildPolicy: &internalserviceconfig.BalancerConfig{
   121  						Name: wtName, Config: wtConfig1},
   122  					},
   123  					"weighted:cluster_1_cluster_3_1": {ChildPolicy: &internalserviceconfig.BalancerConfig{
   124  						Name: wtName, Config: wtConfig2},
   125  					},
   126  				},
   127  			},
   128  			wantErr: false,
   129  		},
   130  	}
   131  
   132  	for _, tt := range tests {
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			got, err := parseConfig([]byte(tt.js))
   135  			if (err != nil) != tt.wantErr {
   136  				t.Errorf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
   137  				return
   138  			}
   139  			if d := cmp.Diff(got, tt.want, cmp.AllowUnexported(lbConfig{})); d != "" {
   140  				t.Errorf("parseConfig() got unexpected result, diff: %v", d)
   141  			}
   142  		})
   143  	}
   144  }
   145  

View as plain text