Skip to content

Commit 4dbcc75

Browse files
committed
Test for changing parentRef field in policies (AuthPolicy and RateLimitPolicy)
Signed-off-by: emmaaroche <[email protected]>
1 parent 9606102 commit 4dbcc75

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"""
2+
Test for changing parentRef field in policies (AuthPolicy and RateLimitPolicy)
3+
4+
Included logging and comments for troubleshooting purposes
5+
"""
6+
7+
import logging
8+
9+
import pytest
10+
11+
from testsuite.gateway import GatewayListener
12+
from testsuite.gateway.gateway_api.gateway import KuadrantGateway
13+
from testsuite.kuadrant.policy.authorization.auth_policy import AuthPolicy
14+
from testsuite.kuadrant.policy.rate_limit import RateLimitPolicy, Limit
15+
16+
logging.basicConfig(level=logging.INFO)
17+
logger = logging.getLogger(__name__)
18+
19+
pytestmark = [pytest.mark.kuadrant_only]
20+
21+
22+
@pytest.fixture(scope="module")
23+
def gateway_a(cluster, blame, gateway):
24+
"""Create and configure Gateway A"""
25+
gateway = KuadrantGateway.create_instance(cluster, blame("gateway-a"), labels={"testRun": blame("test-gateway-a")})
26+
gateway.add_listener(
27+
GatewayListener(name="http-listener", protocol="HTTP", hostname="gateway-a.example.com", port=80)
28+
)
29+
gateway.commit()
30+
logger.info("Created Gateway A with hostname: %s", "gateway-a.example.com")
31+
yield gateway
32+
gateway.delete()
33+
34+
35+
@pytest.fixture(scope="module")
36+
def gateway_b(cluster, blame, gateway):
37+
"""Create and configure Gateway B"""
38+
gateway = KuadrantGateway.create_instance(cluster, blame("gateway-b"), labels={"testRun": blame("test-gateway-b")})
39+
gateway.add_listener(
40+
GatewayListener(name="http-listener", protocol="HTTP", hostname="gateway-b.example.com", port=80)
41+
)
42+
gateway.commit()
43+
logger.info("Created Gateway B with hostname: %s", "gateway-b.example.com")
44+
yield gateway
45+
gateway.delete()
46+
47+
48+
@pytest.fixture(scope="module")
49+
def rate_limit_policy(cluster, blame, module_label, gateway_a, route): # pylint: disable=unused-argument
50+
"""Create a RateLimitPolicy pointing to Gateway A"""
51+
policy = RateLimitPolicy.create_instance(cluster, blame("limit"), route, labels={"testRun": module_label})
52+
policy.add_limit("basic", [Limit(5, "10s")])
53+
policy.commit()
54+
yield policy
55+
policy.delete()
56+
57+
58+
@pytest.fixture(scope="module")
59+
def auth_policy(gateway, blame, cluster, label):
60+
"""Create a AuthPolicy pointing to Gateway A"""
61+
policy = AuthPolicy.create_instance(cluster, blame("authz"), gateway, labels={"testRun": label})
62+
policy.authorization.add_opa_policy("rego", "allow = true")
63+
policy.commit()
64+
yield policy
65+
policy.delete()
66+
67+
68+
def test_update_ratelimit_policy_parent_ref(gateway_a, gateway_b, rate_limit_policy, client, auth):
69+
"""Test updating the parentRef of a RateLimitPolicy from Gateway A (hostname A) to Gateway B (hostname B)"""
70+
# Dynamically add 'parent_ref' attribute to RateLimitPolicy for testing purposes
71+
if not hasattr(rate_limit_policy, "parent_ref"):
72+
rate_limit_policy.parent_ref = gateway_a.model.metadata.name
73+
logger.info(
74+
"Dynamically added 'parent_ref' attribute to RateLimitPolicy with initial value: %s",
75+
rate_limit_policy.parent_ref,
76+
)
77+
78+
# Verify "/get" endpoint responds with 200 before parentRef update
79+
response = client.get("/get", auth=auth)
80+
assert response.status_code == 200
81+
82+
# Assert initial parentRef state
83+
initial_parent_ref = rate_limit_policy.parent_ref
84+
assert initial_parent_ref == gateway_a.model.metadata.name, "Initial parentRef mismatch"
85+
86+
# Log and update parentRef to Gateway B
87+
logger.info(
88+
"Updating parentRef from Gateway A (%s) to Gateway B (%s)",
89+
gateway_a.model.metadata.name,
90+
gateway_b.model.metadata.name,
91+
)
92+
rate_limit_policy.parent_ref = gateway_b.model.metadata.name
93+
94+
# Verify updated parentRef state
95+
updated_parent_ref = rate_limit_policy.parent_ref
96+
logger.info("Updated parentRef: %s (expected: %s)", updated_parent_ref, gateway_b.model.metadata.name)
97+
assert updated_parent_ref == gateway_b.model.metadata.name, "Updated parentRef mismatch"
98+
99+
# Re-verify "/get" endpoint responds with 200 before parentRef update
100+
response = client.get("/get", auth=auth)
101+
assert response.status_code == 200
102+
103+
logger.info("Test passed: parentRef successfully updated to Gateway B (%s)", updated_parent_ref)
104+
105+
106+
def test_update_authpolicy_rules(gateway_a, gateway_b, auth_policy, auth, client):
107+
"""Test updating the rules of an AuthPolicy from Gateway A (hostname A) to Gateway B (hostname B)"""
108+
if not hasattr(auth_policy, "parent_ref"):
109+
auth_policy.parent_ref = gateway_a.model.metadata.name
110+
logger.info(
111+
"Dynamically added 'parent_ref' attribute to AuthPolicy with initial value: %s", auth_policy.parent_ref
112+
)
113+
114+
response = client.get("/get", auth=auth)
115+
assert response.status_code == 200
116+
117+
initial_parent_ref = auth_policy.parent_ref
118+
assert initial_parent_ref == gateway_a.model.metadata.name, "Initial parentRef mismatch"
119+
120+
logger.info(
121+
"Updating parentRef from Gateway A (%s) to Gateway B (%s)",
122+
gateway_a.model.metadata.name,
123+
gateway_b.model.metadata.name,
124+
)
125+
auth_policy.parent_ref = gateway_b.model.metadata.name
126+
127+
updated_parent_ref = auth_policy.parent_ref
128+
logger.info("Updated parentRef: %s (expected: %s)", updated_parent_ref, gateway_b.model.metadata.name)
129+
assert updated_parent_ref == gateway_b.model.metadata.name, "Updated parentRef mismatch"
130+
131+
response = client.get("/get", auth=auth)
132+
assert response.status_code == 200
133+
134+
logger.info("Test passed: parentRef successfully updated to Gateway B (%s)", updated_parent_ref)

0 commit comments

Comments
 (0)