22import math
33import re
44import shlex
5- import time
65import urllib
76from contextlib import contextmanager
87from datetime import datetime , timezone
3130 CAPACITY ,
3231 NODE_STR ,
3332 OS_FLAVOR_WINDOWS ,
33+ REGISTRY_STR ,
3434 TIMEOUT_1MIN ,
3535 TIMEOUT_2MIN ,
3636 TIMEOUT_4MIN ,
3939 TIMEOUT_15SEC ,
4040 TIMEOUT_20SEC ,
4141 TIMEOUT_30SEC ,
42+ TIMEOUT_40MIN ,
4243 USED ,
4344 VIRT_HANDLER ,
4445 Images ,
4748 cleanup_artifactory_secret_and_config_map ,
4849 get_artifactory_config_map ,
4950 get_artifactory_secret ,
50- get_http_image_url ,
51+ get_test_artifact_server_url ,
5152)
5253from utilities .monitoring import get_metrics_value
5354from utilities .virt import VirtualMachineForTests , running_vm
@@ -323,65 +324,57 @@ def validate_metric_value_within_range(
323324 raise
324325
325326
326- def network_packets_received (vm : VirtualMachineForTests , interface_name : str ) -> dict [str , str ]:
327- ip_link_show_content = run_ssh_commands (host = vm .ssh_exec , commands = shlex .split ("ip -s link show" ))[0 ]
327+ def network_packets_received (
328+ vm : VirtualMachineForTests , interface_name : str , windows_wsl : bool = False
329+ ) -> dict [str , str ]:
330+ ip_link_show_content = run_ssh_commands (
331+ host = vm .ssh_exec , commands = shlex .split (f"{ 'wsl' if windows_wsl else '' } ip -s link show" )
332+ )[0 ]
333+
328334 pattern = re .compile (
329335 rf".*?{ re .escape (interface_name )} :.*?" # Match the line with the interface name
330- r"(?: RX:\s+bytes\s+packets \s+errors\s+dropped \s+.*?( \d+)\s+(\d+)\s+( \d+) \s+( \d+)) .*?" # Capture RX stats
331- r"(?: TX:\s+bytes \s+packets\s+errors\s+dropped\s+.*?( \d+)\s+(\d+)\s+(\d+)\s+(\d+))" , # Capture TX stats
336+ r"RX:.*?\n\s+(?P<rx_bytes>\d+) \s+(?P<rx_packets>\d+) \s+(?P<rx_errs> \d+)\s+(?P<rx_drop> \d+)\s+\d+\s+\d+.*?"
337+ r"TX:.*?\n \s+(?P<tx_bytes> \d+)\s+(?P<tx_packets> \d+)\s+(?P<tx_errs> \d+)\s+(?P<tx_drop> \d+)" ,
332338 re .DOTALL | re .IGNORECASE ,
333339 )
334340 match = pattern .search (string = ip_link_show_content )
341+
335342 if match :
336- rx_bytes , rx_packets , rx_errs , rx_drop , tx_bytes , tx_packets , tx_errs , tx_drop = match .groups ()
337- return {
338- "rx_bytes" : rx_bytes ,
339- "rx_packets" : rx_packets ,
340- "rx_errs" : rx_errs ,
341- "rx_drop" : rx_drop ,
342- "tx_bytes" : tx_bytes ,
343- "tx_packets" : tx_packets ,
344- "tx_errs" : tx_errs ,
345- "tx_drop" : tx_drop ,
346- }
347- return {}
343+ result = match .groupdict ()
344+ LOGGER .info (f"Successfully parsed network stats: { result } " )
345+ return result
346+ raise ValueError (f"No match found for interface '{ interface_name } ' in ip link show output : { ip_link_show_content } " )
348347
349348
350349def compare_network_traffic_bytes_and_metrics (
351- prometheus : Prometheus , vm : VirtualMachineForTests , vm_interface_name : str
350+ prometheus : Prometheus , vm : VirtualMachineForTests , network_packet_received : dict [ str , str ]
352351) -> bool :
353- packet_received = network_packets_received (vm = vm , interface_name = vm_interface_name )
354352 rx_tx_indicator = False
355- LOGGER .info ("Waiting for metric kubevirt_vmi_network_traffic_bytes_total to update" )
356- time .sleep (TIMEOUT_15SEC )
357353 metric_result = (
358354 prometheus .query (query = f"kubevirt_vmi_network_traffic_bytes_total{{name='{ vm .name } '}}" )
359355 .get ("data" )
360356 .get ("result" )
361357 )
362358 for entry in metric_result :
363359 entry_value = entry .get ("value" )[1 ]
364- if math .isclose (
365- int (entry_value ), int (packet_received [f"{ entry .get ('metric' ).get ('type' )} _bytes" ]), rel_tol = 0.05
366- ):
360+ if int (entry_value ) >= int (network_packet_received [f"{ entry .get ('metric' ).get ('type' )} _bytes" ]):
367361 rx_tx_indicator = True
368362 else :
369363 break
370- if rx_tx_indicator :
371- return True
372- return False
364+
365+ return rx_tx_indicator
373366
374367
375368def validate_network_traffic_metrics_value (
376- prometheus : Prometheus , vm : VirtualMachineForTests , interface_name : str
369+ prometheus : Prometheus , vm : VirtualMachineForTests , network_packet_received : dict [ str , str ]
377370) -> None :
378371 samples = TimeoutSampler (
379372 wait_timeout = TIMEOUT_4MIN ,
380373 sleep = TIMEOUT_10SEC ,
381374 func = compare_network_traffic_bytes_and_metrics ,
382375 prometheus = prometheus ,
383376 vm = vm ,
384- vm_interface_name = interface_name ,
377+ network_packet_received = network_packet_received ,
385378 )
386379 try :
387380 for sample in samples :
@@ -664,14 +657,14 @@ def create_windows11_wsl2_vm(
664657 artifactory_secret = get_artifactory_secret (namespace = namespace )
665658 artifactory_config_map = get_artifactory_config_map (namespace = namespace )
666659 dv = DataVolume (
660+ client = client ,
667661 name = dv_name ,
668662 namespace = namespace ,
669- storage_class = storage_class ,
670- source = "http" ,
671- url = get_http_image_url (image_directory = Images .Windows .DIR , image_name = Images .Windows .WIN11_WSL2_IMG ),
672- size = Images .Windows .DEFAULT_DV_SIZE ,
673- client = client ,
674663 api_name = "storage" ,
664+ source = REGISTRY_STR ,
665+ size = Images .Windows .CONTAINER_DISK_DV_SIZE ,
666+ storage_class = storage_class ,
667+ url = f"{ get_test_artifact_server_url (schema = REGISTRY_STR )} /docker/windows-qe/win_11:virtio" ,
675668 secret = artifactory_secret ,
676669 cert_configmap = artifactory_config_map .name ,
677670 )
@@ -681,15 +674,17 @@ def create_windows11_wsl2_vm(
681674 name = vm_name ,
682675 namespace = namespace ,
683676 client = client ,
684- vm_instance_type = VirtualMachineClusterInstancetype (name = "u1.xlarge " ),
685- vm_preference = VirtualMachineClusterPreference (name = "windows.11" ),
677+ vm_instance_type = VirtualMachineClusterInstancetype (client = client , name = "u1.large " ),
678+ vm_preference = VirtualMachineClusterPreference (client = client , name = "windows.11" ),
686679 data_volume_template = {"metadata" : dv .res ["metadata" ], "spec" : dv .res ["spec" ]},
687680 ) as vm :
688- running_vm (vm = vm )
689- yield vm
690- cleanup_artifactory_secret_and_config_map (
691- artifactory_secret = artifactory_secret , artifactory_config_map = artifactory_config_map
692- )
681+ try :
682+ running_vm (vm = vm , dv_wait_timeout = TIMEOUT_40MIN )
683+ yield vm
684+ finally :
685+ cleanup_artifactory_secret_and_config_map (
686+ artifactory_secret = artifactory_secret , artifactory_config_map = artifactory_config_map
687+ )
693688
694689
695690def get_vm_comparison_info_dict (vm : VirtualMachineForTests ) -> dict [str , str ]:
@@ -734,7 +729,10 @@ def get_pvc_size_bytes(vm: VirtualMachineForTests) -> str:
734729
735730
736731def validate_metric_value_greater_than_initial_value (
737- prometheus : Prometheus , metric_name : str , initial_value : float , timeout : int = TIMEOUT_4MIN
732+ prometheus : Prometheus ,
733+ metric_name : str ,
734+ initial_value : float ,
735+ timeout : int = TIMEOUT_4MIN ,
738736) -> None :
739737 samples = TimeoutSampler (
740738 wait_timeout = timeout ,
0 commit comments