Skip to content

Commit 9c725f9

Browse files
committed
change: wait for the pod to be in the correct status before to read the logs
1 parent 7b1a01b commit 9c725f9

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

tests/model_registry/negative_tests/test_db_migration.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from utilities.constants import DscComponents
88
from tests.model_registry.constants import MR_INSTANCE_NAME
99
from kubernetes.dynamic.client import DynamicClient
10-
from utilities.general import wait_for_pods_by_labels
10+
from utilities.general import wait_for_pods_by_labels, wait_for_container_status
1111

1212

1313
LOGGER = get_logger(name=__name__)
@@ -51,6 +51,9 @@ def test_db_migration_negative(
5151
expected_num_pods=1,
5252
)
5353
mr_pod = mr_pods[0]
54+
LOGGER.info("Waiting for model registry pod to crash")
55+
assert wait_for_container_status(mr_pod, "rest-container", Pod.Status.CRASH_LOOPBACK_OFF)
56+
5457
LOGGER.info("Checking the logs for the expected error")
5558

5659
log_output = mr_pod.log(container="rest-container")

utilities/general.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
from simple_logger.logger import get_logger
1212

1313
import utilities.infra
14-
from utilities.constants import Annotations, KServeDeploymentType, MODELMESH_SERVING
15-
from utilities.exceptions import UnexpectedResourceCountError
14+
from utilities.constants import Annotations, KServeDeploymentType, MODELMESH_SERVING, Timeout
15+
from utilities.exceptions import UnexpectedResourceCountError, ResourceValueMismatch
1616
from ocp_resources.resource import Resource
1717
from timeout_sampler import retry
1818

@@ -331,3 +331,27 @@ def generate_random_name(prefix: str = "", length: int = 8) -> str:
331331
# random_uuid.hex is 32 characters long.
332332
suffix = random_uuid.hex[:length]
333333
return f"{prefix}-{suffix}" if prefix else suffix
334+
335+
336+
@retry(wait_timeout=Timeout.TIMEOUT_15_SEC, sleep=1)
337+
def wait_for_container_status(pod: Pod, container_name: str, expected_status: str) -> bool:
338+
"""
339+
Wait for a container to be in the expected status.
340+
341+
Args:
342+
pod: The pod to wait for
343+
container_name: The name of the container to wait for
344+
expected_status: The expected status
345+
346+
Returns:
347+
bool: True if the container is in the expected status, False otherwise
348+
349+
Raises:
350+
ResourceValueMismatch: If the container is not in the expected status
351+
"""
352+
353+
for cs in pod.instance.status.get("containerStatuses", []):
354+
if cs.name == container_name and cs.state.waiting.reason == expected_status:
355+
LOGGER.info(f"Pod is in the expected status {expected_status}")
356+
return True
357+
raise ResourceValueMismatch(f"Pod is not in the expected status {expected_status}")

0 commit comments

Comments
 (0)