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
294 lines (256 loc) · 11.3 KB
/
conftest.py
File metadata and controls
294 lines (256 loc) · 11.3 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
import pytest
import re
import schemathesis
from typing import Generator, Any
from kubernetes.dynamic.exceptions import ResourceNotFoundError
from ocp_resources.pod import Pod
from ocp_resources.secret import Secret
from ocp_resources.namespace import Namespace
from ocp_resources.service import Service
from ocp_resources.persistent_volume_claim import PersistentVolumeClaim
from ocp_resources.data_science_cluster import DataScienceCluster
from ocp_resources.deployment import Deployment
from ocp_resources.model_registry import ModelRegistry
from schemathesis.specs.openapi.schemas import BaseOpenAPISchema
from schemathesis.generation.stateful.state_machine import APIStateMachine
from schemathesis.core.transport import Response
from schemathesis.generation.case import Case
from ocp_resources.resource import ResourceEditor
from pytest import FixtureRequest
from simple_logger.logger import get_logger
from kubernetes.dynamic import DynamicClient
from pytest_testconfig import config as py_config
from model_registry.types import RegisteredModel
from tests.model_registry.constants import (
MR_OPERATOR_NAME,
MR_INSTANCE_NAME,
ISTIO_CONFIG_DICT,
DB_RESOURCES_NAME,
MODEL_REGISTRY_DB_SECRET_STR_DATA,
MODEL_REGISTRY_DB_SECRET_ANNOTATIONS,
)
from tests.model_registry.utils import (
get_endpoint_from_mr_service,
get_mr_service_by_label,
get_model_registry_deployment_template_dict,
get_model_registry_db_label_dict,
wait_for_pods_running,
)
from utilities.constants import Annotations, Protocols, DscComponents
from model_registry import ModelRegistry as ModelRegistryClient
LOGGER = get_logger(name=__name__)
@pytest.fixture(scope="class")
def model_registry_namespace(updated_dsc_component_state_scope_class: DataScienceCluster) -> str:
return updated_dsc_component_state_scope_class.instance.spec.components.modelregistry.registriesNamespace
@pytest.fixture(scope="class")
def model_registry_db_service(
admin_client: DynamicClient, model_registry_namespace: str
) -> Generator[Service, Any, Any]:
with Service(
client=admin_client,
name=DB_RESOURCES_NAME,
namespace=model_registry_namespace,
ports=[
{
"name": "mysql",
"nodePort": 0,
"port": 3306,
"protocol": "TCP",
"appProtocol": "tcp",
"targetPort": 3306,
}
],
selector={
"name": DB_RESOURCES_NAME,
},
label=get_model_registry_db_label_dict(db_resource_name=DB_RESOURCES_NAME),
annotations={
"template.openshift.io/expose-uri": r"mysql://{.spec.clusterIP}:{.spec.ports[?(.name==\mysql\)].port}",
},
) as mr_db_service:
yield mr_db_service
@pytest.fixture(scope="class")
def model_registry_db_pvc(
admin_client: DynamicClient, model_registry_namespace: str
) -> Generator[PersistentVolumeClaim, Any, Any]:
with PersistentVolumeClaim(
accessmodes="ReadWriteOnce",
name=DB_RESOURCES_NAME,
namespace=model_registry_namespace,
client=admin_client,
size="5Gi",
label=get_model_registry_db_label_dict(db_resource_name=DB_RESOURCES_NAME),
) as pvc:
yield pvc
@pytest.fixture(scope="class")
def model_registry_db_secret(
admin_client: DynamicClient,
model_registry_namespace: str,
) -> Generator[Secret, Any, Any]:
with Secret(
client=admin_client,
name=DB_RESOURCES_NAME,
namespace=model_registry_namespace,
string_data=MODEL_REGISTRY_DB_SECRET_STR_DATA,
label=get_model_registry_db_label_dict(db_resource_name=DB_RESOURCES_NAME),
annotations=MODEL_REGISTRY_DB_SECRET_ANNOTATIONS,
) as mr_db_secret:
yield mr_db_secret
@pytest.fixture(scope="class")
def model_registry_db_deployment(
admin_client: DynamicClient,
model_registry_namespace: str,
model_registry_db_secret: Secret,
model_registry_db_pvc: PersistentVolumeClaim,
model_registry_db_service: Service,
) -> Generator[Deployment, Any, Any]:
with Deployment(
name=DB_RESOURCES_NAME,
namespace=model_registry_namespace,
annotations={
"template.alpha.openshift.io/wait-for-ready": "true",
},
label=get_model_registry_db_label_dict(db_resource_name=DB_RESOURCES_NAME),
replicas=1,
revision_history_limit=0,
selector={"matchLabels": {"name": DB_RESOURCES_NAME}},
strategy={"type": "Recreate"},
template=get_model_registry_deployment_template_dict(
secret_name=model_registry_db_secret.name, resource_name=DB_RESOURCES_NAME
),
wait_for_resource=True,
) as mr_db_deployment:
mr_db_deployment.wait_for_replicas(deployed=True)
yield mr_db_deployment
@pytest.fixture(scope="class")
def model_registry_instance(
admin_client: DynamicClient,
model_registry_namespace: str,
model_registry_db_deployment: Deployment,
model_registry_db_secret: Secret,
model_registry_db_service: Service,
) -> Generator[ModelRegistry, Any, Any]:
with ModelRegistry(
name=MR_INSTANCE_NAME,
namespace=model_registry_namespace,
label={
Annotations.KubernetesIo.NAME: MR_INSTANCE_NAME,
Annotations.KubernetesIo.INSTANCE: MR_INSTANCE_NAME,
Annotations.KubernetesIo.PART_OF: MR_OPERATOR_NAME,
Annotations.KubernetesIo.CREATED_BY: MR_OPERATOR_NAME,
},
grpc={},
rest={},
istio=ISTIO_CONFIG_DICT,
mysql={
"host": f"{model_registry_db_deployment.name}.{model_registry_db_deployment.namespace}.svc.cluster.local",
"database": model_registry_db_secret.string_data["database-name"],
"passwordSecret": {"key": "database-password", "name": DB_RESOURCES_NAME},
"port": 3306,
"skipDBCreation": False,
"username": model_registry_db_secret.string_data["database-user"],
},
wait_for_resource=True,
) as mr:
mr.wait_for_condition(condition="Available", status="True")
yield mr
@pytest.fixture(scope="class")
def model_registry_instance_service(
admin_client: DynamicClient,
model_registry_namespace: str,
model_registry_instance: ModelRegistry,
) -> Service:
return get_mr_service_by_label(
client=admin_client, ns=Namespace(name=model_registry_namespace), mr_instance=model_registry_instance
)
@pytest.fixture(scope="class")
def model_registry_instance_rest_endpoint(
model_registry_instance_service: Service,
) -> str:
return get_endpoint_from_mr_service(svc=model_registry_instance_service, protocol=Protocols.REST)
@pytest.fixture(scope="class")
def generated_schema(model_registry_instance_rest_endpoint: str) -> BaseOpenAPISchema:
schema = schemathesis.openapi.from_url(
url="https://raw.githubusercontent.com/kubeflow/model-registry/main/api/openapi/model-registry.yaml"
)
schema.configure(base_url=f"https://{model_registry_instance_rest_endpoint}/")
return schema
@pytest.fixture
def state_machine(generated_schema: BaseOpenAPISchema, current_client_token: str) -> APIStateMachine:
BaseAPIWorkflow = generated_schema.as_state_machine()
class APIWorkflow(BaseAPIWorkflow): # type: ignore
headers: dict[str, str]
def setup(self) -> None:
self.headers = {"Authorization": f"Bearer {current_client_token}", "Content-Type": "application/json"}
# these kwargs are passed to requests.request()
def get_call_kwargs(self, case: Case) -> dict[str, Any]:
return {"verify": False, "headers": self.headers}
def after_call(self, response: Response, case: Case) -> None:
LOGGER.info(f"{case.method} {case.path} -> {response.status_code}")
return APIWorkflow
@pytest.fixture(scope="class")
def updated_dsc_component_state_scope_class(
request: FixtureRequest, dsc_resource: DataScienceCluster, admin_client: DynamicClient
) -> Generator[DataScienceCluster, Any, Any]:
original_components = dsc_resource.instance.spec.components
with ResourceEditor(patches={dsc_resource: {"spec": {"components": request.param["component_patch"]}}}):
for component_name in request.param["component_patch"]:
dsc_resource.wait_for_condition(condition=DscComponents.COMPONENT_MAPPING[component_name], status="True")
if request.param["component_patch"].get(DscComponents.MODELREGISTRY):
namespace = Namespace(
name=dsc_resource.instance.spec.components.modelregistry.registriesNamespace, ensure_exists=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,
)
yield dsc_resource
for component_name, value in request.param["component_patch"].items():
LOGGER.info(f"Waiting for component {component_name} to be updated.")
if original_components[component_name]["managementState"] == DscComponents.ManagementState.MANAGED:
dsc_resource.wait_for_condition(condition=DscComponents.COMPONENT_MAPPING[component_name], status="True")
if (
component_name == DscComponents.MODELREGISTRY
and value.get("managementState") == DscComponents.ManagementState.MANAGED
):
# Since namespace specified in registriesNamespace is automatically created after setting
# managementStateto Managed. We need to explicitly delete it on clean up.
namespace = Namespace(name=value["registriesNamespace"], ensure_exists=True)
if namespace:
namespace.delete(wait=True)
@pytest.fixture(scope="class")
def model_registry_client(current_client_token: str, model_registry_instance_rest_endpoint: str) -> ModelRegistryClient:
# address and port need to be split in the client instantiation
server, port = model_registry_instance_rest_endpoint.split(":")
return ModelRegistryClient(
server_address=f"{Protocols.HTTPS}://{server}",
port=port,
author="opendatahub-test",
user_token=current_client_token,
is_secure=False,
)
@pytest.fixture(scope="class")
def registered_model(request: FixtureRequest, model_registry_client: ModelRegistryClient) -> RegisteredModel:
return model_registry_client.register_model(
name=request.param.get("model_name"),
uri=request.param.get("model_uri"),
version=request.param.get("model_version"),
description=request.param.get("model_description"),
model_format_name=request.param.get("model_format"),
model_format_version=request.param.get("model_format_version"),
storage_key=request.param.get("model_storage_key"),
storage_path=request.param.get("model_storage_path"),
metadata=request.param.get("model_metadata"),
)
@pytest.fixture()
def model_registry_operator_pod(admin_client: DynamicClient) -> Pod:
model_registry_operator_pods = [
pod
for pod in Pod.get(dyn_client=admin_client, namespace=py_config["applications_namespace"])
if re.match(MR_OPERATOR_NAME, pod.name)
]
if not model_registry_operator_pods:
raise ResourceNotFoundError("Model registry operator pod not found")
return model_registry_operator_pods[0]