|
| 1 | +"""Test gateway level default merging with and being partially overriden by another policy.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from testsuite.kuadrant.policy import CelPredicate, has_condition |
| 6 | +from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit, Strategy |
| 7 | + |
| 8 | +pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="module") |
| 12 | +def rate_limit(rate_limit): |
| 13 | + """Create a RateLimitPolicy with a basic limit with same target as one default.""" |
| 14 | + when = CelPredicate("request.path == '/get'") |
| 15 | + rate_limit.add_limit("route_limit", [Limit(5, "5s")], when=[when]) |
| 16 | + return rate_limit |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(scope="module") |
| 20 | +def global_rate_limit(cluster, blame, module_label, gateway): |
| 21 | + """Create a RateLimitPolicy with default policies and a merge strategy.""" |
| 22 | + global_rate_limit = RateLimitPolicy.create_instance( |
| 23 | + cluster, blame("limit"), gateway, labels={"testRun": module_label} |
| 24 | + ) |
| 25 | + gateway_when = CelPredicate("request.path == '/anything'") |
| 26 | + global_rate_limit.defaults.add_limit("gateway_limit", [Limit(3, "5s")], when=[gateway_when]) |
| 27 | + route_when = CelPredicate("request.path == '/get'") |
| 28 | + global_rate_limit.defaults.add_limit("route_limit", [Limit(10, "5s")], when=[route_when]) |
| 29 | + global_rate_limit.defaults.strategy(Strategy.MERGE) |
| 30 | + return global_rate_limit |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture(scope="module", autouse=True) |
| 34 | +def commit(request, route, rate_limit, global_rate_limit): # pylint: disable=unused-argument |
| 35 | + """Commits RateLimitPolicy after the HTTPRoute is created""" |
| 36 | + for policy in [global_rate_limit, rate_limit]: # Forcing order of creation. |
| 37 | + request.addfinalizer(policy.delete) |
| 38 | + policy.commit() |
| 39 | + policy.wait_for_ready() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("rate_limit", ["gateway", "route"], indirect=True) |
| 43 | +def test_gateway_default_merge(client, global_rate_limit): |
| 44 | + """Test Gateway default policy being partially overriden when another policy with the same target is created.""" |
| 45 | + assert global_rate_limit.wait_until( |
| 46 | + has_condition("Enforced", "True", "Enforced", "RateLimitPolicy has been partially enforced") |
| 47 | + ) |
| 48 | + |
| 49 | + get = client.get_many("/get", 5) |
| 50 | + get.assert_all(status_code=200) |
| 51 | + assert client.get("/get").status_code == 429 |
| 52 | + |
| 53 | + anything = client.get_many("/anything", 3) |
| 54 | + anything.assert_all(status_code=200) |
| 55 | + assert client.get("/anything").status_code == 429 |
0 commit comments