From 05da8e783df56ca1565856e512e9977f07083268 Mon Sep 17 00:00:00 2001 From: averevki Date: Mon, 17 Feb 2025 16:31:00 +0100 Subject: [PATCH] Add section targeting tests for ratelimit policy Signed-off-by: averevki --- testsuite/kuadrant/policy/rate_limit.py | 13 +++++- .../singlecluster/limitador/route/conftest.py | 20 -------- .../route/test_limit_targeting_two_rules.py | 33 ------------- .../route/test_multiple_same_rule.py | 28 ----------- .../limitador/route/test_route_rule.py | 27 ----------- .../limitador/{route => section}/__init__.py | 0 .../limitador/section/conftest.py | 20 ++++++++ .../limitador/section/test_listener.py | 24 ++++++++++ .../limitador/section/test_multiple_rules.py | 46 +++++++++++++++++++ .../section/test_multiple_same_listener.py | 26 +++++++++++ .../section/test_multiple_same_rule.py | 26 +++++++++++ .../limitador/section/test_route_rule.py | 25 ++++++++++ 12 files changed, 178 insertions(+), 110 deletions(-) delete mode 100644 testsuite/tests/singlecluster/limitador/route/conftest.py delete mode 100644 testsuite/tests/singlecluster/limitador/route/test_limit_targeting_two_rules.py delete mode 100644 testsuite/tests/singlecluster/limitador/route/test_multiple_same_rule.py delete mode 100644 testsuite/tests/singlecluster/limitador/route/test_route_rule.py rename testsuite/tests/singlecluster/limitador/{route => section}/__init__.py (100%) create mode 100644 testsuite/tests/singlecluster/limitador/section/conftest.py create mode 100644 testsuite/tests/singlecluster/limitador/section/test_listener.py create mode 100644 testsuite/tests/singlecluster/limitador/section/test_multiple_rules.py create mode 100644 testsuite/tests/singlecluster/limitador/section/test_multiple_same_listener.py create mode 100644 testsuite/tests/singlecluster/limitador/section/test_multiple_same_rule.py create mode 100644 testsuite/tests/singlecluster/limitador/section/test_route_rule.py diff --git a/testsuite/kuadrant/policy/rate_limit.py b/testsuite/kuadrant/policy/rate_limit.py index a46ee1ea..7332f751 100644 --- a/testsuite/kuadrant/policy/rate_limit.py +++ b/testsuite/kuadrant/policy/rate_limit.py @@ -27,9 +27,16 @@ def __init__(self, *args, **kwargs): self.spec_section = None @classmethod - def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, labels: dict[str, str] = None): + def create_instance( + cls, + cluster: KubernetesClient, + name, + target: Referencable, + section_name: str = None, + labels: dict[str, str] = None, + ): """Creates new instance of RateLimitPolicy""" - model = { + model: dict = { "apiVersion": "kuadrant.io/v1", "kind": "RateLimitPolicy", "metadata": {"name": name, "labels": labels}, @@ -37,6 +44,8 @@ def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, "targetRef": target.reference, }, } + if section_name: + model["spec"]["targetRef"]["sectionName"] = section_name return cls(model, context=cluster.context) diff --git a/testsuite/tests/singlecluster/limitador/route/conftest.py b/testsuite/tests/singlecluster/limitador/route/conftest.py deleted file mode 100644 index eaf09489..00000000 --- a/testsuite/tests/singlecluster/limitador/route/conftest.py +++ /dev/null @@ -1,20 +0,0 @@ -"""Conftest for RLP targeting route tests""" - -import pytest - -from testsuite.gateway import PathMatch, RouteMatch, MatchType - - -@pytest.fixture(scope="module") -def route(route, backend): - """Add two new rules to the route""" - route.remove_all_rules() - route.add_rule( - backend, - RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)), - ) - route.add_rule( - backend, - RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)), - ) - return route diff --git a/testsuite/tests/singlecluster/limitador/route/test_limit_targeting_two_rules.py b/testsuite/tests/singlecluster/limitador/route/test_limit_targeting_two_rules.py deleted file mode 100644 index 35548ff7..00000000 --- a/testsuite/tests/singlecluster/limitador/route/test_limit_targeting_two_rules.py +++ /dev/null @@ -1,33 +0,0 @@ -"""Tests that one RLP limit targeting two rules limits them together""" - -import pytest - -from testsuite.kuadrant.policy import CelPredicate -from testsuite.kuadrant.policy.rate_limit import Limit - - -pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] - - -@pytest.fixture(scope="module") -def rate_limit(rate_limit): - """Add limit to the policy""" - rate_limit.add_limit("test", [Limit(5, "10s")], when=[CelPredicate("request.method == 'GET'")]) - return rate_limit - - -@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561") -def test_limit_targeting_two_rules(client): - """Tests that one RLP limit targeting two rules limits them together""" - responses = client.get_many("/get", 3) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" - - responses = client.get_many("/anything", 2) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" - - assert client.get("/get").status_code == 429 - assert client.get("/anything").status_code == 429 diff --git a/testsuite/tests/singlecluster/limitador/route/test_multiple_same_rule.py b/testsuite/tests/singlecluster/limitador/route/test_multiple_same_rule.py deleted file mode 100644 index 0796e1bb..00000000 --- a/testsuite/tests/singlecluster/limitador/route/test_multiple_same_rule.py +++ /dev/null @@ -1,28 +0,0 @@ -"""Test that multiple limits targeting same rule are correctly applied""" - -import pytest - -from testsuite.kuadrant.policy import CelPredicate -from testsuite.kuadrant.policy.rate_limit import Limit - - -pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] - - -@pytest.fixture(scope="module") -def rate_limit(rate_limit): - """Add limit to the policy""" - when = CelPredicate("request.path == '/get'") - rate_limit.add_limit("test1", [Limit(8, "10s")], when=[when]) - rate_limit.add_limit("test2", [Limit(3, "5s")], when=[when]) - return rate_limit - - -@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561") -def test_two_rules_targeting_one_limit(client): - """Test that one limit ends up shadowing others""" - responses = client.get_many("/get", 3) - assert all( - r.status_code == 200 for r in responses - ), f"Rate Limited resource unexpectedly rejected requests {responses}" - assert client.get("/get").status_code == 429 diff --git a/testsuite/tests/singlecluster/limitador/route/test_route_rule.py b/testsuite/tests/singlecluster/limitador/route/test_route_rule.py deleted file mode 100644 index f199eb10..00000000 --- a/testsuite/tests/singlecluster/limitador/route/test_route_rule.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Tests that the RLP is correctly apply to the route rule""" - -import pytest - -from testsuite.kuadrant.policy import CelPredicate -from testsuite.kuadrant.policy.rate_limit import Limit - -pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] - - -@pytest.fixture(scope="module") -def rate_limit(rate_limit): - """Add limit to the policy""" - rate_limit.add_limit("multiple", [Limit(5, "10s")], when=[CelPredicate("request.path == '/get'")]) - return rate_limit - - -@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561") -def test_rule_match(client): - """Tests that RLP correctly applies to the given HTTPRoute rule""" - responses = client.get_many("/get", 5) - responses.assert_all(status_code=200) - - assert client.get("/get").status_code == 429 - - response = client.get("/anything") - assert response.status_code == 200 diff --git a/testsuite/tests/singlecluster/limitador/route/__init__.py b/testsuite/tests/singlecluster/limitador/section/__init__.py similarity index 100% rename from testsuite/tests/singlecluster/limitador/route/__init__.py rename to testsuite/tests/singlecluster/limitador/section/__init__.py diff --git a/testsuite/tests/singlecluster/limitador/section/conftest.py b/testsuite/tests/singlecluster/limitador/section/conftest.py new file mode 100644 index 00000000..75af36c2 --- /dev/null +++ b/testsuite/tests/singlecluster/limitador/section/conftest.py @@ -0,0 +1,20 @@ +"""Conftest for RLP section_name targeting tests""" + +import pytest + + +@pytest.fixture(scope="module") +def route(route, backend): + """Add two backend rules for different paths to the route""" + route.remove_all_rules() + route.add_backend(backend, "/get") + route.add_backend(backend, "/anything") + return route + + +@pytest.fixture(scope="module", autouse=True) +def commit(request, route, rate_limit): # pylint: disable=unused-argument + """Commits RateLimitPolicy after the HTTPRoute is created""" + request.addfinalizer(rate_limit.delete) + rate_limit.commit() + rate_limit.wait_for_ready() diff --git a/testsuite/tests/singlecluster/limitador/section/test_listener.py b/testsuite/tests/singlecluster/limitador/section/test_listener.py new file mode 100644 index 00000000..7cc9d469 --- /dev/null +++ b/testsuite/tests/singlecluster/limitador/section/test_listener.py @@ -0,0 +1,24 @@ +"""Tests that the RLP is correctly applies to the chosen Gateway Listener""" + +import pytest + +from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy + +pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] + + +@pytest.fixture(scope="module") +def rate_limit(cluster, blame, module_label, gateway): + """Add a RateLimitPolicy targeting the specific Gateway Listener""" + rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, "api", labels={"testRun": module_label}) + rlp.add_limit("basic", [Limit(2, "10s")]) + return rlp + + +def test_limit_match_gateway_listener(client): + """Tests that RLP correctly applies to the specific Gateway Listener""" + responses = client.get_many("/get", 2) + responses.assert_all(status_code=200) + + assert client.get("/get").status_code == 429 + assert client.get("/anything").status_code == 429 diff --git a/testsuite/tests/singlecluster/limitador/section/test_multiple_rules.py b/testsuite/tests/singlecluster/limitador/section/test_multiple_rules.py new file mode 100644 index 00000000..fcf6d01a --- /dev/null +++ b/testsuite/tests/singlecluster/limitador/section/test_multiple_rules.py @@ -0,0 +1,46 @@ +"""Test multiple RLP's targeting different HTTPRoute Rules do not interfere with each other""" + +import pytest + +from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy + + +pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] + + +@pytest.fixture(scope="module") +def rate_limit(cluster, blame, module_label, route): + """Add a RateLimitPolicy targeting the first HTTPRoute Rule""" + rate_limit = RateLimitPolicy.create_instance( + cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label} + ) + rate_limit.add_limit("basic", [Limit(3, "5s")]) + return rate_limit + + +@pytest.fixture(scope="module") +def rate_limit2(cluster, blame, module_label, route): + """Add a RateLimitPolicy targeting the second HTTPRoute Rule""" + rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-2", labels={"testRun": module_label}) + rlp.add_limit("basic", [Limit(2, "5s")]) + return rlp + + +@pytest.fixture(scope="module", autouse=True) +def commit(request, rate_limit, rate_limit2): + """Commit and wait for RateLimitPolicies to be fully enforced""" + for policy in [rate_limit, rate_limit2]: + request.addfinalizer(policy.delete) + policy.commit() + policy.wait_for_ready() + + +def test_multiple_limits_targeting_different_route_rules(client): + """Test targeting separate HTTPRoute Rules with different limits""" + responses = client.get_many("/get", 3) + responses.assert_all(status_code=200) + assert client.get("/get").status_code == 429 + + responses = client.get_many("/anything", 2) + responses.assert_all(status_code=200) + assert client.get("/anything").status_code == 429 diff --git a/testsuite/tests/singlecluster/limitador/section/test_multiple_same_listener.py b/testsuite/tests/singlecluster/limitador/section/test_multiple_same_listener.py new file mode 100644 index 00000000..716212b7 --- /dev/null +++ b/testsuite/tests/singlecluster/limitador/section/test_multiple_same_listener.py @@ -0,0 +1,26 @@ +"""Test that multiple limits targeting same Gateway Listener are correctly applied""" + +import pytest + +from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy + + +pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] + + +@pytest.fixture(scope="module") +def rate_limit(cluster, blame, module_label, gateway): + """Add a RateLimitPolicy targeting the Gateway Listener with two limits""" + rate_limit = RateLimitPolicy.create_instance( + cluster, blame("limit"), gateway, "api", labels={"testRun": module_label} + ) + rate_limit.add_limit("test1", [Limit(8, "10s")]) + rate_limit.add_limit("test2", [Limit(3, "5s")]) + return rate_limit + + +def test_two_limits_targeting_one_gateway_listener(client): + """Test that one limit ends up shadowing others""" + responses = client.get_many("/get", 3) + responses.assert_all(status_code=200) + assert client.get("/get").status_code == 429 diff --git a/testsuite/tests/singlecluster/limitador/section/test_multiple_same_rule.py b/testsuite/tests/singlecluster/limitador/section/test_multiple_same_rule.py new file mode 100644 index 00000000..af904d3a --- /dev/null +++ b/testsuite/tests/singlecluster/limitador/section/test_multiple_same_rule.py @@ -0,0 +1,26 @@ +"""Test that multiple limits targeting same rule are correctly applied""" + +import pytest + +from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy + + +pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] + + +@pytest.fixture(scope="module") +def rate_limit(cluster, blame, module_label, route): + """Add a RateLimitPolicy targeting the first HTTPRoute Rule with two limits""" + rate_limit = RateLimitPolicy.create_instance( + cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label} + ) + rate_limit.add_limit("test1", [Limit(8, "10s")]) + rate_limit.add_limit("test2", [Limit(3, "5s")]) + return rate_limit + + +def test_two_limits_targeting_one_route_rule(client): + """Test that one limit ends up shadowing others""" + responses = client.get_many("/get", 3) + responses.assert_all(status_code=200) + assert client.get("/get").status_code == 429 diff --git a/testsuite/tests/singlecluster/limitador/section/test_route_rule.py b/testsuite/tests/singlecluster/limitador/section/test_route_rule.py new file mode 100644 index 00000000..b53daa6c --- /dev/null +++ b/testsuite/tests/singlecluster/limitador/section/test_route_rule.py @@ -0,0 +1,25 @@ +"""Tests that the RLP is correctly applied to the specific route rule""" + +import pytest + +from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy + +pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] + + +@pytest.fixture(scope="module") +def rate_limit(cluster, blame, module_label, route): + """Add a RateLimitPolicy targeting the first HTTPRoute Rule.""" + rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label}) + rlp.add_limit("basic", [Limit(2, "10s")]) + return rlp + + +def test_limit_match_route_rule(client): + """Tests that RLP correctly applies to the specific HTTPRoute Rule""" + responses = client.get_many("/get", 2) + responses.assert_all(status_code=200) + assert client.get("/get").status_code == 429 + + response = client.get("/anything") + assert response.status_code == 200