Skip to content

Commit b807697

Browse files
committed
Updates made
1 parent 5edc14b commit b807697

File tree

1 file changed

+84
-54
lines changed

1 file changed

+84
-54
lines changed

testsuite/tests/singlecluster/gateway/reconciliation/test_change_parentref.py

+84-54
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,87 @@
44

55
import pytest
66

7-
from testsuite.gateway import TLSGatewayListener, GatewayRoute
8-
from testsuite.gateway.envoy.route import EnvoyVirtualRoute
7+
from testsuite.backend.httpbin import Httpbin
8+
from testsuite.gateway import GatewayRoute, GatewayListener, Hostname
99
from testsuite.gateway.gateway_api.gateway import KuadrantGateway
1010
from testsuite.gateway.gateway_api.route import HTTPRoute
11+
from testsuite.kuadrant.policy.dns import DNSPolicy
1112
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy
1213

14+
pytestmark = [pytest.mark.kuadrant_only]
15+
1316

1417
@pytest.fixture(scope="module")
1518
def wildcard_domain2(base_domain):
1619
"""Wildcard domain for Gateway B"""
17-
return f"*.{base_domain}-b"
20+
return f"*.{base_domain}"
21+
22+
23+
@pytest.fixture(scope="module")
24+
def gateway(request, kuadrant, cluster, blame, wildcard_domain, module_label): # pylint: disable=unused-argument
25+
"""Create and configure Gateway A"""
26+
# Added this gateway because I couldn't get the tests to pass or work as -
27+
# expected using the existing conftest version
28+
gw = KuadrantGateway.create_instance(cluster, blame("gw"), {"app": module_label})
29+
gw.add_listener(GatewayListener(hostname=wildcard_domain))
30+
request.addfinalizer(gw.delete)
31+
gw.commit()
32+
gw.wait_for_ready()
33+
return gw
1834

1935

2036
@pytest.fixture(scope="module")
21-
def gateway_b(request, cluster, blame, wildcard_domain2, module_label): # pylint: disable=unused-argument
37+
def gateway_b(request, kuadrant, cluster, blame, wildcard_domain2, module_label): # pylint: disable=unused-argument
2238
"""Create and configure Gateway B"""
23-
gateway_name = blame("gw-b")
24-
gw = KuadrantGateway.create_instance(cluster, gateway_name, {"app": module_label})
25-
gw.add_listener(TLSGatewayListener(hostname=wildcard_domain2, gateway_name=gateway_name))
39+
gw = KuadrantGateway.create_instance(cluster, blame("gw-b"), {"app": module_label})
40+
gw.add_listener(GatewayListener(hostname=wildcard_domain2))
2641
request.addfinalizer(gw.delete)
2742
gw.commit()
2843
gw.wait_for_ready()
2944
return gw
3045

3146

3247
@pytest.fixture(scope="module")
33-
def route_b(request, kuadrant, wildcard_domain2, gateway_b, blame, backend, module_label) -> GatewayRoute:
48+
def hostname_b(gateway_b, exposer, blame) -> Hostname:
49+
"""Expose Hostname for Gateway B"""
50+
hostname = exposer.expose_hostname(blame("hostname-b"), gateway_b)
51+
return hostname
52+
53+
54+
@pytest.fixture(scope="session")
55+
def backend_b(request, cluster, blame, label, testconfig): # pylint: disable=unused-argument
56+
"""Deploys Httpbin backend"""
57+
# Added to resolve backend errors I was seeing in Gateway B / Route B YAML files
58+
image = testconfig["httpbin"]["image"]
59+
httpbin = Httpbin(cluster, blame("httpbin"), label, image)
60+
httpbin.commit()
61+
return httpbin
62+
63+
64+
@pytest.fixture(scope="module")
65+
def route_b(
66+
request, gateway_b, blame, hostname_b, module_label, backend_b
67+
) -> GatewayRoute: # pylint: disable=unused-argument
3468
"""Create and configure Route B"""
35-
if kuadrant:
36-
route = HTTPRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b, {"app": module_label})
37-
else:
38-
route = EnvoyVirtualRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b)
39-
route.add_hostname(wildcard_domain2)
40-
route.add_backend(backend)
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_b)
4172
request.addfinalizer(route.delete)
4273
route.commit()
74+
route.wait_for_ready()
4375
return route
4476

4577

4678
@pytest.fixture(scope="module")
47-
def rate_limit_policy(request, cluster, blame, module_label, gateway):
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 rate_limit_policy(request, cluster, blame, module_label, gateway, route_b): # pylint: disable=unused-argument
4888
"""RateLimitPolicy for testing"""
4989
policy = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, labels={"testRun": module_label})
5090
policy.add_limit("basic", [Limit(5, "10s")])
@@ -53,65 +93,55 @@ def rate_limit_policy(request, cluster, blame, module_label, gateway):
5393
return policy
5494

