diff --git a/testsuite/tests/singlecluster/defaults/merge/same_target/__init__.py b/testsuite/tests/singlecluster/defaults/merge/same_target/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/testsuite/tests/singlecluster/defaults/merge/same_target/conftest.py b/testsuite/tests/singlecluster/defaults/merge/same_target/conftest.py new file mode 100644 index 00000000..0042e36e --- /dev/null +++ b/testsuite/tests/singlecluster/defaults/merge/same_target/conftest.py @@ -0,0 +1,51 @@ +import pytest + +from testsuite.gateway import MatchType, PathMatch, RouteMatch +from testsuite.kuadrant.policy import CelPredicate, Strategy +from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy + +LIMIT = Limit(8, "10s") +MERGE_LIMIT = Limit(6, "10s") +NO_LIMIT = Limit(10, "10s") + + +@pytest.fixture(scope="module", autouse=True) +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(cluster, blame, module_label, route): + """Add a RateLimitPolicy targeting the first HTTPRouteRule.""" + rate_limit = RateLimitPolicy.create_instance(cluster, blame("sp"), route, labels={"testRun": module_label}) + basic_when = [ + CelPredicate("request.path == '/get'"), + ] + rate_limit.add_limit("basic", [LIMIT], when=basic_when) + return rate_limit + + +@pytest.fixture(scope="module") +def default_merge_rate_limit(cluster, blame, module_label, route): + """Add a RateLimitPolicy targeting the first HTTPRouteRule.""" + policy = RateLimitPolicy.create_instance(cluster, blame("dmp"), route, labels={"testRun": module_label}) + basic_when = [ + CelPredicate("request.path == '/get'"), + ] + merge_when = [ + CelPredicate("request.path == '/anything'"), + ] + policy.defaults.add_limit("basic", [MERGE_LIMIT], when=basic_when) + policy.defaults.add_limit("merge", [MERGE_LIMIT], when=merge_when) + policy.strategy(Strategy.MERGE) + return policy diff --git a/testsuite/tests/singlecluster/defaults/merge/same_target/test_ab_strategy.py b/testsuite/tests/singlecluster/defaults/merge/same_target/test_ab_strategy.py new file mode 100644 index 00000000..a66e9e47 --- /dev/null +++ b/testsuite/tests/singlecluster/defaults/merge/same_target/test_ab_strategy.py @@ -0,0 +1,28 @@ +"""Test defaults policy aimed at the same resoure uses oldested policy.""" + +import pytest + +from .conftest import MERGE_LIMIT + +pytestmark = [pytest.mark.kuadrant_only] + + +@pytest.fixture(scope="module", autouse=True) +def commit(request, route, rate_limit, default_merge_rate_limit): # pylint: disable=unused-argument + """Commits RateLimitPolicy after the HTTPRoute is created""" + for policy in [rate_limit, default_merge_rate_limit]: + request.addfinalizer(policy.delete) + policy.commit() + policy.wait_for_accepted() + + +@pytest.mark.parametrize("rate_limit", ["gateway", "route"], indirect=True) +def test_multiple_policies_merge_default_ab(client): + """Test RateLimitPolicy with merge defaults being ingored due to age""" + responses = client.get_many("/get", MERGE_LIMIT.limit) + responses.assert_all(200) + assert client.get("/get").status_code == 429 + + responses = client.get_many("/anything", MERGE_LIMIT.limit) + responses.assert_all(200) + assert client.get("/anything").status_code == 429 diff --git a/testsuite/tests/singlecluster/defaults/merge/same_target/test_ba_startegy.py b/testsuite/tests/singlecluster/defaults/merge/same_target/test_ba_startegy.py new file mode 100644 index 00000000..a451a38a --- /dev/null +++ b/testsuite/tests/singlecluster/defaults/merge/same_target/test_ba_startegy.py @@ -0,0 +1,28 @@ +"""Test defaults policy aimed at the same resoure uses oldested policy.""" + +import pytest + +from .conftest import LIMIT, MERGE_LIMIT + +pytestmark = [pytest.mark.kuadrant_only] + + +@pytest.fixture(scope="module", autouse=True) +def commit(request, route, rate_limit, default_merge_rate_limit): # pylint: disable=unused-argument + """Commits RateLimitPolicy after the HTTPRoute is created""" + for policy in [default_merge_rate_limit, rate_limit]: + request.addfinalizer(policy.delete) + policy.commit() + policy.wait_for_accepted() + + +@pytest.mark.parametrize("rate_limit", ["gateway", "route"], indirect=True) +def test_multiple_policies_merge_default_ba(client): + """Test RateLimitPolicy with merge defaults being enforced due to age""" + responses = client.get_many("/get", LIMIT.limit) + responses.assert_all(200) + assert client.get("/get").status_code == 429 + + responses = client.get_many("/anything", MERGE_LIMIT.limit) + responses.assert_all(200) + assert client.get("/anything").status_code == 429