|
| 1 | +from typing import Generator, Any |
| 2 | + |
| 3 | +import pytest |
| 4 | +from _pytest.fixtures import FixtureRequest |
| 5 | +from kubernetes.dynamic import DynamicClient |
| 6 | +from ocp_resources.config_map import ConfigMap |
| 7 | +from ocp_resources.deployment import Deployment |
| 8 | +from ocp_resources.guardrails_orchestrator import GuardrailsOrchestrator |
| 9 | +from ocp_resources.namespace import Namespace |
| 10 | +from ocp_resources.pod import Pod |
| 11 | +from ocp_resources.resource import ResourceEditor |
| 12 | +from ocp_resources.route import Route |
| 13 | + |
| 14 | +from utilities.constants import Labels, Annotations |
| 15 | + |
| 16 | +GUARDRAILS_ORCHESTRATOR_NAME: str = "guardrails-orchestrator" |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(scope="class") |
| 20 | +def guardrails_orchestrator( |
| 21 | + request: FixtureRequest, |
| 22 | + admin_client: DynamicClient, |
| 23 | + model_namespace: Namespace, |
| 24 | + orchestrator_config: ConfigMap, |
| 25 | +) -> Generator[GuardrailsOrchestrator, Any, Any]: |
| 26 | + gorch_kwargs = { |
| 27 | + "client": admin_client, |
| 28 | + "name": GUARDRAILS_ORCHESTRATOR_NAME, |
| 29 | + "namespace": model_namespace.name, |
| 30 | + "orchestrator_config": orchestrator_config.name, |
| 31 | + "replicas": 1, |
| 32 | + "wait_for_resource": True, |
| 33 | + } |
| 34 | + |
| 35 | + if enable_built_in_detectors := request.param.get("enable_built_in_detectors"): |
| 36 | + gorch_kwargs["enable_built_in_detectors"] = enable_built_in_detectors |
| 37 | + |
| 38 | + if request.param.get("enable_guardrails_gateway"): |
| 39 | + guardrails_gateway_config = request.getfixturevalue(argname="guardrails_gateway_config") |
| 40 | + gorch_kwargs["enable_guardrails_gateway"] = True |
| 41 | + gorch_kwargs["guardrails_gateway_config"] = guardrails_gateway_config.name |
| 42 | + |
| 43 | + with GuardrailsOrchestrator(**gorch_kwargs) as gorch: |
| 44 | + gorch_deployment = Deployment(name=gorch.name, namespace=gorch.namespace, wait_for_resource=True) |
| 45 | + gorch_deployment.wait_for_replicas() |
| 46 | + yield gorch |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture(scope="class") |
| 50 | +def orchestrator_config( |
| 51 | + request: FixtureRequest, admin_client: DynamicClient, model_namespace: Namespace |
| 52 | +) -> Generator[ConfigMap, Any, Any]: |
| 53 | + with ConfigMap( |
| 54 | + client=admin_client, |
| 55 | + name="fms-orchestr8-config-nlp", |
| 56 | + namespace=model_namespace.name, |
| 57 | + data=request.param["orchestrator_config_data"], |
| 58 | + ) as cm: |
| 59 | + yield cm |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture(scope="class") |
| 63 | +def guardrails_gateway_config( |
| 64 | + request: FixtureRequest, admin_client: DynamicClient, model_namespace: Namespace |
| 65 | +) -> Generator[ConfigMap, Any, Any]: |
| 66 | + with ConfigMap( |
| 67 | + client=admin_client, |
| 68 | + name="fms-orchestr8-config-gateway", |
| 69 | + namespace=model_namespace.name, |
| 70 | + label={Labels.Openshift.APP: "fmstack-nlp"}, |
| 71 | + data=request.param["guardrails_gateway_config_data"], |
| 72 | + ) as cm: |
| 73 | + yield cm |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture(scope="class") |
| 77 | +def guardrails_orchestrator_pod( |
| 78 | + admin_client: DynamicClient, |
| 79 | + model_namespace: Namespace, |
| 80 | + guardrails_orchestrator: GuardrailsOrchestrator, |
| 81 | +) -> Pod: |
| 82 | + return list( |
| 83 | + Pod.get( |
| 84 | + namespace=model_namespace.name, label_selector=f"app.kubernetes.io/instance={GUARDRAILS_ORCHESTRATOR_NAME}" |
| 85 | + ) |
| 86 | + )[0] |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture(scope="class") |
| 90 | +def guardrails_orchestrator_route( |
| 91 | + admin_client: DynamicClient, |
| 92 | + model_namespace: Namespace, |
| 93 | + guardrails_orchestrator: GuardrailsOrchestrator, |
| 94 | +) -> Generator[Route, Any, Any]: |
| 95 | + guardrails_orchestrator_route = Route( |
| 96 | + name=f"{guardrails_orchestrator.name}", |
| 97 | + namespace=guardrails_orchestrator.namespace, |
| 98 | + wait_for_resource=True, |
| 99 | + ensure_exists=True, |
| 100 | + ) |
| 101 | + with ResourceEditor( |
| 102 | + patches={ |
| 103 | + guardrails_orchestrator_route: { |
| 104 | + "metadata": { |
| 105 | + "annotations": {Annotations.HaproxyRouterOpenshiftIo.TIMEOUT: "10m"}, |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + ): |
| 110 | + yield guardrails_orchestrator_route |
| 111 | + |
| 112 | + |
| 113 | +@pytest.fixture(scope="class") |
| 114 | +def guardrails_orchestrator_url( |
| 115 | + guardrails_orchestrator_route: Route, |
| 116 | +) -> str: |
| 117 | + return f"https://{guardrails_orchestrator_route.host}" |
| 118 | + |
| 119 | + |
| 120 | +@pytest.fixture(scope="class") |
| 121 | +def guardrails_orchestrator_health_route( |
| 122 | + admin_client: DynamicClient, |
| 123 | + model_namespace: Namespace, |
| 124 | + guardrails_orchestrator: GuardrailsOrchestrator, |
| 125 | +) -> Generator[Route, Any, Any]: |
| 126 | + guardrails_orchestrator_health_route = Route( |
| 127 | + name=f"{guardrails_orchestrator.name}-health", |
| 128 | + namespace=guardrails_orchestrator.namespace, |
| 129 | + wait_for_resource=True, |
| 130 | + ensure_exists=True, |
| 131 | + ) |
| 132 | + with ResourceEditor( |
| 133 | + patches={ |
| 134 | + guardrails_orchestrator_health_route: { |
| 135 | + "metadata": { |
| 136 | + "annotations": {Annotations.HaproxyRouterOpenshiftIo.TIMEOUT: "10m"}, |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + ): |
| 141 | + yield guardrails_orchestrator_health_route |
0 commit comments