Skip to content

Commit 79295be

Browse files
author
Jakub Smolar
authored
Merge pull request #592 from martinhesko/overrides-fix
Modify overrides test to include overrides on HTTPRoutes
2 parents 8b22c80 + 9301b5c commit 79295be

File tree

7 files changed

+34
-74
lines changed

7 files changed

+34
-74
lines changed

testsuite/gateway/gateway_api/gateway.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def is_affected_by(self, policy: Policy) -> bool:
7878
f"kuadrant.io/{policy.kind(lowercase=False)}Affected",
7979
"True",
8080
"Accepted",
81-
f"Object affected by {policy.kind(lowercase=False)} [{policy.namespace()}/{policy.name()}]",
81+
f"Object affected by {policy.kind(lowercase=False)}",
82+
f"{policy.namespace()}/{policy.name()}",
8283
):
8384
return True
8485
return False

testsuite/gateway/gateway_api/route.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def is_affected_by(self, policy: Policy):
5454
f"kuadrant.io/{policy.kind(lowercase=False)}Affected",
5555
"True",
5656
"Accepted",
57-
f"Object affected by {policy.kind(lowercase=False)} [{policy.namespace()}/{policy.name()}]",
57+
f"Object affected by {policy.kind(lowercase=False)}",
58+
f"{policy.namespace()}/{policy.name()}",
5859
):
5960
return True
6061
return False

testsuite/kuadrant/policy/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ def _check(obj):
4242
f"kuadrant.io/{policy.kind(lowercase=False)}Affected",
4343
"True",
4444
"Accepted",
45-
f"Object affected by {policy.kind(lowercase=False)} {policy.namespace()}/{policy.name()}",
45+
f"Object affected by {policy.kind(lowercase=False)}",
46+
f"{policy.namespace()}/{policy.name()}",
4647
):
4748
return True
4849
return False

testsuite/tests/singlecluster/overrides/test_basic_auth.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import pytest
44

55
from testsuite.httpx.auth import HttpxOidcClientAuth
6-
from testsuite.kuadrant.policy.authorization.auth_policy import AuthPolicy
76

87
pytestmark = [pytest.mark.kuadrant_only]
98

109

1110
@pytest.fixture(scope="module")
12-
def authorization(route, gateway, blame, cluster, label, oidc_provider): # pylint: disable=unused-argument
13-
"""Add oidc identity to overrides block of gateway-attached AuthPolicy"""
14-
auth_policy = AuthPolicy.create_instance(cluster, blame("authz"), gateway, labels={"testRun": label})
15-
auth_policy.overrides.identity.add_oidc("override", oidc_provider.well_known["issuer"])
16-
return auth_policy
11+
def authorization(authorization, oidc_provider):
12+
"""Add oidc identity to defaults block of AuthPolicy"""
13+
authorization.overrides.identity.add_oidc("override", oidc_provider.well_known["issuer"])
14+
return authorization
1715

1816

1917
@pytest.fixture(scope="module")
@@ -28,12 +26,12 @@ def rate_limit():
2826
return None
2927

3028

29+
@pytest.mark.parametrize("authorization", ["route", "gateway"], indirect=True)
3130
def test_basic_auth(route, authorization, client, auth):
3231
"""Test if rules inside overrides block of Gateway's AuthPolicy are inherited by the HTTPRoute
3332
and enforced like any other normal rule"""
3433
route.refresh()
3534
assert route.is_affected_by(authorization)
3635

