|
1 | 1 | import pytest |
| 2 | +import re |
2 | 3 | import schemathesis |
3 | 4 | from typing import Generator, Any |
| 5 | +from kubernetes.dynamic.exceptions import ResourceNotFoundError |
| 6 | +from ocp_resources.pod import Pod |
4 | 7 | from ocp_resources.secret import Secret |
5 | 8 | from ocp_resources.namespace import Namespace |
6 | 9 | from ocp_resources.service import Service |
7 | 10 | from ocp_resources.persistent_volume_claim import PersistentVolumeClaim |
| 11 | +from ocp_resources.data_science_cluster import DataScienceCluster |
8 | 12 | from ocp_resources.deployment import Deployment |
| 13 | + |
9 | 14 | from ocp_resources.model_registry import ModelRegistry |
| 15 | +from ocp_resources.resource import ResourceEditor |
| 16 | + |
| 17 | +from pytest import FixtureRequest |
10 | 18 | from simple_logger.logger import get_logger |
11 | 19 | from kubernetes.dynamic import DynamicClient |
12 | | - |
| 20 | +from pytest_testconfig import config as py_config |
| 21 | +from model_registry.types import RegisteredModel |
| 22 | +from tests.model_registry.constants import ( |
| 23 | + MR_NAMESPACE, |
| 24 | + MR_OPERATOR_NAME, |
| 25 | + MR_INSTANCE_NAME, |
| 26 | + ISTIO_CONFIG_DICT, |
| 27 | + DB_RESOURCES_NAME, |
| 28 | + MR_DB_IMAGE_DIGEST, |
| 29 | +) |
13 | 30 | from tests.model_registry.utils import get_endpoint_from_mr_service, get_mr_service_by_label |
14 | 31 | from utilities.infra import create_ns |
15 | | -from utilities.constants import Annotations, Protocols |
16 | | -from .constants import MR_DB_IMAGE_DIGEST |
| 32 | +from utilities.constants import Annotations, Protocols, DscComponents |
| 33 | +from model_registry import ModelRegistry as ModelRegistryClient |
17 | 34 |
|
18 | 35 |
|
19 | 36 | LOGGER = get_logger(name=__name__) |
20 | 37 |
|
21 | | -DB_RESOURCES_NAME: str = "model-registry-db" |
22 | | -MR_INSTANCE_NAME: str = "model-registry" |
23 | | -MR_OPERATOR_NAME: str = "model-registry-operator" |
24 | | -MR_NAMESPACE: str = "rhoai-model-registries" |
25 | 38 | DEFAULT_LABEL_DICT_DB: dict[str, str] = { |
26 | 39 | Annotations.KubernetesIo.NAME: DB_RESOURCES_NAME, |
27 | 40 | Annotations.KubernetesIo.INSTANCE: DB_RESOURCES_NAME, |
|
30 | 43 |
|
31 | 44 |
|
32 | 45 | @pytest.fixture(scope="class") |
33 | | -def model_registry_namespace(admin_client: DynamicClient) -> Generator[Namespace, Any, Any]: |
34 | | - # This namespace should exist after Model Registry is enabled, but it can also be deleted |
35 | | - # from the cluster and does not get reconciled. Fetch if it exists, create otherwise. |
36 | | - ns = Namespace(name=MR_NAMESPACE, client=admin_client) |
37 | | - if ns.exists: |
| 46 | +def model_registry_namespace(request: FixtureRequest, admin_client: DynamicClient) -> Generator[Namespace, Any, Any]: |
| 47 | + with create_ns( |
| 48 | + name=request.param.get("namespace_name", MR_NAMESPACE), |
| 49 | + admin_client=admin_client, |
| 50 | + ) as ns: |
38 | 51 | yield ns |
39 | | - else: |
40 | | - LOGGER.warning(f"{MR_NAMESPACE} namespace was not present, creating it") |
41 | | - with create_ns( |
42 | | - name=MR_NAMESPACE, |
43 | | - admin_client=admin_client, |
44 | | - teardown=False, |
45 | | - ) as ns: |
46 | | - yield ns |
47 | 52 |
|
48 | 53 |
|
49 | 54 | @pytest.fixture(scope="class") |
@@ -259,10 +264,7 @@ def model_registry_instance( |
259 | 264 | }, |
260 | 265 | grpc={}, |
261 | 266 | rest={}, |
262 | | - istio={ |
263 | | - "authProvider": "redhat-ods-applications-auth-provider", |
264 | | - "gateway": {"grpc": {"tls": {}}, "rest": {"tls": {}}}, |
265 | | - }, |
| 267 | + istio=ISTIO_CONFIG_DICT, |
266 | 268 | mysql={ |
267 | 269 | "host": f"{model_registry_db_deployment.name}.{model_registry_db_deployment.namespace}.svc.cluster.local", |
268 | 270 | "database": model_registry_db_secret.string_data["database-name"], |
@@ -304,3 +306,70 @@ def generated_schema(model_registry_instance_rest_endpoint: str) -> Any: |
304 | 306 | uri="https://raw.githubusercontent.com/kubeflow/model-registry/main/api/openapi/model-registry.yaml", |
305 | 307 | base_url=f"https://{model_registry_instance_rest_endpoint}/", |
306 | 308 | ) |
| 309 | + |
| 310 | + |
| 311 | +@pytest.fixture(scope="class") |
| 312 | +def updated_dsc_component_state_scope_class( |
| 313 | + request: FixtureRequest, |
| 314 | + model_registry_namespace: Namespace, |
| 315 | + dsc_resource: DataScienceCluster, |
| 316 | +) -> Generator[DataScienceCluster, Any, Any]: |
| 317 | + original_components = dsc_resource.instance.spec.components |
| 318 | + with ResourceEditor(patches={dsc_resource: {"spec": {"components": request.param["component_patch"]}}}): |
| 319 | + for component_name in request.param["component_patch"]: |
| 320 | + dsc_resource.wait_for_condition(condition=DscComponents.COMPONENT_MAPPING[component_name], status="True") |
| 321 | + yield dsc_resource |
| 322 | + |
| 323 | + for component_name, value in request.param["component_patch"].items(): |
| 324 | + LOGGER.info(f"Waiting for component {component_name} to be updated.") |
| 325 | + if original_components[component_name]["managementState"] == DscComponents.ManagementState.MANAGED: |
| 326 | + dsc_resource.wait_for_condition(condition=DscComponents.COMPONENT_MAPPING[component_name], status="True") |
| 327 | + if ( |
| 328 | + component_name == DscComponents.MODELREGISTRY |
| 329 | + and value.get("managementState") == DscComponents.ManagementState.MANAGED |
| 330 | + ): |
| 331 | + # Since namespace specified in registriesNamespace is automatically created after setting |
| 332 | + # managementStateto Managed. We need to explicitly delete it on clean up. |
| 333 | + namespace = Namespace(name=value["registriesNamespace"], ensure_exists=True) |
| 334 | + if namespace: |
| 335 | + namespace.delete(wait=True) |
| 336 | + |
| 337 | + |
| 338 | +@pytest.fixture(scope="class") |
| 339 | +def model_registry_client(current_client_token: str, model_registry_instance_rest_endpoint: str) -> ModelRegistryClient: |
| 340 | + # address and port need to be split in the client instantiation |
| 341 | + server, port = model_registry_instance_rest_endpoint.split(":") |
| 342 | + return ModelRegistryClient( |
| 343 | + server_address=f"{Protocols.HTTPS}://{server}", |
| 344 | + port=port, |
| 345 | + author="opendatahub-test", |
| 346 | + user_token=current_client_token, |
| 347 | + is_secure=False, |
| 348 | + ) |
| 349 | + |
| 350 | + |
| 351 | +@pytest.fixture(scope="class") |
| 352 | +def registered_model(request: FixtureRequest, model_registry_client: ModelRegistryClient) -> RegisteredModel: |
| 353 | + return model_registry_client.register_model( |
| 354 | + name=request.param.get("model_name"), |
| 355 | + uri=request.param.get("model_uri"), |
| 356 | + version=request.param.get("model_version"), |
| 357 | + description=request.param.get("model_description"), |
| 358 | + model_format_name=request.param.get("model_format"), |
| 359 | + model_format_version=request.param.get("model_format_version"), |
| 360 | + storage_key=request.param.get("model_storage_key"), |
| 361 | + storage_path=request.param.get("model_storage_path"), |
| 362 | + metadata=request.param.get("model_metadata"), |
| 363 | + ) |
| 364 | + |
| 365 | + |
| 366 | +@pytest.fixture() |
| 367 | +def model_registry_operator_pod(admin_client: DynamicClient) -> Pod: |
| 368 | + model_registry_operator_pods = [ |
| 369 | + pod |
| 370 | + for pod in Pod.get(dyn_client=admin_client, namespace=py_config["applications_namespace"]) |
| 371 | + if re.match(MR_OPERATOR_NAME, pod.name) |
| 372 | + ] |
| 373 | + if not model_registry_operator_pods: |
| 374 | + raise ResourceNotFoundError("Model registry operator pod not found") |
| 375 | + return model_registry_operator_pods[0] |
0 commit comments