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