Skip to content

Commit

Permalink
Updates made
Browse files Browse the repository at this point in the history
  • Loading branch information
emmaaroche committed Jan 31, 2025
1 parent 5edc14b commit b807697
Showing 1 changed file with 84 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,87 @@

import pytest

from testsuite.gateway import TLSGatewayListener, GatewayRoute
from testsuite.gateway.envoy.route import EnvoyVirtualRoute
from testsuite.backend.httpbin import Httpbin
from testsuite.gateway import GatewayRoute, GatewayListener, Hostname
from testsuite.gateway.gateway_api.gateway import KuadrantGateway
from testsuite.gateway.gateway_api.route import HTTPRoute
from testsuite.kuadrant.policy.dns import DNSPolicy
from testsuite.kuadrant.policy.rate_limit import Limit, RateLimitPolicy

pytestmark = [pytest.mark.kuadrant_only]


@pytest.fixture(scope="module")
def wildcard_domain2(base_domain):
"""Wildcard domain for Gateway B"""
return f"*.{base_domain}-b"
return f"*.{base_domain}"


@pytest.fixture(scope="module")
def gateway(request, kuadrant, cluster, blame, wildcard_domain, module_label): # pylint: disable=unused-argument
"""Create and configure Gateway A"""
# Added this gateway because I couldn't get the tests to pass or work as -
# expected using the existing conftest version
gw = KuadrantGateway.create_instance(cluster, blame("gw"), {"app": module_label})
gw.add_listener(GatewayListener(hostname=wildcard_domain))
request.addfinalizer(gw.delete)
gw.commit()
gw.wait_for_ready()
return gw


@pytest.fixture(scope="module")
def gateway_b(request, cluster, blame, wildcard_domain2, module_label): # pylint: disable=unused-argument
def gateway_b(request, kuadrant, cluster, blame, wildcard_domain2, module_label): # pylint: disable=unused-argument
"""Create and configure Gateway B"""
gateway_name = blame("gw-b")
gw = KuadrantGateway.create_instance(cluster, gateway_name, {"app": module_label})
gw.add_listener(TLSGatewayListener(hostname=wildcard_domain2, gateway_name=gateway_name))
gw = KuadrantGateway.create_instance(cluster, blame("gw-b"), {"app": module_label})
gw.add_listener(GatewayListener(hostname=wildcard_domain2))
request.addfinalizer(gw.delete)
gw.commit()
gw.wait_for_ready()
return gw


@pytest.fixture(scope="module")
def route_b(request, kuadrant, wildcard_domain2, gateway_b, blame, backend, module_label) -> GatewayRoute:
def hostname_b(gateway_b, exposer, blame) -> Hostname:
"""Expose Hostname for Gateway B"""
hostname = exposer.expose_hostname(blame("hostname-b"), gateway_b)
return hostname


@pytest.fixture(scope="session")
def backend_b(request, cluster, blame, label, testconfig): # pylint: disable=unused-argument
"""Deploys Httpbin backend"""
# Added to resolve backend errors I was seeing in Gateway B / Route B YAML files
image = testconfig["httpbin"]["image"]
httpbin = Httpbin(cluster, blame("httpbin"), label, image)
httpbin.commit()
return httpbin


@pytest.fixture(scope="module")
def route_b(
request, gateway_b, blame, hostname_b, module_label, backend_b
) -> GatewayRoute: # pylint: disable=unused-argument
"""Create and configure Route B"""
if kuadrant:
route = HTTPRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b, {"app": module_label})
else:
route = EnvoyVirtualRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b)
route.add_hostname(wildcard_domain2)
route.add_backend(backend)
route = HTTPRoute.create_instance(gateway_b.cluster, blame("route-b"), gateway_b, {"app": module_label})
route.add_hostname(hostname_b.hostname)
route.add_backend(backend_b)
request.addfinalizer(route.delete)
route.commit()
route.wait_for_ready()
return route


