Skip to content

Commit 38cfcac

Browse files
committed
Test for changing targetRef field in policies (AuthPolicy and RateLimitPolicy)
Signed-off-by: emmaaroche <[email protected]> Updates made Signed-off-by: emmaaroche <[email protected]> Put tests into seperate files Signed-off-by: emmaaroche <[email protected]> Update auth and rate limit policy targetRef tests Signed-off-by: emmaaroche <[email protected]>
1 parent 9606102 commit 38cfcac

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-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,79 @@
1+
"""
2+
Test for changing targetRef field in policies
3+
"""
4+
5+
import pytest
6+
7+
from testsuite.gateway import GatewayRoute, GatewayListener, Hostname
8+
from testsuite.gateway.gateway_api.gateway import KuadrantGateway
9+
from testsuite.gateway.gateway_api.route import HTTPRoute
10+
from testsuite.kuadrant.policy.dns import DNSPolicy
11+
12+
pytestmark = [pytest.mark.kuadrant_only, pytest.mark.dnspolicy]
13+
14+
15+
@pytest.fixture(scope="module")
16+
def wildcard_domain2(base_domain):
17+
"""Wildcard domain for Gateway B"""
18+
return f"*.{base_domain}"
19+
20+
21+
@pytest.fixture(scope="module")
22+
def gateway(request, cluster, blame, wildcard_domain, module_label):
23+
"""Create and configure Gateway A"""
24+
gw = KuadrantGateway.create_instance(cluster, blame("gw"), {"app": module_label})
25+
gw.add_listener(GatewayListener(hostname=wildcard_domain))
26+
request.addfinalizer(gw.delete)
27+
gw.commit()
28+
gw.wait_for_ready()
29+
return gw
30+
31+
32+
@pytest.fixture(scope="module")
33+
def gateway_b(request, cluster, blame, wildcard_domain2, module_label):
34+
"""Create and configure Gateway B"""
35+
gw = KuadrantGateway.create_instance(cluster, blame("gw-b"), {"app": module_label})
36+
gw.add_listener(GatewayListener(hostname=wildcard_domain2))
37+
request.addfinalizer(gw.delete)
38+
gw.commit()
39+
gw.wait_for_ready()
40+
return gw
41+
42+
43+
@pytest.fixture(scope="module")
44+
def hostname_b(gateway_b, exposer, blame) -> Hostname:
45+
"""Expose Hostname for Gateway B"""
46+
hostname = exposer.expose_hostname(blame("hostname-b"), gateway_b)
47+
return hostname
48+
49+
50+
@pytest.fixture(scope="module")
51+
def route_b(request, gateway_b, blame, hostname_b, module_label, backend) -> GatewayRoute:
52+
"""Create and configure Route B"""
53+
route = HTTPRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b, {"app": module_label})
54+
route.add_hostname(hostname_b.hostname)
55+
route.add_backend(backend)
56+
request.addfinalizer(route.delete)
57+
route.commit()
58+
route.wait_for_ready()
59+
return route
60+
61+
62+
@pytest.fixture(scope="module")
63+
def client_b(route_b, hostname_b): # pylint: disable=unused-argument
64+
"""Returns httpx client for Gateway B"""
65+
client = hostname_b.client()
66+
yield client
67+
client.close()
68+
69+
70+
@pytest.fixture(scope="module")
71+
def dns_policy_b(blame, gateway_b, module_label, dns_provider_secret, request):
72+
"""DNSPolicy fixture for Gateway B"""
73+
policy = DNSPolicy.create_instance(
74+
gateway_b.cluster, blame("dns-b"), gateway_b, dns_provider_secret, labels={"app": module_label}
75+
)
76+
request.addfinalizer(policy.delete)
77+
policy.commit()
78+
policy.wait_for_ready()
79+
return policy
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 auth_policy(
14+
oidc_provider, gateway, request, cluster, blame, module_label, route_b
15+
): # pylint: disable=unused-argument
16+
"""Create AuthPolicy for testing"""
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+
request.addfinalizer(policy.delete)
20+
policy.commit()
21+
return policy
22+
23+
24+
def test_update_auth_policy_target_ref(
25+
gateway, gateway_b, auth_policy, client, client_b, auth, blame, dns_policy_b, dns_policy
26+
): # pylint: disable=unused-argument
27+
"""Test updating the targetRef of an AuthPolicy from Gateway A to Gateway B"""
28+
dns_policy.wait_for_ready()
29+
30+
response = client.get("/get", auth=auth)
31+
assert response.status_code == 200
32+
33+
response = client.get("/get")
34+
assert response.status_code == 401
35+
36+
response = client_b.get("/get")
37+
assert response.status_code == 200
38+
39+
auth_policy.refresh().model.spec.targetRef = gateway_b.reference
40+
res = auth_policy.apply()
41+
assert res.status() == 0, res.err()
42+
auth_policy.wait_for_ready()
43+
44+
response = client_b.get("/get", auth=auth)
45+
assert response.status_code == 200
46+
47+
response = client_b.get("/get")
48+
assert response.status_code == 401
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
dns_policy.wait_for_ready()
36+
37+
responses = client.get_many("/get", 5)
38+
responses.assert_all(status_code=200)
39+
assert client.get("/get").status_code == 429
40+
41+
responses = client_b.get_many("/get", 6)
42+
responses.assert_all(status_code=200)
43+
44+
rate_limit_policy.refresh().model.spec.targetRef.name = gateway_b.model.metadata.name
45+
res = rate_limit_policy.apply()
46+
assert res.status() == 0, res.err()
47+
rate_limit_policy.wait_for_ready()
48+
dns_policy_b.wait_for_ready()
49+
50+
responses = client_b.get_many("/get", 5)
51+
responses.assert_all(status_code=200)
52+
assert client_b.get("/get").status_code == 429

0 commit comments

Comments
 (0)