Skip to content

Commit c4c14bd

Browse files
authored
Merge branch 'main' into upgrade
2 parents 64d59f7 + 76ce3db commit c4c14bd

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

tests/model_registry/conftest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,55 @@ def model_registry_instance_pod(admin_client: DynamicClient) -> Generator[Pod, A
460460
)[0]
461461

462462

463+
@pytest.fixture()
464+
def model_registry_db_instance_pod(admin_client: DynamicClient) -> Generator[Pod, Any, Any]:
465+
"""Get the model registry instance pod."""
466+
yield wait_for_pods_by_labels(
467+
admin_client=admin_client,
468+
namespace=py_config["model_registry_namespace"],
469+
label_selector=f"name={DB_RESOURCES_NAME}",
470+
expected_num_pods=1,
471+
)[0]
472+
473+
474+
@pytest.fixture()
475+
def set_mr_db_dirty(model_registry_db_instance_pod: Pod) -> int:
476+
"""Set the model registry database dirty and return the latest migration version"""
477+
output = model_registry_db_instance_pod.execute(
478+
command=[
479+
"mysql",
480+
"-u",
481+
MODEL_REGISTRY_DB_SECRET_STR_DATA["database-user"],
482+
f"-p{MODEL_REGISTRY_DB_SECRET_STR_DATA['database-password']}",
483+
"-e",
484+
"SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1;",
485+
MODEL_REGISTRY_DB_SECRET_STR_DATA["database-name"],
486+
]
487+
)
488+
latest_migration_version = int(output.strip().split()[1])
489+
model_registry_db_instance_pod.execute(
490+
command=[
491+
"mysql",
492+
"-u",
493+
MODEL_REGISTRY_DB_SECRET_STR_DATA["database-user"],
494+
f"-p{MODEL_REGISTRY_DB_SECRET_STR_DATA['database-password']}",
495+
"-e",
496+
f"UPDATE schema_migrations SET dirty = 1 WHERE version = {latest_migration_version};",
497+
MODEL_REGISTRY_DB_SECRET_STR_DATA["database-name"],
498+
]
499+
)
500+
return latest_migration_version
501+
502+
503+
@pytest.fixture()
504+
def delete_mr_deployment() -> None:
505+
"""Delete the model registry deployment"""
506+
mr_deployment = Deployment(
507+
name=MR_INSTANCE_NAME, namespace=py_config["model_registry_namespace"], ensure_exists=True
508+
)
509+
mr_deployment.delete(wait=True)
510+
511+
463512
@pytest.fixture(scope="class")
464513
def is_model_registry_oauth(request: FixtureRequest) -> bool:
465514
return getattr(request, "param", {}).get("use_oauth_proxy", True)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import pytest
2+
from typing import Self
3+
from simple_logger.logger import get_logger
4+
from ocp_resources.model_registry_modelregistry_opendatahub_io import ModelRegistry
5+
from pytest_testconfig import config as py_config
6+
from ocp_resources.pod import Pod
7+
from utilities.constants import DscComponents
8+
from tests.model_registry.constants import MR_INSTANCE_NAME
9+
from kubernetes.dynamic.client import DynamicClient
10+
from utilities.general import wait_for_pods_by_labels
11+
12+
13+
LOGGER = get_logger(name=__name__)
14+
15+
16+
@pytest.mark.parametrize(
17+
"updated_dsc_component_state_scope_class",
18+
[
19+
pytest.param({
20+
"component_patch": {
21+
DscComponents.MODELREGISTRY: {
22+
"managementState": DscComponents.ManagementState.MANAGED,
23+
"registriesNamespace": py_config["model_registry_namespace"],
24+
},
25+
}
26+
}),
27+
],
28+
indirect=True,
29+
)
30+
@pytest.mark.usefixtures("updated_dsc_component_state_scope_class")
31+
class TestDBMigration:
32+
def test_db_migration_negative(
33+
self: Self,
34+
admin_client: DynamicClient,
35+
model_registry_instance: ModelRegistry,
36+
model_registry_db_instance_pod: Pod,
37+
set_mr_db_dirty: int,
38+
delete_mr_deployment: None,
39+
):
40+
"""
41+
RHOAIENG-27505: This test is to check the migration error when the database is dirty.
42+
The test will:
43+
1. Set the dirty flag to 1 for the latest migration version
44+
2. Delete the model registry deployment
45+
3. Check the logs for the expected error
46+
"""
47+
mr_pods = wait_for_pods_by_labels(
48+
admin_client=admin_client,
49+
namespace=py_config["model_registry_namespace"],
50+
label_selector=f"app={MR_INSTANCE_NAME}",
51+
expected_num_pods=1,
52+
)
53+
mr_pod = mr_pods[0]
54+
LOGGER.info("Checking the logs for the expected error")
55+
56+
log_output = mr_pod.log(container="rest-container")
57+
expected_error = (
58+
f"Error: {{{{ALERT}}}} error connecting to datastore: Dirty database version {set_mr_db_dirty}. "
59+
"Fix and force version."
60+
)
61+
assert expected_error in log_output, "Expected error message not found in logs!"

0 commit comments

Comments
 (0)