Skip to content

Commit 2e3e201

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

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-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+
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+
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 gateway2(request, cluster, blame, wildcard_domain2, module_label):
50+
"""Create and configure Gateway B"""
51+
gw = KuadrantGateway.create_instance(cluster, blame("gw2"), {"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 hostname2(gateway2, exposer2, blame) -> Hostname:
61+
"""Expose Hostname for Gateway B"""
62+
hostname = exposer2.expose_hostname(blame("hostname2"), gateway2)
63+
return hostname
64+
65+
66+
@pytest.fixture(scope="module")
67+
def route2(request, gateway2, blame, hostname2, module_label, backend) -> GatewayRoute:
68+
"""Create and configure Route B"""
69+
route = HTTPRoute.create_instance(gateway2.cluster, blame("route2"), gateway2, {"app": module_label})
70+
route.add_hostname(hostname2.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 client2(route2, hostname2): # pylint: disable=unused-argument
80+
"""Returns httpx client for Gateway B"""
81+
client = hostname2.client()
82+
yield client
83+
client.close()
84+
85+
86+
@pytest.fixture(scope="module")
87+
def dns_policy2(blame, gateway2, module_label, dns_provider_secret, request):
88+
"""DNSPolicy fixture for Gateway B"""
89+
policy = DNSPolicy.create_instance(
90+
gateway2.cluster, blame("dns2"), gateway2, 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,42 @@
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
22+
): # pylint: disable=unused-argument
23+
"""Test updating the targetRef of an AuthPolicy from Gateway A to Gateway B"""
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+
authorization.refresh().model.spec.targetRef = gateway2.reference
34+
res = authorization.apply()
35+
assert res.status() == 0, res.err()
36+
authorization.wait_for_ready()
37+
38+
response = client2.get("/get", auth=auth)
39+
assert response.status_code == 200
40+
41+
response = client2.get("/get")
42+
assert response.status_code == 401
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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
31+
): # pylint: disable=unused-argument
32+
"""Test updating the targetRef of a RateLimitPolicy from Gateway A to Gateway B"""
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+
rate_limit.refresh().model.spec.targetRef.name = gateway2.model.metadata.name
41+
res = rate_limit.apply()
42+
assert res.status() == 0, res.err()
43+
rate_limit.wait_for_ready()
44+
45+
responses = client2.get_many("/get", 2)
46+
responses.assert_all(status_code=200)
47+
assert client2.get("/get").status_code == 429

0 commit comments

Comments
 (0)