Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for Reconcile Sub Component Limitador CR #352

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions testsuite/openshift/kuadrant.py
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 22 additions & 10 deletions testsuite/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -265,13 +267,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")
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Tests for Kuadrant sub component - Limitador CR configuration via Kuadrant CR"""

import pytest

pytestmark = [pytest.mark.kuadrant_only]
jsmolar marked this conversation as resolved.
Show resolved Hide resolved


@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"}}}
averevki marked this conversation as resolved.
Show resolved Hide resolved

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)
averevki marked this conversation as resolved.
Show resolved Hide resolved


@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)
Loading