Skip to content

Commit

Permalink
Add basic tests for DNSPolicy health checks
Browse files Browse the repository at this point in the history
  • Loading branch information
averevki committed Nov 6, 2023
1 parent 91b33c3 commit 4d5238a
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 6 deletions.
13 changes: 7 additions & 6 deletions testsuite/openshift/objects/dnspolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ def create_instance(
openshift: OpenShiftClient,
name: str,
parent: Referencable,
healthCheck: HealthCheck = None,
labels: dict[str, str] = None,
): # pylint: disable=invalid-name
):
"""Creates new instance of DNSPolicy"""

model: dict = {
Expand All @@ -52,12 +51,14 @@ def create_instance(
"spec": {"targetRef": parent.reference},
}

if healthCheck:
model["spec"]["healthCheck"] = asdict(healthCheck)

return cls(model, context=openshift.context)

def set_health_check(self, health_check: HealthCheck):
"""Sets health check for DNSPolicy"""
self.model["spec"]["healthCheck"] = asdict(health_check)

def get_dns_health_probe(self) -> oc.APIObject:
"""Returns DNSHealthCheckProbe object for the created DNSPolicy"""
dns_probe = oc.selector("DNSHealthCheckProbe", labels={"kuadrant.io/dnspolicy": self.name()}).object()
with self.context:
dns_probe = oc.selector("DNSHealthCheckProbe", labels={"kuadrant.io/dnspolicy": self.name()}).object()
return DNSHealthCheckProbe(dns_probe.model, context=self.context)
Empty file.
28 changes: 28 additions & 0 deletions testsuite/tests/mgc/dnspolicy/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Conftest for DNSPolicy tests"""
import pytest

from testsuite.openshift.objects.gateway_api.gateway import MGCGateway


@pytest.fixture(scope="module")
def upstream_gateway(request, openshift, blame, hostname, module_label):
"""Creates and returns configured and ready upstream Gateway with disabled tls"""
upstream_gateway = MGCGateway.create_instance(
openshift=openshift,
name=blame("mgc-gateway"),
gateway_class="kuadrant-multi-cluster-gateway-instance-per-cluster",
hostname=f"*.{hostname}",
tls=False,
placement="http-gateway",
labels={"app": module_label},
)
request.addfinalizer(upstream_gateway.delete)
upstream_gateway.commit()

return upstream_gateway


@pytest.fixture(scope="module")
def tls_policy():
"""Don't need TLSPolicy in the DNSPolicy only tests"""
return None
Empty file.
40 changes: 40 additions & 0 deletions testsuite/tests/mgc/dnspolicy/health_check/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Conftest for DNSPolicy health checks"""
import time
import pytest

from testsuite.openshift.objects.gateway_api.gateway import MGCGateway


@pytest.fixture(scope="module")
def upstream_gateway(request, openshift, blame, module_label, initial_host):
"""
Creates and returns configured and ready upstream Gateway with FQDN hostname
Health checks available only with Fully Qualified Domain Names in gateway (no wildcards are allowed)
"""
upstream_gateway = MGCGateway.create_instance(
openshift=openshift,
name=blame("mgc-gateway"),
gateway_class="kuadrant-multi-cluster-gateway-instance-per-cluster",
hostname=initial_host,
tls=False,
placement="http-gateway",
labels={"app": module_label},
)
request.addfinalizer(upstream_gateway.delete)
upstream_gateway.commit()

return upstream_gateway


@pytest.fixture(scope="module")
def dns_policy(dns_policy, health_check):
"""Add health check to DNSPolicy"""
dns_policy.set_health_check(health_check)
return dns_policy


@pytest.fixture(scope="module")
def dns_health_probe(dns_policy, route): # pylint: disable=unused-argument
"""Wait for health check to start monitoring endpoint and return according DNSHealthCheckProbe object"""
time.sleep(10)
return dns_policy.get_dns_health_probe()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Tests for DNSPolicy health checks - healthy endpoint"""
import pytest

from testsuite.openshift.objects.dnspolicy import HealthCheck

pytestmark = [pytest.mark.mgc]


@pytest.fixture(scope="module")
def health_check():
"""Returns healthy endpoint specification for DNSPolicy health check"""
return HealthCheck(
allowInsecureCertificates=True,
endpoint="/get",
interval="5s",
port=80,
protocol="http",
)


def test_healthy_endpoint(dns_health_probe):
"""Test healthy endpoint check"""
assert dns_health_probe.is_healthy()
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Tests for DNSPolicy health checks - unhealthy endpoint"""
import pytest

from testsuite.openshift.objects.dnspolicy import HealthCheck

pytestmark = [pytest.mark.mgc]


@pytest.fixture(scope="module")
def health_check():
"""Returns unhealthy endpoint specification for DNSPolicy health check"""
return HealthCheck(
allowInsecureCertificates=True,
endpoint="/unknown-endpoint",
interval="5s",
port=80,
protocol="http",
)


def test_unhealthy_endpoint(dns_health_probe):
"""Test unhealthy endpoint check"""
assert not dns_health_probe.is_healthy()

0 comments on commit 4d5238a

Please sign in to comment.