Skip to content

Commit 7c9960c

Browse files
authored
CherryPicked: [cnv-4.18] Remove Artifactory dependency from CPU affinity (#3324)
The test for kubevirt_vmi_node_cpu_affinity is flaky duo to connectivity issues with the artifactory, in this PR I modified the test to not rely on the artifactory to avoid this kind of failures to stabilize the observability lanes. https://issues.redhat.com/browse/CNV-75209 original PR:#3134 ##### Short description: ##### More details: ##### What this PR does / why we need it: ##### Which issue(s) this PR fixes: ##### Special notes for reviewer: ##### jira-ticket: https://issues.redhat.com/browse/CNV-75854
1 parent 9670e23 commit 7c9960c

4 files changed

Lines changed: 22 additions & 70 deletions

File tree

tests/observability/metrics/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,16 @@ def pvc_size_bytes(vm_for_vm_disk_allocation_size_test):
396396
name=vm_for_vm_disk_allocation_size_test.instance.spec.dataVolumeTemplates[0].metadata.name,
397397
namespace=vm_for_vm_disk_allocation_size_test.namespace,
398398
).instance.spec.resources.requests.storage
399+
400+
401+
@pytest.fixture(scope="class")
402+
def expected_cpu_affinity_metric_value(vm_with_cpu_spec):
403+
"""Calculate expected kubevirt_vmi_node_cpu_affinity metric value."""
404+
# Calculate VM CPU count
405+
vm_cpu = vm_with_cpu_spec.vmi.instance.spec.domain.cpu
406+
cpu_count_from_vm = (vm_cpu.threads or 1) * (vm_cpu.cores or 1) * (vm_cpu.sockets or 1)
407+
# Get node CPU capacity
408+
cpu_count_from_vm_node = int(vm_with_cpu_spec.privileged_vmi.node.instance.status.capacity.cpu)
409+
410+
# return multiplication for multi-CPU VMs
411+
return str(cpu_count_from_vm_node * cpu_count_from_vm)

tests/observability/metrics/constants.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@
4848
KUBEVIRT_VMI_MIGRATION_DATA_TOTAL_BYTES = "kubevirt_vmi_migration_data_total_bytes{{name='{vm_name}'}}"
4949
BINDING_NAME = "binding_name"
5050
BINDING_TYPE = "binding_type"
51+
52+
KUBEVIRT_VMI_NODE_CPU_AFFINITY = "kubevirt_vmi_node_cpu_affinity{{kubernetes_vmi_label_kubevirt_io_domain='{vm_name}'}}"

tests/observability/metrics/test_general_metrics.py

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
import pytest
44
from ocp_resources.resource import Resource
55
from ocp_resources.virtual_machine import VirtualMachine
6-
from pytest_testconfig import config as py_config
76

8-
from tests.observability.metrics.utils import (
9-
validate_vmi_node_cpu_affinity_with_prometheus,
10-
)
7+
from tests.observability.metrics.constants import KUBEVIRT_VMI_NODE_CPU_AFFINITY
118
from tests.observability.utils import validate_metrics_value
12-
from tests.os_params import RHEL_LATEST, RHEL_LATEST_LABELS, RHEL_LATEST_OS
139
from utilities.virt import VirtualMachineForTests, fedora_vm_body, running_vm
1410

1511
KUBEVIRT_VM_TAG = f"{Resource.ApiGroup.KUBEVIRT_IO}/vm"
@@ -41,36 +37,17 @@ def fedora_vm_without_name_in_label(
4137
client=unprivileged_client,
4238
run_strategy=VirtualMachine.RunStrategy.ALWAYS,
4339
) as vm:
44-
running_vm(vm=vm, check_ssh_connectivity=False)
40+
running_vm(vm=vm, wait_for_interfaces=False, check_ssh_connectivity=False)
4541
yield vm
4642

4743

