Skip to content

Commit

Permalink
Merge pull request Kuadrant#364 from pehala/session_gw
Browse files Browse the repository at this point in the history
Make gateway fixture session scope
  • Loading branch information
averevki authored Apr 10, 2024
2 parents 7e72827 + e05af09 commit ee481e4
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 62 deletions.
11 changes: 11 additions & 0 deletions testsuite/gateway/envoy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,17 @@ def create_instance(
labels,
)

def has_backend(self, backend: Backend, prefix: str) -> bool:
"""Returns True, if backend & prefix combination is already setup"""
config = yaml.safe_load(self["envoy.yaml"])
virtual_hosts = config["static_resources"]["listeners"][0]["filter_chains"][0]["filters"][0]["typed_config"][
"route_config"
]["virtual_hosts"][0]["routes"]
for host in virtual_hosts:
if host["match"]["prefix"] == prefix and host["route"]["cluster"] == backend.url:
return True
return False

@modify
def add_backend(self, backend: Backend, prefix: str):
"""Adds backend to the EnvoyConfig"""
Expand Down
5 changes: 3 additions & 2 deletions testsuite/gateway/envoy/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ def __init__(self, openshift, gateway) -> None:
self.hostnames: list[str] = []

def add_backend(self, backend: "Backend", prefix="/"):
self.gateway.config.add_backend(backend, prefix)
self.gateway.rollout()
if not self.gateway.config.has_backend(backend, prefix):
self.gateway.config.add_backend(backend, prefix)
self.gateway.rollout()

def remove_all_backend(self):
self.gateway.config.remove_all_backends()
Expand Down
23 changes: 12 additions & 11 deletions testsuite/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def testconfig():
return settings


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def openshift(testconfig):
"""OpenShift client for the primary namespace"""
client = testconfig["service_protection"]["project"]
Expand All @@ -137,7 +137,7 @@ def openshift(testconfig):
return client


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def openshift2(testconfig, skip_or_fail):
"""OpenShift client for the secondary namespace located on the same cluster as primary Openshift"""
client = testconfig["service_protection"]["project2"]
Expand Down Expand Up @@ -265,7 +265,7 @@ def module_label(label):
return randomize(label)


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def kuadrant(request):
"""Returns Kuadrant instance if exists, or None"""
if request.config.getoption("--standalone"):
Expand All @@ -275,7 +275,7 @@ def kuadrant(request):
return True


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def backend(request, openshift, blame, label):
"""Deploys Httpbin backend"""
httpbin = Httpbin(openshift, blame("httpbin"), label)
Expand All @@ -284,22 +284,23 @@ def backend(request, openshift, blame, label):
return httpbin


@pytest.fixture(scope="module")
def gateway(request, kuadrant, openshift, blame, backend, module_label, testconfig, wildcard_domain) -> Gateway:
"""Deploys Envoy that wire up the Backend behind the reverse-proxy and Authorino instance"""
@pytest.fixture(scope="session")
def gateway(request, kuadrant, openshift, blame, label, testconfig, wildcard_domain) -> Gateway:
"""Deploys Gateway that wires up the Backend behind the reverse-proxy and Authorino instance"""
if kuadrant:
gw = KuadrantGateway.create_instance(openshift, blame("gw"), wildcard_domain, {"app": module_label})
gw = KuadrantGateway.create_instance(openshift, blame("gw"), wildcard_domain, {"app": label})
else:
authorino = request.getfixturevalue("authorino")
gw = Envoy(
openshift,
blame("gw"),
authorino,
testconfig["service_protection"]["envoy"]["image"],
labels={"app": module_label},
labels={"app": label},
)
request.addfinalizer(gw.delete)
gw.commit()
gw.wait_for_ready(timeout=10 * 60)
return gw


Expand All @@ -317,7 +318,7 @@ def route(request, kuadrant, gateway, blame, hostname, backend, module_label) ->
return route


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def exposer(request) -> Exposer:
"""Exposer object instance"""
exposer = OpenShiftExposer()
Expand All @@ -333,7 +334,7 @@ def hostname(gateway, exposer, blame) -> Hostname:
return hostname


