From f6a46aea66823560a965988d8f416a8681362c16 Mon Sep 17 00:00:00 2001 From: phala Date: Wed, 6 Mar 2024 11:38:32 +0100 Subject: [PATCH 1/2] Move all backend classes into a backend module --- testsuite/backend/__init__.py | 15 +++++ testsuite/{openshift => backend}/httpbin.py | 6 +- testsuite/backend/mockserver.py | 69 +++++++++++++++++++++ testsuite/mockserver.py | 63 ------------------- testsuite/openshift/backend.py | 8 --- 5 files changed, 87 insertions(+), 74 deletions(-) create mode 100644 testsuite/backend/__init__.py rename testsuite/{openshift => backend}/httpbin.py (94%) create mode 100644 testsuite/backend/mockserver.py delete mode 100644 testsuite/openshift/backend.py diff --git a/testsuite/backend/__init__.py b/testsuite/backend/__init__.py new file mode 100644 index 00000000..df7fd49d --- /dev/null +++ b/testsuite/backend/__init__.py @@ -0,0 +1,15 @@ +"""Module containing all the Backends""" + +from abc import abstractmethod + +from testsuite.gateway import Referencable +from testsuite.lifecycle import LifecycleObject + + +class Backend(LifecycleObject, Referencable): + """Backend (workload) deployed in Kubernetes""" + + @property + @abstractmethod + def url(self): + """Returns internal URL for this backend""" diff --git a/testsuite/openshift/httpbin.py b/testsuite/backend/httpbin.py similarity index 94% rename from testsuite/openshift/httpbin.py rename to testsuite/backend/httpbin.py index c1349855..2c8e7ab6 100644 --- a/testsuite/openshift/httpbin.py +++ b/testsuite/backend/httpbin.py @@ -1,8 +1,8 @@ -"""Httpbin related classes""" +"""Httpbin implementation of Backend""" from functools import cached_property -from testsuite.openshift.backend import Backend +from testsuite.backend import Backend from testsuite.openshift import Selector from testsuite.openshift.client import OpenShiftClient from testsuite.openshift.deployment import Deployment @@ -10,7 +10,7 @@ class Httpbin(Backend): - """Httpbin deployed in OpenShift""" + """Httpbin deployed in Kubernetes as Backend""" def __init__(self, openshift: OpenShiftClient, name, label, replicas=1) -> None: super().__init__() diff --git a/testsuite/backend/mockserver.py b/testsuite/backend/mockserver.py new file mode 100644 index 00000000..c1324302 --- /dev/null +++ b/testsuite/backend/mockserver.py @@ -0,0 +1,69 @@ +"""Mockserver implementation as Backend""" + +from testsuite.backend import Backend +from testsuite.openshift import Selector +from testsuite.openshift.client import OpenShiftClient +from testsuite.openshift.deployment import Deployment, ContainerResources +from testsuite.openshift.service import Service, ServicePort + + +class MockserverBackend(Backend): + """Mockserver deployed as backend in Kubernetes""" + + PORT = 8080 + + def __init__(self, openshift: OpenShiftClient, name: str, label: str): + self.openshift = openshift + self.name = name + self.label = label + + self.deployment = None + self.service = None + + @property + def reference(self): + return { + "group": "", + "kind": "Service", + "port": self.PORT, + "name": self.name, + "namespace": self.openshift.project, + } + + @property + def url(self): + return f"{self.name}.{self.openshift.project}.svc.cluster.local" + + def commit(self): + match_labels = {"app": self.label, "deployment": self.name} + self.deployment = Deployment.create_instance( + self.openshift, + self.name, + container_name="mockserver", + image="quay.io/mganisin/mockserver:latest", + ports={"api": 1080}, + selector=Selector(matchLabels=match_labels), + labels={"app": self.label}, + resources=ContainerResources(limits_memory="2G"), + lifecycle={"postStart": {"exec": {"command": ["/bin/sh", "init-mockserver"]}}}, + ) + self.deployment.commit() + self.deployment.wait_for_ready() + + self.service = Service.create_instance( + self.openshift, + self.name, + selector=match_labels, + ports=[ServicePort(name="1080-tcp", port=self.PORT, targetPort="api")], + labels={"app": self.label}, + ) + self.service.commit() + + def delete(self): + with self.openshift.context: + if self.service: + self.service.delete() + self.service = None + if self.deployment: + self.deployment.delete() + self.deployment = None diff --git a/testsuite/mockserver.py b/testsuite/mockserver.py index 6c3087d1..212115a3 100644 --- a/testsuite/mockserver.py +++ b/testsuite/mockserver.py @@ -6,11 +6,6 @@ from testsuite.utils import ContentType from testsuite.httpx import KuadrantClient -from testsuite.openshift import Selector -from testsuite.openshift.backend import Backend -from testsuite.openshift.service import Service, ServicePort -from testsuite.openshift.deployment import Deployment, ContainerResources -from testsuite.openshift.client import OpenShiftClient class Mockserver: @@ -77,61 +72,3 @@ def retrieve_requests(self, expectation_id): params={"type": "REQUESTS", "format": "JSON"}, json={"path": "/" + expectation_id}, ).json() - - -class MockserverBackend(Backend): - """Mockserver deployed as backend in Openshift""" - - PORT = 8080 - - def __init__(self, openshift: OpenShiftClient, name: str, label: str): - self.openshift = openshift - self.name = name - self.label = label - - self.deployment = None - self.service = None - - @property - def reference(self): - return { - "group": "", - "kind": "Service", - "port": self.PORT, - "name": self.name, - "namespace": self.openshift.project, - } - - def commit(self): - match_labels = {"app": self.label, "deployment": self.name} - self.deployment = Deployment.create_instance( - self.openshift, - self.name, - container_name="mockserver", - image="quay.io/mganisin/mockserver:latest", - ports={"api": 1080}, - selector=Selector(matchLabels=match_labels), - labels={"app": self.label}, - resources=ContainerResources(limits_memory="2G"), - lifecycle={"postStart": {"exec": {"command": ["/bin/sh", "init-mockserver"]}}}, - ) - self.deployment.commit() - self.deployment.wait_for_ready() - - self.service = Service.create_instance( - self.openshift, - self.name, - selector=match_labels, - ports=[ServicePort(name="1080-tcp", port=self.PORT, targetPort="api")], - labels={"app": self.label}, - ) - self.service.commit() - - def delete(self): - with self.openshift.context: - if self.service: - self.service.delete() - self.service = None - if self.deployment: - self.deployment.delete() - self.deployment = None diff --git a/testsuite/openshift/backend.py b/testsuite/openshift/backend.py deleted file mode 100644 index 72244337..00000000 --- a/testsuite/openshift/backend.py +++ /dev/null @@ -1,8 +0,0 @@ -"""Backend for Openshift""" - -from testsuite.lifecycle import LifecycleObject -from testsuite.gateway import Referencable - - -class Backend(LifecycleObject, Referencable): - """Backend for Openshift""" From 9acb45c9cb1c90c93db9a957bf5b2fe2fb0bbe82 Mon Sep 17 00:00:00 2001 From: phala Date: Wed, 6 Mar 2024 11:40:03 +0100 Subject: [PATCH 2/2] Change all Httpbin references to Backend --- testsuite/gateway/__init__.py | 4 ++-- testsuite/gateway/envoy/config.py | 4 ++-- testsuite/gateway/envoy/route.py | 4 ++-- testsuite/gateway/gateway_api/route.py | 8 ++++---- testsuite/tests/conftest.py | 2 +- .../mgc/dnspolicy/health_check/test_additional_headers.py | 3 ++- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/testsuite/gateway/__init__.py b/testsuite/gateway/__init__.py index 94fc2895..0196dbc9 100644 --- a/testsuite/gateway/__init__.py +++ b/testsuite/gateway/__init__.py @@ -13,7 +13,7 @@ if TYPE_CHECKING: from testsuite.openshift.client import OpenShiftClient - from testsuite.openshift.httpbin import Httpbin + from testsuite.backend import Backend class Referencable(ABC): @@ -164,7 +164,7 @@ def remove_all_hostnames(self): """Remove all hostnames from the Route""" @abstractmethod - def add_backend(self, backend: "Httpbin", prefix): + def add_backend(self, backend: "Backend", prefix): """Adds another backend to the Route, with specific prefix""" @abstractmethod diff --git a/testsuite/gateway/envoy/config.py b/testsuite/gateway/envoy/config.py index 59dce3b3..4a57a9ae 100644 --- a/testsuite/gateway/envoy/config.py +++ b/testsuite/gateway/envoy/config.py @@ -2,7 +2,7 @@ import yaml -from testsuite.openshift.httpbin import Httpbin +from testsuite.backend import Backend from testsuite.openshift import modify from testsuite.openshift.config_map import ConfigMap @@ -108,7 +108,7 @@ def create_instance( ) @modify - def add_backend(self, backend: Httpbin, prefix: str): + def add_backend(self, backend: Backend, prefix: str): """Adds backend to the EnvoyConfig""" config = yaml.safe_load(self["envoy.yaml"]) config["static_resources"]["clusters"].append(yaml.safe_load(CLUSTER.replace("${backend_url}", backend.url))) diff --git a/testsuite/gateway/envoy/route.py b/testsuite/gateway/envoy/route.py index bb679ba3..43bd26bb 100644 --- a/testsuite/gateway/envoy/route.py +++ b/testsuite/gateway/envoy/route.py @@ -6,7 +6,7 @@ if typing.TYPE_CHECKING: from testsuite.openshift.client import OpenShiftClient - from testsuite.openshift.httpbin import Httpbin + from testsuite.backend import Backend from testsuite.policy.authorization.auth_config import AuthConfig @@ -28,7 +28,7 @@ def __init__(self, openshift, gateway) -> None: self.auth_configs: list["AuthConfig"] = [] self.hostnames: list[str] = [] - def add_backend(self, backend: "Httpbin", prefix="/"): + def add_backend(self, backend: "Backend", prefix="/"): self.gateway.config.add_backend(backend, prefix) self.gateway.rollout() diff --git a/testsuite/gateway/gateway_api/route.py b/testsuite/gateway/gateway_api/route.py index 83206d6c..42e5e88f 100644 --- a/testsuite/gateway/gateway_api/route.py +++ b/testsuite/gateway/gateway_api/route.py @@ -11,7 +11,7 @@ from testsuite.utils import asdict if typing.TYPE_CHECKING: - from testsuite.openshift.httpbin import Httpbin + from testsuite.backend import Backend class HTTPRoute(OpenShiftObject, GatewayRoute): @@ -74,9 +74,9 @@ def remove_all_hostnames(self): self.model.spec.hostnames = [] @modify - def add_rule(self, backend: "Httpbin", *route_matches: RouteMatch): + def add_rule(self, backend: "Backend", *route_matches: RouteMatch): """Adds rule to the Route""" - rules = {"backendRefs": [backend.reference]} + rules: dict[str, typing.Any] = {"backendRefs": [backend.reference]} matches = list(route_matches) if len(matches) == 0: matches.append(RouteMatch(path=PathMatch(type=MatchType.PATH_PREFIX, value="/"))) @@ -90,7 +90,7 @@ def remove_all_rules(self): self.model.spec.rules = [] @modify - def add_backend(self, backend: "Httpbin", prefix="/"): + def add_backend(self, backend: "Backend", prefix="/"): self.model.spec.rules.append( {"backendRefs": [backend.reference], "matches": [{"path": {"value": prefix, "type": "PathPrefix"}}]} ) diff --git a/testsuite/tests/conftest.py b/testsuite/tests/conftest.py index 83e06199..a094c85e 100644 --- a/testsuite/tests/conftest.py +++ b/testsuite/tests/conftest.py @@ -14,7 +14,7 @@ from testsuite.gateway import Gateway, GatewayRoute, Hostname, Exposer from testsuite.oidc import OIDCProvider from testsuite.oidc.auth0 import Auth0Provider -from testsuite.openshift.httpbin import Httpbin +from testsuite.backend.httpbin import Httpbin from testsuite.oidc.rhsso import RHSSO from testsuite.gateway.envoy import Envoy from testsuite.gateway.envoy.route import EnvoyVirtualRoute diff --git a/testsuite/tests/mgc/dnspolicy/health_check/test_additional_headers.py b/testsuite/tests/mgc/dnspolicy/health_check/test_additional_headers.py index d2bb74a7..0b161815 100644 --- a/testsuite/tests/mgc/dnspolicy/health_check/test_additional_headers.py +++ b/testsuite/tests/mgc/dnspolicy/health_check/test_additional_headers.py @@ -3,7 +3,8 @@ import pytest from testsuite.openshift.secret import Secret -from testsuite.mockserver import Mockserver, MockserverBackend +from testsuite.mockserver import Mockserver +from testsuite.backend.mockserver import MockserverBackend from testsuite.policy.dns_policy import HealthCheck, AdditionalHeadersRef pytestmark = [pytest.mark.mgc]