From 6962e2ee9afb87bcfd0261e7b30c8e21e3ccf028 Mon Sep 17 00:00:00 2001 From: Martin Hesko Date: Fri, 29 Nov 2024 12:46:15 +0100 Subject: [PATCH] merge strategy tests for D&O Signed-off-by: Martin Hesko --- .../singlecluster/defaults/merge/__init__.py | 0 .../singlecluster/defaults/merge/conftest.py | 20 ++++++++ .../merge/test_gateway_default_merge.py | 51 +++++++++++++++++++ .../singlecluster/overrides/merge/__init__.py | 0 .../singlecluster/overrides/merge/conftest.py | 20 ++++++++ .../merge/test_gateway_override_merge.py | 51 +++++++++++++++++++ 6 files changed, 142 insertions(+) create mode 100644 testsuite/tests/singlecluster/defaults/merge/__init__.py create mode 100644 testsuite/tests/singlecluster/defaults/merge/conftest.py create mode 100644 testsuite/tests/singlecluster/defaults/merge/test_gateway_default_merge.py create mode 100644 testsuite/tests/singlecluster/overrides/merge/__init__.py create mode 100644 testsuite/tests/singlecluster/overrides/merge/conftest.py create mode 100644 testsuite/tests/singlecluster/overrides/merge/test_gateway_override_merge.py diff --git a/testsuite/tests/singlecluster/defaults/merge/__init__.py b/testsuite/tests/singlecluster/defaults/merge/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/testsuite/tests/singlecluster/defaults/merge/conftest.py b/testsuite/tests/singlecluster/defaults/merge/conftest.py new file mode 100644 index 00000000..153964bc --- /dev/null +++ b/testsuite/tests/singlecluster/defaults/merge/conftest.py @@ -0,0 +1,20 @@ +"""Conftest for merge strategy tests""" + +import pytest + +from testsuite.gateway import RouteMatch, PathMatch, 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/defaults/merge/test_gateway_default_merge.py b/testsuite/tests/singlecluster/defaults/merge/test_gateway_default_merge.py new file mode 100644 index 00000000..b363a5f7 --- /dev/null +++ b/testsuite/tests/singlecluster/defaults/merge/test_gateway_default_merge.py @@ -0,0 +1,51 @@ +"""Test gateway level default merging with and being patrially overriden by another policy.""" + +import pytest + +from testsuite.kuadrant.policy import CelPredicate +from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit, Strategy + +pytestmark = [pytest.mark.kuadrant_only] + + +@pytest.fixture(scope="module") +def rate_limit(rate_limit): + """Create a RateLimitPolicy with a basic limit with same target as one default.""" + when = CelPredicate("request.path == '/get'") + rate_limit.add_limit("route_limit", [Limit(3, "5s")], when=[when]) + return rate_limit + + +@pytest.fixture(scope="module") +def global_rate_limit(cluster, blame, module_label, gateway): + """Create a RateLimitPolicy with default policies and a merge strategy.""" + global_rate_limit = RateLimitPolicy.create_instance( + cluster, blame("limit"), gateway, labels={"testRun": module_label} + ) + gateway_when = CelPredicate("request.path == '/anything'") + global_rate_limit.defaults.add_limit("gateway_limit", [Limit(3, "5s")], when=[gateway_when]) + route_when = CelPredicate("request.path == '/get'") + global_rate_limit.defaults.add_limit("route_limit", [Limit(10, "5s")], when=[route_when]) + global_rate_limit.defaults.strategy(Strategy.MERGE) + return global_rate_limit + + +@pytest.fixture(scope="module", autouse=True) +def commit(request, route, rate_limit, global_rate_limit): # pylint: disable=unused-argument + """Commits RateLimitPolicy after the HTTPRoute is created""" + for policy in [global_rate_limit, rate_limit]: # Forcing order of creation. + request.addfinalizer(policy.delete) + policy.commit() + policy.wait_for_ready() + + +@pytest.mark.parametrize("rate_limit", ["gateway", "route"], indirect=True) +def test_gateway_default_merge(client): + """Test Gateway default policy being partially overriden when another policy with the same target is created.""" + get = client.get_many("/get", 3) + get.assert_all(status_code=200) + assert client.get("/get").status_code == 429 + + anything = client.get_many("/anything", 3) + anything.assert_all(status_code=200) + assert client.get("/anything").status_code == 429 diff --git a/testsuite/tests/singlecluster/overrides/merge/__init__.py b/testsuite/tests/singlecluster/overrides/merge/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/testsuite/tests/singlecluster/overrides/merge/conftest.py b/testsuite/tests/singlecluster/overrides/merge/conftest.py new file mode 100644 index 00000000..153964bc --- /dev/null +++ b/testsuite/tests/singlecluster/overrides/merge/conftest.py @@ -0,0 +1,20 @@ +"""Conftest for merge strategy tests""" + +import pytest + +from testsuite.gateway import RouteMatch, PathMatch, 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/overrides/merge/test_gateway_override_merge.py b/testsuite/tests/singlecluster/overrides/merge/test_gateway_override_merge.py new file mode 100644 index 00000000..b0aa8bb4 --- /dev/null +++ b/testsuite/tests/singlecluster/overrides/merge/test_gateway_override_merge.py @@ -0,0 +1,51 @@ +"""Test override overriding another policy aimed at the same Gateway Listener.""" + +import pytest + +from testsuite.kuadrant.policy import CelPredicate +from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit, Strategy + +pytestmark = [pytest.mark.kuadrant_only] + + +@pytest.fixture(scope="function") +def override_rate_limit(cluster, blame, module_label, gateway): + """Add a RateLimitPolicy with a merge strategy override targeting a specific endpoint.""" + override_rate_limit = RateLimitPolicy.create_instance( + cluster, blame("limit"), gateway, labels={"testRun": module_label} + ) + when = CelPredicate("request.path == '/get'") + override_rate_limit.overrides.add_limit("route_limit", [Limit(3, "5s")], when=[when]) + override_rate_limit.overrides.strategy(Strategy.MERGE) + return override_rate_limit + + +@pytest.fixture(scope="module") +def rate_limit(rate_limit): + """Add limits targeted at specific endpoints to the RateLimitPolicy.""" + gateway_when = CelPredicate("request.path == '/anything'") + rate_limit.add_limit("gateway_limit", [Limit(3, "5s")], when=[gateway_when]) + route_when = CelPredicate("request.path == '/get'") + rate_limit.add_limit("route_limit", [Limit(10, "5s")], when=[route_when]) + return rate_limit + + +@pytest.fixture(scope="function", autouse=True) +def commit(request, route, rate_limit, override_rate_limit): # pylint: disable=unused-argument + """Commits RateLimitPolicy after the HTTPRoute is created""" + for policy in [override_rate_limit, rate_limit]: # Forcing order of creation. + request.addfinalizer(policy.delete) + policy.commit() + policy.wait_for_accepted() + + +@pytest.mark.parametrize("rate_limit", ["gateway", "route"], indirect=True) +def test_gateway_override_merge(client): + """Test RateLimitPolicy with an override and merge strategy overriding only a part of a new policy.""" + get = client.get_many("/get", 3) + get.assert_all(status_code=200) + assert client.get("/get").status_code == 429 + + anything = client.get_many("/anything", 3) + anything.assert_all(status_code=200) + assert client.get("/anything").status_code == 429