From 0107675eb1116f24747d1278895b8f8ae2e8bf34 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Wed, 15 Apr 2026 11:33:15 +0100 Subject: [PATCH] tests: Remove explicit sleeps from net tests Use `wait_until()` with the SSH command for detecting if the net device is present/absent as part of hotplugging/unplugging. Signed-off-by: Rob Bradford --- .../tests/common/tests_wrappers.rs | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/cloud-hypervisor/tests/common/tests_wrappers.rs b/cloud-hypervisor/tests/common/tests_wrappers.rs index 8094aa822..f42fce902 100644 --- a/cloud-hypervisor/tests/common/tests_wrappers.rs +++ b/cloud-hypervisor/tests/common/tests_wrappers.rs @@ -2928,18 +2928,12 @@ pub(crate) fn _test_net_hotplug( ); } - thread::sleep(std::time::Duration::new(5, 0)); - - // 2 network interfaces + default localhost ==> 3 interfaces - assert_eq!( + // Wait for the hotplugged network interface to appear + assert!(wait_until(Duration::from_secs(10), || { guest .ssh_command("ip -o link | wc -l") - .unwrap() - .trim() - .parse::() - .unwrap_or_default(), - 3 - ); + .is_ok_and(|s| s.trim().parse::().unwrap_or_default() == 3) + })); // Test the same using the added network interface's IP assert_eq!( @@ -2956,9 +2950,13 @@ pub(crate) fn _test_net_hotplug( 3 ); - // Remove network + // Remove network and wait for it to disappear assert!(remote_command(&api_socket, "remove-device", Some("test0"),)); - thread::sleep(std::time::Duration::new(5, 0)); + assert!(wait_until(Duration::from_secs(10), || { + guest + .ssh_command("ip -o link | wc -l") + .is_ok_and(|s| s.trim().parse::().unwrap_or_default() == 2) + })); // Add network let (cmd_success, cmd_output) = remote_command_w_output( @@ -2991,18 +2989,12 @@ pub(crate) fn _test_net_hotplug( ); } - thread::sleep(std::time::Duration::new(5, 0)); - - // 2 network interfaces + default localhost ==> 3 interfaces - assert_eq!( + // Wait for the hotplugged network interface to appear + assert!(wait_until(Duration::from_secs(10), || { guest .ssh_command("ip -o link | wc -l") - .unwrap() - .trim() - .parse::() - .unwrap_or_default(), - 3 - ); + .is_ok_and(|s| s.trim().parse::().unwrap_or_default() == 3) + })); guest.reboot_linux(0); @@ -3345,10 +3337,12 @@ pub(crate) fn _test_macvtap( let mut child = guest_command.capture_output().spawn().unwrap(); if hotplug { - // Give some time to the VMM process to listen to the API - // socket. This is the only requirement to avoid the following - // call to ch-remote from failing. - thread::sleep(std::time::Duration::new(10, 0)); + // Wait for the VMM process to listen to the API socket + assert!(wait_until(Duration::from_secs(10), || remote_command( + &api_socket, + "ping", + None + ))); // Hotplug the virtio-net device let (cmd_success, cmd_output) = remote_command_w_output(&api_socket, "add-net", Some(&net_params));