22import math
33import re
44import shlex
5+ import time
56import urllib
67from contextlib import contextmanager
78from datetime import datetime , timezone
@@ -374,17 +375,36 @@ def validate_metric_value_within_range(
374375
375376
376377def 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
383401def 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-
698704def 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