Skip to content

Commit

Permalink
Merge pull request Kuadrant#319 from jsmolar/rate_limit_auth
Browse files Browse the repository at this point in the history
Add RLP route rule test
  • Loading branch information
pehala authored Jan 15, 2024
2 parents 71a6548 + 3b39550 commit e98d70a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
17 changes: 11 additions & 6 deletions testsuite/tests/kuadrant/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ def authorization(authorino, kuadrant, oidc_provider, route, authorization_name,
return None


@pytest.fixture(scope="module", params=["route", "gateway"])
def rate_limit(kuadrant, openshift, blame, request, module_label):
"""Rate limit"""
@pytest.fixture(scope="module")
def rate_limit(kuadrant, openshift, blame, request, module_label, route, gateway):
"""
Rate limit object.
Request is used for indirect parametrization, with two possible parameters:
1. `route` (default)
2. `gateway`
"""
target_ref = request.getfixturevalue(getattr(request, "param", "route"))

if kuadrant:
return RateLimitPolicy.create_instance(
openshift, blame("limit"), request.getfixturevalue(request.param), labels={"testRun": module_label}
)
return RateLimitPolicy.create_instance(openshift, blame("limit"), target_ref, labels={"testRun": module_label})
return None


Expand Down
1 change: 1 addition & 0 deletions testsuite/tests/kuadrant/limitador/test_basic_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def rate_limit(rate_limit, limit):
return rate_limit


@pytest.mark.parametrize("rate_limit", ["route", "gateway"], indirect=True)
def test_limit(client, limit):
"""Tests that simple limit is applied successfully"""
responses = client.get_many("/get", limit.limit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def rate_limit(rate_limit):
return rate_limit


@pytest.mark.parametrize("rate_limit", ["route", "gateway"], indirect=True)
def test_multiple_iterations(client):
"""Tests that simple limit is applied successfully and works for multiple iterations"""
for _ in range(10):
Expand Down
39 changes: 39 additions & 0 deletions testsuite/tests/kuadrant/limitador/test_route_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Tests that the RLP is correctly apply to the route rule"""
import pytest

from testsuite.gateway import RouteMatch, PathMatch, MatchType
from testsuite.policy.rate_limit_policy import Limit, RouteSelector


@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)))
rate_limit.add_limit("multiple", [Limit(5, 10)], route_selectors=[selector])
return rate_limit


def test_rule(client):
"""Tests that RLP correctly applies to the given HTTPRoute rule"""
responses = client.get_many("/get", 5)
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
response = client.get("/anything")
assert response.status_code == 200
1 change: 1 addition & 0 deletions testsuite/tests/kuadrant/test_rate_limit_authz.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def auth2(rhsso, blame):
return HttpxOidcClientAuth.from_user(rhsso.get_token, user=user)


@pytest.mark.parametrize("rate_limit", ["route", "gateway"], indirect=True)
def test_authz_limit(client, auth, auth2):
"""Tests that rate limit is applied for two users independently"""
responses = client.get_many("/get", 5, auth=auth)
Expand Down

0 comments on commit e98d70a

Please sign in to comment.