@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def wildcard_domain(openshift):
"""
Wildcard domain of openshift cluster
Expand Down
32 changes: 8 additions & 24 deletions testsuite/tests/kuadrant/authorino/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,39 @@
from testsuite.openshift.authorino import AuthorinoCR, Authorino, PreexistingAuthorino


@pytest.fixture(scope="module")
def authorino_parameters():
"""Optional parameters for Authorino creation, passed to the __init__"""
return {}


@pytest.fixture(scope="module")
def authorino(authorino, openshift, blame, request, testconfig, module_label, authorino_parameters) -> Authorino:
@pytest.fixture(scope="session")
def authorino(authorino, openshift, blame, request, testconfig, label) -> Authorino:
"""Authorino instance"""
if authorino:
return authorino

authorino_config = testconfig["service_protection"]["authorino"]
if not authorino_config["deploy"]:
if len(authorino_parameters) > 0:
return pytest.skip("Can't change parameters of already deployed Authorino")
return PreexistingAuthorino(
authorino_config["auth_url"],
authorino_config["oidc_url"],
authorino_config["metrics_service_name"],
)

labels = authorino_parameters.setdefault("label_selectors", [])
labels.append(f"testRun={module_label}")

authorino_parameters.setdefault("name", blame("authorino"))

authorino = AuthorinoCR.create_instance(
openshift,
image=authorino_config.get("image"),
log_level=authorino_config.get("log_level"),
**authorino_parameters,
name=blame("authorino"),
label_selectors=[f"testRun={label}"],
)
request.addfinalizer(lambda: authorino.delete(ignore_not_found=True))
request.addfinalizer(authorino.delete)
authorino.commit()
authorino.wait_for_ready()
return authorino


# pylint: disable=unused-argument
@pytest.fixture(scope="module")
def authorization(
authorization, oidc_provider, authorino, route, authorization_name, openshift, module_label
) -> AuthConfig:
"""In case of Authorino, AuthConfig used for authorization"""
def authorization(authorization, oidc_provider, route, authorization_name, openshift, label) -> AuthConfig:
"""In case of Authorino, AuthC onfig used for authorization"""
if authorization is None:
authorization = AuthConfig.create_instance(
openshift, authorization_name, route, labels={"testRun": module_label}
)
authorization = AuthConfig.create_instance(openshift, authorization_name, route, labels={"testRun": label})
authorization.identity.add_oidc("rhsso", oidc_provider.well_known["issuer"])
return authorization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def route2(request, gateway, blame, hostname2):


@pytest.fixture(scope="module")
def authorization2(route2, blame, openshift2, module_label, oidc_provider):
def authorization2(route2, blame, openshift2, label, oidc_provider):
"""Second valid hostname"""
auth = AuthConfig.create_instance(openshift2, blame("ac"), route2, labels={"testRun": module_label})
auth = AuthConfig.create_instance(openshift2, blame("ac"), route2, labels={"testRun": label})
auth.identity.add_oidc("rhsso", oidc_provider.well_known["issuer"])
return auth

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ def route(route, wildcard_domain, hostname):

# pylint: disable = unused-argument
@pytest.fixture(scope="module")
def authorization(authorino, blame, route, openshift, module_label, gateway):
def authorization(authorino, blame, route, openshift, label, gateway):
"""Create AuthConfig with host set to wildcard_domain"""
auth = AuthConfig.create_instance(openshift, blame("ac"), route, labels={"testRun": module_label})
auth = AuthConfig.create_instance(openshift, blame("ac"), route, labels={"testRun": label})
auth.responses.add_success_header("header", JsonResponse({"anything": Value("one")}))
return auth


# pylint: disable = unused-argument
@pytest.fixture(scope="module")
def authorization2(authorino, blame, route, openshift2, module_label, gateway):
def authorization2(authorino, blame, route, openshift2, label, gateway):
"""Create AuthConfig with host set to wildcard_domain in another project"""
auth = AuthConfig.create_instance(openshift2, blame("ac"), route, labels={"testRun": module_label})
auth = AuthConfig.create_instance(openshift2, blame("ac"), route, labels={"testRun": label})
auth.responses.add_success_header("header", JsonResponse({"anything": Value("two")}))
return auth

Expand Down
52 changes: 52 additions & 0 deletions testsuite/tests/kuadrant/authorino/operator/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Conftest for all tests requiring direct access to Authorino Operator"""

import pytest

from testsuite.gateway.envoy import Envoy
from testsuite.openshift.authorino import AuthorinoCR, Authorino


@pytest.fixture(scope="module")
def gateway(request, authorino, openshift, blame, label, testconfig) -> Envoy:
"""Deploys Envoy that wires up the Backend behind the reverse-proxy and Authorino instance"""
gw = Envoy(
openshift,
blame("gw"),
authorino,
testconfig["service_protection"]["envoy"]["image"],
labels={"app": label},
)
request.addfinalizer(gw.delete)
gw.commit()
gw.wait_for_ready(timeout=10 * 60)
return gw


@pytest.fixture(scope="module")
def authorino_parameters():
"""Optional parameters for Authorino creation, passed to the __init__"""
return {}


@pytest.fixture(scope="module")
def authorino(openshift, blame, request, testconfig, label, authorino_parameters) -> Authorino:
"""Module scoped Authorino instance, with specific parameters"""
authorino_config = testconfig["service_protection"]["authorino"]
if not authorino_config["deploy"]:
return pytest.skip("Can't change parameters of already deployed Authorino")

labels = authorino_parameters.setdefault("label_selectors", [])
labels.append(f"testRun={label}")

authorino_parameters.setdefault("name", blame("authorino"))

