forked from opendatahub-io/opendatahub-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
321 lines (275 loc) · 10.5 KB
/
conftest.py
File metadata and controls
321 lines (275 loc) · 10.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
from collections.abc import Generator
from typing import Any
import pytest
import structlog
from kubernetes.dynamic import DynamicClient
from ocp_resources.deployment import Deployment
from ocp_resources.namespace import Namespace
from ocp_resources.role import Role
from ocp_resources.role_binding import RoleBinding
from ocp_resources.route import Route
from ocp_resources.service import Service
from ocp_resources.service_account import ServiceAccount
from timeout_sampler import TimeoutSampler
from tests.model_explainability.evalhub.constants import (
EVALHUB_MT_CR_NAME,
EVALHUB_TENANT_LABEL_KEY,
EVALHUB_VLLM_EMULATOR_PORT,
)
from utilities.certificates_utils import create_ca_bundle_file
from utilities.constants import Labels, Protocols, Timeout
from utilities.infra import create_inference_token, create_ns
from utilities.resources.evalhub import EvalHub
LOGGER = structlog.get_logger(name=__name__)
# ---------------------------------------------------------------------------
# EvalHub instance (shared across the class)
# ---------------------------------------------------------------------------
@pytest.fixture(scope="class")
def evalhub_mt_cr(
admin_client: DynamicClient,
model_namespace: Namespace,
) -> Generator[EvalHub, Any, Any]:
"""Create an EvalHub CR for multi-tenancy tests.
Uses a distinct name ('evalhub-mt') to avoid RoleBinding name collisions
with the production EvalHub instance. The operator names tenant RoleBindings
as '{instance.Name}-{ns}-job-config-rb' and uses Get-or-Create (not Update),
so two instances named 'evalhub' would collide and the first one wins.
"""
with EvalHub(
client=admin_client,
name="evalhub-mt",
namespace=model_namespace.name,
database={"type": "sqlite"},
wait_for_resource=True,
) as evalhub:
yield evalhub
@pytest.fixture(scope="class")
def evalhub_mt_deployment(
admin_client: DynamicClient,
model_namespace: Namespace,
evalhub_mt_cr: EvalHub,
) -> Deployment:
"""Wait for the EvalHub deployment to become available."""
deployment = Deployment(
client=admin_client,
name=evalhub_mt_cr.name,
namespace=model_namespace.name,
)
deployment.wait_for_replicas(timeout=Timeout.TIMEOUT_5MIN)
return deployment
@pytest.fixture(scope="class")
def evalhub_mt_route(
admin_client: DynamicClient,
model_namespace: Namespace,
evalhub_mt_deployment: Deployment,
) -> Route:
"""Get the Route for the EvalHub service."""
return Route(
client=admin_client,
name=evalhub_mt_deployment.name,
namespace=model_namespace.name,
ensure_exists=True,
)
@pytest.fixture(scope="class")
def evalhub_mt_ca_bundle_file(
admin_client: DynamicClient,
) -> str:
"""CA bundle file for verifying TLS on the EvalHub route."""
return create_ca_bundle_file(client=admin_client)
# ---------------------------------------------------------------------------
# Tenant namespaces
# ---------------------------------------------------------------------------
@pytest.fixture(scope="class")
def tenant_a_namespace(
admin_client: DynamicClient,
) -> Generator[Namespace, Any, Any]:
"""Tenant namespace where the test user HAS access."""
with create_ns(
admin_client=admin_client,
name="test-evalhub-tenant-a",
labels={EVALHUB_TENANT_LABEL_KEY: "true"},
) as ns:
yield ns
@pytest.fixture(scope="class")
def tenant_b_namespace(
admin_client: DynamicClient,
) -> Generator[Namespace, Any, Any]:
"""Tenant namespace where the test user does NOT have access."""
with create_ns(
admin_client=admin_client,
name="test-evalhub-tenant-b",
labels={EVALHUB_TENANT_LABEL_KEY: "true"},
) as ns:
yield ns
# ---------------------------------------------------------------------------
# Wait for operator to provision tenant RBAC
# ---------------------------------------------------------------------------
def _tenant_rbac_ready(admin_client: DynamicClient, namespace: str) -> bool:
"""Check if the operator has provisioned job RBAC for the test EvalHub instance."""
rbs = list(RoleBinding.get(client=admin_client, namespace=namespace))
rb_names = [rb.name for rb in rbs]
# Look for RoleBindings prefixed with the test instance name to avoid
# matching RoleBindings from the production EvalHub instance.
has_job_config = any(name.startswith(EVALHUB_MT_CR_NAME) and "job-config" in name for name in rb_names)
has_job_writer = any(name.startswith(EVALHUB_MT_CR_NAME) and "job-writer" in name for name in rb_names)
return has_job_config and has_job_writer
@pytest.fixture(scope="class")
def tenant_a_rbac_ready(
admin_client: DynamicClient,
tenant_a_namespace: Namespace,
evalhub_mt_deployment: Deployment,
) -> None:
"""Wait for the operator to provision job RBAC in tenant-a.
The operator watches for namespaces with the tenant label and
creates jobs-writer + job-config RoleBindings. This fixture
blocks until those RoleBindings exist.
"""
for ready in TimeoutSampler(
wait_timeout=120,
sleep=5,
func=_tenant_rbac_ready,
admin_client=admin_client,
namespace=tenant_a_namespace.name,
):
if ready:
LOGGER.info(f"Operator RBAC provisioned in {tenant_a_namespace.name}")
return
# ---------------------------------------------------------------------------
# ServiceAccount and RBAC (only in tenant-a)
# ---------------------------------------------------------------------------
# Mirrors the user RBAC template from resources/evalhub-user-rbac-template.yaml.
# evaluations/collections/providers are virtual SAR resources — not real CRDs.
EVALHUB_USER_ROLE_RULES: list[dict] = [
{
"apiGroups": ["trustyai.opendatahub.io"],
"resources": ["evaluations", "collections", "providers"],
"verbs": ["get", "list", "create", "update", "delete"],
},
{
"apiGroups": ["mlflow.kubeflow.org"],
"resources": ["experiments"],
"verbs": ["create", "get"],
},
]
@pytest.fixture(scope="class")
def tenant_a_service_account(
admin_client: DynamicClient,
tenant_a_namespace: Namespace,
) -> Generator[ServiceAccount, Any, Any]:
"""ServiceAccount in tenant-a for multi-tenancy tests."""
with ServiceAccount(
client=admin_client,
name="evalhub-test-user",
namespace=tenant_a_namespace.name,
wait_for_resource=True,
) as sa:
yield sa
@pytest.fixture(scope="class")
def tenant_a_evalhub_role(
admin_client: DynamicClient,
tenant_a_namespace: Namespace,
) -> Generator[Role, Any, Any]:
"""Role granting full EvalHub API access in tenant-a (virtual SAR resources)."""
with Role(
client=admin_client,
name="evalhub-test-user-access",
namespace=tenant_a_namespace.name,
rules=EVALHUB_USER_ROLE_RULES,
wait_for_resource=True,
) as role:
yield role
@pytest.fixture(scope="class")
def tenant_a_evalhub_role_binding(
admin_client: DynamicClient,
tenant_a_namespace: Namespace,
tenant_a_service_account: ServiceAccount,
tenant_a_evalhub_role: Role,
) -> Generator[RoleBinding, Any, Any]:
"""RoleBinding granting the test SA EvalHub access in tenant-a only."""
with RoleBinding(
client=admin_client,
name="evalhub-test-user-binding",
namespace=tenant_a_namespace.name,
subjects_kind="ServiceAccount",
subjects_name=tenant_a_service_account.name,
role_ref_kind="Role",
role_ref_name=tenant_a_evalhub_role.name,
wait_for_resource=True,
) as rb:
yield rb
@pytest.fixture(scope="class")
def tenant_a_token(
tenant_a_service_account: ServiceAccount,
tenant_a_evalhub_role_binding: RoleBinding,
) -> str:
"""Bearer token for the test SA (has access to tenant-a, not tenant-b)."""
return create_inference_token(model_service_account=tenant_a_service_account)
# ---------------------------------------------------------------------------
# vLLM emulator (deployed in tenant-a for job submission tests)
# ---------------------------------------------------------------------------
VLLM_EMULATOR: str = "vllm-emulator"
VLLM_EMULATOR_IMAGE: str = (
"quay.io/trustyai_testing/vllm_emulator@sha256:c4bdd5bb93171dee5b4c8454f36d7c42b58b2a4ceb74f29dba5760ac53b5c12d"
)
@pytest.fixture(scope="class")
def evalhub_vllm_emulator_deployment(
admin_client: DynamicClient,
tenant_a_namespace: Namespace,
tenant_a_rbac_ready: None,
) -> Generator[Deployment, Any, Any]:
"""Deploy the vLLM emulator in tenant-a.
Depends on tenant_a_rbac_ready to ensure the operator has provisioned
the jobs-writer and job-config RoleBindings before any job is submitted.
"""
label = {Labels.Openshift.APP: VLLM_EMULATOR}
with Deployment(
client=admin_client,
namespace=tenant_a_namespace.name,
name=VLLM_EMULATOR,
label=label,
selector={"matchLabels": label},
template={
"metadata": {
"labels": label,
"name": VLLM_EMULATOR,
},
"spec": {
"containers": [
{
"image": VLLM_EMULATOR_IMAGE,
"name": VLLM_EMULATOR,
"securityContext": {
"allowPrivilegeEscalation": False,
"capabilities": {"drop": ["ALL"]},
"seccompProfile": {"type": "RuntimeDefault"},
},
}
]
},
},
replicas=1,
) as deployment:
deployment.wait_for_replicas(timeout=Timeout.TIMEOUT_5MIN)
yield deployment
@pytest.fixture(scope="class")
def evalhub_vllm_emulator_service(
admin_client: DynamicClient,
tenant_a_namespace: Namespace,
evalhub_vllm_emulator_deployment: Deployment,
) -> Generator[Service, Any, Any]:
"""Service fronting the vLLM emulator in tenant-a."""
with Service(
client=admin_client,
namespace=tenant_a_namespace.name,
name=f"{VLLM_EMULATOR}-service",
ports=[
{
"name": f"{VLLM_EMULATOR}-endpoint",
"port": EVALHUB_VLLM_EMULATOR_PORT,
"protocol": Protocols.TCP,
"targetPort": EVALHUB_VLLM_EMULATOR_PORT,
}
],
selector={Labels.Openshift.APP: VLLM_EMULATOR},
) as service:
yield service