gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/balancer/clusterresolver/config_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 19 package clusterresolver 20 21 import ( 22 "encoding/json" 23 "testing" 24 25 "gitee.com/ks-custle/core-gm/grpc/internal/balancer/stub" 26 internalserviceconfig "gitee.com/ks-custle/core-gm/grpc/internal/serviceconfig" 27 "gitee.com/ks-custle/core-gm/grpc/xds/internal/balancer/ringhash" 28 "github.com/google/go-cmp/cmp" 29 ) 30 31 func TestDiscoveryMechanismTypeMarshalJSON(t *testing.T) { 32 tests := []struct { 33 name string 34 typ DiscoveryMechanismType 35 want string 36 }{ 37 { 38 name: "eds", 39 typ: DiscoveryMechanismTypeEDS, 40 want: `"EDS"`, 41 }, 42 { 43 name: "dns", 44 typ: DiscoveryMechanismTypeLogicalDNS, 45 want: `"LOGICAL_DNS"`, 46 }, 47 } 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 if got, err := json.Marshal(tt.typ); err != nil || string(got) != tt.want { 51 t.Fatalf("DiscoveryMechanismTypeEDS.MarshalJSON() = (%v, %v), want (%s, nil)", string(got), err, tt.want) 52 } 53 }) 54 } 55 } 56 func TestDiscoveryMechanismTypeUnmarshalJSON(t *testing.T) { 57 tests := []struct { 58 name string 59 js string 60 want DiscoveryMechanismType 61 wantErr bool 62 }{ 63 { 64 name: "eds", 65 js: `"EDS"`, 66 want: DiscoveryMechanismTypeEDS, 67 }, 68 { 69 name: "dns", 70 js: `"LOGICAL_DNS"`, 71 want: DiscoveryMechanismTypeLogicalDNS, 72 }, 73 { 74 name: "error", 75 js: `"1234"`, 76 wantErr: true, 77 }, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 var got DiscoveryMechanismType 82 err := json.Unmarshal([]byte(tt.js), &got) 83 if (err != nil) != tt.wantErr { 84 t.Fatalf("DiscoveryMechanismTypeEDS.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) 85 } 86 if diff := cmp.Diff(got, tt.want); diff != "" { 87 t.Fatalf("DiscoveryMechanismTypeEDS.UnmarshalJSON() got unexpected output, diff (-got +want): %v", diff) 88 } 89 }) 90 } 91 } 92 93 func init() { 94 // This is needed now for the config parsing tests to pass. Otherwise they 95 // will fail with "RING_HASH unsupported". 96 // 97 // TODO: delete this once ring-hash policy is implemented and imported. 98 stub.Register(rhName, stub.BalancerFuncs{}) 99 } 100 101 const ( 102 testJSONConfig1 = `{ 103 "discoveryMechanisms": [{ 104 "cluster": "test-cluster-name", 105 "lrsLoadReportingServerName": "test-lrs-server", 106 "maxConcurrentRequests": 314, 107 "type": "EDS", 108 "edsServiceName": "test-eds-service-name" 109 }] 110 }` 111 testJSONConfig2 = `{ 112 "discoveryMechanisms": [{ 113 "cluster": "test-cluster-name", 114 "lrsLoadReportingServerName": "test-lrs-server", 115 "maxConcurrentRequests": 314, 116 "type": "EDS", 117 "edsServiceName": "test-eds-service-name" 118 },{ 119 "type": "LOGICAL_DNS" 120 }] 121 }` 122 testJSONConfig3 = `{ 123 "discoveryMechanisms": [{ 124 "cluster": "test-cluster-name", 125 "lrsLoadReportingServerName": "test-lrs-server", 126 "maxConcurrentRequests": 314, 127 "type": "EDS", 128 "edsServiceName": "test-eds-service-name" 129 }], 130 "xdsLbPolicy":[{"ROUND_ROBIN":{}}] 131 }` 132 testJSONConfig4 = `{ 133 "discoveryMechanisms": [{ 134 "cluster": "test-cluster-name", 135 "lrsLoadReportingServerName": "test-lrs-server", 136 "maxConcurrentRequests": 314, 137 "type": "EDS", 138 "edsServiceName": "test-eds-service-name" 139 }], 140 "xdsLbPolicy":[{"ring_hash_experimental":{}}] 141 }` 142 testJSONConfig5 = `{ 143 "discoveryMechanisms": [{ 144 "cluster": "test-cluster-name", 145 "lrsLoadReportingServerName": "test-lrs-server", 146 "maxConcurrentRequests": 314, 147 "type": "EDS", 148 "edsServiceName": "test-eds-service-name" 149 }], 150 "xdsLbPolicy":[{"pick_first":{}}] 151 }` 152 ) 153 154 func TestParseConfig(t *testing.T) { 155 tests := []struct { 156 name string 157 js string 158 want *LBConfig 159 wantErr bool 160 }{ 161 { 162 name: "empty json", 163 js: "", 164 want: nil, 165 wantErr: true, 166 }, 167 { 168 name: "OK with one discovery mechanism", 169 js: testJSONConfig1, 170 want: &LBConfig{ 171 DiscoveryMechanisms: []DiscoveryMechanism{ 172 { 173 Cluster: testClusterName, 174 LoadReportingServerName: newString(testLRSServer), 175 MaxConcurrentRequests: newUint32(testMaxRequests), 176 Type: DiscoveryMechanismTypeEDS, 177 EDSServiceName: testEDSServcie, 178 }, 179 }, 180 XDSLBPolicy: nil, 181 }, 182 wantErr: false, 183 }, 184 { 185 name: "OK with multiple discovery mechanisms", 186 js: testJSONConfig2, 187 want: &LBConfig{ 188 DiscoveryMechanisms: []DiscoveryMechanism{ 189 { 190 Cluster: testClusterName, 191 LoadReportingServerName: newString(testLRSServer), 192 MaxConcurrentRequests: newUint32(testMaxRequests), 193 Type: DiscoveryMechanismTypeEDS, 194 EDSServiceName: testEDSServcie, 195 }, 196 { 197 Type: DiscoveryMechanismTypeLogicalDNS, 198 }, 199 }, 200 XDSLBPolicy: nil, 201 }, 202 wantErr: false, 203 }, 204 { 205 name: "OK with picking policy round_robin", 206 js: testJSONConfig3, 207 want: &LBConfig{ 208 DiscoveryMechanisms: []DiscoveryMechanism{ 209 { 210 Cluster: testClusterName, 211 LoadReportingServerName: newString(testLRSServer), 212 MaxConcurrentRequests: newUint32(testMaxRequests), 213 Type: DiscoveryMechanismTypeEDS, 214 EDSServiceName: testEDSServcie, 215 }, 216 }, 217 XDSLBPolicy: &internalserviceconfig.BalancerConfig{ 218 Name: "ROUND_ROBIN", 219 Config: nil, 220 }, 221 }, 222 wantErr: false, 223 }, 224 { 225 name: "OK with picking policy ring_hash", 226 js: testJSONConfig4, 227 want: &LBConfig{ 228 DiscoveryMechanisms: []DiscoveryMechanism{ 229 { 230 Cluster: testClusterName, 231 LoadReportingServerName: newString(testLRSServer), 232 MaxConcurrentRequests: newUint32(testMaxRequests), 233 Type: DiscoveryMechanismTypeEDS, 234 EDSServiceName: testEDSServcie, 235 }, 236 }, 237 XDSLBPolicy: &internalserviceconfig.BalancerConfig{ 238 Name: ringhash.Name, 239 Config: nil, 240 }, 241 }, 242 wantErr: false, 243 }, 244 { 245 name: "unsupported picking policy", 246 js: testJSONConfig5, 247 wantErr: true, 248 }, 249 } 250 for _, tt := range tests { 251 t.Run(tt.name, func(t *testing.T) { 252 got, err := parseConfig([]byte(tt.js)) 253 if (err != nil) != tt.wantErr { 254 t.Fatalf("parseConfig() error = %v, wantErr %v", err, tt.wantErr) 255 } 256 if diff := cmp.Diff(got, tt.want); diff != "" { 257 t.Errorf("parseConfig() got unexpected output, diff (-got +want): %v", diff) 258 } 259 }) 260 } 261 } 262 263 func newString(s string) *string { 264 return &s 265 } 266 267 func newUint32(i uint32) *uint32 { 268 return &i 269 }