github.com/polarismesh/polaris@v1.17.8/service/routing_config_v2_test.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package service_test 19 20 import ( 21 "fmt" 22 "testing" 23 24 "github.com/golang/protobuf/ptypes" 25 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 26 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 27 apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" 28 "github.com/stretchr/testify/assert" 29 "google.golang.org/protobuf/types/known/anypb" 30 31 "github.com/polarismesh/polaris/common/utils" 32 "github.com/polarismesh/polaris/service" 33 ) 34 35 // checkSameRoutingConfigV2 检查routingConfig前后是否一致 36 func checkSameRoutingConfigV2V2(t *testing.T, lhs []*apitraffic.RouteRule, rhs []*apitraffic.RouteRule) { 37 if len(lhs) != len(rhs) { 38 t.Fatal("error: len(lhs) != len(rhs)") 39 } 40 } 41 42 // TestCreateRoutingConfigV2 测试创建路由配置 43 func TestCreateRoutingConfigV2(t *testing.T) { 44 45 discoverSuit := &DiscoverTestSuit{} 46 if err := discoverSuit.Initialize(); err != nil { 47 t.Fatal(err) 48 } 49 defer discoverSuit.Destroy() 50 51 t.Run("正常创建路由配置配置请求", func(t *testing.T) { 52 req := discoverSuit.createCommonRoutingConfigV2(t, 3) 53 defer discoverSuit.truncateCommonRoutingConfigV2() 54 55 // 对写进去的数据进行查询 56 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 57 out := discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 58 "limit": "100", 59 "offset": "0", 60 }) 61 if !respSuccess(out) { 62 t.Fatalf("error: %+v", out) 63 } 64 65 ret, _ := unmarshalRoutingV2toAnySlice(out.GetData()) 66 t.Logf("query routing v2 : %#v", ret) 67 68 // assert.Equal(t, int(3), int(out.Amount), "query routing size") 69 70 // 按照名字查询 71 72 out = discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 73 "limit": "100", 74 "offset": "0", 75 "name": req[0].Name, 76 }) 77 if !respSuccess(out) { 78 t.Fatalf("error: %+v", out) 79 } 80 rulesV2, err := unmarshalRoutingV2toAnySlice(out.GetData()) 81 assert.NoError(t, err) 82 for i := range rulesV2 { 83 item := rulesV2[i] 84 msg := &apitraffic.RuleRoutingConfig{} 85 err := ptypes.UnmarshalAny(item.GetRoutingConfig(), msg) 86 assert.NoError(t, err) 87 assert.True(t, len(msg.GetSources()) == 0, "RuleRoutingConfig.Sources len != 0") 88 assert.True(t, len(msg.GetDestinations()) == 0, "RuleRoutingConfig.Destinations len != 0") 89 assert.True(t, len(msg.GetRules()) != 0, "RuleRoutingConfig.Rules len == 0") 90 } 91 92 assert.Equal(t, int(1), int(out.Amount.GetValue()), "query routing size") 93 94 item, err := service.Api2RoutingConfigV2(req[0]) 95 assert.NoError(t, err) 96 expendItem, err := item.ToExpendRoutingConfig() 97 assert.NoError(t, err) 98 99 // 基于服务信息查询 100 out = discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 101 "limit": "100", 102 "offset": "0", 103 "namespace": expendItem.RuleRouting.Rules[0].Sources[0].Namespace, 104 "service": expendItem.RuleRouting.Rules[0].Sources[0].Service, 105 }) 106 if !respSuccess(out) { 107 t.Fatalf("error: %+v", out) 108 } 109 rulesV2, err = unmarshalRoutingV2toAnySlice(out.GetData()) 110 assert.NoError(t, err) 111 for i := range rulesV2 { 112 item := rulesV2[i] 113 msg := &apitraffic.RuleRoutingConfig{} 114 err := ptypes.UnmarshalAny(item.GetRoutingConfig(), msg) 115 assert.NoError(t, err) 116 assert.True(t, len(msg.GetSources()) == 0, "RuleRoutingConfig.Sources len != 0") 117 assert.True(t, len(msg.GetDestinations()) == 0, "RuleRoutingConfig.Destinations len != 0") 118 assert.True(t, len(msg.GetRules()) != 0, "RuleRoutingConfig.Rules len == 0") 119 } 120 }) 121 } 122 123 // TestCompatibleRoutingConfigV2AndV1 测试V2版本的路由规则和V1版本的路由规则 124 func TestCompatibleRoutingConfigV2AndV1(t *testing.T) { 125 126 svc := &apiservice.Service{ 127 Name: utils.NewStringValue("compatible-routing"), 128 Namespace: utils.NewStringValue("compatible"), 129 } 130 131 initSuitFunc := func(t *testing.T) *DiscoverTestSuit { 132 discoverSuit := &DiscoverTestSuit{} 133 if err := discoverSuit.Initialize(); err != nil { 134 t.Fatal(err) 135 } 136 t.Cleanup(func() { 137 discoverSuit.Destroy() 138 }) 139 140 createSvcResp := discoverSuit.DiscoverServer().CreateServices(discoverSuit.DefaultCtx, []*apiservice.Service{svc}) 141 if !respSuccess(createSvcResp) { 142 t.Fatalf("error: %s", createSvcResp.GetInfo().GetValue()) 143 } 144 145 _ = createSvcResp.Responses[0].GetService() 146 t.Cleanup(func() { 147 discoverSuit.cleanServices([]*apiservice.Service{svc}) 148 }) 149 return discoverSuit 150 } 151 152 t.Run("V1的存量规则-走V2接口可以查询到,ExtendInfo符合要求", func(t *testing.T) { 153 discoverSuit := initSuitFunc(t) 154 _, _ = discoverSuit.createCommonRoutingConfigV1IntoOldStore(t, svc, 3, 0) 155 t.Cleanup(func() { 156 discoverSuit.cleanCommonRoutingConfig(svc.GetName().GetValue(), svc.GetNamespace().GetValue()) 157 discoverSuit.truncateCommonRoutingConfigV2() 158 }) 159 160 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 161 // 从缓存中查询应该查到 3+3 条 v2 的路由规则 162 out := discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 163 "limit": "100", 164 "offset": "0", 165 }) 166 if !respSuccess(out) { 167 t.Fatalf("error: %+v", out) 168 } 169 assert.Equal(t, int(3), int(out.GetAmount().GetValue()), "query routing size") 170 171 rulesV2, err := unmarshalRoutingV2toAnySlice(out.GetData()) 172 assert.NoError(t, err) 173 for i := range rulesV2 { 174 item := rulesV2[i] 175 assert.True(t, item.Enable, "v1 to v2 need default open enable") 176 msg := &apitraffic.RuleRoutingConfig{} 177 err := ptypes.UnmarshalAny(item.GetRoutingConfig(), msg) 178 assert.NoError(t, err) 179 assert.True(t, len(msg.GetSources()) == 0, "RuleRoutingConfig.Sources len != 0") 180 assert.True(t, len(msg.GetDestinations()) == 0, "RuleRoutingConfig.Destinations len != 0") 181 assert.True(t, len(msg.GetRules()) != 0, "RuleRoutingConfig.Rules len == 0") 182 } 183 }) 184 185 t.Run("V1的存量规则-走v2规则的启用可正常迁移v1规则", func(t *testing.T) { 186 discoverSuit := initSuitFunc(t) 187 _, _ = discoverSuit.createCommonRoutingConfigV1IntoOldStore(t, svc, 3, 0) 188 t.Cleanup(func() { 189 discoverSuit.cleanCommonRoutingConfig(svc.GetName().GetValue(), svc.GetNamespace().GetValue()) 190 discoverSuit.truncateCommonRoutingConfigV2() 191 }) 192 193 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 194 // 从缓存中查询应该查到 3 条 v2 的路由规则 195 out := discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 196 "limit": "100", 197 "offset": "0", 198 }) 199 if !respSuccess(out) { 200 t.Fatalf("error: %+v", out) 201 } 202 assert.Equal(t, int(3), int(out.GetAmount().GetValue()), "query routing size") 203 rulesV2, err := unmarshalRoutingV2toAnySlice(out.GetData()) 204 assert.NoError(t, err) 205 206 // 选择其中一条规则进行enable操作 207 v2resp := discoverSuit.DiscoverServer().EnableRoutings(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{rulesV2[0]}) 208 if !respSuccess(v2resp) { 209 t.Fatalf("error: %+v", v2resp) 210 } 211 // 直接查询存储无法查询到 v1 的路由规则 212 total, routingsV1, err := discoverSuit.Storage.GetRoutingConfigs(map[string]string{}, 0, 100) 213 assert.NoError(t, err, err) 214 assert.Equal(t, uint32(0), total, "v1 routing must delete and transfer to v1") 215 assert.Equal(t, 0, len(routingsV1), "v1 routing ret len need zero") 216 217 // 从缓存中查询应该查到 3 条 v2 的路由规则 218 out = discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 219 "limit": "100", 220 "offset": "0", 221 }) 222 if !respSuccess(out) { 223 t.Fatalf("error: %+v", out) 224 } 225 assert.Equal(t, int(3), int(out.GetAmount().GetValue()), "query routing size") 226 rulesV2, err = unmarshalRoutingV2toAnySlice(out.GetData()) 227 assert.NoError(t, err) 228 for i := range rulesV2 { 229 item := rulesV2[i] 230 assert.True(t, item.Enable, "v1 to v2 need default open enable") 231 msg := &apitraffic.RuleRoutingConfig{} 232 err := ptypes.UnmarshalAny(item.GetRoutingConfig(), msg) 233 assert.NoError(t, err) 234 assert.True(t, len(msg.GetSources()) == 0, "RuleRoutingConfig.Sources len != 0") 235 assert.True(t, len(msg.GetDestinations()) == 0, "RuleRoutingConfig.Destinations len != 0") 236 assert.True(t, len(msg.GetRules()) != 0, "RuleRoutingConfig.Rules len == 0") 237 } 238 }) 239 240 t.Run("V1的存量规则-走v2规则的删除可正常迁移v1规则", func(t *testing.T) { 241 discoverSuit := initSuitFunc(t) 242 _, _ = discoverSuit.createCommonRoutingConfigV1IntoOldStore(t, svc, 3, 0) 243 t.Cleanup(func() { 244 discoverSuit.cleanCommonRoutingConfig(svc.GetName().GetValue(), svc.GetNamespace().GetValue()) 245 discoverSuit.truncateCommonRoutingConfigV2() 246 }) 247 248 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 249 // 从缓存中查询应该查到 3+3 条 v2 的路由规则 250 out := discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 251 "limit": "100", 252 "offset": "0", 253 }) 254 if !respSuccess(out) { 255 t.Fatalf("error: %+v", out) 256 } 257 assert.Equal(t, int(3), int(out.GetAmount().GetValue()), "query routing size") 258 259 rulesV2, err := unmarshalRoutingV2toAnySlice(out.GetData()) 260 assert.NoError(t, err) 261 262 // 选择其中一条规则进行删除操作 263 v2resp := discoverSuit.DiscoverServer().DeleteRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{rulesV2[0]}) 264 if !respSuccess(v2resp) { 265 t.Fatalf("error: %+v", v2resp) 266 } 267 // 直接查询存储无法查询到 v1 的路由规则 268 total, routingsV1, err := discoverSuit.Storage.GetRoutingConfigs(map[string]string{}, 0, 100) 269 assert.NoError(t, err, err) 270 assert.Equal(t, uint32(0), total, "v1 routing must delete and transfer to v1") 271 assert.Equal(t, 0, len(routingsV1), "v1 routing ret len need zero") 272 273 // 查询对应的 v2 规则也查询不到 274 ruleV2, err := discoverSuit.Storage.GetRoutingConfigV2WithID(rulesV2[0].Id) 275 assert.NoError(t, err, err) 276 assert.Nil(t, ruleV2, "v2 routing must delete") 277 278 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 279 // 从缓存中查询应该查到 2 条 v2 的路由规则 280 out = discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 281 "limit": "100", 282 "offset": "0", 283 }) 284 if !respSuccess(out) { 285 t.Fatalf("error: %+v", out) 286 } 287 assert.Equal(t, int(2), int(out.GetAmount().GetValue()), "query routing size") 288 rulesV2, err = unmarshalRoutingV2toAnySlice(out.GetData()) 289 assert.NoError(t, err) 290 for i := range rulesV2 { 291 item := rulesV2[i] 292 assert.True(t, item.Enable, "v1 to v2 need default open enable") 293 msg := &apitraffic.RuleRoutingConfig{} 294 err := ptypes.UnmarshalAny(item.GetRoutingConfig(), msg) 295 assert.NoError(t, err) 296 assert.True(t, len(msg.GetSources()) == 0, "RuleRoutingConfig.Sources len != 0") 297 assert.True(t, len(msg.GetDestinations()) == 0, "RuleRoutingConfig.Destinations len != 0") 298 assert.True(t, len(msg.GetRules()) != 0, "RuleRoutingConfig.Rules len == 0") 299 } 300 }) 301 302 t.Run("V1的存量规则-走v2规则的编辑可正常迁移v1规则", func(t *testing.T) { 303 discoverSuit := initSuitFunc(t) 304 _, _ = discoverSuit.createCommonRoutingConfigV1IntoOldStore(t, svc, 3, 0) 305 t.Cleanup(func() { 306 discoverSuit.cleanCommonRoutingConfig(svc.GetName().GetValue(), svc.GetNamespace().GetValue()) 307 discoverSuit.truncateCommonRoutingConfigV2() 308 }) 309 310 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 311 // 从缓存中查询应该查到 3+3 条 v2 的路由规则 312 out := discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 313 "limit": "100", 314 "offset": "0", 315 }) 316 if !respSuccess(out) { 317 t.Fatalf("error: %+v", out) 318 } 319 assert.Equal(t, int(3), int(out.GetAmount().GetValue()), "query routing size") 320 321 rulesV2, err := unmarshalRoutingV2toAnySlice(out.GetData()) 322 assert.NoError(t, err) 323 324 // 需要将 v2 规则的 extendInfo 规则清理掉 325 // 选择其中一条规则进行enable操作 326 rulesV2[0].Description = "update v2 rule and transfer v1 to v2" 327 v2resp := discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{rulesV2[0]}) 328 if !respSuccess(v2resp) { 329 t.Fatalf("error: %+v", v2resp) 330 } 331 // 直接查询存储无法查询到 v1 的路由规则 332 total, routingsV1, err := discoverSuit.Storage.GetRoutingConfigs(map[string]string{}, 0, 100) 333 assert.NoError(t, err, err) 334 assert.Equal(t, uint32(0), total, "v1 routing must delete and transfer to v1") 335 assert.Equal(t, 0, len(routingsV1), "v1 routing ret len need zero") 336 337 // 查询对应的 v2 规则能够查询到 338 ruleV2, err := discoverSuit.Storage.GetRoutingConfigV2WithID(rulesV2[0].Id) 339 assert.NoError(t, err, err) 340 assert.NotNil(t, ruleV2, "v2 routing must exist") 341 assert.Equal(t, rulesV2[0].Description, ruleV2.Description) 342 343 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 344 out = discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 345 "limit": "100", 346 "offset": "0", 347 }) 348 if !respSuccess(out) { 349 t.Fatalf("error: %+v", out) 350 } 351 assert.Equal(t, int(3), int(out.GetAmount().GetValue()), "query routing size") 352 rulesV2, err = unmarshalRoutingV2toAnySlice(out.GetData()) 353 assert.NoError(t, err) 354 for i := range rulesV2 { 355 item := rulesV2[i] 356 assert.True(t, item.Enable, "v1 to v2 need default open enable") 357 msg := &apitraffic.RuleRoutingConfig{} 358 err := ptypes.UnmarshalAny(item.GetRoutingConfig(), msg) 359 assert.NoError(t, err) 360 assert.True(t, len(msg.GetSources()) == 0, "RuleRoutingConfig.Sources len != 0") 361 assert.True(t, len(msg.GetDestinations()) == 0, "RuleRoutingConfig.Destinations len != 0") 362 assert.True(t, len(msg.GetRules()) != 0, "RuleRoutingConfig.Rules len == 0") 363 } 364 }) 365 } 366 367 // TestDeleteRoutingConfigV2 测试删除路由配置 368 func TestDeleteRoutingConfigV2(t *testing.T) { 369 370 discoverSuit := &DiscoverTestSuit{} 371 if err := discoverSuit.Initialize(); err != nil { 372 t.Fatal(err) 373 } 374 defer discoverSuit.Destroy() 375 376 t.Run("可以正常删除路由配置", func(t *testing.T) { 377 resp := discoverSuit.createCommonRoutingConfigV2(t, 1) 378 discoverSuit.deleteCommonRoutingConfigV2(t, resp[0]) 379 defer discoverSuit.cleanCommonRoutingConfigV2(resp) 380 381 serviceName := fmt.Sprintf("in-source-service-%d", 0) 382 namespaceName := fmt.Sprintf("in-source-service-%d", 0) 383 384 // 删除之后,数据不见 385 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 386 out := discoverSuit.DiscoverServer().GetRoutingConfigWithCache(discoverSuit.DefaultCtx, &apiservice.Service{ 387 Name: utils.NewStringValue(serviceName), 388 Namespace: utils.NewStringValue(namespaceName), 389 }) 390 391 noExist := out.GetRouting() == nil || 392 ((len(out.GetRouting().Inbounds) == 0 && len(out.GetRouting().GetOutbounds()) == 0) || 393 len(out.Routing.GetRules()) == 0) 394 assert.True(t, noExist) 395 }) 396 } 397 398 // TestUpdateRoutingConfigV2 测试更新路由配置 399 func TestUpdateRoutingConfigV2(t *testing.T) { 400 401 discoverSuit := &DiscoverTestSuit{} 402 if err := discoverSuit.Initialize(); err != nil { 403 t.Fatal(err) 404 } 405 defer discoverSuit.Destroy() 406 407 t.Run("可以正常更新路由配置", func(t *testing.T) { 408 req := discoverSuit.createCommonRoutingConfigV2(t, 1) 409 defer discoverSuit.cleanCommonRoutingConfigV2(req) 410 // 对写进去的数据进行查询 411 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 412 out := discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 413 "limit": "100", 414 "offset": "0", 415 }) 416 if !respSuccess(out) { 417 t.Fatalf("error: %+v", out) 418 } 419 420 assert.Equal(t, uint32(1), out.Size.GetValue(), "query routing size") 421 422 ret, err := unmarshalRoutingV2toAnySlice(out.GetData()) 423 assert.NoError(t, err) 424 routing := ret[0] 425 426 updateName := "update routing second" 427 routing.Name = updateName 428 429 discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{routing}) 430 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 431 out = discoverSuit.DiscoverServer().QueryRoutingConfigsV2(discoverSuit.DefaultCtx, map[string]string{ 432 "limit": "100", 433 "offset": "0", 434 "id": routing.Id, 435 }) 436 437 if !respSuccess(out) { 438 t.Fatalf("error: %+v", out) 439 } 440 441 assert.Equal(t, uint32(1), out.Size.GetValue(), "query routing size") 442 ret, err = unmarshalRoutingV2toAnySlice(out.GetData()) 443 assert.NoError(t, err) 444 assert.Equal(t, updateName, ret[0].Name) 445 }) 446 } 447 448 // test对routing字段进行校验 449 func TestCreateCheckRoutingFieldLenV2(t *testing.T) { 450 451 discoverSuit := &DiscoverTestSuit{} 452 if err := discoverSuit.Initialize(); err != nil { 453 t.Fatal(err) 454 } 455 defer discoverSuit.Destroy() 456 457 any, _ := ptypes.MarshalAny(&apitraffic.RuleRoutingConfig{}) 458 459 req := &apitraffic.RouteRule{ 460 Id: "", 461 Name: "test-routing", 462 Namespace: "", 463 Enable: false, 464 RoutingPolicy: apitraffic.RoutingPolicy_RulePolicy, 465 RoutingConfig: any, 466 Revision: "", 467 Ctime: "", 468 Mtime: "", 469 Etime: "", 470 Priority: 0, 471 Description: "", 472 } 473 474 t.Run("创建路由规则,规则名称超长", func(t *testing.T) { 475 str := genSpecialStr(129) 476 oldName := req.Name 477 req.Name = str 478 resp := discoverSuit.DiscoverServer().CreateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 479 req.Name = oldName 480 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingName) { 481 t.Fatalf("%+v", resp) 482 } 483 }) 484 t.Run("创建路由规则,路由规则类型不正确", func(t *testing.T) { 485 oldPolicy := req.RoutingPolicy 486 req.RoutingPolicy = apitraffic.RoutingPolicy(123) 487 resp := discoverSuit.DiscoverServer().CreateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 488 req.RoutingPolicy = oldPolicy 489 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingPolicy) { 490 t.Fatalf("%+v", resp) 491 } 492 }) 493 t.Run("创建路由规则,路由规则类型不正确", func(t *testing.T) { 494 oldPolicy := req.RoutingPolicy 495 req.RoutingPolicy = apitraffic.RoutingPolicy_MetadataPolicy 496 resp := discoverSuit.DiscoverServer().CreateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 497 req.RoutingPolicy = oldPolicy 498 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingPolicy) { 499 t.Fatalf("%+v", resp) 500 } 501 }) 502 t.Run("创建路由规则,路由优先级不正确", func(t *testing.T) { 503 oldPriority := req.Priority 504 req.Priority = 11 505 resp := discoverSuit.DiscoverServer().CreateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 506 req.Priority = oldPriority 507 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingPriority) { 508 t.Fatalf("%+v", resp) 509 } 510 }) 511 } 512 513 // TestUpdateCheckRoutingFieldLenV2 test对routing字段进行校验 514 func TestUpdateCheckRoutingFieldLenV2(t *testing.T) { 515 516 discoverSuit := &DiscoverTestSuit{} 517 if err := discoverSuit.Initialize(); err != nil { 518 t.Fatal(err) 519 } 520 defer discoverSuit.Destroy() 521 522 any, _ := ptypes.MarshalAny(&apitraffic.RuleRoutingConfig{}) 523 524 req := &apitraffic.RouteRule{ 525 Id: "12312312312312313", 526 Name: "test-routing", 527 Namespace: "", 528 Enable: false, 529 RoutingPolicy: apitraffic.RoutingPolicy_RulePolicy, 530 RoutingConfig: any, 531 Revision: "", 532 Ctime: "", 533 Mtime: "", 534 Etime: "", 535 Priority: 0, 536 Description: "", 537 } 538 539 t.Run("更新路由规则,规则ID为空", func(t *testing.T) { 540 oldId := req.Id 541 req.Id = "" 542 resp := discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 543 req.Id = oldId 544 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingID) { 545 t.Fatalf("%+v", resp) 546 } 547 }) 548 t.Run("更新路由规则,规则名称超长", func(t *testing.T) { 549 str := genSpecialStr(129) 550 oldName := req.Name 551 req.Name = str 552 resp := discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 553 req.Name = oldName 554 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingName) { 555 t.Fatalf("%+v", resp) 556 } 557 }) 558 t.Run("更新路由规则,路由规则类型不正确", func(t *testing.T) { 559 oldPolicy := req.RoutingPolicy 560 req.RoutingPolicy = apitraffic.RoutingPolicy(123) 561 resp := discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 562 req.RoutingPolicy = oldPolicy 563 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingPolicy) { 564 t.Fatalf("%+v", resp) 565 } 566 }) 567 t.Run("更新路由规则,路由规则类型不正确", func(t *testing.T) { 568 oldPolicy := req.RoutingPolicy 569 req.RoutingPolicy = apitraffic.RoutingPolicy_MetadataPolicy 570 resp := discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 571 req.RoutingPolicy = oldPolicy 572 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingPolicy) { 573 t.Fatalf("%+v", resp) 574 } 575 }) 576 t.Run("更新路由规则,路由优先级不正确", func(t *testing.T) { 577 oldPriority := req.Priority 578 req.Priority = 11 579 resp := discoverSuit.DiscoverServer().UpdateRoutingConfigsV2(discoverSuit.DefaultCtx, []*apitraffic.RouteRule{req}) 580 req.Priority = oldPriority 581 if resp.Code.GetValue() != uint32(apimodel.Code_InvalidRoutingPriority) { 582 t.Fatalf("%+v", resp) 583 } 584 }) 585 } 586 587 // marshalRoutingV2toAnySlice 转换为 []*apitraffic.RouteRule 数组 588 func unmarshalRoutingV2toAnySlice(routings []*anypb.Any) ([]*apitraffic.RouteRule, error) { 589 ret := make([]*apitraffic.RouteRule, 0, len(routings)) 590 591 for i := range routings { 592 entry := routings[i] 593 594 msg := &apitraffic.RouteRule{} 595 if err := ptypes.UnmarshalAny(entry, msg); err != nil { 596 return nil, err 597 } 598 599 ret = append(ret, msg) 600 } 601 602 return ret, nil 603 }