authorino = AuthorinoCR.create_instance(
openshift,
image=authorino_config.get("image"),
log_level=authorino_config.get("log_level"),
**authorino_parameters,
)
request.addfinalizer(authorino.delete)
authorino.commit()
authorino.wait_for_ready()
return authorino
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ def _route(hostname, gateway):

# pylint: disable=unused-argument
@pytest.fixture(scope="module")
def setup_authorization(request, blame, openshift, module_label):
def setup_authorization(request, blame, openshift, label):
"""Factory method for creating AuthConfigs in the test run"""

def _authorization(route, sharding_label=None):
auth = AuthConfig.create_instance(
openshift,
blame("ac"),
route,
labels={"testRun": module_label, "sharding": sharding_label},
labels={"testRun": label, "sharding": sharding_label},
)
auth.responses.add_success_header("header", JsonResponse({"anything": Value(sharding_label)}))
request.addfinalizer(auth.delete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@


@pytest.fixture(scope="module")
def setup_authorino(openshift, blame, testconfig, module_label, request):
def setup_authorino(openshift, blame, testconfig, label, request):
"""Authorino instance"""

def _authorino(sharding_label):
authorino = AuthorinoCR.create_instance(
openshift,
blame("authorino"),
image=weakget(testconfig)["authorino"]["image"] % None,
label_selectors=[f"sharding={sharding_label}", f"testRun={module_label}"],
label_selectors=[f"sharding={sharding_label}", f"testRun={label}"],
)
request.addfinalizer(authorino.delete)
authorino.commit()
Expand Down
4 changes: 2 additions & 2 deletions testsuite/tests/kuadrant/authorino/operator/test_wildcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ def route(route, wildcard_domain, hostname):


@pytest.fixture(scope="module")
def authorization(blame, route, openshift, module_label):
def authorization(blame, route, openshift, label):
"""In case of Authorino, AuthConfig used for authorization"""
return AuthConfig.create_instance(openshift, blame("ac"), route, labels={"testRun": module_label})
return AuthConfig.create_instance(openshift, blame("ac"), route, labels={"testRun": label})


def test_wildcard(client):
Expand Down
4 changes: 2 additions & 2 deletions testsuite/tests/kuadrant/authorino/wristband/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def wristband_authorization(
wristband_name,
oidc_provider,
wristband_hostname,
module_label,
label,
wristband_endpoint,
wristband_secret,
):
Expand All @@ -112,7 +112,7 @@ def wristband_authorization(
gateway.openshift,
wristband_name,
route,
labels={"testRun": module_label},
labels={"testRun": label},
)

authorization.identity.add_oidc("rhsso", oidc_provider.well_known["issuer"])
Expand Down
6 changes: 3 additions & 3 deletions testsuite/tests/kuadrant/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


# pylint: disable=unused-argument
@pytest.fixture(scope="module")
@pytest.fixture(scope="session")
def authorino(kuadrant):
"""Authorino instance when configured through Kuadrant"""
if kuadrant:
Expand All @@ -24,10 +24,10 @@ def authorization_name(blame):


@pytest.fixture(scope="module")
def authorization(authorino, kuadrant, oidc_provider, route, authorization_name, openshift, module_label):
def authorization(kuadrant, oidc_provider, route, authorization_name, openshift, label):
"""Authorization object (In case of Kuadrant AuthPolicy)"""
if kuadrant:
return AuthPolicy.create_instance(openshift, authorization_name, route, labels={"testRun": module_label})
return AuthPolicy.create_instance(openshift, authorization_name, route, labels={"testRun": label})
return None


Expand Down
16 changes: 8 additions & 8 deletions testsuite/tests/kuadrant/gateway/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

import pytest

from testsuite.gateway.gateway_api.gateway import KuadrantGateway
from testsuite.httpx.auth import HttpxOidcClientAuth
from testsuite.policy.authorization.auth_policy import AuthPolicy


@pytest.fixture(scope="module")
def gateway_ready(gateway):
def gateway(request, openshift, blame, wildcard_domain, module_label):
"""Returns ready gateway"""
gateway.wait_for_ready()
return gateway
gw = KuadrantGateway.create_instance(openshift, blame("gw"), wildcard_domain, {"app": module_label})
request.addfinalizer(gw.delete)
gw.commit()
gw.wait_for_ready(timeout=10 * 60)
return gw


@pytest.fixture(scope="module")
def authorization(gateway_ready, route, oidc_provider, authorization_name, openshift, module_label):
def authorization(authorization, oidc_provider):
# pylint: disable=unused-argument
"""Create AuthPolicy attached to gateway"""
authorization = AuthPolicy.create_instance(
openshift, authorization_name, gateway_ready, labels={"testRun": module_label}
)
authorization.identity.add_oidc("rhsso", oidc_provider.well_known["issuer"])
return authorization

Expand Down

0 comments on commit ee481e4

Please sign in to comment.