Skip to content

Commit

Permalink
merge strategy tests for D&O
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Hesko <[email protected]>
  • Loading branch information
martinhesko committed Nov 29, 2024
1 parent dcf8668 commit 6962e2e
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 0 deletions.
Empty file.
20 changes: 20 additions & 0 deletions testsuite/tests/singlecluster/defaults/merge/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Empty file.
20 changes: 20 additions & 0 deletions testsuite/tests/singlecluster/overrides/merge/conftest.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6962e2e

Please sign in to comment.