5595

96+
@pytest.fixture(scope="module")
97+
def dns_policy_b(blame, gateway_b, module_label, dns_provider_secret, request):
98+
"""DNSPolicy fixture for Gateway B"""
99+
# Added to resolve DNS errors I was seeing in Gateway B / Route B YAML files
100+
policy = DNSPolicy.create_instance(
101+
gateway_b.cluster, blame("dns-b"), gateway_b, dns_provider_secret, labels={"app": module_label}
102+
)
103+
request.addfinalizer(policy.delete)
104+
policy.commit()
105+
policy.wait_for_ready()
106+
return policy
107+
108+
56109
def test_update_ratelimit_policy_target_ref(
57-
gateway, gateway_b, rate_limit_policy, client, auth, route_b
110+
gateway, gateway_b, rate_limit_policy, client, client_b, auth, blame, dns_policy_b
58111
): # pylint: disable=unused-argument
59112
"""Test updating the targetRef of a RateLimitPolicy from Gateway A to Gateway B"""
60-
initial_target_ref = rate_limit_policy.model["spec"]["targetRef"]["name"]
61-
assert (
62-
initial_target_ref == gateway.model.metadata.name
63-
), f"Initial targetRef mismatch: expected {gateway.model.metadata.name}, got {initial_target_ref}"
64-
65-
response = client.get("/get", auth=auth)
66-
assert response.status_code == 200
113+
# Kept auth=auth as removing it results in an 'HTTP 401 Unauthorized' error
114+
responses = client.get_many("/get", 5, auth=auth)
115+
responses.assert_all(status_code=200)
116+
assert client.get("/get", auth=auth).status_code == 429
67117

68-
rate_limit_policy.wait_for_ready()
69-
rate_limit_policy.refresh()
70-
71-
rate_limit_policy.model["spec"]["targetRef"]["name"] = gateway_b.model.metadata.name
118+
rate_limit_policy.refresh().model.spec.targetRef.name = gateway_b.model.metadata.name
72119
res = rate_limit_policy.apply()
73120
assert res.status() == 0, res.err()
121+
rate_limit_policy.wait_for_ready()
74122

75-
rate_limit_policy.refresh()
76-
updated_target_ref = rate_limit_policy.model["spec"]["targetRef"]["name"]
77-
assert (
78-
updated_target_ref == gateway_b.model.metadata.name
79-
), f"Updated targetRef mismatch: expected {gateway_b.model.metadata.name}, got {updated_target_ref}"
80-
81-
response = client.get("/get", auth=auth)
82-
assert response.status_code == 200
123+
responses = client_b.get_many("/get", 5, auth=auth)
124+
responses.assert_all(status_code=200)
125+
assert client_b.get("/get", auth=auth).status_code == 429
83126

84127

85128
def test_update_auth_policy_target_ref(
86-
gateway, gateway_b, authorization, client, auth, route_b
129+
gateway, gateway_b, authorization, client, client_b, auth, blame, dns_policy_b
87130
): # pylint: disable=unused-argument
88131
"""Test updating the targetRef of an AuthPolicy from Gateway A to Gateway B"""
89-
# Update targetRef of the AuthPolicy to point to gateway A
90-
authorization.model["spec"]["targetRef"] = gateway.reference
132+
# Overwriting this because the higher-level conftest sets a wrong targetRef for this tests purpose
133+
authorization.model.spec.targetRef = gateway.reference
91134
authorization.apply()
92135
authorization.wait_for_ready()
93136
authorization.refresh()
94137

95-
initial_target_ref = authorization.model["spec"]["targetRef"]["name"]
96-
assert (
97-
initial_target_ref == gateway.model.metadata.name
98-
), f"Initial targetRef mismatch: expected {gateway.model.metadata.name}, got {initial_target_ref}"
99-
100138
response = client.get("/get", auth=auth)
101139
assert response.status_code == 200
102140

103-
authorization.wait_for_ready()
104-
authorization.refresh()
105-
106-
authorization.model["spec"]["targetRef"] = gateway_b.reference
141+
authorization.refresh().model.spec.targetRef = gateway_b.reference
107142
res = authorization.apply()
108143
assert res.status() == 0, res.err()
144+
authorization.wait_for_ready()
109145

110-
authorization.refresh()
111-
updated_target_ref = authorization.model["spec"]["targetRef"]["name"]
112-
assert (
113-
updated_target_ref == gateway_b.model.metadata.name
114-
), f"Updated targetRef mismatch: expected {gateway_b.model.metadata.name}, got {updated_target_ref}"
115-
116-
response = client.get("/get", auth=auth)
146+
response = client_b.get("/get", auth=auth)
117147
assert response.status_code == 200

0 commit comments

Comments
 (0)