Skip to content

Commit 73bfb91

Browse files
committed
refactored virt_api_pod, moved wait_for_pod_running_by_prefix to tests/utils.py to reuse it
1 parent d5dfc24 commit 73bfb91

File tree

4 files changed

+62
-109
lines changed

4 files changed

+62
-109
lines changed

tests/install_upgrade_operators/product_install/utils.py

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
import subprocess
44

55
from ocp_resources.deployment import Deployment
6-
from ocp_utilities.operators import TIMEOUT_5MIN
7-
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
86

7+
from tests.utils import wait_for_pod_running_by_prefix
98
from utilities.constants import (
109
HOSTPATH_PROVISIONER,
1110
HOSTPATH_PROVISIONER_OPERATOR,
12-
TIMEOUT_5SEC,
1311
TIMEOUT_15MIN,
1412
)
1513
from utilities.data_collector import write_to_file
16-
from utilities.infra import get_not_running_pods, get_pod_by_name_prefix
1714
from utilities.storage import verify_hpp_pool_health
1815

1916
PRINT_COMMAND = '{printf "%s%s",sep,$0;sep=","}'
@@ -71,49 +68,6 @@ def get_all_resources(file_name, base_directory):
7168
}
7269

7370

74-
def wait_for_pod_running_by_prefix(
75-
admin_client,
76-
namespace_name,
77-
pod_prefix,
78-
expected_number_of_pods,
79-
number_of_consecutive_checks=3,
80-
timeout=TIMEOUT_5MIN,
81-
):
82-
samples = TimeoutSampler(
83-
wait_timeout=timeout,
84-
sleep=TIMEOUT_5SEC,
85-
func=get_pod_by_name_prefix,
86-
dyn_client=admin_client,
87-
pod_prefix=pod_prefix,
88-
namespace=namespace_name,
89-
get_all=True,
90-
)
91-
pod_names = None
92-
not_running_pods = None
93-
try:
94-
current_check = 0
95-
for sample in samples:
96-
if sample:
97-
not_running_pods = get_not_running_pods(pods=sample)
98-
pod_names = [pod.name for pod in sample]
99-
LOGGER.info(f"All {pod_prefix} pods: {pod_names}, not running: {not_running_pods}")
100-
if not_running_pods:
101-
current_check = 0
102-
else:
103-
if expected_number_of_pods == len(sample):
104-
current_check += 1
105-
else:
106-
current_check = 0
107-
if current_check >= number_of_consecutive_checks:
108-
return True
109-
except TimeoutExpiredError:
110-
LOGGER.error(
111-
f"timeout waiting for all {pod_prefix} pods in namespace {namespace_name} to reach "
112-
f"running state, out of {pod_names}, following pods are in not running state: {not_running_pods}"
113-
)
114-
raise
115-
116-
11771
def validate_hpp_installation(admin_client, cnv_namespace, schedulable_nodes):
11872
hpp_deployment = Deployment(name=HOSTPATH_PROVISIONER_OPERATOR, namespace=cnv_namespace.name)
11973
assert hpp_deployment.exists

