Skip to content

Commit 35f5731

Browse files
committed
change: Create a general function and update tests
1 parent 6a9a00c commit 35f5731

File tree

4 files changed

+59
-97
lines changed

4 files changed

+59
-97
lines changed

tests/model_registry/negative_tests/test_db_migration.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
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_container_status
11-
from tests.model_registry.utils import wait_for_new_running_mr_pods
10+
from utilities.general import wait_for_container_status, wait_for_new_running_pods
11+
from utilities.constants import Timeout
1212

1313
LOGGER = get_logger(name=__name__)
1414

@@ -48,11 +48,17 @@ def test_db_migration_negative(
4848
4. Check the logs for the expected error
4949
"""
5050
LOGGER.info(f"Model registry pod: {model_registry_pod.name}")
51-
mr_pods = wait_for_new_running_mr_pods(
52-
admin_client=admin_client,
51+
mr_pods = wait_for_new_running_pods(
5352
orig_pods=[model_registry_pod],
54-
namespace=py_config["model_registry_namespace"],
55-
instance_name=MR_INSTANCE_NAME,
53+
pod_fetcher_func=lambda: list(
54+
Pod.get(
55+
dyn_client=admin_client,
56+
namespace=py_config["model_registry_namespace"],
57+
label_selector=f"app={MR_INSTANCE_NAME}",
58+
)
59+
),
60+
timeout=Timeout.TIMEOUT_2MIN,
61+
return_pods=True,
5662
)
5763
mr_pod = mr_pods[0]
5864
LOGGER.info(f"Pod that should contains the container in CrashLoopBackOff state: {mr_pod.name}")

tests/model_registry/utils.py

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -235,54 +235,6 @@ def wait_for_pods_running(
235235
return None
236236

237237

238-
def wait_for_new_running_mr_pods(
239-
admin_client: DynamicClient,
240-
orig_pods: list[Pod],
241-
namespace: str,
242-
instance_name: str,
243-
expected_num_pods: int | None = None,
244-
) -> list[Pod]:
245-
"""
246-
Wait for the model registry pod to be replaced.
247-
248-
Args:
249-
admin_client (DynamicClient): The admin client.
250-
orig_pods (list): List of Pod objects.
251-
expected_num_pods (int): Number of pods expected to be running.
252-
If not provided, the number of pods is expected to be len(orig_pods)
253-
Returns:
254-
List of Pod objects.
255-
256-
Raises:
257-
TimeoutError: If the pods are not replaced.
258-
259-
"""
260-
LOGGER.info("Waiting for pods to be replaced")
261-
oring_pods_names = [pod.name for pod in orig_pods]
262-
263-
expected_num_pods = expected_num_pods or len(orig_pods)
264-
265-
try:
266-
for pods in TimeoutSampler(
267-
wait_timeout=180,
268-
sleep=5,
269-
func=lambda: list(
270-
Pod.get(
271-
dyn_client=admin_client,
272-
namespace=namespace,
273-
label_selector=f"app={instance_name}",
274-
)
275-
),
276-
):
277-
if pods and len(pods) == expected_num_pods:
278-
if all(pod.name not in oring_pods_names and pod.status == pod.Status.RUNNING for pod in pods):
279-
return pods
280-
281-
except TimeoutError:
282-
LOGGER.error(f"Timeout waiting for pods {oring_pods_names} to be replaced")
283-
raise
284-
285-
286238
def generate_namespace_name(file_path: str) -> str:
287239
return (file_path.removesuffix(".py").replace("/", "-").replace("_", "-"))[-63:].split("-", 1)[-1]
288240

tests/model_serving/model_server/inference_service_configuration/utils.py

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33

44
from kubernetes.dynamic import DynamicClient
55
from ocp_resources.inference_service import InferenceService
6-
from ocp_resources.pod import Pod
76
from ocp_resources.resource import ResourceEditor
87
from simple_logger.logger import get_logger
9-
from timeout_sampler import TimeoutSampler
108

119
from utilities.constants import Timeout
1210
from utilities.infra import get_pods_by_isvc_label
11+
from utilities.general import wait_for_new_running_pods
1312

1413
LOGGER = get_logger(name=__name__)
1514

@@ -32,7 +31,11 @@ def update_inference_service(
3231

3332
with ResourceEditor(patches={isvc: isvc_updated_dict}):
3433
if wait_for_new_pods:
35-
wait_for_new_running_inference_pods(isvc=isvc, orig_pods=orig_pods)
34+
wait_for_new_running_pods(
35+
orig_pods=orig_pods,
36+
pod_fetcher_func=lambda: get_pods_by_isvc_label(client=isvc.client, isvc=isvc),
37+
timeout=Timeout.TIMEOUT_10MIN,
38+
)
3639

3740
yield isvc
3841

@@ -72,44 +75,6 @@ def verify_env_vars_in_isvc_pods(isvc: InferenceService, env_vars: list[dict[str
7275
)
7376

7477

75-
def wait_for_new_running_inference_pods(
76-
isvc: InferenceService, orig_pods: list[Pod], expected_num_pods: int | None = None
77-
) -> None:
78-
"""
79-
Wait for the inference pod to be replaced.
80-
81-
Args:
82-
isvc (InferenceService): InferenceService object.
83-
orig_pods (list): List of Pod objects.
84-
expected_num_pods (int): Number of pods expected to be running. I
85-
f not provided, the number of pods is expected to be len(orig_pods)
86-
87-
Raises:
88-
TimeoutError: If the pods are not replaced.
89-
90-
"""
91-
LOGGER.info("Waiting for pods to be replaced")
92-
oring_pods_names = [pod.name for pod in orig_pods]
93-
94-
expected_num_pods = expected_num_pods or len(orig_pods)
95-
96-
try:
97-
for pods in TimeoutSampler(
98-
wait_timeout=Timeout.TIMEOUT_10MIN,
99-
sleep=5,
100-
func=get_pods_by_isvc_label,
101-
client=isvc.client,
102-
isvc=isvc,
103-
):
104-
if pods and len(pods) == expected_num_pods:
105-
if all(pod.name not in oring_pods_names and pod.status == pod.Status.RUNNING for pod in pods):
106-
return
107-
108-
except TimeoutError:
109-
LOGGER.error(f"Timeout waiting for pods {oring_pods_names} to be replaced")
110-
raise
111-
112-
11378
def verify_pull_secret(isvc: InferenceService, pull_secret: str, secret_exists: bool) -> None:
11479
"""
11580
Verify that the ImagePullSecret in the InferenceService pods match the expected values.

utilities/general.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import base64
22
import re
3-
from typing import List, Tuple
3+
from typing import List, Tuple, Callable
44
import uuid
55

66
from kubernetes.dynamic import DynamicClient
@@ -14,7 +14,7 @@
1414
from utilities.constants import Annotations, KServeDeploymentType, MODELMESH_SERVING, Timeout
1515
from utilities.exceptions import UnexpectedResourceCountError, ResourceValueMismatch
1616
from ocp_resources.resource import Resource
17-
from timeout_sampler import retry
17+
from timeout_sampler import retry, TimeoutSampler
1818

1919
# Constants for image validation
2020
SHA256_DIGEST_PATTERN = r"@sha256:[a-f0-9]{64}$"
@@ -378,3 +378,42 @@ def wait_for_container_status(pod: Pod, container_name: str, expected_status: st
378378
LOGGER.info(f"Container {container_name} is in the expected status {expected_status}")
379379
return True
380380
raise ResourceValueMismatch(f"Container {container_name} is not in the expected status {container_status.state}")
381+
382+
383+
def wait_for_new_running_pods( # type: ignore[return]
384+
orig_pods: list[Pod],
385+
pod_fetcher_func: Callable[[], list[Pod]],
386+
expected_num_pods: int | None = None,
387+
timeout: int = Timeout.TIMEOUT_10MIN,
388+
return_pods: bool = False,
389+
) -> list[Pod] | None:
390+
"""
391+
Wait for pods to be replaced with new running pods.
392+
393+
Args:
394+
orig_pods: List of original Pod objects to be replaced
395+
pod_fetcher_func: Function that returns list of current pods
396+
expected_num_pods: Expected number of pods (defaults to len(orig_pods))
397+
timeout: Timeout in seconds
398+
return_pods: Whether to return the new pods list
399+
400+
Returns:
401+
List of new pods if return_pods=True, otherwise None
402+
"""
403+
LOGGER.info("Waiting for pods to be replaced")
404+
orig_pods_names = [pod.name for pod in orig_pods]
405+
expected_num_pods = expected_num_pods or len(orig_pods)
406+
407+
try:
408+
for pods in TimeoutSampler(
409+
wait_timeout=timeout,
410+
sleep=5,
411+
func=pod_fetcher_func,
412+
):
413+
if pods and len(pods) == expected_num_pods:
414+
if all(pod.name not in orig_pods_names and pod.status == pod.Status.RUNNING for pod in pods):
415+
return pods if return_pods else None
416+
417+
except TimeoutError:
418+
LOGGER.error(f"Timeout waiting for pods {orig_pods_names} to be replaced")
419+
raise

0 commit comments

Comments
 (0)