|
1 | 1 | import time
|
2 | 2 |
|
| 3 | +import jwt |
3 | 4 | import pytest
|
4 | 5 | import requests
|
5 | 6 | from settings import TEST_DATA
|
|
24 | 25 | rl_vsr_override_src = f"{TEST_DATA}/rate-limit/route-subroute/virtual-server-route-override-subroute.yaml"
|
25 | 26 | rl_vsr_override_vs_spec_src = f"{TEST_DATA}/rate-limit/route-subroute/virtual-server-vsr-spec-override.yaml"
|
26 | 27 | rl_vsr_override_vs_route_src = f"{TEST_DATA}/rate-limit/route-subroute/virtual-server-vsr-route-override.yaml"
|
| 28 | +rl_vsr_jwt_claim_sub_src = f"{TEST_DATA}/rate-limit/route-subroute/virtual-server-route-jwt-claim-sub.yaml" |
| 29 | +rl_pol_jwt_claim_sub_src = f"{TEST_DATA}/rate-limit/policies/rate-limit-jwt-claim-sub.yaml" |
27 | 30 |
|
28 | 31 |
|
29 | 32 | @pytest.mark.policies
|
@@ -416,3 +419,89 @@ def test_rl_policy_scaled_vsr(
|
416 | 419 | and policy_info["status"]["reason"] == "AddedOrUpdated"
|
417 | 420 | and policy_info["status"]["state"] == "Valid"
|
418 | 421 | )
|
| 422 | + |
| 423 | + @pytest.mark.smoke |
| 424 | + @pytest.mark.parametrize("src", [rl_vsr_jwt_claim_sub_src]) |
| 425 | + def test_rl_policy_jwt_claim_sub_vsr( |
| 426 | + self, |
| 427 | + kube_apis, |
| 428 | + ingress_controller_prerequisites, |
| 429 | + crd_ingress_controller, |
| 430 | + v_s_route_app_setup, |
| 431 | + v_s_route_setup, |
| 432 | + test_namespace, |
| 433 | + src, |
| 434 | + ): |
| 435 | + """ |
| 436 | + Test if rate-limiting policy is working with 1 rps using $jwt_claim_sub as the rate limit key in vsr:subroute |
| 437 | + """ |
| 438 | + |
| 439 | + req_url = f"http://{v_s_route_setup.public_endpoint.public_ip}:{v_s_route_setup.public_endpoint.port}" |
| 440 | + print(f"Create rl policy") |
| 441 | + pol_name = create_policy_from_yaml( |
| 442 | + kube_apis.custom_objects, rl_pol_jwt_claim_sub_src, v_s_route_setup.route_m.namespace |
| 443 | + ) |
| 444 | + wait_before_test(1) |
| 445 | + policy_info = read_custom_resource( |
| 446 | + kube_apis.custom_objects, v_s_route_setup.route_m.namespace, "policies", pol_name |
| 447 | + ) |
| 448 | + assert ( |
| 449 | + policy_info["status"] |
| 450 | + and policy_info["status"]["reason"] == "AddedOrUpdated" |
| 451 | + and policy_info["status"]["state"] == "Valid" |
| 452 | + ) |
| 453 | + |
| 454 | + print(f"Patch vsr with policy: {src}") |
| 455 | + patch_v_s_route_from_yaml( |
| 456 | + kube_apis.custom_objects, |
| 457 | + v_s_route_setup.route_m.name, |
| 458 | + src, |
| 459 | + v_s_route_setup.route_m.namespace, |
| 460 | + ) |
| 461 | + wait_before_test(1) |
| 462 | + vsr_info = read_custom_resource( |
| 463 | + kube_apis.custom_objects, |
| 464 | + v_s_route_setup.route_m.namespace, |
| 465 | + "virtualserverroutes", |
| 466 | + v_s_route_setup.route_m.name, |
| 467 | + ) |
| 468 | + assert ( |
| 469 | + vsr_info["status"] |
| 470 | + and vsr_info["status"]["reason"] == "AddedOrUpdated" |
| 471 | + and vsr_info["status"]["state"] == "Valid" |
| 472 | + ) |
| 473 | + |
| 474 | + vs_info = read_custom_resource( |
| 475 | + kube_apis.custom_objects, v_s_route_setup.namespace, "virtualservers", v_s_route_setup.vs_name |
| 476 | + ) |
| 477 | + assert ( |
| 478 | + vs_info["status"] |
| 479 | + and vs_info["status"]["reason"] == "AddedOrUpdated" |
| 480 | + and vs_info["status"]["state"] == "Valid" |
| 481 | + ) |
| 482 | + |
| 483 | + wait_before_test() |
| 484 | + jwt_token = jwt.encode( |
| 485 | + {"sub": "client1"}, |
| 486 | + "nginx", |
| 487 | + algorithm="HS256", |
| 488 | + ) |
| 489 | + occur = [] |
| 490 | + t_end = time.perf_counter() + 1 |
| 491 | + resp = requests.get( |
| 492 | + f"{req_url}{v_s_route_setup.route_m.paths[0]}", |
| 493 | + headers={"host": v_s_route_setup.vs_host, "Authorization": f"Bearer {jwt_token}"}, |
| 494 | + ) |
| 495 | + |
| 496 | + print(resp.status_code) |
| 497 | + assert resp.status_code == 200 |
| 498 | + while time.perf_counter() < t_end: |
| 499 | + resp = requests.get( |
| 500 | + f"{req_url}{v_s_route_setup.route_m.paths[0]}", |
| 501 | + headers={"host": v_s_route_setup.vs_host, "Authorization": f"Bearer {jwt_token}"}, |
| 502 | + ) |
| 503 | + occur.append(resp.status_code) |
| 504 | + delete_policy(kube_apis.custom_objects, pol_name, v_s_route_setup.route_m.namespace) |
| 505 | + self.restore_default_vsr(kube_apis, v_s_route_setup) |
| 506 | + |
| 507 | + assert occur.count(200) <= 1 |
0 commit comments