|
2 | 2 | from typing import Generator, Any |
3 | 3 |
|
4 | 4 | from _pytest.config import Config |
| 5 | +from pytest_testconfig import config as py_config |
5 | 6 |
|
6 | 7 | from ocp_resources.data_science_cluster import DataScienceCluster |
| 8 | +from ocp_resources.pod import Pod |
7 | 9 | from ocp_resources.secret import Secret |
8 | 10 | from ocp_resources.namespace import Namespace |
9 | 11 | from ocp_resources.service import Service |
|
15 | 17 |
|
16 | 18 | from tests.model_registry.constants import ( |
17 | 19 | MODEL_REGISTRY_DB_SECRET_STR_DATA, |
18 | | - MODEL_REGISTRY_DB_SECRET_ANNOTATIONS, |
| 20 | + MODEL_REGISTRY_DB_SECRET_ANNOTATIONS, DB_RESOURCE_NAME, MR_INSTANCE_NAME, |
19 | 21 | ) |
20 | 22 | from tests.model_registry.utils import get_model_registry_deployment_template_dict, get_model_registry_db_label_dict |
21 | 23 | from utilities.constants import MODEL_REGISTRY_CUSTOM_NAMESPACE |
| 24 | +from utilities.general import wait_for_pods_by_labels |
22 | 25 | from utilities.infra import create_ns |
23 | 26 |
|
24 | 27 | DB_RESOURCES_NAME_NEGATIVE = "db-model-registry-negative" |
@@ -121,9 +124,58 @@ def model_registry_db_deployment_negative_test( |
121 | 124 | selector={"matchLabels": {"name": DB_RESOURCES_NAME_NEGATIVE}}, |
122 | 125 | strategy={"type": "Recreate"}, |
123 | 126 | template=get_model_registry_deployment_template_dict( |
124 | | - secret_name=model_registry_db_secret_negative_test.name, resource_name=DB_RESOURCES_NAME_NEGATIVE |
| 127 | + secret_name=model_registry_db_secret_negative_test.name, resource_name=DB_RESOURCES_NAME_NEGATIVE, db_backend="mysql" |
125 | 128 | ), |
126 | 129 | wait_for_resource=True, |
127 | 130 | ) as mr_db_deployment: |
128 | 131 | mr_db_deployment.wait_for_replicas(deployed=True) |
129 | 132 | yield mr_db_deployment |
| 133 | + |
| 134 | + |
| 135 | +@pytest.fixture() |
| 136 | +def set_mr_db_dirty(model_registry_db_instance_pod: Pod) -> int: |
| 137 | + """Set the model registry database dirty and return the latest migration version""" |
| 138 | + output = model_registry_db_instance_pod.execute( |
| 139 | + command=[ |
| 140 | + "mysql", |
| 141 | + "-u", |
| 142 | + MODEL_REGISTRY_DB_SECRET_STR_DATA["database-user"], |
| 143 | + f"-p{MODEL_REGISTRY_DB_SECRET_STR_DATA['database-password']}", |
| 144 | + "-e", |
| 145 | + "SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1;", |
| 146 | + MODEL_REGISTRY_DB_SECRET_STR_DATA["database-name"], |
| 147 | + ] |
| 148 | + ) |
| 149 | + latest_migration_version = int(output.strip().split()[1]) |
| 150 | + model_registry_db_instance_pod.execute( |
| 151 | + command=[ |
| 152 | + "mysql", |
| 153 | + "-u", |
| 154 | + MODEL_REGISTRY_DB_SECRET_STR_DATA["database-user"], |
| 155 | + f"-p{MODEL_REGISTRY_DB_SECRET_STR_DATA['database-password']}", |
| 156 | + "-e", |
| 157 | + f"UPDATE schema_migrations SET dirty = 1 WHERE version = {latest_migration_version};", |
| 158 | + MODEL_REGISTRY_DB_SECRET_STR_DATA["database-name"], |
| 159 | + ] |
| 160 | + ) |
| 161 | + return latest_migration_version |
| 162 | + |
| 163 | + |
| 164 | +@pytest.fixture() |
| 165 | +def model_registry_db_instance_pod(admin_client: DynamicClient) -> Generator[Pod, Any, Any]: |
| 166 | + """Get the model registry instance pod.""" |
| 167 | + yield wait_for_pods_by_labels( |
| 168 | + admin_client=admin_client, |
| 169 | + namespace=py_config["model_registry_namespace"], |
| 170 | + label_selector=f"name={DB_RESOURCE_NAME}", |
| 171 | + expected_num_pods=1, |
| 172 | + )[0] |
| 173 | + |
| 174 | + |
| 175 | +@pytest.fixture() |
| 176 | +def delete_mr_deployment() -> None: |
| 177 | + """Delete the model registry deployment""" |
| 178 | + mr_deployment = Deployment( |
| 179 | + name=MR_INSTANCE_NAME, namespace=py_config["model_registry_namespace"], ensure_exists=True |
| 180 | + ) |
| 181 | + mr_deployment.delete(wait=True) |
0 commit comments