Skip to content

Commit 86e4d8f

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

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed

testsuite/tests/singlecluster/gateway/reconciliation/change_targetref/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
Conftest for changing targetRef field in policies
3+
"""
4+
5+
import pytest
6+
7+
from testsuite.gateway import GatewayRoute, GatewayListener, Hostname, Exposer
8+
from testsuite.gateway.gateway_api.gateway import KuadrantGateway
9+
from testsuite.gateway.gateway_api.hostname import DNSPolicyExposer
10+
from testsuite.gateway.gateway_api.route import HTTPRoute
11+
from testsuite.kuadrant.policy.dns import DNSPolicy
12+
13+
14+
@pytest.fixture(scope="module")
15+
def exposer2(request, cluster) -> Exposer:
16+
"""Second DNSPolicyExposer setup for Gateway 2"""
17+
exposer = DNSPolicyExposer(cluster)
18+
request.addfinalizer(exposer.delete)
19+
exposer.commit()
20+
return exposer
21+
22+
23+
@pytest.fixture(scope="module")
24+
def base_domain2(exposer2):
25+
"""Returns preconfigured base domain for the second Gateway"""
26+
return exposer2.base_domain
27+
28+
29+
@pytest.fixture(scope="module")
30+
def wildcard_domain2(base_domain2):
31+
"""Wildcard domain for Gateway 2"""
32+
return f"*.{base_domain2}"
33+
34+
35+
@pytest.fixture(scope="module")
36+
def gateway(request, cluster, blame, wildcard_domain, module_label):
37+
"""Create and configure Gateway 1"""
38+
gw = KuadrantGateway.create_instance(cluster, blame("gw"), {"app": module_label})
39+
gw.add_listener(GatewayListener(hostname=wildcard_domain))
40+
request.addfinalizer(gw.delete)
41+
gw.commit()
42+
gw.wait_for_ready()
43+
return gw
44+
45+
46+
@pytest.fixture(scope="module")
47+
def gateway2(request, cluster, blame, wildcard_domain2, module_label):
48+
"""Create and configure Gateway 2"""
49+
gw = KuadrantGateway.create_instance(cluster, blame("gw2"), {"app": module_label})
50+
gw.add_listener(GatewayListener(hostname=wildcard_domain2))
51+
request.addfinalizer(gw.delete)
52+
gw.commit()
53+
gw.wait_for_ready()
54+
return gw
55+
56+
57+
@pytest.fixture(scope="module")
58+
def hostname2(gateway2, exposer2, blame) -> Hostname:
59+
"""Expose Hostname for Gateway 2"""
60+
hostname = exposer2.expose_hostname(blame("hostname2"), gateway2)
61+
return hostname
62+
63+
64+
@pytest.fixture(scope="module")
65+
def route2(request, gateway2, blame, hostname2, module_label, backend) -> GatewayRoute:
66+
"""Create and configure Route 2"""
67+
route = HTTPRoute.create_instance(gateway2.cluster, blame("route2"), gateway2, {"app": module_label})
68+
route.add_hostname(hostname2.hostname)
69+
route.add_backend(backend)
70+
request.addfinalizer(route.delete)
71+
route.commit()
72+
route.wait_for_ready()
73+
return route
74+
75+
76+
@pytest.fixture(scope="module")
77+
def client2(route2, hostname2): # pylint: disable=unused-argument
78+
"""Returns httpx client for Gateway 2"""
79+
client = hostname2.client()
80+
yield client
81+
client.close()
82+
83+
84+
@pytest.fixture(scope="module")
85+
def dns_policy2(blame, gateway2, module_label, dns_provider_secret, request):
86+
"""DNSPolicy fixture for Gateway 2"""
87+
policy = DNSPolicy.create_instance(
88+
gateway2.cluster, blame("dns2"), gateway2, dns_provider_secret, labels={"app": module_label}
89+
)
90+
request.addfinalizer(policy.delete)
91+
policy.commit()
92+
policy.wait_for_ready()
93+
return policy
94+
95+
96+
@pytest.fixture(scope="session")
97+
def change_target_ref():
98+
"""Function that changes targetRef of given policy"""
99+
100+
def _change_targetref(policy, gateway):
101+
def _apply_target_ref(apiobj):
102+
apiobj.model.spec.targetRef = gateway.reference
103+
return True
104+
105+
policy.modify_and_apply(_apply_target_ref)
106+
policy.wait_for_ready()
107+
108+
return _change_targetref
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Test for changing targetRef field in AuthPolicy
3+
"""
4+
5+
import pytest
6+
7+
from testsuite.kuadrant.policy.authorization.auth_policy import AuthPolicy
8+
9+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.dnspolicy]
10+
11+
12+
@pytest.fixture(scope="module")
13+
def authorization(oidc_provider, gateway, cluster, blame, module_label, route): # pylint: disable=unused-argument
14+
"""Overwrite the authorization fixture and attach it to the gateway"""
15+
policy = AuthPolicy.create_instance(cluster, blame("authz"), gateway, labels={"testRun": module_label})
16+
policy.identity.add_oidc("default", oidc_provider.well_known["issuer"])
17+
return policy
18+
19+
20+
def test_update_auth_policy_target_ref(
21+
gateway2, authorization, client, client2, auth, dns_policy, dns_policy2, change_target_ref
22+
): # pylint: disable=unused-argument
23+
"""Test updating the targetRef of an AuthPolicy from Gateway 1 to Gateway 2"""
24+
response = client.get("/get", auth=auth)
25+
assert response.status_code == 200
26+
27+
response = client.get("/get")
28+
assert response.status_code == 401
29+
30+
response = client2.get("/get")
31+
assert response.status_code == 200
32+
33+
change_target_ref(authorization, gateway2)
34+
35+
response = client2.get("/get", auth=auth)
36+
assert response.status_code == 200
37+
38+
response = client2.get("/get")
39+
assert response.status_code == 401
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Test for changing targetRef field in RateLimitPolicy
3+
"""
4+
5+
import pytest
6+
7+
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
8+
9+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.dnspolicy]
10+
11+
12+
@pytest.fixture(scope="module")
13+
def authorization():
14+
"""
15+
Override the authorization fixture to prevent the creation of an AuthPolicy.
16+
This ensures no authentication is enforced during the test
17+
"""
18+
return None
19+
20+
21+
@pytest.fixture(scope="module")
22+
def rate_limit(cluster, blame, module_label, gateway, route): # pylint: disable=unused-argument
23+
"""RateLimitPolicy for testing"""
24+
policy = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, labels={"testRun": module_label})
25+
policy.add_limit("basic", [Limit(2, "10s")])
26+
return policy
27+
28+
29+
def test_update_ratelimit_policy_target_ref(
30+
gateway2, rate_limit, client, client2, dns_policy, dns_policy2, change_target_ref
31+
): # pylint: disable=unused-argument
32+
"""Test updating the targetRef of a RateLimitPolicy from Gateway 1 to Gateway 2"""
33+
responses = client.get_many("/get", 2)
34+
responses.assert_all(status_code=200)
35+
assert client.get("/get").status_code == 429
36+
37+
responses = client2.get_many("/get", 3)
38+
responses.assert_all(status_code=200)
39+
40+
change_target_ref(rate_limit, gateway2)
41+
42+
responses = client2.get_many("/get", 2)
43+
responses.assert_all(status_code=200)
44+
assert client2.get("/get").status_code == 429

0 commit comments

Comments
 (0)