Skip to content

Commit bf4ca52

Browse files
Changed virt-launcher usage in network metrics (RedHatQE#1822)
* Changed virt-launcher usage in network metrics Change of the implementation of fixtures in network metrics that used virt-launcher. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4a53659 commit bf4ca52

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

tests/observability/metrics/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
create_windows11_wsl2_vm,
2727
disk_file_system_info,
2828
enable_swap_fedora_vm,
29-
get_interface_name_from_vm,
3029
get_metric_sum_value,
3130
get_vm_comparison_info_dict,
3231
get_vmi_guest_os_kernel_release_info_metric_from_vm,
@@ -266,12 +265,12 @@ def generated_network_traffic_windows_vm(windows_vm_for_test):
266265

267266
@pytest.fixture(scope="class")
268267
def linux_vm_for_test_interface_name(vm_for_test):
269-
return get_interface_name_from_vm(vm=vm_for_test)
268+
return vm_for_test.vmi.interfaces[0].interfaceName
270269

271270

272271
@pytest.fixture(scope="class")
273272
def windows_vm_for_test_interface_name(windows_vm_for_test):
274-
return get_interface_name_from_vm(vm=windows_vm_for_test)
273+
return windows_vm_for_test.vmi.interfaces[0].interfaceName
275274

276275

277276
@pytest.fixture(scope="class")

tests/observability/metrics/utils.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import math
33
import re
44
import shlex
5+
import time
56
import urllib
67
from contextlib import contextmanager
78
from datetime import datetime, timezone
@@ -374,17 +375,36 @@ def validate_metric_value_within_range(
374375

375376

376377
def network_packets_received(vm: VirtualMachineForTests, interface_name: str) -> dict[str, str]:
377-
virsh_domifstat_content = vm.privileged_vmi.virt_launcher_pod.execute(
378-
command=shlex.split(f"virsh domifstat {vm.namespace}_{vm.name} {interface_name}")
379-
).splitlines()
380-
return {line.split()[1]: line.split()[2] for line in virsh_domifstat_content if line}
378+
ip_link_show_content = run_ssh_commands(host=vm.ssh_exec, commands=shlex.split("ip -s link show"))[0]
379+
pattern = re.compile(
380+
rf".*?{re.escape(interface_name)}:.*?" # Match the line with the interface name
381+
r"(?:RX:\s+bytes\s+packets\s+errors\s+dropped\s+.*?(\d+)\s+(\d+)\s+(\d+)\s+(\d+)).*?" # Capture RX stats
382+
r"(?:TX:\s+bytes\s+packets\s+errors\s+dropped\s+.*?(\d+)\s+(\d+)\s+(\d+)\s+(\d+))", # Capture TX stats
383+
re.DOTALL | re.IGNORECASE,
384+
)
385+
match = pattern.search(string=ip_link_show_content)
386+
if match:
387+
rx_bytes, rx_packets, rx_errs, rx_drop, tx_bytes, tx_packets, tx_errs, tx_drop = match.groups()
388+
return {
389+
"rx_bytes": rx_bytes,
390+
"rx_packets": rx_packets,
391+
"rx_errs": rx_errs,
392+
"rx_drop": rx_drop,
393+
"tx_bytes": tx_bytes,
394+
"tx_packets": tx_packets,
395+
"tx_errs": tx_errs,
396+
"tx_drop": tx_drop,
397+
}
398+
return {}
381399

382400

383401
def compare_network_traffic_bytes_and_metrics(
384402
prometheus: Prometheus, vm: VirtualMachineForTests, vm_interface_name: str
385403
) -> bool:
386404
packet_received = network_packets_received(vm=vm, interface_name=vm_interface_name)
387405
rx_tx_indicator = False
406+
LOGGER.info("Waiting for metric kubevirt_vmi_network_traffic_bytes_total to update")
407+
time.sleep(TIMEOUT_15SEC)
388408
metric_result = (
389409
prometheus.query(query=f"kubevirt_vmi_network_traffic_bytes_total{{name='{vm.name}'}}")
390410
.get("data")
@@ -393,7 +413,7 @@ def compare_network_traffic_bytes_and_metrics(
393413
for entry in metric_result:
394414
entry_value = entry.get("value")[1]
395415
if math.isclose(
396-
int(entry_value), int(packet_received[f"{entry.get('metric').get('type')}_bytes"]), rel_tol=0.02
416+
int(entry_value), int(packet_received[f"{entry.get('metric').get('type')}_bytes"]), rel_tol=0.05
397417
):
398418
rx_tx_indicator = True
399419
else:
@@ -415,15 +435,9 @@ def validate_network_traffic_metrics_value(
415435
vm_interface_name=interface_name,
416436
)
417437
try:
418-
match_counter = 0
419438
for sample in samples:
420439
if sample:
421-
match_counter += 1
422-
if match_counter >= 3:
423-
return
424-
else:
425-
match_counter = 0
426-
440+
return
427441
except TimeoutExpiredError:
428442
LOGGER.error("Metric value and domistat value not correlate.")
429443
raise
@@ -687,14 +701,6 @@ def validate_vnic_info(prometheus: Prometheus, vnic_info_to_compare: dict[str, s
687701
assert not mismatch_vnic_info, f"There is a mismatch between expected and actual results:\n {mismatch_vnic_info}"
688702

689703

690-
def get_interface_name_from_vm(vm: VirtualMachineForTests) -> str:
691-
interface_name = vm.privileged_vmi.virt_launcher_pod.execute(
692-
command=shlex.split("bash -c \"virsh domiflist 1 | grep ethernet | awk '{print $1}'\"")
693-
)
694-
assert interface_name, f"Interface not found for vm {vm.name}"
695-
return interface_name
696-
697-
698704
def get_metric_labels_non_empty_value(prometheus: Prometheus, metric_name: str) -> dict[str, str]:
699705
samples = TimeoutSampler(
700706
wait_timeout=TIMEOUT_5MIN,

0 commit comments

Comments
 (0)