diff --git a/testsuite/gateway/gateway_api/gateway.py b/testsuite/gateway/gateway_api/gateway.py index d78d1fe3..12551c11 100644 --- a/testsuite/gateway/gateway_api/gateway.py +++ b/testsuite/gateway/gateway_api/gateway.py @@ -8,6 +8,8 @@ from testsuite.gateway import Gateway from testsuite.openshift.client import OpenShiftClient from testsuite.openshift import OpenShiftObject +from testsuite.policy import Policy +from testsuite.utils import check_condition class KuadrantGateway(OpenShiftObject, Gateway): @@ -77,6 +79,19 @@ def wait_for_ready(self, timeout: int = 180): assert success, "Gateway didn't get ready in time" self.refresh() + def is_affected_by(self, policy: Policy) -> bool: + """Returns True, if affected by status is found within the object for the specific policy""" + for condition in self.model.status.conditions: + if check_condition( + condition, + f"kuadrant.io/{policy.kind(lowercase=False)}Affected", + "True", + "Accepted", + f"Object affected by {policy.kind(lowercase=False)} {policy.namespace()}/{policy.name()}", + ): + return True + return False + def get_tls_cert(self): if "tls" not in self.model.spec.listeners[0]: return None diff --git a/testsuite/gateway/gateway_api/route.py b/testsuite/gateway/gateway_api/route.py index 42e5e88f..902e5d9c 100644 --- a/testsuite/gateway/gateway_api/route.py +++ b/testsuite/gateway/gateway_api/route.py @@ -8,7 +8,8 @@ from testsuite.gateway import Gateway, GatewayRoute, PathMatch, MatchType, RouteMatch from testsuite.openshift.client import OpenShiftClient from testsuite.openshift import OpenShiftObject, modify -from testsuite.utils import asdict +from testsuite.policy import Policy +from testsuite.utils import asdict, check_condition if typing.TYPE_CHECKING: from testsuite.backend import Backend @@ -43,6 +44,21 @@ def create_instance( return cls(model, context=openshift.context) + def is_affected_by(self, policy: Policy): + """Returns True, if affected by status is found within the object for the specific policy""" + for condition_set in self.model.status.parents: + if condition_set.controllerName == "kuadrant.io/policy-controller": + for condition in condition_set.conditions: + if check_condition( + condition, + f"kuadrant.io/{policy.kind(lowercase=False)}Affected", + "True", + "Accepted", + f"Object affected by {policy.kind(lowercase=False)} {policy.namespace()}/{policy.name()}", + ): + return True + return False + @property def reference(self): return { diff --git a/testsuite/tests/kuadrant/gateway/reconciliation/test_affected_by.py b/testsuite/tests/kuadrant/gateway/reconciliation/test_affected_by.py new file mode 100644 index 00000000..339cd887 --- /dev/null +++ b/testsuite/tests/kuadrant/gateway/reconciliation/test_affected_by.py @@ -0,0 +1,31 @@ +"""Tests that affected by status is applied correctly to the HTTPRoute and Gateway""" + +import pytest + +pytestmark = [pytest.mark.kuadrant_only] + + +def test_route_status(route, rate_limit, authorization): + """Tests affected by status for HTTPRoute""" + route.refresh() + assert route.is_affected_by(rate_limit) + assert route.is_affected_by(authorization) + + rate_limit.delete() + assert not route.wait_until(lambda obj: obj.is_affected_by(rate_limit)) + + authorization.delete() + assert not route.wait_until(lambda obj: obj.is_affected_by(authorization)) + + +def test_gateway_status(gateway, dns_policy, tls_policy): + """Tests affected by status for Gateway""" + gateway.refresh() + assert gateway.is_affected_by(dns_policy) + assert gateway.is_affected_by(tls_policy) + + dns_policy.delete() + assert not gateway.wait_until(lambda obj: obj.is_affected_by(dns_policy)) + + tls_policy.delete() + assert not gateway.wait_until(lambda obj: obj.is_affected_by(tls_policy)) diff --git a/testsuite/tests/kuadrant/gateway/reconciliation/test_gw_doesnt_exist.py b/testsuite/tests/kuadrant/gateway/reconciliation/test_gw_doesnt_exist.py index 72d7ca85..8f1e373f 100644 --- a/testsuite/tests/kuadrant/gateway/reconciliation/test_gw_doesnt_exist.py +++ b/testsuite/tests/kuadrant/gateway/reconciliation/test_gw_doesnt_exist.py @@ -4,7 +4,6 @@ from testsuite.gateway import CustomReference from testsuite.policy.tls_policy import TLSPolicy -from testsuite.utils import has_condition from . import dns_policy pytestmark = [pytest.mark.kuadrant_only] @@ -33,6 +32,6 @@ def test_no_gw(request, create_cr, hub_openshift, blame, module_label, cluster_i request.addfinalizer(policy.delete) policy.commit() - assert policy.wait_until( - has_condition("Accepted", "False", "TargetNotFound", "target does-not-exist was not found"), timelimit=20 + assert policy.wait_for_condition( + "Accepted", "False", "TargetNotFound", "target does-not-exist was not found", timelimit=20 ), f"Policy did not reach expected status, instead it was: {policy.refresh().model.status.conditions}" diff --git a/testsuite/utils.py b/testsuite/utils.py index 694bd16b..df2113e0 100644 --- a/testsuite/utils.py +++ b/testsuite/utils.py @@ -170,17 +170,24 @@ def _asdict_recurse(obj): return result +def check_condition(condition, condition_type, status, reason=None, message=None): + """Checks if condition matches expectation, won't check message and reason if they are None""" + if ( # pylint: disable=too-many-boolean-expressions + condition.type == condition_type + and condition.status == status + and (message is None or message in condition.message) + and (reason is None or reason == condition.reason) + ): + return True + return False + + def has_condition(condition_type, status="True", reason=None, message=None): """Returns function, that returns True if the Kubernetes object has a specific value""" def _check(obj): for condition in obj.model.status.conditions: - if ( # pylint: disable=too-many-boolean-expressions - condition.type == condition_type - and condition.status == status - and (message is None or message in condition.message) - and (reason is None or reason == condition.reason) - ): + if check_condition(condition, condition_type, status, reason, message): return True return False