48-
@pytest.mark.parametrize(
49-
"golden_image_data_volume_scope_class, vm_from_template_scope_class",
50-
[
51-
pytest.param(
52-
{
53-
"dv_name": RHEL_LATEST_OS,
54-
"image": RHEL_LATEST["image_path"],
55-
"storage_class": py_config["default_storage_class"],
56-
"dv_size": RHEL_LATEST["dv_size"],
57-
},
58-
{
59-
"vm_name": "rhel-latest",
60-
"template_labels": RHEL_LATEST_LABELS,
61-
"guest_agent": False,
62-
"ssh": False,
63-
},
64-
),
65-
],
66-
indirect=True,
67-
)
6844
class TestVmiNodeCpuAffinity:
6945
@pytest.mark.polarion("CNV-7295")
70-
def test_kubevirt_vmi_node_cpu_affinity(self, prometheus, vm_from_template_scope_class):
71-
validate_vmi_node_cpu_affinity_with_prometheus(
72-
vm=vm_from_template_scope_class,
46+
def test_kubevirt_vmi_node_cpu_affinity(self, prometheus, vm_with_cpu_spec, expected_cpu_affinity_metric_value):
47+
validate_metrics_value(
7348
prometheus=prometheus,
49+
metric_name=KUBEVIRT_VMI_NODE_CPU_AFFINITY.format(vm_name=vm_with_cpu_spec.name),
50+
expected_value=expected_cpu_affinity_metric_value,
7451
)
7552

7653

tests/observability/metrics/utils.py

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import re
44
import shlex
55
import time
6-
import urllib
76
from datetime import datetime, timezone
8-
from typing import Any, Optional
7+
from typing import Any
98

109
from kubernetes.dynamic import DynamicClient
1110
from ocp_resources.resource import Resource
@@ -205,45 +204,6 @@ def enable_swap_fedora_vm(vm: VirtualMachineForTests) -> None:
205204
vm.ssh_exec.executor(sudo=True).run_cmd(cmd=shlex.split("sysctl vm.swappiness=100"))
206205

207206

208-
def get_vm_cpu_info_from_prometheus(prometheus: Prometheus, vm_name: str) -> Optional[int]:
209-
query = urllib.parse.quote_plus(
210-
f'kubevirt_vmi_node_cpu_affinity{{kubernetes_vmi_label_kubevirt_io_domain="{vm_name}"}}'
211-
)
212-
samples = TimeoutSampler(
213-
wait_timeout=TIMEOUT_1MIN,
214-
sleep=2,
215-
func=prometheus.query_sampler,
216-
query=query,
217-
)
218-
sample = None
219-
try:
220-
for sample in samples:
221-
if sample:
222-
return int(sample[0]["value"][1])
223-
except TimeoutExpiredError:
224-
LOGGER.error(f"Failed to get data from query '{query}' in time. Current data: {sample}")
225-
raise
226-
return None
227-
228-
229-
def validate_vmi_node_cpu_affinity_with_prometheus(prometheus: Prometheus, vm: VirtualMachineForTests) -> None:
230-
vm_cpu = vm.instance.spec.template.spec.domain.cpu
231-
cpu_count_from_vm = (vm_cpu.threads or 1) * (vm_cpu.cores or 1) * (vm_cpu.sockets or 1)
232-
LOGGER.info(f"Cpu count from vm {vm.name}: {cpu_count_from_vm}")
233-
cpu_info_from_prometheus = get_vm_cpu_info_from_prometheus(prometheus=prometheus, vm_name=vm.name)
234-
LOGGER.info(f"CPU information from prometheus: {cpu_info_from_prometheus}")
235-
cpu_count_from_vm_node = int(vm.privileged_vmi.node.instance.status.capacity.cpu)
236-
LOGGER.info(f"Cpu count from node {vm.privileged_vmi.node.name}: {cpu_count_from_vm_node}")
237-
238-
if cpu_count_from_vm > 1:
239-
cpu_count_from_vm_node = cpu_count_from_vm_node * cpu_count_from_vm
240-
241-
assert cpu_count_from_vm_node == cpu_info_from_prometheus, (
242-
f"Actual CPU count {cpu_count_from_vm_node} not matching with "
243-
f"expected CPU count {cpu_info_from_prometheus} for VM CPU {cpu_count_from_vm}"
244-
)
245-
246-
247207
def get_resource_object(
248208
admin_client: DynamicClient, related_objects: list, resource_kind, resource_name: str
249209
) -> Resource:

0 commit comments

Comments
 (0)