-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathconftest.py
More file actions
346 lines (297 loc) · 11.5 KB
/
conftest.py
File metadata and controls
346 lines (297 loc) · 11.5 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import subprocess
from base64 import b64encode
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.inference_service import InferenceService
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 ocp_resources.secret import Secret
from ocp_resources.serving_runtime import ServingRuntime
from pytest_testconfig import py_config
from utilities.certificates_utils import create_ca_bundle_file
from utilities.constants import KServeDeploymentType, Labels, RuntimeTemplates, Annotations
from utilities.inference_utils import create_isvc
from utilities.serving_runtime import ServingRuntimeFromTemplate
GUARDRAILS_ORCHESTRATOR_NAME = "guardrails-orchestrator"
# GuardrailsOrchestrator related fixtures
@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": {"haproxy.router.openshift.io/timeout": "10m"},
}
}
}
):
yield guardrails_orchestrator_route
@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
# ServingRuntimes, InferenceServices, and related resources
# for generation and detection models
@pytest.fixture(scope="class")
def huggingface_sr(
admin_client: DynamicClient,
model_namespace: Namespace,
) -> Generator[ServingRuntime, Any, Any]:
with ServingRuntimeFromTemplate(
client=admin_client,
name="guardrails-detector-runtime-prompt-injection",
template_name=RuntimeTemplates.GUARDRAILS_DETECTOR_HUGGINGFACE,
namespace=model_namespace.name,
supported_model_formats=[{"name": "guardrails-detector-huggingface", "autoSelect": True}],
) as serving_runtime:
yield serving_runtime
@pytest.fixture(scope="class")
def prompt_injection_detector_isvc(
admin_client: DynamicClient,
model_namespace: Namespace,
minio_data_connection: Secret,
huggingface_sr: ServingRuntime,
) -> Generator[InferenceService, Any, Any]:
with create_isvc(
client=admin_client,
name="prompt-injection-detector",
namespace=model_namespace.name,
deployment_mode=KServeDeploymentType.RAW_DEPLOYMENT,
model_format="guardrails-detector-huggingface",
runtime=huggingface_sr.name,
storage_key=minio_data_connection.name,
storage_path="deberta-v3-base-prompt-injection-v2",
wait_for_predictor_pods=False,
enable_auth=False,
resources={
"requests": {"cpu": "1", "memory": "2Gi", "nvidia.com/gpu": "0"},
"limits": {"cpu": "1", "memory": "2Gi", "nvidia.com/gpu": "0"},
},
max_replicas=1,
min_replicas=1,
labels={
"opendatahub.io/dashboard": "true",
},
) as isvc:
yield isvc
@pytest.fixture(scope="class")
def prompt_injection_detector_route(
admin_client: DynamicClient,
model_namespace: Namespace,
prompt_injection_detector_isvc: InferenceService,
) -> Generator[Route, Any, Any]:
yield Route(
name="prompt-injection-detector-route",
namespace=model_namespace.name,
service=prompt_injection_detector_isvc.name,
wait_for_resource=True,
)
# Other "helper" fixtures
@pytest.fixture(scope="class")
def openshift_ca_bundle_file(
admin_client: DynamicClient,
) -> str:
return create_ca_bundle_file(client=admin_client, ca_type="openshift")
@pytest.fixture(scope="class")
def guardrails_orchestrator_ssl_cert(guardrails_orchestrator_route: Route):
hostname = guardrails_orchestrator_route.host
try:
result = subprocess.run(
args=["openssl", "s_client", "-showcerts", "-connect", f"{hostname}:443"],
input="",
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0 and "CONNECTED" not in result.stdout:
raise RuntimeError(f"Failed to connect to {hostname}: {result.stderr}")
cert_lines = []
in_cert = False
for line in result.stdout.splitlines():
if "-----BEGIN CERTIFICATE-----" in line:
in_cert = True
if in_cert:
cert_lines.append(line)
if "-----END CERTIFICATE-----" in line:
in_cert = False
if not cert_lines:
raise RuntimeError(f"No certificate found in response from {hostname}")
filepath = os.path.join(py_config["tmp_base_dir"], "gorch_cert.crt")
with open(filepath, "w") as f:
f.write("\n".join(cert_lines))
return filepath
except Exception as e:
raise RuntimeError(f"Could not get certificate from {hostname}: {e}")
@pytest.fixture(scope="class")
def guardrails_orchestrator_ssl_cert_secret(
admin_client: DynamicClient,
model_namespace: Namespace,
guardrails_orchestrator_ssl_cert: str, # ← Add dependency and use correct cert
) -> Generator[Secret, Any, None]:
with open(guardrails_orchestrator_ssl_cert, "r") as f:
cert_content = f.read()
with Secret(
client=admin_client,
name="orch-certificate",
namespace=model_namespace.name,
data_dict={"orch-certificate.crt": b64encode(cert_content.encode("utf-8")).decode("utf-8")},
) as secret:
yield secret
@pytest.fixture(scope="class")
def patched_llamastack_deployment_tls_certs(llamastack_distribution, guardrails_orchestrator_ssl_cert_secret):
lls_deployment = Deployment(
name=llamastack_distribution.name,
namespace=llamastack_distribution.namespace,
ensure_exists=True,
)
current_spec = lls_deployment.instance.spec.template.spec.to_dict()
current_spec["volumes"].append({
"name": "router-ca",
"secret": {"secretName": "orch-certificate"}, # pragma: allowlist secret
})
for container in current_spec["containers"]:
if container["name"] == "llama-stack":
container["volumeMounts"].append({"name": "router-ca", "mountPath": "/etc/llama/certs", "readOnly": True})
break
with ResourceEditor(patches={lls_deployment: {"spec": {"template": {"spec": current_spec}}}}) as _:
initial_replicas = lls_deployment.replicas
lls_deployment.scale_replicas(replica_count=0)
lls_deployment.scale_replicas(replica_count=initial_replicas)
lls_deployment.wait_for_replicas()
yield lls_deployment
@pytest.fixture(scope="class")
def hap_detector_isvc(
admin_client: DynamicClient,
model_namespace: Namespace,
minio_data_connection: Secret,
huggingface_sr: ServingRuntime,
) -> Generator[InferenceService, Any, Any]:
with create_isvc(
client=admin_client,
name="hap-detector",
namespace=model_namespace.name,
deployment_mode=KServeDeploymentType.RAW_DEPLOYMENT,
model_format="guardrails-detector-huggingface",
runtime=huggingface_sr.name,
storage_key=minio_data_connection.name,
storage_path="granite-guardian-hap-38m",
wait_for_predictor_pods=False,
enable_auth=False,
resources={
"requests": {"cpu": "1", "memory": "4Gi", "nvidia.com/gpu": "0"},
"limits": {"cpu": "1", "memory": "4Gi", "nvidia.com/gpu": "0"},
},
max_replicas=1,
min_replicas=1,
labels={
"opendatahub.io/dashboard": "true",
},
) as isvc:
yield isvc
@pytest.fixture(scope="class")
def hap_detector_route(
admin_client: DynamicClient,
model_namespace: Namespace,
hap_detector_isvc: InferenceService,
) -> Generator[Route, Any, Any]:
yield Route(
name="hap-detector-route",
namespace=model_namespace.name,
service=hap_detector_isvc.name,
wait_for_resource=True,
)