Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section targeting tests for ratelimit policy #634

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions testsuite/kuadrant/policy/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,25 @@ def __init__(self, *args, **kwargs):
self.spec_section = None

@classmethod
def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, labels: dict[str, str] = None):
def create_instance(
cls,
cluster: KubernetesClient,
name,
target: Referencable,
section_name: str = None,
labels: dict[str, str] = None,
):
"""Creates new instance of RateLimitPolicy"""
model = {
model: dict = {
"apiVersion": "kuadrant.io/v1",
"kind": "RateLimitPolicy",
"metadata": {"name": name, "labels": labels},
"spec": {
"targetRef": target.reference,
},
}
if section_name:
model["spec"]["targetRef"]["sectionName"] = section_name

return cls(model, context=cluster.context)

Expand Down
20 changes: 0 additions & 20 deletions testsuite/tests/singlecluster/limitador/route/conftest.py

This file was deleted.

This file was deleted.

This file was deleted.

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

This file was deleted.

20 changes: 20 additions & 0 deletions testsuite/tests/singlecluster/limitador/section/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Conftest for RLP section_name targeting tests"""

import pytest


@pytest.fixture(scope="module")
def route(route, backend):
"""Add two backend rules for different paths to the route"""
route.remove_all_rules()
route.add_backend(backend, "/get")
route.add_backend(backend, "/anything")
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 chosen 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 specific Gateway Listener"""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, "api", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(2, "10s")])
return rlp


def test_limit_match_gateway_listener(client):
"""Tests that RLP correctly applies to the specific Gateway Listener"""
responses = client.get_many("/get", 2)
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,46 @@
"""Test multiple RLP's targeting different HTTPRoute Rules 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 HTTPRoute Rule"""
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 HTTPRoute Rule"""
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_different_route_rules(client):
"""Test targeting separate HTTPRoute Rules with different limits"""
responses = client.get_many("/get", 3)
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429

responses = client.get_many("/anything", 2)
responses.assert_all(status_code=200)
assert client.get("/anything").status_code == 429
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""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 two 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_gateway_listener(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test that multiple limits targeting same rule 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, route):
"""Add a RateLimitPolicy targeting the first HTTPRoute Rule 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


def test_two_limits_targeting_one_route_rule(client):
"""Test that one limit ends up shadowing others"""
responses = client.get_many("/get", 3)
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429
25 changes: 25 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,25 @@
"""Tests that the RLP is correctly applied to the specific 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 HTTPRoute Rule."""
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label})
rlp.add_limit("basic", [Limit(2, "10s")])
return rlp


def test_limit_match_route_rule(client):
"""Tests that RLP correctly applies to the specific HTTPRoute Rule"""
responses = client.get_many("/get", 2)
responses.assert_all(status_code=200)
assert client.get("/get").status_code == 429

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