github.com/polarismesh/polaris@v1.17.8/service/ratelimit_config_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 "context" 22 "encoding/json" 23 "fmt" 24 "sync" 25 "testing" 26 "time" 27 28 "github.com/golang/protobuf/ptypes/duration" 29 apimodel "github.com/polarismesh/specification/source/go/api/v1/model" 30 apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage" 31 apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage" 32 "github.com/stretchr/testify/assert" 33 34 api "github.com/polarismesh/polaris/common/api/v1" 35 "github.com/polarismesh/polaris/common/utils" 36 ) 37 38 type CacheListener struct { 39 onCreated func(value interface{}) 40 onUpdated func(value interface{}) 41 onDeleted func(value interface{}) 42 onBatchCreated func(value interface{}) 43 onBatchUpdated func(value interface{}) 44 onBatchDeleted func(value interface{}) 45 } 46 47 // OnCreated callback when cache value created 48 func (l *CacheListener) OnCreated(value interface{}) { 49 if l.onCreated != nil { 50 l.onCreated(value) 51 } 52 } 53 54 // OnUpdated callback when cache value updated 55 func (l *CacheListener) OnUpdated(value interface{}) { 56 if l.onUpdated != nil { 57 l.onUpdated(value) 58 } 59 } 60 61 // OnDeleted callback when cache value deleted 62 func (l *CacheListener) OnDeleted(value interface{}) { 63 if l.onDeleted != nil { 64 l.onDeleted(value) 65 } 66 } 67 68 // OnBatchCreated callback when cache value created 69 func (l *CacheListener) OnBatchCreated(value interface{}) { 70 if l.onBatchCreated != nil { 71 l.onBatchCreated(value) 72 } 73 } 74 75 // OnBatchUpdated callback when cache value updated 76 func (l *CacheListener) OnBatchUpdated(value interface{}) { 77 if l.onBatchUpdated != nil { 78 l.onBatchUpdated(value) 79 } 80 } 81 82 // OnBatchDeleted callback when cache value deleted 83 func (l *CacheListener) OnBatchDeleted(value interface{}) { 84 if l.onBatchDeleted != nil { 85 l.onBatchDeleted(value) 86 } 87 } 88 89 /** 90 * @brief 测试创建限流规则 91 */ 92 func TestCreateRateLimit(t *testing.T) { 93 94 discoverSuit := &DiscoverTestSuit{} 95 if err := discoverSuit.Initialize(); err != nil { 96 t.Fatal(err) 97 } 98 defer discoverSuit.Destroy() 99 100 _, serviceResp := discoverSuit.createCommonService(t, 0) 101 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 102 defer discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 103 104 t.Run("正常创建限流规则", func(t *testing.T) { 105 _ = discoverSuit.DiscoverServer().Cache().Clear() 106 107 time.Sleep(5 * time.Second) 108 109 rateLimitReq, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 110 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 111 112 // 等待缓存更新 113 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 114 resp := discoverSuit.DiscoverServer().GetRateLimitWithCache(context.Background(), serviceResp) 115 checkRateLimit(t, rateLimitReq, resp.GetRateLimit().GetRules()[0]) 116 }) 117 118 t.Run("创建限流规则,删除,再创建,可以正常创建", func(t *testing.T) { 119 _ = discoverSuit.DiscoverServer().Cache().Clear() 120 time.Sleep(5 * time.Second) 121 122 rateLimitReq, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 123 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 124 discoverSuit.deleteRateLimit(t, rateLimitResp) 125 if resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimitReq}); !respSuccess(resp) { 126 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 127 } 128 129 // 等待缓存更新 130 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 131 resp := discoverSuit.DiscoverServer().GetRateLimitWithCache(context.Background(), serviceResp) 132 checkRateLimit(t, rateLimitReq, resp.GetRateLimit().GetRules()[0]) 133 discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 134 }) 135 136 t.Run("重复创建限流规则,返回成功", func(t *testing.T) { 137 rateLimitReq, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 138 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 139 if resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimitReq}); !respSuccess(resp) { 140 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 141 } else { 142 t.Log("pass") 143 } 144 discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 145 }) 146 147 t.Run("创建限流规则时,没有传递token,返回失败", func(t *testing.T) { 148 149 oldCtx := discoverSuit.DefaultCtx 150 151 discoverSuit.DefaultCtx = context.Background() 152 153 defer func() { 154 discoverSuit.DefaultCtx = oldCtx 155 }() 156 157 rateLimit := &apitraffic.Rule{ 158 Service: serviceResp.GetName(), 159 Namespace: serviceResp.GetNamespace(), 160 Labels: map[string]*apimodel.MatchString{}, 161 } 162 if resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}); !respSuccess(resp) { 163 t.Logf("pass: %s", resp.GetInfo().GetValue()) 164 } else { 165 t.Fatal("error") 166 } 167 }) 168 169 // t.Run("创建限流规则时,没有传递labels,返回失败", func(t *testing.T) { 170 // rateLimit := &apitraffic.Rule{ 171 // Service: serviceResp.GetName(), 172 // Namespace: serviceResp.GetNamespace(), 173 // ServiceToken: serviceResp.GetToken(), 174 // } 175 // if resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}); !respSuccess(resp) { 176 // t.Logf("pass: %s", resp.GetInfo().GetValue()) 177 // } else { 178 // t.Fatalf("error") 179 // } 180 // }) 181 182 t.Run("创建限流规则时,amounts具有相同的duration,返回失败", func(t *testing.T) { 183 rateLimit := &apitraffic.Rule{ 184 Service: serviceResp.GetName(), 185 Namespace: serviceResp.GetNamespace(), 186 Labels: map[string]*apimodel.MatchString{}, 187 Amounts: []*apitraffic.Amount{ 188 { 189 MaxAmount: utils.NewUInt32Value(1), 190 ValidDuration: &duration.Duration{ 191 Seconds: 10, 192 Nanos: 10, 193 }, 194 }, 195 { 196 MaxAmount: utils.NewUInt32Value(2), 197 ValidDuration: &duration.Duration{ 198 Seconds: 10, 199 Nanos: 10, 200 }, 201 }, 202 }, 203 ServiceToken: serviceResp.GetToken(), 204 } 205 if resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}); !respSuccess(resp) { 206 t.Logf("pass: %s", resp.GetInfo().GetValue()) 207 } else { 208 t.Fatalf("error") 209 } 210 }) 211 212 t.Run("并发创建同一服务的限流规则,可以正常创建", func(t *testing.T) { 213 var wg sync.WaitGroup 214 for i := 1; i <= 50; i++ { 215 wg.Add(1) 216 go func(index int) { 217 defer wg.Done() 218 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, index) 219 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 220 }(i) 221 } 222 wg.Wait() 223 t.Log("pass") 224 }) 225 226 t.Run("并发创建不同服务的限流规则,可以正常创建", func(t *testing.T) { 227 var wg sync.WaitGroup 228 for i := 1; i <= 50; i++ { 229 wg.Add(1) 230 go func(index int) { 231 defer wg.Done() 232 _, serviceResp := discoverSuit.createCommonService(t, index) 233 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 234 defer discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 235 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 236 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 237 }(i) 238 } 239 wg.Wait() 240 t.Log("pass") 241 }) 242 243 t.Run("为不存在的服务创建限流规则,返回成功", func(t *testing.T) { 244 _, serviceResp := discoverSuit.createCommonService(t, 2) 245 discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 246 rateLimit := &apitraffic.Rule{ 247 Service: serviceResp.GetName(), 248 Namespace: serviceResp.GetNamespace(), 249 Labels: map[string]*apimodel.MatchString{}, 250 ServiceToken: serviceResp.GetToken(), 251 } 252 if resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}); respSuccess(resp) { 253 t.Logf("pass: %s", resp.GetInfo().GetValue()) 254 } else { 255 t.Fatalf("error : %s", resp.GetInfo().GetValue()) 256 } 257 }) 258 } 259 260 /** 261 * @brief 测试删除限流规则 262 */ 263 func TestDeleteRateLimit(t *testing.T) { 264 265 discoverSuit := &DiscoverTestSuit{} 266 if err := discoverSuit.Initialize(); err != nil { 267 t.Fatal(err) 268 } 269 defer discoverSuit.Destroy() 270 271 _, serviceResp := discoverSuit.createCommonService(t, 0) 272 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 273 defer discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 274 275 getRateLimits := func(t *testing.T, service *apiservice.Service, expectNum uint32) []*apitraffic.Rule { 276 filters := map[string]string{ 277 "service": service.GetName().GetValue(), 278 "namespace": service.GetNamespace().GetValue(), 279 } 280 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 281 if !respSuccess(resp) { 282 t.Fatalf("error") 283 } 284 if resp.GetAmount().GetValue() != expectNum { 285 t.Fatalf("error") 286 } 287 return resp.GetRateLimits() 288 } 289 290 t.Run("删除存在的限流规则,可以正常删除", func(t *testing.T) { 291 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 292 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 293 discoverSuit.deleteRateLimit(t, rateLimitResp) 294 getRateLimits(t, serviceResp, 0) 295 t.Log("pass") 296 }) 297 298 t.Run("删除不存在的限流规则,返回正常", func(t *testing.T) { 299 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 300 discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 301 discoverSuit.deleteRateLimit(t, rateLimitResp) 302 getRateLimits(t, serviceResp, 0) 303 t.Log("pass") 304 }) 305 306 t.Run("删除限流规则时,没有传递token,返回失败", func(t *testing.T) { 307 rateLimitReq, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 308 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 309 rateLimitReq.ServiceToken = utils.NewStringValue("") 310 311 oldCtx := discoverSuit.DefaultCtx 312 313 discoverSuit.DefaultCtx = context.Background() 314 315 defer func() { 316 discoverSuit.DefaultCtx = oldCtx 317 }() 318 319 if resp := discoverSuit.DiscoverServer().DeleteRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimitReq}); !respSuccess(resp) { 320 t.Logf("pass: %s", resp.GetInfo().GetValue()) 321 } else { 322 t.Fatal("error") 323 } 324 }) 325 326 t.Run("并发删除限流规则,可以正常删除", func(t *testing.T) { 327 var wg sync.WaitGroup 328 for i := 1; i <= 50; i++ { 329 wg.Add(1) 330 go func(index int) { 331 defer wg.Done() 332 _, serviceResp := discoverSuit.createCommonService(t, index) 333 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 334 defer discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 335 rateLimitReq, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 3) 336 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 337 discoverSuit.deleteRateLimit(t, rateLimitReq) 338 }(i) 339 } 340 wg.Wait() 341 t.Log("pass") 342 }) 343 } 344 345 /** 346 * @brief 测试更新限流规则 347 */ 348 func TestUpdateRateLimit(t *testing.T) { 349 350 discoverSuit := &DiscoverTestSuit{} 351 if err := discoverSuit.Initialize(); err != nil { 352 t.Fatal(err) 353 } 354 defer discoverSuit.Destroy() 355 356 _, serviceResp := discoverSuit.createCommonService(t, 0) 357 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 358 defer discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 359 360 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 1) 361 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 362 363 t.Run("更新单个限流规则,可以正常更新", func(t *testing.T) { 364 updateRateLimitContent(rateLimitResp, 2) 365 discoverSuit.updateRateLimit(t, rateLimitResp) 366 filters := map[string]string{ 367 "service": serviceResp.GetName().GetValue(), 368 "namespace": serviceResp.GetNamespace().GetValue(), 369 } 370 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 371 if !respSuccess(resp) { 372 t.Fatalf("error") 373 } 374 assert.True(t, len(resp.GetRateLimits()) > 0) 375 checkRateLimit(t, rateLimitResp, resp.GetRateLimits()[0]) 376 }) 377 378 t.Run("更新一个不存在的限流规则", func(t *testing.T) { 379 discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 380 if resp := discoverSuit.DiscoverServer().UpdateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimitResp}); !respSuccess(resp) { 381 t.Logf("pass: %s", resp.GetInfo().GetValue()) 382 } else { 383 t.Fatalf("error") 384 } 385 }) 386 387 t.Run("更新限流规则时,没有传递token,正常", func(t *testing.T) { 388 oldCtx := discoverSuit.DefaultCtx 389 discoverSuit.DefaultCtx = context.Background() 390 391 defer func() { 392 discoverSuit.DefaultCtx = oldCtx 393 }() 394 395 rateLimitResp.ServiceToken = utils.NewStringValue("") 396 if resp := discoverSuit.DiscoverServer().UpdateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimitResp}); !respSuccess(resp) { 397 t.Logf("pass: %s", resp.GetInfo().GetValue()) 398 } else { 399 t.Fatalf("error") 400 } 401 }) 402 403 t.Run("并发更新限流规则时,可以正常更新", func(t *testing.T) { 404 var wg sync.WaitGroup 405 errs := make(chan error) 406 for i := 1; i <= 50; i++ { 407 wg.Add(1) 408 go func(index int) { 409 defer wg.Done() 410 _, serviceResp := discoverSuit.createCommonService(t, index) 411 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 412 discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 413 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, index) 414 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 415 updateRateLimitContent(rateLimitResp, index+1) 416 discoverSuit.updateRateLimit(t, rateLimitResp) 417 filters := map[string]string{ 418 "service": serviceResp.GetName().GetValue(), 419 "namespace": serviceResp.GetNamespace().GetValue(), 420 } 421 _ = discoverSuit.DiscoverServer().Cache().TestUpdate() 422 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 423 if !respSuccess(resp) { 424 errs <- fmt.Errorf("error : %v", resp) 425 } 426 checkRateLimit(t, rateLimitResp, resp.GetRateLimits()[0]) 427 }(i) 428 } 429 go func() { 430 wg.Wait() 431 close(errs) 432 }() 433 434 for err := range errs { 435 if err != nil { 436 t.Fatal(err) 437 } 438 } 439 440 t.Log("pass") 441 }) 442 } 443 444 func TestDisableRateLimit(t *testing.T) { 445 discoverSuit := &DiscoverTestSuit{} 446 if err := discoverSuit.Initialize(); err != nil { 447 t.Fatal(err) 448 } 449 defer discoverSuit.Destroy() 450 451 _, serviceResp := discoverSuit.createCommonService(t, 0) 452 defer discoverSuit.cleanServiceName(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 453 defer discoverSuit.cleanRateLimitRevision(serviceResp.GetName().GetValue(), serviceResp.GetNamespace().GetValue()) 454 455 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 1) 456 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 457 458 t.Run("反复启用禁止限流规则, 正常下发客户端", func(t *testing.T) { 459 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, serviceResp, 10000) 460 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 461 delay := time.NewTimer(time.Second) 462 t.Cleanup(func() { 463 delay.Stop() 464 }) 465 466 check := func(label string, disable bool) { 467 ruleContents := []*apitraffic.Rule{ 468 { 469 Id: utils.NewStringValue(rateLimitResp.GetId().GetValue()), 470 Disable: utils.NewBoolValue(disable), 471 }, 472 } 473 474 t.Logf("start run : %s", label) 475 if resp := discoverSuit.DiscoverServer().EnableRateLimits(discoverSuit.DefaultCtx, ruleContents); !respSuccess(resp) { 476 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 477 } 478 filters := map[string]string{ 479 "id": rateLimitResp.GetId().GetValue(), 480 } 481 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 482 if !respSuccess(resp) { 483 t.Fatalf("error : %s", resp.GetInfo().GetValue()) 484 } 485 assert.Equal(t, 1, len(resp.GetRateLimits())) 486 487 data, _ := json.Marshal(resp.GetRateLimits()) 488 t.Logf("find target ratelimit rule from store : %s", string(data)) 489 490 assert.Equal(t, rateLimitResp.GetId().GetValue(), resp.GetRateLimits()[0].GetId().GetValue()) 491 assert.Equal(t, disable, resp.GetRateLimits()[0].GetDisable().GetValue()) 492 493 time.Sleep(10 * time.Second) 494 495 var ok bool 496 for i := 0; i < 3; i++ { 497 discoverResp := discoverSuit.DiscoverServer().GetRateLimitWithCache(discoverSuit.DefaultCtx, serviceResp) 498 if !respSuccess(discoverResp) { 499 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 500 } 501 502 assert.True(t, len(discoverResp.GetRateLimit().GetRules()) > 0) 503 504 for i := range discoverResp.GetRateLimit().GetRules() { 505 rule := discoverResp.GetRateLimit().GetRules()[i] 506 if rule.GetId().GetValue() == rateLimitResp.GetId().GetValue() { 507 data, _ := json.Marshal(rule) 508 t.Logf("find target ratelimit rule from cache : %s", string(data)) 509 if disable == rule.GetDisable().GetValue() { 510 ok = true 511 break 512 } 513 time.Sleep(time.Second) 514 } 515 } 516 } 517 if !ok { 518 t.Fatalf("%s match ratelimit disable status", label) 519 } else { 520 t.Logf("start run : success : %s %s", rateLimitResp.GetId().GetValue(), resp.GetRateLimits()[0].GetId().GetValue()) 521 } 522 } 523 524 check("禁用限流规则", true) 525 time.Sleep(time.Second) 526 check("启用限流规则", false) 527 time.Sleep(time.Second) 528 check("禁用限流规则", true) 529 time.Sleep(time.Second) 530 check("启用限流规则", false) 531 time.Sleep(time.Second) 532 533 }) 534 } 535 536 /* 537 * @brief 测试查询限流规则 538 */ 539 func TestGetRateLimit(t *testing.T) { 540 541 discoverSuit := &DiscoverTestSuit{} 542 if err := discoverSuit.Initialize(); err != nil { 543 t.Fatal(err) 544 } 545 defer discoverSuit.Destroy() 546 547 serviceNum := 10 548 rateLimitsNum := 30 549 rateLimits := make([]*apitraffic.Rule, rateLimitsNum) 550 serviceName := "" 551 namespaceName := "" 552 for i := 0; i < serviceNum; i++ { 553 serviceName = fmt.Sprintf("ratelimit_service_%d", i) 554 namespaceName = fmt.Sprintf("ratelimit_namespace_%d", i) 555 defer discoverSuit.cleanRateLimitRevision(serviceName, namespaceName) 556 for j := 0; j < rateLimitsNum/serviceNum; j++ { 557 _, rateLimitResp := discoverSuit.createCommonRateLimit(t, &apiservice.Service{ 558 Name: utils.NewStringValue(serviceName), 559 Namespace: utils.NewStringValue(namespaceName), 560 }, j) 561 defer discoverSuit.cleanRateLimit(rateLimitResp.GetId().GetValue()) 562 rateLimits = append(rateLimits, rateLimitResp) 563 } 564 } 565 566 t.Run("查询限流规则,过滤条件为service", func(t *testing.T) { 567 filters := map[string]string{ 568 "service": serviceName, 569 } 570 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 571 if !respSuccess(resp) { 572 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 573 } 574 if resp.GetSize().GetValue() != uint32(rateLimitsNum/serviceNum) { 575 t.Fatalf("expect num is %d, actual num is %d", rateLimitsNum/serviceNum, resp.GetSize().GetValue()) 576 } 577 t.Logf("pass: num is %d", resp.GetSize().GetValue()) 578 }) 579 580 t.Run("查询限流规则,过滤条件为namespace", func(t *testing.T) { 581 filters := map[string]string{ 582 "namespace": namespaceName, 583 } 584 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 585 if !respSuccess(resp) { 586 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 587 } 588 if resp.GetSize().GetValue() != uint32(rateLimitsNum/serviceNum) { 589 t.Fatalf("expect num is %d, actual num is %d", rateLimitsNum/serviceNum, resp.GetSize().GetValue()) 590 } 591 t.Logf("pass: num is %d", resp.GetSize().GetValue()) 592 }) 593 594 t.Run("查询限流规则,过滤条件为不存在的namespace", func(t *testing.T) { 595 filters := map[string]string{ 596 "namespace": "Development", 597 } 598 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 599 if !respSuccess(resp) { 600 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 601 } 602 if resp.GetSize().GetValue() != 0 { 603 t.Fatalf("expect num is 0, actual num is %d", resp.GetSize().GetValue()) 604 } 605 t.Logf("pass: num is %d", resp.GetSize().GetValue()) 606 }) 607 608 t.Run("查询限流规则,过滤条件为namespace和service", func(t *testing.T) { 609 filters := map[string]string{ 610 "service": serviceName, 611 "namespace": namespaceName, 612 } 613 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 614 if !respSuccess(resp) { 615 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 616 } 617 if resp.GetSize().GetValue() != uint32(rateLimitsNum/serviceNum) { 618 t.Fatalf("expect num is %d, actual num is %d", rateLimitsNum/serviceNum, resp.GetSize().GetValue()) 619 } 620 t.Logf("pass: num is %d", resp.GetSize().GetValue()) 621 }) 622 623 t.Run("查询限流规则,过滤条件为offset和limit", func(t *testing.T) { 624 filters := map[string]string{ 625 "offset": "1", 626 "limit": "5", 627 } 628 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 629 if !respSuccess(resp) { 630 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 631 } 632 if resp.GetSize().GetValue() != 5 { 633 t.Fatalf("expect num is 5, actual num is %d", resp.GetSize().GetValue()) 634 } 635 t.Logf("pass: num is %d", resp.GetSize().GetValue()) 636 }) 637 638 t.Run("查询限流规则列表,过滤条件为name", func(t *testing.T) { 639 filters := map[string]string{ 640 "name": "rule_name_0", 641 "brief": "true", 642 } 643 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 644 if !respSuccess(resp) { 645 t.Fatalf("error: %s", resp.GetInfo().GetValue()) 646 } 647 if resp.GetSize().GetValue() != uint32(serviceNum) { 648 t.Fatalf("expect num is %d, actual num is %d", serviceNum, resp.GetSize().GetValue()) 649 } 650 }) 651 652 t.Run("查询限流规则,offset为负数,返回错误", func(t *testing.T) { 653 filters := map[string]string{ 654 "service": serviceName, 655 "namespace": namespaceName, 656 "offset": "-5", 657 } 658 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 659 if !respSuccess(resp) { 660 t.Logf("pass: %s", resp.GetInfo().GetValue()) 661 } else { 662 t.Fatalf("error") 663 } 664 }) 665 666 t.Run("查询限流规则,limit为负数,返回错误", func(t *testing.T) { 667 filters := map[string]string{ 668 "service": serviceName, 669 "namespace": namespaceName, 670 "limit": "-5", 671 } 672 resp := discoverSuit.DiscoverServer().GetRateLimits(discoverSuit.DefaultCtx, filters) 673 if !respSuccess(resp) { 674 t.Logf("pass: %s", resp.GetInfo().GetValue()) 675 } else { 676 t.Fatalf("error") 677 } 678 }) 679 } 680 681 // test对ratelimit字段进行校验 682 func TestCheckRatelimitFieldLen(t *testing.T) { 683 684 discoverSuit := &DiscoverTestSuit{} 685 if err := discoverSuit.Initialize(); err != nil { 686 t.Fatal(err) 687 } 688 defer discoverSuit.Destroy() 689 690 rateLimit := &apitraffic.Rule{ 691 Service: utils.NewStringValue("test"), 692 Namespace: utils.NewStringValue("default"), 693 Labels: map[string]*apimodel.MatchString{}, 694 ServiceToken: utils.NewStringValue("test"), 695 } 696 t.Run("创建限流规则,服务名超长", func(t *testing.T) { 697 str := genSpecialStr(129) 698 oldName := rateLimit.Service 699 rateLimit.Service = utils.NewStringValue(str) 700 resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}) 701 rateLimit.Service = oldName 702 if resp.Code.Value != api.InvalidServiceName { 703 t.Fatalf("%+v", resp) 704 } 705 }) 706 t.Run("创建限流规则,命名空间超长", func(t *testing.T) { 707 str := genSpecialStr(129) 708 oldNamespace := rateLimit.Namespace 709 rateLimit.Namespace = utils.NewStringValue(str) 710 resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}) 711 rateLimit.Namespace = oldNamespace 712 if resp.Code.Value != api.InvalidNamespaceName { 713 t.Fatalf("%+v", resp) 714 } 715 }) 716 t.Run("创建限流规则,名称超长", func(t *testing.T) { 717 str := genSpecialStr(2049) 718 oldName := rateLimit.Name 719 rateLimit.Name = utils.NewStringValue(str) 720 resp := discoverSuit.DiscoverServer().CreateRateLimits(discoverSuit.DefaultCtx, []*apitraffic.Rule{rateLimit}) 721 rateLimit.Name = oldName 722 if resp.Code.Value != api.InvalidRateLimitName { 723 t.Fatalf("%+v", resp) 724 } 725 }) 726 }