Skip to content

Commit cb69252

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

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-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,95 @@
1+
"""
2+
Test 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+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.dnspolicy]
14+
15+
16+
@pytest.fixture(scope="module")
17+
def exposer2(request, cluster) -> Exposer:
18+
"""Second DNSPolicyExposer setup for Gateway B"""
19+
exposer = DNSPolicyExposer(cluster)
20+
request.addfinalizer(exposer.delete)
21+
exposer.commit()
22+
return exposer
23+
24+
25+
@pytest.fixture(scope="module")
26+
def base_domain2(exposer2):
27+
"""Returns preconfigured base domain for the second Gateway"""
28+
return exposer2.base_domain
29+
30+
31+
@pytest.fixture(scope="module")
32+
def wildcard_domain2(base_domain2):
33+
"""Wildcard domain for Gateway B"""
34+
return f"*.{base_domain2}"
35+
36+
37+
@pytest.fixture(scope="module")
38+
def gateway(request, cluster, blame, wildcard_domain, module_label):
39+
"""Create and configure Gateway A"""
40+
gw = KuadrantGateway.create_instance(cluster, blame("gw"), {"app": module_label})
41+
gw.add_listener(GatewayListener(hostname=wildcard_domain))
42+
request.addfinalizer(gw.delete)
43+
gw.commit()
44+
gw.wait_for_ready()
45+
return gw
46+
47+
48+
@pytest.fixture(scope="module")
49+
def gateway_b(request, cluster, blame, wildcard_domain2, module_label):
50+
"""Create and configure Gateway B"""
51+
gw = KuadrantGateway.create_instance(cluster, blame("gw-b"), {"app": module_label})
52+
gw.add_listener(GatewayListener(hostname=wildcard_domain2))
53+
request.addfinalizer(gw.delete)
54+
gw.commit()
55+
gw.wait_for_ready()
56+
return gw
57+
58+
59+
@pytest.fixture(scope="module")
60+
def hostname_b(gateway_b, exposer2, blame) -> Hostname:
61+
"""Expose Hostname for Gateway B"""
62+
hostname = exposer2.expose_hostname(blame("hostname-b"), gateway_b)
63+
return hostname
64+
65+
66+
@pytest.fixture(scope="module")
67+
def route_b(request, gateway_b, blame, hostname_b, module_label, backend) -> GatewayRoute:
68+
"""Create and configure Route B"""
69+
route = HTTPRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b, {"app": module_label})
70+
route.add_hostname(hostname_b.hostname)
71+
route.add_backend(backend)
72+
request.addfinalizer(route.delete)
73+
route.commit()
74+
route.wait_for_ready()
75+
return route
76+
77+
78+
@pytest.fixture(scope="module")
79+
def client_b(route_b, hostname_b): # pylint: disable=unused-argument
80+
"""Returns httpx client for Gateway B"""
81+
client = hostname_b.client()
82+
yield client
83+
client.close()
84+
85+
86+
@pytest.fixture(scope="module")
87+
def dns_policy_b(blame, gateway_b, module_label, dns_provider_secret, request):
88+
"""DNSPolicy fixture for Gateway B"""
89+
policy = DNSPolicy.create_instance(
90+
gateway_b.cluster, blame("dns-b"), gateway_b, dns_provider_secret, labels={"app": module_label}
91+
)
92+
request.addfinalizer(policy.delete)
93+
policy.commit()
94+
policy.wait_for_ready()
95+
return policy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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(
14+
oidc_provider, gateway, cluster, blame, module_label, route, route_b
15+
): # pylint: disable=unused-argument
16+
"""Overwrite the authorization fixture and attach it to the gateway"""
17+
policy = AuthPolicy.create_instance(cluster, blame("authz"), gateway, labels={"testRun": module_label})
18+
policy.identity.add_oidc("default", oidc_provider.well_known["issuer"])
19+
return policy
20+
21+
22+
def test_update_auth_policy_target_ref(
23+
gateway, gateway_b, authorization, client, client_b, auth, blame, dns_policy_b, dns_policy
24+
): # pylint: disable=unused-argument
25+
"""Test updating the targetRef of an AuthPolicy from Gateway A to Gateway B"""
26+
response = client.get("/get", auth=auth)
27+
assert response.status_code == 200
28+
29+
response = client.get("/get")
30+
assert response.status_code == 401
31+
32+
response = client_b.get("/get")
33+
assert response.status_code == 200
34+
35+
authorization.refresh().model.spec.targetRef = gateway_b.reference
36+
res = authorization.apply()
37+
assert res.status() == 0, res.err()
38+
authorization.wait_for_ready()
39+
40+
response = client_b.get("/get", auth=auth)
41+
assert response.status_code == 200
42+
43+
response = client_b.get("/get")
44+
assert response.status_code == 401
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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_policy(request, cluster, blame, module_label, gateway, route_b): # 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(5, "10s")])
26+
request.addfinalizer(policy.delete)
27+
policy.commit()
28+
return policy
29+
30+
31+
def test_update_ratelimit_policy_target_ref(
32+
gateway, gateway_b, rate_limit_policy, client, client_b, blame, dns_policy_b, dns_policy
33+
): # pylint: disable=unused-argument
34+
"""Test updating the targetRef of a RateLimitPolicy from Gateway A to Gateway B"""
35+
responses = client.get_many("/get", 5)
36+
responses.assert_all(status_code=200)
37+
assert client.get("/get").status_code == 429
38+
39+
responses = client_b.get_many("/get", 6)
40+
responses.assert_all(status_code=200)
41+
42+
rate_limit_policy.refresh().model.spec.targetRef.name = gateway_b.model.metadata.name
43+
res = rate_limit_policy.apply()
44+
assert res.status() == 0, res.err()
45+
rate_limit_policy.wait_for_ready()
46+
47+
responses = client_b.get_many("/get", 5)
48+
responses.assert_all(status_code=200)
49+
assert client_b.get("/get").status_code == 429

0 commit comments

Comments
 (0)