37-
response = client.get("/get")
38-
assert response.status_code == 401
36+
assert client.get("/get").status_code == 401
3937
assert client.get("/get", auth=auth).status_code == 200 # assert that AuthPolicy is enforced
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""Test basic enforcement of the rules inside the 'overrides' block of the RateLimitPolicy assigned to a Gateway"""
1+
"""Test enforcement of the rules inside the 'overrides' block of the RateLimitPolicy assigned to a Gateway/HTTPRoute"""
22

33
import pytest
44

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

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

9-
GATEWAY_LIMIT = Limit(3, "5s")
9+
OVERRIDE_LIMIT = Limit(3, "5s")
1010
ROUTE_LIMIT = Limit(2, "5s")
1111

1212

@@ -16,33 +16,34 @@ def authorization():
1616
return None
1717

1818

19-
@pytest.fixture(scope="module")
20-
def rate_limit_gw(request, cluster, blame, module_label, gateway):
21-
"""Add a RateLimitPolicy to the Gateway with an overrides block to override the Route-level policy."""
22-
rate_limit_gateway = RateLimitPolicy.create_instance(
23-
cluster, blame("limit-gateway"), gateway, labels={"testRun": module_label}
19+
@pytest.fixture(scope="function")
20+
def rate_limit_route(request, cluster, blame, module_label, route):
21+
"""Add a RateLimitPolicy to the HTTPRoute with a basic limit to be overriden."""
22+
rate_limit_route = RateLimitPolicy.create_instance(
23+
cluster, blame("limit-route"), route, labels={"testRun": module_label}
2424
)
25-
rate_limit_gateway.overrides.add_limit("basic", [GATEWAY_LIMIT])
26-
request.addfinalizer(rate_limit_gateway.delete)
27-
rate_limit_gateway.commit()
28-
rate_limit_gateway.wait_for_ready()
29-
return rate_limit_gateway
25+
rate_limit_route.add_limit("basic", [ROUTE_LIMIT])
26+
request.addfinalizer(rate_limit_route.delete)
27+
rate_limit_route.commit()
28+
rate_limit_route.wait_for_accepted()
29+
return rate_limit_route
3030

3131

3232
@pytest.fixture(scope="module")
3333
def rate_limit(rate_limit):
34-
"""Add basic requests limit to RateLimitPolicy"""
35-
rate_limit.add_limit("basic", [ROUTE_LIMIT])
34+
"""Add an override to RateLimitPolicy"""
35+
rate_limit.overrides.add_limit("override-limit", [OVERRIDE_LIMIT])
3636
return rate_limit
3737

3838

39-
def test_basic_rate_limit(rate_limit, rate_limit_gw, route, client):
40-
"""Test if rules inside overrides block of Gateway's RateLimitPolicy are inherited by the HTTPRoute
41-
and enforced like any other normal rule"""
39+
@pytest.mark.parametrize("rate_limit", ["route", "gateway"], indirect=True)
40+
def test_basic_rate_limit(rate_limit, rate_limit_route, route, client):
41+
"""Test if rules inside overrides block of Gateway/HTTPRoute RateLimitPolicy are inherited by the HTTPRoute
42+
and override the rate limit targeting the route."""
4243
route.refresh()
4344
assert route.is_affected_by(rate_limit)
44-
rate_limit_gw.wait_for_full_enforced()
45+
assert route.is_affected_by(rate_limit_route)
4546

46-
responses = client.get_many("/get", GATEWAY_LIMIT.limit)
47+
responses = client.get_many("/get", OVERRIDE_LIMIT.limit)
4748
responses.assert_all(status_code=200)
4849
assert client.get("/get").status_code == 429 # assert that RateLimitPolicy is enforced

testsuite/tests/singlecluster/overrides/test_route_override.py

-43
This file was deleted.

testsuite/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ def _asdict_recurse(obj):
168168
return result
169169

170170

171-
def check_condition(condition, condition_type, status, reason=None, message=None):
171+
def check_condition(condition, condition_type, status, reason=None, message=None, policy=None):
172172
"""Checks if condition matches expectation, won't check message and reason if they are None"""
173173
if ( # pylint: disable=too-many-boolean-expressions
174174
condition.type == condition_type
175175
and condition.status == status
176176
and (message is None or message in condition.message)
177+
and (policy is None or policy in condition.message)
177178
and (reason is None or reason == condition.reason)
178179
):
179180
return True

0 commit comments

Comments
 (0)