diff --git a/testsuite/openshift/kuadrant.py b/testsuite/openshift/kuadrant.py new file mode 100644 index 00000000..01e8ea4e --- /dev/null +++ b/testsuite/openshift/kuadrant.py @@ -0,0 +1,29 @@ +"""Kuadrant CR object""" + +from openshift_client import selector + +from testsuite.openshift import OpenShiftObject, modify +from testsuite.openshift.deployment import Deployment + + +class KuadrantCR(OpenShiftObject): + """Represents Kuadrant CR objects""" + + LIMITADOR = "limitador-limitador" + + @property + def limitador(self) -> dict: + """Returns spec.limitador from Kuadrant object""" + return self.model.spec.setdefault("limitador", {}) + + @limitador.setter + @modify + def limitador(self, value: dict): + """Sets the value of spec.limitador""" + self.model.spec["limitador"] = value + + @property + def limitador_deployment(self): + """Returns Deployment object for this Authorino""" + with self.context: + return selector(f"deployment/{self.LIMITADOR}").object(cls=Deployment) diff --git a/testsuite/tests/conftest.py b/testsuite/tests/conftest.py index 9bb70996..71ac0032 100644 --- a/testsuite/tests/conftest.py +++ b/testsuite/tests/conftest.py @@ -6,22 +6,24 @@ import pytest from dynaconf import ValidationError from keycloak import KeycloakAuthenticationError +from openshift_client import selector, OpenShiftPythonException +from testsuite.backend.httpbin import Httpbin from testsuite.capabilities import has_kuadrant from testsuite.certificates import CFSSLClient from testsuite.config import settings -from testsuite.httpx import KuadrantClient -from testsuite.mockserver import Mockserver -from testsuite.tracing import TracingClient from testsuite.gateway import Gateway, GatewayRoute, Hostname, Exposer -from testsuite.oidc import OIDCProvider -from testsuite.oidc.auth0 import Auth0Provider -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 from testsuite.gateway.gateway_api.gateway import KuadrantGateway from testsuite.gateway.gateway_api.route import HTTPRoute +from testsuite.httpx import KuadrantClient +from testsuite.mockserver import Mockserver +from testsuite.oidc import OIDCProvider +from testsuite.oidc.auth0 import Auth0Provider +from testsuite.oidc.rhsso import RHSSO +from testsuite.openshift.kuadrant import KuadrantCR +from testsuite.tracing import TracingClient from testsuite.utils import randomize, _whoami @@ -262,13 +264,23 @@ def module_label(label): @pytest.fixture(scope="session") -def kuadrant(request): +def kuadrant(request, testconfig): """Returns Kuadrant instance if exists, or None""" if request.config.getoption("--standalone"): return None - # TODO: Return actual Kuadrant object - return True + ocp = settings["service_protection"]["project"] + project = settings["service_protection"]["system_project"] + kuadrant_openshift = ocp.change_project(project) + + try: + with kuadrant_openshift.context: + kuadrant = selector("kuadrant").object(cls=KuadrantCR) + kuadrant.committed = True + except OpenShiftPythonException: + pytest.fail("Running Kuadrant tests, but Kuadrant resource was not found") + + return kuadrant @pytest.fixture(scope="session") diff --git a/testsuite/tests/kuadrant/reconciliation/cr_configuration/__init__.py b/testsuite/tests/kuadrant/reconciliation/cr_configuration/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/testsuite/tests/kuadrant/reconciliation/cr_configuration/test_reconcile_limitador.py b/testsuite/tests/kuadrant/reconciliation/cr_configuration/test_reconcile_limitador.py new file mode 100644 index 00000000..045261d6 --- /dev/null +++ b/testsuite/tests/kuadrant/reconciliation/cr_configuration/test_reconcile_limitador.py @@ -0,0 +1,63 @@ +"""Tests for Kuadrant sub component - Limitador CR configuration via Kuadrant CR""" + +import pytest + +pytestmark = [pytest.mark.kuadrant_only] + + +@pytest.fixture() +def commit(): + """Omitting authorization and rate limiting as it is not needed""" + + +@pytest.fixture(autouse=True) +def kuadrant_clean_up(request, kuadrant): + """ + Return fields to default values. + This can be simplified once https://github.com/Kuadrant/kuadrant-operator/issues/617 is fixed. + """ + + def _finalize(): + kuadrant.limitador = {"replicas": 1, "resourceRequirements": {"requests": {"cpu": "250m", "memory": "32Mi"}}} + + request.addfinalizer(_finalize) + + +@pytest.mark.parametrize( + "field, value", + [ + pytest.param("replicas", 2, id="replicas"), + pytest.param( + "resourceRequirements", {"requests": {"cpu": "300m", "memory": "40Mi"}}, id="resourceRequirements" + ), + ], +) +def test_fields_to_reconcile(kuadrant, field, value): + """ + Test: + - change specific `field` to `value` in Kuadrant CR + - assert that `field` in Kuadrant CR Limitador is equal to `value` + - assert that `field` in Limitador deployment is equal to `value` + """ + kuadrant.limitador = {field: value} + + assert value == kuadrant.limitador[field] + assert str(value) in str(kuadrant.limitador_deployment.model.spec) + + +@pytest.mark.xfail +@pytest.mark.issue("https://github.com/Kuadrant/kuadrant-operator/issues/617") +def test_blank_fields_wont_reconcile(kuadrant): + """ + Test: + - setup limitador with replicas and resourceRequirements != default + - change replicas to 3 + - assert replicas field is 3 for limitador deployment + - assert blank field resourceRequirements is returned to default for limitador deployment + """ + kuadrant.limitador = {"replicas": 2, "resourceRequirements": {"requests": {"cpu": "300m", "memory": "40Mi"}}} + + kuadrant.limitador = {"replicas": 3} + + assert kuadrant.limitador_deployment.model.spec.replicas == 3 + assert "'cpu': '250m', 'memory': '32Mi'" in str(kuadrant.limitador_deployment.model.spec)