Skip to content

Commit 05da8e7

Browse files
committed
Add section targeting tests for ratelimit policy
Signed-off-by: averevki <[email protected]>
1 parent 8713552 commit 05da8e7

12 files changed

+178
-110
lines changed

testsuite/kuadrant/policy/rate_limit.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,25 @@ def __init__(self, *args, **kwargs):
2727
self.spec_section = None
2828

2929
@classmethod
30-
def create_instance(cls, cluster: KubernetesClient, name, target: Referencable, labels: dict[str, str] = None):
30+
def create_instance(
31+
cls,
32+
cluster: KubernetesClient,
33+
name,
34+
target: Referencable,
35+
section_name: str = None,
36+
labels: dict[str, str] = None,
37+
):
3138
"""Creates new instance of RateLimitPolicy"""
32-
model = {
39+
model: dict = {
3340
"apiVersion": "kuadrant.io/v1",
3441
"kind": "RateLimitPolicy",
3542
"metadata": {"name": name, "labels": labels},
3643
"spec": {
3744
"targetRef": target.reference,
3845
},
3946
}
47+
if section_name:
48+
model["spec"]["targetRef"]["sectionName"] = section_name
4049

4150
return cls(model, context=cluster.context)
4251

testsuite/tests/singlecluster/limitador/route/conftest.py

-20
This file was deleted.

testsuite/tests/singlecluster/limitador/route/test_limit_targeting_two_rules.py

-33
This file was deleted.

testsuite/tests/singlecluster/limitador/route/test_multiple_same_rule.py

-28
This file was deleted.

testsuite/tests/singlecluster/limitador/route/test_route_rule.py

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""Conftest for RLP section_name targeting tests"""
2+
3+
import pytest
4+
5+
6+
@pytest.fixture(scope="module")
7+
def route(route, backend):
8+
"""Add two backend rules for different paths to the route"""
9+
route.remove_all_rules()
10+
route.add_backend(backend, "/get")
11+
route.add_backend(backend, "/anything")
12+
return route
13+
14+
15+
@pytest.fixture(scope="module", autouse=True)
16+
def commit(request, route, rate_limit): # pylint: disable=unused-argument
17+
"""Commits RateLimitPolicy after the HTTPRoute is created"""
18+
request.addfinalizer(rate_limit.delete)
19+
rate_limit.commit()
20+
rate_limit.wait_for_ready()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Tests that the RLP is correctly applies to the chosen Gateway Listener"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
6+
7+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
8+
9+
10+
@pytest.fixture(scope="module")
11+
def rate_limit(cluster, blame, module_label, gateway):
12+
"""Add a RateLimitPolicy targeting the specific Gateway Listener"""
13+
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, "api", labels={"testRun": module_label})
14+
rlp.add_limit("basic", [Limit(2, "10s")])
15+
return rlp
16+
17+
18+
def test_limit_match_gateway_listener(client):
19+
"""Tests that RLP correctly applies to the specific Gateway Listener"""
20+
responses = client.get_many("/get", 2)
21+
responses.assert_all(status_code=200)
22+
23+
assert client.get("/get").status_code == 429
24+
assert client.get("/anything").status_code == 429
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Test multiple RLP's targeting different HTTPRoute Rules do not interfere with each other"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
6+
7+
8+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
9+
10+
11+
@pytest.fixture(scope="module")
12+
def rate_limit(cluster, blame, module_label, route):
13+
"""Add a RateLimitPolicy targeting the first HTTPRoute Rule"""
14+
rate_limit = RateLimitPolicy.create_instance(
15+
cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label}
16+
)
17+
rate_limit.add_limit("basic", [Limit(3, "5s")])
18+
return rate_limit
19+
20+
21+
@pytest.fixture(scope="module")
22+
def rate_limit2(cluster, blame, module_label, route):
23+
"""Add a RateLimitPolicy targeting the second HTTPRoute Rule"""
24+
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-2", labels={"testRun": module_label})
25+
rlp.add_limit("basic", [Limit(2, "5s")])
26+
return rlp
27+
28+
29+
@pytest.fixture(scope="module", autouse=True)
30+
def commit(request, rate_limit, rate_limit2):
31+
"""Commit and wait for RateLimitPolicies to be fully enforced"""
32+
for policy in [rate_limit, rate_limit2]:
33+
request.addfinalizer(policy.delete)
34+
policy.commit()
35+
policy.wait_for_ready()
36+
37+
38+
def test_multiple_limits_targeting_different_route_rules(client):
39+
"""Test targeting separate HTTPRoute Rules with different limits"""
40+
responses = client.get_many("/get", 3)
41+
responses.assert_all(status_code=200)
42+
assert client.get("/get").status_code == 429
43+
44+
responses = client.get_many("/anything", 2)
45+
responses.assert_all(status_code=200)
46+
assert client.get("/anything").status_code == 429
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test that multiple limits targeting same Gateway Listener are correctly applied"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
6+
7+
8+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
9+
10+
11+
@pytest.fixture(scope="module")
12+
def rate_limit(cluster, blame, module_label, gateway):
13+
"""Add a RateLimitPolicy targeting the Gateway Listener with two limits"""
14+
rate_limit = RateLimitPolicy.create_instance(
15+
cluster, blame("limit"), gateway, "api", labels={"testRun": module_label}
16+
)
17+
rate_limit.add_limit("test1", [Limit(8, "10s")])
18+
rate_limit.add_limit("test2", [Limit(3, "5s")])
19+
return rate_limit
20+
21+
22+
def test_two_limits_targeting_one_gateway_listener(client):
23+
"""Test that one limit ends up shadowing others"""
24+
responses = client.get_many("/get", 3)
25+
responses.assert_all(status_code=200)
26+
assert client.get("/get").status_code == 429
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test that multiple limits targeting same rule are correctly applied"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
6+
7+
8+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
9+
10+
11+
@pytest.fixture(scope="module")
12+
def rate_limit(cluster, blame, module_label, route):
13+
"""Add a RateLimitPolicy targeting the first HTTPRoute Rule with two limits"""
14+
rate_limit = RateLimitPolicy.create_instance(
15+
cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label}
16+
)
17+
rate_limit.add_limit("test1", [Limit(8, "10s")])
18+
rate_limit.add_limit("test2", [Limit(3, "5s")])
19+
return rate_limit
20+
21+
22+
def test_two_limits_targeting_one_route_rule(client):
23+
"""Test that one limit ends up shadowing others"""
24+
responses = client.get_many("/get", 3)
25+
responses.assert_all(status_code=200)
26+
assert client.get("/get").status_code == 429
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Tests that the RLP is correctly applied to the specific route rule"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
6+
7+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
8+
9+
10+
@pytest.fixture(scope="module")
11+
def rate_limit(cluster, blame, module_label, route):
12+
"""Add a RateLimitPolicy targeting the first HTTPRoute Rule."""
13+
rlp = RateLimitPolicy.create_instance(cluster, blame("limit"), route, "rule-1", labels={"testRun": module_label})
14+
rlp.add_limit("basic", [Limit(2, "10s")])
15+
return rlp
16+
17+
18+
def test_limit_match_route_rule(client):
19+
"""Tests that RLP correctly applies to the specific HTTPRoute Rule"""
20+
responses = client.get_many("/get", 2)
21+
responses.assert_all(status_code=200)
22+
assert client.get("/get").status_code == 429
23+
24+
response = client.get("/anything")
25+
assert response.status_code == 200

0 commit comments

Comments
 (0)