gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/balancer/weightedtarget/weightedtarget_config_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 weightedtarget
    20  
    21  import (
    22  	"testing"
    23  
    24  	"gitee.com/ks-custle/core-gm/grpc/balancer"
    25  	_ "gitee.com/ks-custle/core-gm/grpc/balancer/grpclb"
    26  	"gitee.com/ks-custle/core-gm/grpc/balancer/roundrobin"
    27  	internalserviceconfig "gitee.com/ks-custle/core-gm/grpc/internal/serviceconfig"
    28  	"github.com/google/go-cmp/cmp"
    29  )
    30  
    31  const (
    32  	testJSONConfig = `{
    33    "targets": {
    34  	"cluster_1": {
    35  	  "weight": 75,
    36  	  "childPolicy": [{
    37          "grpclb": {
    38            "childPolicy": [{"pick_first":{}}],
    39            "targetName": "foo-service"
    40          }
    41        }]
    42  	},
    43  	"cluster_2": {
    44  	  "weight": 25,
    45  	  "childPolicy": [{"round_robin": ""}]
    46  	}
    47    }
    48  }`
    49  )
    50  
    51  var (
    52  	grpclbConfigParser = balancer.Get("grpclb").(balancer.ConfigParser)
    53  	grpclbConfigJSON   = `{"childPolicy": [{"pick_first":{}}], "targetName": "foo-service"}`
    54  	grpclbConfig, _    = grpclbConfigParser.ParseConfig([]byte(grpclbConfigJSON))
    55  )
    56  
    57  func (s) TestParseConfig(t *testing.T) {
    58  	tests := []struct {
    59  		name    string
    60  		js      string
    61  		want    *LBConfig
    62  		wantErr bool
    63  	}{
    64  		{
    65  			name:    "empty json",
    66  			js:      "",
    67  			want:    nil,
    68  			wantErr: true,
    69  		},
    70  		{
    71  			name: "OK",
    72  			js:   testJSONConfig,
    73  			want: &LBConfig{
    74  				Targets: map[string]Target{
    75  					"cluster_1": {
    76  						Weight: 75,
    77  						ChildPolicy: &internalserviceconfig.BalancerConfig{
    78  							Name:   "grpclb",
    79  							Config: grpclbConfig,
    80  						},
    81  					},
    82  					"cluster_2": {
    83  						Weight: 25,
    84  						ChildPolicy: &internalserviceconfig.BalancerConfig{
    85  							Name: roundrobin.Name,
    86  						},
    87  					},
    88  				},
    89  			},
    90  			wantErr: false,
    91  		},
    92  	}
    93  	for _, tt := range tests {
    94  		t.Run(tt.name, func(t *testing.T) {
    95  			got, err := parseConfig([]byte(tt.js))
    96  			if (err != nil) != tt.wantErr {
    97  				t.Fatalf("parseConfig() error = %v, wantErr %v", err, tt.wantErr)
    98  			}
    99  			if !cmp.Equal(got, tt.want) {
   100  				t.Errorf("parseConfig() got unexpected result, diff: %v", cmp.Diff(got, tt.want))
   101  			}
   102  		})
   103  	}
   104  }