@pytest.fixture(scope="module")
def rate_limit_policy(request, cluster, blame, module_label, gateway):
def client_b(route_b, hostname_b): # pylint: disable=unused-argument
"""Returns httpx client for Gateway B"""
client = hostname_b.client()
yield client
client.close()


@pytest.fixture(scope="module")
def rate_limit_policy(request, cluster, blame, module_label, gateway, route_b): # pylint: disable=unused-argument
"""RateLimitPolicy for testing"""
policy = RateLimitPolicy.create_instance(cluster, blame("limit"), gateway, labels={"testRun": module_label})
policy.add_limit("basic", [Limit(5, "10s")])
Expand All @@ -53,65 +93,55 @@ def rate_limit_policy(request, cluster, blame, module_label, gateway):
return policy


@pytest.fixture(scope="module")
def dns_policy_b(blame, gateway_b, module_label, dns_provider_secret, request):
"""DNSPolicy fixture for Gateway B"""
# Added to resolve DNS errors I was seeing in Gateway B / Route B YAML files
policy = DNSPolicy.create_instance(
gateway_b.cluster, blame("dns-b"), gateway_b, dns_provider_secret, labels={"app": module_label}
)
request.addfinalizer(policy.delete)
policy.commit()
policy.wait_for_ready()
return policy


def test_update_ratelimit_policy_target_ref(
gateway, gateway_b, rate_limit_policy, client, auth, route_b
gateway, gateway_b, rate_limit_policy, client, client_b, auth, blame, dns_policy_b
): # pylint: disable=unused-argument
"""Test updating the targetRef of a RateLimitPolicy from Gateway A to Gateway B"""
initial_target_ref = rate_limit_policy.model["spec"]["targetRef"]["name"]
assert (
initial_target_ref == gateway.model.metadata.name
), f"Initial targetRef mismatch: expected {gateway.model.metadata.name}, got {initial_target_ref}"

response = client.get("/get", auth=auth)
assert response.status_code == 200
# Kept auth=auth as removing it results in an 'HTTP 401 Unauthorized' error
responses = client.get_many("/get", 5, auth=auth)
responses.assert_all(status_code=200)
assert client.get("/get", auth=auth).status_code == 429

rate_limit_policy.wait_for_ready()
rate_limit_policy.refresh()

rate_limit_policy.model["spec"]["targetRef"]["name"] = gateway_b.model.metadata.name
rate_limit_policy.refresh().model.spec.targetRef.name = gateway_b.model.metadata.name
res = rate_limit_policy.apply()
assert res.status() == 0, res.err()
rate_limit_policy.wait_for_ready()

rate_limit_policy.refresh()
updated_target_ref = rate_limit_policy.model["spec"]["targetRef"]["name"]
assert (
updated_target_ref == gateway_b.model.metadata.name
), f"Updated targetRef mismatch: expected {gateway_b.model.metadata.name}, got {updated_target_ref}"

response = client.get("/get", auth=auth)
assert response.status_code == 200
responses = client_b.get_many("/get", 5, auth=auth)
responses.assert_all(status_code=200)
assert client_b.get("/get", auth=auth).status_code == 429


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

initial_target_ref = authorization.model["spec"]["targetRef"]["name"]
assert (
initial_target_ref == gateway.model.metadata.name
), f"Initial targetRef mismatch: expected {gateway.model.metadata.name}, got {initial_target_ref}"

response = client.get("/get", auth=auth)
assert response.status_code == 200

authorization.wait_for_ready()
authorization.refresh()

authorization.model["spec"]["targetRef"] = gateway_b.reference
authorization.refresh().model.spec.targetRef = gateway_b.reference
res = authorization.apply()
assert res.status() == 0, res.err()
authorization.wait_for_ready()

authorization.refresh()
updated_target_ref = authorization.model["spec"]["targetRef"]["name"]
assert (
updated_target_ref == gateway_b.model.metadata.name
), f"Updated targetRef mismatch: expected {gateway_b.model.metadata.name}, got {updated_target_ref}"

response = client.get("/get", auth=auth)
response = client_b.get("/get", auth=auth)
assert response.status_code == 200

0 comments on commit b807697

Please sign in to comment.