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