Skip to content

Commit

Permalink
Updated RouteSelector tests to use SectionNames
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Hesko <[email protected]>
  • Loading branch information
martinhesko committed Nov 21, 2024
1 parent 489cc18 commit 1eb61c1
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 70 deletions.

This file was deleted.

27 changes: 0 additions & 27 deletions testsuite/tests/singlecluster/limitador/route/test_route_rule.py

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Conftest for RLP targeting route tests """
"""Conftest for RLP section_name targeting tests"""

import pytest

Expand All @@ -18,3 +18,11 @@ def route(route, backend):
RouteMatch(path=PathMatch(value="/anything", type=MatchType.PATH_PREFIX)),
)
return route


@pytest.fixture(scope="module", autouse=True)
def commit(request, route, rate_limit): # pylint: disable=unused-argument
"""Commits RateLimitPolicy after the HTTPRoute is created"""
request.addfinalizer(rate_limit.delete)
rate_limit.commit()
rate_limit.wait_for_ready()
24 changes: 24 additions & 0 deletions testsuite/tests/singlecluster/limitador/section/test_listener.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests that the RLP is correctly applies to the Gateway Listener."""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, gateway):
"""Add a RateLimitPolicy targeting the Gateway Listener."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, "api", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(5, "10s")])
return rlp


def test_listener_match(client):
"""Tests that RLP correctly applies to the given Gateway Listener"""
responses = client.get_many("/get", 5)
responses.assert_all(status_code=200)

assert client.get("/get").status_code == 429
assert client.get("/anything").status_code == 429
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Test multiple RLP's targeting different HTTPRouteRules do not interfere with each other."""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@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("limit"), route, "rule-1", labels={"testRun": module_label}
)
rate_limit.add_limit("basic", [Limit(3, "5s")])
return rate_limit


@pytest.fixture(scope="module")
def rate_limit2(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the second HTTPRouteRule."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-2", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(2, "5s")])
return rlp


@pytest.fixture(scope="module", autouse=True)
def commit(request, rate_limit, rate_limit2):
"""Commit and wait for RateLimitPolicies to be fully enforced"""
for policy in [rate_limit, rate_limit2]:
request.addfinalizer(policy.delete)
policy.commit()
policy.wait_for_ready()


def test_multiple_limits_targeting_rules(client):
"""Test targeting separate HTTPRouteRules with different limits"""
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Test that multiple limits targeting same Gateway Listener are correctly applied"""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, gateway):
"""Add a RateLimitPolicy targeting the Gateway Listener with multiple limits."""
rate_limit = RateLimitPolicy.create_instance(
cluster, blame("limit"), gateway, "api", labels={"testRun": module_label}
)
rate_limit.add_limit("test1", [Limit(8, "10s")])
rate_limit.add_limit("test2", [Limit(3, "5s")])
return rate_limit


def test_two_limits_targeting_one_listener(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
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

import pytest

from testsuite.kuadrant.policy import CelPredicate
from testsuite.kuadrant.policy.rate_limit import Limit
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy


pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(rate_limit):
"""Add limit to the policy"""
when = CelPredicate("request.path == '/get'")
rate_limit.add_limit("test1", [Limit(8, "10s")], when=[when])
rate_limit.add_limit("test2", [Limit(3, "5s")], when=[when])
def rate_limit(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the first HTTPRouteRule with two limits."""
rate_limit = RateLimitPolicy.create_instance(
cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label}
)
rate_limit.add_limit("test1", [Limit(8, "10s")])
rate_limit.add_limit("test2", [Limit(3, "5s")])
return rate_limit


@pytest.mark.issue("https://github.com/Kuadrant/testsuite/issues/561")
def test_two_rules_targeting_one_limit(client):
def test_two_limits_targeting_one_rule(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
assert all(
Expand Down
26 changes: 26 additions & 0 deletions testsuite/tests/singlecluster/limitador/section/test_route_rule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Tests that the RLP is correctly applied to the route rule"""

import pytest

from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy

pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]


@pytest.fixture(scope="module")
def rate_limit(cluster, blame, module_label, route):
"""Add a RateLimitPolicy targeting the first HTTPRouteRule."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(5, "10s")])
return rlp


def test_rule_match(client):
"""Tests that RLP correctly applies to the given HTTPRouteRule"""
responses = client.get_many("/get", 5)
responses.assert_all(status_code=200)

assert client.get("/get").status_code == 429

response = client.get("/anything")
assert response.status_code == 200

0 comments on commit 1eb61c1

Please sign in to comment.