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
426 lines (385 loc) · 16.9 KB
/
conftest.py
File metadata and controls
426 lines (385 loc) · 16.9 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import time
from contextlib import ExitStack
import pytest
from pytest import Config, FixtureRequest
from typing import Generator, Any
import os
from ocp_resources.infrastructure import Infrastructure
from ocp_resources.oauth import OAuth
from ocp_resources.pod import Pod
from ocp_resources.secret import Secret
from ocp_resources.namespace import Namespace
from ocp_resources.data_science_cluster import DataScienceCluster
from ocp_resources.config_map import ConfigMap
from ocp_resources.service import Service
from ocp_resources.persistent_volume_claim import PersistentVolumeClaim
from ocp_resources.deployment import Deployment
from ocp_resources.service_account import ServiceAccount
from ocp_resources.resource import ResourceEditor
from simple_logger.logger import get_logger
from kubernetes.dynamic import DynamicClient
from pytest_testconfig import config as py_config
from utilities.general import wait_for_oauth_openshift_deployment
from utilities.resources.model_registry_modelregistry_opendatahub_io import ModelRegistry
from tests.model_registry.utils import (
get_byoidc_user_credentials,
get_model_registry_objects,
get_model_registry_metadata_resources,
wait_for_default_resource_cleanedup,
generate_namespace_name,
get_rest_headers,
)
from utilities.general import generate_random_name, wait_for_pods_running
from tests.model_registry.constants import (
MR_OPERATOR_NAME,
MR_INSTANCE_BASE_NAME,
DB_BASE_RESOURCES_NAME,
DB_RESOURCE_NAME,
MR_INSTANCE_NAME,
KUBERBACPROXY_STR,
)
from utilities.constants import Labels
from utilities.constants import DscComponents
from utilities.general import wait_for_pods_by_labels
from utilities.infra import get_data_science_cluster, wait_for_dsc_status_ready, login_with_user_password
from utilities.user_utils import UserTestSession, wait_for_user_creation, create_htpasswd_file
DEFAULT_TOKEN_DURATION = "10m"
LOGGER = get_logger(name=__name__)
@pytest.fixture(scope="session")
def model_registry_namespace(updated_dsc_component_state_scope_session: DataScienceCluster) -> str:
return updated_dsc_component_state_scope_session.instance.spec.components.modelregistry.registriesNamespace
@pytest.fixture(scope="session")
def updated_dsc_component_state_scope_session(
pytestconfig: Config,
admin_client: DynamicClient,
) -> Generator[DataScienceCluster, Any, Any]:
dsc_resource = get_data_science_cluster(client=admin_client)
original_namespace_name = dsc_resource.instance.spec.components.modelregistry.registriesNamespace
if pytestconfig.option.custom_namespace:
resource_editor = ResourceEditor(
patches={
dsc_resource: {
"spec": {
"components": {
DscComponents.MODELREGISTRY: {
"managementState": DscComponents.ManagementState.REMOVED,
"registriesNamespace": original_namespace_name,
},
}
}
}
}
)
try:
# first disable MR
resource_editor.update(backup_resources=True)
wait_for_dsc_status_ready(dsc_resource=dsc_resource)
# now delete the original namespace:
original_namespace = Namespace(client=admin_client, name=original_namespace_name, wait_for_resource=True)
original_namespace.delete(wait=True)
# Now enable it with the custom namespace
with ResourceEditor(
patches={
dsc_resource: {
"spec": {
"components": {
DscComponents.MODELREGISTRY: {
"managementState": DscComponents.ManagementState.MANAGED,
"registriesNamespace": py_config["model_registry_namespace"],
},
}
}
}
}
):
namespace = Namespace(
client=admin_client, name=py_config["model_registry_namespace"], wait_for_resource=True
)
namespace.wait_for_status(status=Namespace.Status.ACTIVE)
wait_for_pods_running(
admin_client=admin_client,
namespace_name=py_config["applications_namespace"],
number_of_consecutive_checks=6,
)
wait_for_pods_running(
admin_client=admin_client,
namespace_name=py_config["model_registry_namespace"],
number_of_consecutive_checks=6,
)
yield dsc_resource
finally:
resource_editor.restore()
Namespace(name=py_config["model_registry_namespace"]).delete(wait=True)
# create the original namespace object again, so that we can wait for it to be created first
original_namespace = Namespace(name=original_namespace_name, wait_for_resource=True)
original_namespace.wait_for_status(status=Namespace.Status.ACTIVE)
wait_for_pods_running(
admin_client=admin_client,
namespace_name=py_config["applications_namespace"],
number_of_consecutive_checks=6,
)
else:
LOGGER.info("Model Registry is enabled by default and does not require any setup.")
yield dsc_resource
@pytest.fixture()
def model_registry_operator_pod(admin_client: DynamicClient) -> Generator[Pod, Any, Any]:
"""Get the model registry operator pod."""
yield wait_for_pods_by_labels(
admin_client=admin_client,
namespace=py_config["applications_namespace"],
label_selector=f"{Labels.OpenDataHubIo.NAME}={MR_OPERATOR_NAME}",
expected_num_pods=1,
)[0]
@pytest.fixture(scope="module")
def test_idp_user(
request: pytest.FixtureRequest,
original_user: str,
api_server_url: str,
is_byoidc: bool,
) -> Generator[UserTestSession | None, None, None]:
"""
Session-scoped fixture that creates a test IDP user and cleans it up after all tests.
Returns a UserTestSession object that contains all necessary credentials and contexts.
"""
if is_byoidc:
# For BYOIDC, we would be using a preconfigured group and username for actual api calls.
yield
else:
user_credentials_rbac = request.getfixturevalue(argname="user_credentials_rbac")
_ = request.getfixturevalue(argname="created_htpasswd_secret")
_ = request.getfixturevalue(argname="updated_oauth_config")
idp_session = None
try:
if wait_for_user_creation(
username=user_credentials_rbac["username"],
password=user_credentials_rbac["password"],
cluster_url=api_server_url,
):
# undo the login as test user if we were successful in logging in as test user
LOGGER.info(f"Undoing login as test user and logging in as {original_user}")
login_with_user_password(api_address=api_server_url, user=original_user)
idp_session = UserTestSession(
idp_name=user_credentials_rbac["idp_name"],
secret_name=user_credentials_rbac["secret_name"],
username=user_credentials_rbac["username"],
password=user_credentials_rbac["password"],
original_user=original_user,
api_server_url=api_server_url,
)
LOGGER.info(f"Created session test IDP user: {idp_session.username}")
yield idp_session
finally:
if idp_session:
LOGGER.info(f"Cleaning up test IDP user: {idp_session.username}")
idp_session.cleanup()
@pytest.fixture(scope="session")
def api_server_url(admin_client: DynamicClient) -> str:
"""
Get api server url from the cluster.
"""
infrastructure = Infrastructure(client=admin_client, name="cluster", ensure_exists=True)
return infrastructure.instance.status.apiServerURL
@pytest.fixture(scope="module")
def created_htpasswd_secret(
is_byoidc: bool, admin_client: DynamicClient, original_user: str, user_credentials_rbac: dict[str, str]
) -> Generator[UserTestSession | None, None, None]:
"""
Session-scoped fixture that creates a test IDP user and cleans it up after all tests.
Returns a UserTestSession object that contains all necessary credentials and contexts.
"""
if is_byoidc:
yield
else:
temp_path, htpasswd_b64 = create_htpasswd_file(
username=user_credentials_rbac["username"], password=user_credentials_rbac["password"]
)
try:
LOGGER.info(f"Creating secret {user_credentials_rbac['secret_name']} in openshift-config namespace")
with Secret(
client=admin_client,
name=user_credentials_rbac["secret_name"],
namespace="openshift-config",
htpasswd=htpasswd_b64,
type="Opaque",
wait_for_resource=True,
) as secret:
yield secret
finally:
# Clean up the temporary file
temp_path.unlink(missing_ok=True)
@pytest.fixture(scope="module")
def updated_oauth_config(
is_byoidc: bool, admin_client: DynamicClient, original_user: str, user_credentials_rbac: dict[str, str]
) -> Generator[Any, None, None]:
if is_byoidc:
yield
else:
# Get current providers and add the new one
oauth = OAuth(client=admin_client, name="cluster")
identity_providers = oauth.instance.spec.identityProviders
new_idp = {
"name": user_credentials_rbac["idp_name"],
"mappingMethod": "claim",
"type": "HTPasswd",
"htpasswd": {"fileData": {"name": user_credentials_rbac["secret_name"]}},
}
updated_providers = identity_providers + [new_idp]
LOGGER.info("Updating OAuth")
identity_providers_patch = ResourceEditor(patches={oauth: {"spec": {"identityProviders": updated_providers}}})
identity_providers_patch.update(backup_resources=True)
# Wait for OAuth server to be ready
wait_for_oauth_openshift_deployment()
LOGGER.info(f"Added IDP {user_credentials_rbac['idp_name']} to OAuth configuration")
yield
identity_providers_patch.restore()
wait_for_oauth_openshift_deployment()
@pytest.fixture(scope="module")
def user_credentials_rbac(
is_byoidc: bool,
) -> dict[str, str]:
if is_byoidc:
byoidc_creds = get_byoidc_user_credentials(username="mr-non-admin")
return {
"username": byoidc_creds["username"],
"password": byoidc_creds["password"],
"idp_name": "byoidc",
"secret_name": None,
}
else:
random_str = generate_random_name()
return {
"username": f"test-user-{random_str}",
"password": f"test-password-{random_str}",
"idp_name": f"test-htpasswd-idp-{random_str}",
"secret_name": f"test-htpasswd-secret-{random_str}",
}
@pytest.fixture(scope="class")
def model_registry_instance(
request: pytest.FixtureRequest,
pytestconfig: Config,
admin_client: DynamicClient,
teardown_resources: bool,
model_registry_metadata_db_resources: dict[Any, Any],
model_registry_namespace: str,
) -> Generator[list[Any], Any, Any]:
param = getattr(request, "param", {})
if pytestconfig.option.post_upgrade:
mr_instance = ModelRegistry(
client=admin_client, name=MR_INSTANCE_NAME, namespace=model_registry_namespace, ensure_exists=True
)
yield [mr_instance]
mr_instance.delete(wait=True)
else:
db_name = param.get("db_name", "mysql")
mr_objects = get_model_registry_objects(
client=admin_client,
namespace=model_registry_namespace,
base_name=MR_INSTANCE_BASE_NAME,
num=param.get("num_resources", 1),
teardown_resources=teardown_resources,
params=param,
db_backend=db_name,
)
with ExitStack() as stack:
mr_instances = [stack.enter_context(mr_obj) for mr_obj in mr_objects]
for mr_instance in mr_instances:
mr_instance.wait_for_condition(condition="Available", status="True")
mr_instance.wait_for_condition(condition=KUBERBACPROXY_STR, status="True")
wait_for_pods_running(
admin_client=admin_client, namespace_name=model_registry_namespace, number_of_consecutive_checks=6
)
# TODO remove when RHOAIENG-41728 is addressed
time.sleep(60.0) # noqa: FCN001
yield mr_instances
if db_name == "default":
wait_for_default_resource_cleanedup(admin_client=admin_client, namespace_name=model_registry_namespace)
@pytest.fixture(scope="class")
def model_registry_metadata_db_resources(
request: FixtureRequest,
admin_client: DynamicClient,
pytestconfig: Config,
teardown_resources: bool,
model_registry_namespace: str,
) -> Generator[dict[Any, Any], None, None]:
num_resources = getattr(request, "param", {}).get("num_resources", 1)
db_backend = getattr(request, "param", {}).get("db_name", "mysql")
if pytestconfig.option.post_upgrade:
resources = {
Secret: [
Secret(
client=admin_client, name=DB_RESOURCE_NAME, namespace=model_registry_namespace, ensure_exists=True
)
],
PersistentVolumeClaim: [
PersistentVolumeClaim(
client=admin_client, name=DB_RESOURCE_NAME, namespace=model_registry_namespace, ensure_exists=True
)
],
Service: [
Service(
client=admin_client, name=DB_RESOURCE_NAME, namespace=model_registry_namespace, ensure_exists=True
)
],
ConfigMap: [
ConfigMap(
client=admin_client, name=DB_RESOURCE_NAME, namespace=model_registry_namespace, ensure_exists=True
)
]
if db_backend == "mariadb"
else [],
Deployment: [
Deployment(
client=admin_client, name=DB_RESOURCE_NAME, namespace=model_registry_namespace, ensure_exists=True
)
],
}
yield resources
for kind in [Deployment, ConfigMap, Service, PersistentVolumeClaim, Secret]:
for resource in resources[kind]:
resource.delete(wait=True)
else:
resources_instances = {}
if db_backend == "default":
yield resources_instances
else:
resources = get_model_registry_metadata_resources(
base_name=DB_BASE_RESOURCES_NAME,
namespace=model_registry_namespace,
num_resources=num_resources,
db_backend=db_backend,
teardown_resources=teardown_resources,
client=admin_client,
)
with ExitStack() as stack:
for kind_name in [Secret, PersistentVolumeClaim, Service, ConfigMap, Deployment]:
if resources[kind_name]:
LOGGER.info(f"Creating {num_resources} {kind_name} resources")
resources_instances[kind_name] = [
stack.enter_context(resource_obj) for resource_obj in resources[kind_name]
]
for deployment in resources_instances[Deployment]:
deployment.wait_for_replicas(deployed=True)
yield resources_instances
@pytest.fixture(scope="class")
def model_registry_rest_headers(current_client_token: str) -> dict[str, str]:
return get_rest_headers(token=current_client_token)
@pytest.fixture(scope="class")
def sa_namespace(request: pytest.FixtureRequest, admin_client: DynamicClient) -> Generator[Namespace, None, None]:
"""
Creates a namespace
"""
test_file = os.path.relpath(request.fspath.strpath, start=os.path.dirname(__file__))
ns_name = generate_namespace_name(file_path=test_file)
LOGGER.info(f"Creating temporary namespace: {ns_name}")
with Namespace(client=admin_client, name=ns_name) as ns:
ns.wait_for_status(status=Namespace.Status.ACTIVE, timeout=120)
yield ns
@pytest.fixture(scope="class")
def service_account(admin_client: DynamicClient, sa_namespace: Namespace) -> Generator[Any, None, None]:
"""
Creates a ServiceAccount.
"""
sa_name = generate_random_name(prefix="mr-test-user")
LOGGER.info(f"Creating ServiceAccount: {sa_name} in namespace {sa_namespace.name}")
with ServiceAccount(client=admin_client, name=sa_name, namespace=sa_namespace.name, wait_for_resource=True) as sa:
yield sa