Skip to content

Commit 6533e9a

Browse files
authored
Merge pull request #637 from averevki/defaults_overrides_section
D&O section target tests
2 parents 5322232 + a3aae50 commit 6533e9a

File tree

5 files changed

+179
-2
lines changed

5 files changed

+179
-2
lines changed

testsuite/kuadrant/policy/authorization/auth_policy.py

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def create_instance(
2727
name,
2828
target: Referencable,
2929
labels: Dict[str, str] = None,
30+
section_name: str = None,
3031
):
3132
"""Creates base instance"""
3233
model: Dict = {
@@ -37,6 +38,8 @@ def create_instance(
3738
"targetRef": target.reference,
3839
},
3940
}
41+
if section_name:
42+
model["spec"]["targetRef"]["sectionName"] = section_name
4043

4144
return cls(model, context=cluster.context)
4245

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Test enforcement of policies with defaults targeting a specifics gateway/route section"""
2+
3+
import pytest
4+
5+
from testsuite.httpx.auth import HttpxOidcClientAuth
6+
from testsuite.kuadrant.policy.authorization.auth_policy import AuthPolicy
7+
from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit
8+
9+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
10+
11+
LIMIT = Limit(5, "10s")
12+
13+
14+
@pytest.fixture(scope="module")
15+
def target(request):
16+
"""Returns the test target(gateway or route) and the target section name"""
17+
return request.getfixturevalue(request.param[0]), request.param[1]
18+
19+
20+
@pytest.fixture(scope="module")
21+
def auth(oidc_provider):
22+
"""Returns Authentication object for HTTPX"""
23+
return HttpxOidcClientAuth(oidc_provider.get_token, "authorization")
24+
25+
26+
@pytest.fixture(scope="module")
27+
def authorization(cluster, target, route, oidc_provider, module_label, blame): # pylint: disable=unused-argument
28+
"""Add oidc identity to defaults block of AuthPolicy"""
29+
authorization = AuthPolicy.create_instance(
30+
cluster, blame("authz"), target[0], labels={"testRun": module_label}, section_name=target[1]
31+
)
32+
authorization.defaults.identity.add_oidc("default", oidc_provider.well_known["issuer"])
33+
return authorization
34+
35+
36+
@pytest.fixture(scope="module")
37+
def rate_limit(cluster, target, route, module_label, blame): # pylint: disable=unused-argument
38+
"""Add a RateLimitPolicy targeting specific section"""
39+
rate_limit = RateLimitPolicy.create_instance(
40+
cluster, blame("limit"), target[0], target[1], labels={"testRun": module_label}
41+
)
42+
rate_limit.defaults.add_limit("basic", [LIMIT])
43+
return rate_limit
44+
45+
46+
@pytest.mark.parametrize(
47+
"target",
48+
[pytest.param(("gateway", "api"), id="gateway"), pytest.param(("route", "rule-1"), id="route")],
49+
indirect=True,
50+
)
51+
def test_basic_listener(client, auth):
52+
"""Test the defaults policies are correctly applied to the target section"""
53+
assert client.get("/get").status_code == 401
54+
55+
responses = client.get_many("/get", LIMIT.limit - 1, auth=auth)
56+
responses.assert_all(status_code=200)
57+
assert client.get("/get", auth=auth).status_code == 429
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Test override overriding another policy aimed at the same gateway/route"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit
6+
7+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
8+
9+
LIMIT = Limit(10, "10s")
10+
OVERRIDE_LIMIT = Limit(5, "10s")
11+
12+
13+
@pytest.fixture(scope="module")
14+
def target(request):
15+
"""Returns the test target(gateway or route)"""
16+
return request.getfixturevalue(request.param)
17+
18+
19+
@pytest.fixture(scope="module")
20+
def rate_limit(cluster, blame, module_label, target):
21+
"""Add a RateLimitPolicy with a default limit targeting the gateway/route"""
22+
rate_limit = RateLimitPolicy.create_instance(cluster, blame("limit"), target, labels={"testRun": module_label})
23+
rate_limit.defaults.add_limit("basic", [LIMIT])
24+
return rate_limit
25+
26+
27+
@pytest.fixture(scope="module")
28+
def override_rate_limit(cluster, blame, module_label, target):
29+
"""Add a RateLimitPolicy with an overrride targeting the gateway/route"""
30+
override_rate_limit = RateLimitPolicy.create_instance(
31+
cluster, blame("limit"), target, labels={"testRun": module_label}
32+
)
33+
override_rate_limit.overrides.add_limit("override", [OVERRIDE_LIMIT])
34+
return override_rate_limit
35+
36+
37+
@pytest.fixture(scope="module", autouse=True)
38+
def commit(request, route, rate_limit, override_rate_limit): # pylint: disable=unused-argument
39+
"""Commits RateLimitPolicies after the HTTPRoute is created and checks correct status"""
40+
for policy in [rate_limit, override_rate_limit]:
41+
request.addfinalizer(policy.delete)
42+
policy.commit()
43+
policy.wait_for_ready()
44+
45+
46+
@pytest.mark.parametrize("target", ["gateway", "route"], indirect=True)
47+
def test_multiple_policies_gateway_override(client):
48+
"""Test RateLimitPolicy with an override overriding a default policy targeting the same gateway/route"""
49+
responses = client.get_many("/get", OVERRIDE_LIMIT.limit)
50+
responses.assert_all(status_code=200)
51+
assert client.get("/get").status_code == 429
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Test override overriding another policy aimed at the same gateway/route section"""
2+
3+
import pytest
4+
5+
from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit
6+
7+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.limitador]
8+
9+
LIMIT = Limit(8, "5s")
10+
OVERRIDE_LIMIT = Limit(3, "5s")
11+
12+
13+
@pytest.fixture(scope="module")
14+
def target(request):
15+
"""Returns the test target(gateway or route) and the target section name"""
16+
return request.getfixturevalue(request.param[0]), request.param[1]
17+
18+
19+
@pytest.fixture(scope="module")
20+
def rate_limit(cluster, blame, module_label, target):
21+
"""Add a RateLimitPolicy targeting the specific section of gateway/route"""
22+
rate_limit = RateLimitPolicy.create_instance(
23+
cluster, blame("limit"), target[0], section_name=target[1], labels={"testRun": module_label}
24+
)
25+
rate_limit.defaults.add_limit("basic", [LIMIT])
26+
return rate_limit
27+
28+
29+
@pytest.fixture(scope="module")
30+
def override_rate_limit(cluster, blame, module_label, target):
31+
"""Add a RateLimitPolicy targeting the specific section of gateway/route"""
32+
override_rate_limit = RateLimitPolicy.create_instance(
33+
cluster, blame("limit"), target[0], section_name=target[1], labels={"testRun": module_label}
34+
)
35+
override_rate_limit.overrides.add_limit("override", [OVERRIDE_LIMIT])
36+
return override_rate_limit
37+
38+
39+
@pytest.fixture(scope="module", autouse=True)
40+
def commit(request, route, rate_limit, override_rate_limit): # pylint: disable=unused-argument
41+
"""Commits RateLimitPolicy after the HTTPRoute is created"""
42+
for policy in [rate_limit, override_rate_limit]:
43+
request.addfinalizer(policy.delete)
44+
policy.commit()
45+
policy.wait_for_ready()
46+
47+
48+
@pytest.mark.parametrize(
49+
"target",
50+
[pytest.param(("gateway", "api"), id="gateway"), pytest.param(("route", "rule-1"), id="route")],
51+
indirect=True,
52+
)
53+
def test_multiple_policies_listener_override(client):
54+
"""Test RateLimitPolicy with an override overriding a default policy targeting the same gateway/route section"""
55+
responses = client.get_many("/get", OVERRIDE_LIMIT.limit)
56+
responses.assert_all(status_code=200)
57+
assert client.get("/get").status_code == 429

0 commit comments

Comments
 (0)