From 8015423366a07cde8d07bd81f6305c2fa9186d70 Mon Sep 17 00:00:00 2001 From: Jakub Smolar Date: Wed, 7 Feb 2024 16:35:12 +0100 Subject: [PATCH 1/2] Added tests for RLP from RFC (5,6) --- .../kuadrant/limitador/route/__init__.py | 0 .../kuadrant/limitador/route/conftest.py | 20 +++++++++++ .../route/test_limit_targeting_two_rules.py | 33 +++++++++++++++++++ .../route/test_multiple_same_rule.py | 26 +++++++++++++++ .../limitador/{ => route}/test_route_rule.py | 17 +--------- 5 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 testsuite/tests/kuadrant/limitador/route/__init__.py create mode 100644 testsuite/tests/kuadrant/limitador/route/conftest.py create mode 100644 testsuite/tests/kuadrant/limitador/route/test_limit_targeting_two_rules.py create mode 100644 testsuite/tests/kuadrant/limitador/route/test_multiple_same_rule.py rename testsuite/tests/kuadrant/limitador/{ => route}/test_route_rule.py (66%) diff --git a/testsuite/tests/kuadrant/limitador/route/__init__.py b/testsuite/tests/kuadrant/limitador/route/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/testsuite/tests/kuadrant/limitador/route/conftest.py b/testsuite/tests/kuadrant/limitador/route/conftest.py new file mode 100644 index 00000000..98902700 --- /dev/null +++ b/testsuite/tests/kuadrant/limitador/route/conftest.py @@ -0,0 +1,20 @@ +"""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/kuadrant/limitador/route/test_limit_targeting_two_rules.py b/testsuite/tests/kuadrant/limitador/route/test_limit_targeting_two_rules.py new file mode 100644 index 00000000..6ad2ce8a --- /dev/null +++ b/testsuite/tests/kuadrant/limitador/route/test_limit_targeting_two_rules.py @@ -0,0 +1,33 @@ +"""Tests that one RLP limit targeting two rules limits them together""" + +import pytest + +from testsuite.gateway import RouteMatch, PathMatch, MatchType +from testsuite.policy.rate_limit_policy import RouteSelector, Limit + + +@pytest.fixture(scope="module") +def rate_limit(rate_limit): + """Add limit to the policy""" + selector = RouteSelector( + RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)), + RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)), + ) + rate_limit.add_limit("test", [Limit(5, 10)], route_selectors=[selector]) + return rate_limit + + +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/kuadrant/limitador/route/test_multiple_same_rule.py b/testsuite/tests/kuadrant/limitador/route/test_multiple_same_rule.py new file mode 100644 index 00000000..dd798fab --- /dev/null +++ b/testsuite/tests/kuadrant/limitador/route/test_multiple_same_rule.py @@ -0,0 +1,26 @@ +"""Test that multiple limits targeting same rule are correctly applied""" + +import pytest + +from testsuite.gateway import RouteMatch, PathMatch, MatchType +from testsuite.policy.rate_limit_policy import RouteSelector, Limit + + +@pytest.fixture(scope="module") +def rate_limit(rate_limit): + """Add limit to the policy""" + selector = RouteSelector( + RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)), + ) + rate_limit.add_limit("test1", [Limit(8, 10)], route_selectors=[selector]) + rate_limit.add_limit("test2", [Limit(3, 5)], route_selectors=[selector]) + return rate_limit + + +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/kuadrant/limitador/test_route_rule.py b/testsuite/tests/kuadrant/limitador/route/test_route_rule.py similarity index 66% rename from testsuite/tests/kuadrant/limitador/test_route_rule.py rename to testsuite/tests/kuadrant/limitador/route/test_route_rule.py index cd7cfaeb..e0fb44e0 100644 --- a/testsuite/tests/kuadrant/limitador/test_route_rule.py +++ b/testsuite/tests/kuadrant/limitador/route/test_route_rule.py @@ -8,25 +8,10 @@ pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador] -@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 - - @pytest.fixture(scope="module") def rate_limit(rate_limit): """Add limit to the policy""" - selector = RouteSelector(RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX))) + selector = RouteSelector(RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX))) rate_limit.add_limit("multiple", [Limit(5, 10)], route_selectors=[selector]) return rate_limit From 22557992cdc5737ae4f72ece47a29579c1e0adfe Mon Sep 17 00:00:00 2001 From: Jakub Smolar Date: Thu, 8 Feb 2024 12:09:10 +0100 Subject: [PATCH 2/2] Add tests for RLP and rule mismatch --- .../tests/kuadrant/limitador/route/test_route_rule.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/testsuite/tests/kuadrant/limitador/route/test_route_rule.py b/testsuite/tests/kuadrant/limitador/route/test_route_rule.py index e0fb44e0..51bd2d0a 100644 --- a/testsuite/tests/kuadrant/limitador/route/test_route_rule.py +++ b/testsuite/tests/kuadrant/limitador/route/test_route_rule.py @@ -11,12 +11,15 @@ @pytest.fixture(scope="module") def rate_limit(rate_limit): """Add limit to the policy""" - selector = RouteSelector(RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX))) + selector = RouteSelector( + RouteMatch(path=PathMatch(value="/get", type=MatchType.PATH_PREFIX)), + RouteMatch(path=PathMatch(value="/anything/test", type=MatchType.PATH_PREFIX)), + ) rate_limit.add_limit("multiple", [Limit(5, 10)], route_selectors=[selector]) return rate_limit -def test_rule(client): +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)