tests/observability/metrics/conftest.py

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
wait_for_metric_vmi_request_cpu_cores_output,
3636
)
3737
from tests.observability.utils import validate_metrics_value
38-
from tests.utils import create_vms
38+
from tests.utils import create_vms, wait_for_pod_running_by_prefix
3939
from utilities import console
4040
from utilities.constants import (
4141
IPV4_STR,
@@ -623,78 +623,29 @@ def aaq_resource_hard_limit_and_used(application_aware_resource_quota):
623623

624624

625625
@pytest.fixture()
626-
def virt_api_pod(admin_client, hco_namespace):
627-
"""
628-
Wait for exactly one virt-api pod to be running.
629-
630-
Polls for virt-api pods and waits until exactly one pod remains.
631-
Pods marked for deletion will be fully deleted before this condition is met.
632-
633-
Args:
634-
admin_client: DynamicClient for cluster operations
635-
hco_namespace: HCO namespace object
636-
637-
Returns:
638-
Pod: The single running virt-api pod
639-
640-
Raises:
641-
TimeoutExpiredError: If exactly one pod is not found within timeout period
642-
"""
643-
sample = []
644-
try:
645-
for sample in TimeoutSampler(
646-
wait_timeout=TIMEOUT_2MIN,
647-
sleep=TIMEOUT_15SEC,
648-
func=get_pod_by_name_prefix,
649-
dyn_client=admin_client,
650-
pod_prefix="virt-api",
651-
namespace=hco_namespace.name,
652-
get_all=True,
653-
):
654-
if sample and len(sample) == 1:
655-
return sample[0]
656-
except TimeoutExpiredError:
657-
pods_info = [
658-
(pod.name, pod.instance.status.phase, pod.instance.metadata.get("deletionTimestamp")) for pod in sample
659-
]
660-
LOGGER.exception(f"Should only be 1 virt-api pod running: found {pods_info}")
661-
raise
626+
def virt_api_pod_after_scale_to_one(admin_client, hco_namespace):
627+
wait_for_pod_running_by_prefix(
628+
admin_client=admin_client, namespace_name=hco_namespace.name, pod_prefix="virt-api", expected_number_of_pods=1
629+
)
662630

663631

664632
@pytest.fixture()
665-
def virt_api_initial_metric_value(prometheus, virt_api_pod):
666-
"""
667-
Get the initial value of kubevirt_vm_created_by_pod_total metric for a specific virt-api pod.
668-
669-
Args:
670-
prometheus: Prometheus client fixture
671-
virt_api_pod: The virt-api pod to query metrics for
672-
673-
Returns:
674-
int: The current value of the metric for the specified pod
675-
"""
633+
def virt_api_initial_metric_value(prometheus, virt_api_pod_after_scale_to_one):
676634
metric_query = (
677-
f"{KUBEVIRT_VM_CREATED_BY_POD_TOTAL}{{pod='{virt_api_pod.name}',namespace='{virt_api_pod.namespace}'}}"
635+
f"{KUBEVIRT_VM_CREATED_BY_POD_TOTAL}"
636+
f"{{pod='{virt_api_pod_after_scale_to_one.name}',"
637+
f"namespace='{virt_api_pod_after_scale_to_one.namespace}'}}"
678638
)
679639
return int(get_metrics_value(prometheus=prometheus, metrics_name=metric_query))
680640

681641

682642
@pytest.fixture()
683-
def vm_in_virt_api_ns(virt_api_pod):
684-
"""
685-
Creates a VM in the virt-api namespace
686-
687-
Args:
688-
virt_api_pod: The virt-api pod to fetch the namespace from
689-
690-
Yields:
691-
VM: The created VM
692-
"""
643+
def vm_in_virt_api_ns(virt_api_pod_after_scale_to_one):
693644
vm_name = "virt-api-vm"
694645

695646
with VirtualMachineForTests(
696647
name=vm_name,
697-
namespace=virt_api_pod.namespace,
648+
namespace=virt_api_pod_after_scale_to_one.namespace,
698649
body=fedora_vm_body(name=vm_name),
699650
ssh=False,
700651
) as vm:

tests/observability/metrics/test_vms_metrics.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,14 @@ def test_kubevirt_vm_created_by_pod_total(
529529
prometheus,
530530
disabled_virt_operator,
531531
scaled_deployment,
532-
virt_api_pod,
532+
virt_api_pod_after_scale_to_one,
533533
virt_api_initial_metric_value,
534534
vm_in_virt_api_ns,
535535
):
536536
metric_query = (
537-
f"{KUBEVIRT_VM_CREATED_BY_POD_TOTAL}{{pod='{virt_api_pod.name}',namespace='{virt_api_pod.namespace}'}}"
537+
f"{KUBEVIRT_VM_CREATED_BY_POD_TOTAL}"
538+
f"{{pod='{virt_api_pod_after_scale_to_one.name}',"
539+
f"namespace='{virt_api_pod_after_scale_to_one.namespace}'}}"
538540
)
539541
validate_metrics_value(
540542
prometheus=prometheus, metric_name=metric_query, expected_value=str(virt_api_initial_metric_value + 1)

tests/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ocp_resources.resource import ResourceEditor
1919
from ocp_resources.virtual_machine import VirtualMachine
2020
from ocp_resources.virtual_machine_instance_migration import VirtualMachineInstanceMigration
21+
from ocp_utilities.operators import TIMEOUT_5MIN
2122
from pyhelper_utils.shell import run_ssh_commands
2223
from timeout_sampler import TimeoutExpiredError, TimeoutSampler
2324

@@ -39,6 +40,8 @@
3940
get_artifactory_header,
4041
get_artifactory_secret,
4142
get_http_image_url,
43+
get_not_running_pods,
44+
get_pod_by_name_prefix,
4245
)
4346
from utilities.virt import (
4447
VirtualMachineForTests,
@@ -564,3 +567,46 @@ def create_cirros_vm(
564567
if wait_running:
565568
running_vm(vm=vm, wait_for_interfaces=False)
566569
yield vm
570+
571+
572+
def wait_for_pod_running_by_prefix(
573+
admin_client,
574+
namespace_name,
575+
pod_prefix,
576+
expected_number_of_pods,
577+
number_of_consecutive_checks=3,
578+
timeout=TIMEOUT_5MIN,
579+
):
580+
samples = TimeoutSampler(
581+
wait_timeout=timeout,
582+
sleep=TIMEOUT_5SEC,
583+
func=get_pod_by_name_prefix,
584+
dyn_client=admin_client,
585+
pod_prefix=pod_prefix,
586+
namespace=namespace_name,
587+
get_all=True,
588+
)
589+
pod_names = None
590+
not_running_pods = None
591+
try:
592+
current_check = 0
593+
for sample in samples:
594+
if sample:
595+
not_running_pods = get_not_running_pods(pods=sample)
596+
pod_names = [pod.name for pod in sample]
597+
LOGGER.info(f"All {pod_prefix} pods: {pod_names}, not running: {not_running_pods}")
598+
if not_running_pods:
599+
current_check = 0
600+
else:
601+
if expected_number_of_pods == len(sample):
602+
current_check += 1
603+
else:
604+
current_check = 0
605+
if current_check >= number_of_consecutive_checks:
606+
return True
607+
except TimeoutExpiredError:
608+
LOGGER.error(
609+
f"timeout waiting for all {pod_prefix} pods in namespace {namespace_name} to reach "
610+
f"running state, out of {pod_names}, following pods are in not running state: {not_running_pods}"
611+
)
612+
raise

0 commit comments

Comments
 (0)