tests: Update test_macvtap to rely on exec_host_command_status()

To help with readability, we rely on exec_host_command_status() from the
macvtap test, which replaces the former "bash -c ..." syntax.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2021-07-21 10:18:01 +02:00
parent da4cde70c3
commit 2ecced376e

View File

@ -5107,40 +5107,29 @@ mod tests {
// Create a macvtap interface for the guest VM to use // Create a macvtap interface for the guest VM to use
let guest_macvtap_name = "guestmacvtap0"; let guest_macvtap_name = "guestmacvtap0";
std::process::Command::new("bash") assert!(exec_host_command_status(&format!(
.args(&[ "sudo ip link add link {} name {} type macvtap mod bridge",
"-c", phy_net, guest_macvtap_name
&format!( ))
"sudo ip link add link {} name {} type macvtap mod bridge", .success());
phy_net, guest_macvtap_name assert!(exec_host_command_status(&format!(
), "sudo ip link set {} address {} up",
]) guest_macvtap_name, guest.network.guest_mac
.status() ))
.expect("Expected 'ip link add' to work"); .success());
std::process::Command::new("bash") assert!(
.args(&[ exec_host_command_status(&format!("sudo ip link show {}", guest_macvtap_name))
"-c", .success()
&format!( );
"sudo ip link set {} address {} up",
guest_macvtap_name, guest.network.guest_mac
),
])
.status()
.expect("Expected 'ip link set' to work");
std::process::Command::new("bash")
.args(&["-c", &format!("sudo ip link show {}", guest_macvtap_name)])
.status()
.expect("Expected 'ip link show' to work");
let tap_index = let tap_index =
fs::read_to_string(&format!("/sys/class/net/{}/ifindex", guest_macvtap_name)) fs::read_to_string(&format!("/sys/class/net/{}/ifindex", guest_macvtap_name))
.unwrap(); .unwrap();
let tap_device = format!("/dev/tap{}", tap_index.trim()); let tap_device = format!("/dev/tap{}", tap_index.trim());
std::process::Command::new("bash") assert!(
.args(&["-c", &format!("sudo chown $UID.$UID {}", tap_device)]) exec_host_command_status(&format!("sudo chown $UID.$UID {}", tap_device)).success()
.status() );
.expect("Expected 'chown' to work");
let cstr_tap_device = std::ffi::CString::new(tap_device).unwrap(); let cstr_tap_device = std::ffi::CString::new(tap_device).unwrap();
let tap_fd = unsafe { libc::open(cstr_tap_device.as_ptr(), libc::O_RDWR) }; let tap_fd = unsafe { libc::open(cstr_tap_device.as_ptr(), libc::O_RDWR) };
@ -5149,34 +5138,23 @@ mod tests {
// Create a macvtap on the same physical net interface for // Create a macvtap on the same physical net interface for
// the host machine to use // the host machine to use
let host_macvtap_name = "hostmacvtap0"; let host_macvtap_name = "hostmacvtap0";
std::process::Command::new("bash")
.args(&[ assert!(exec_host_command_status(&format!(
"-c", "sudo ip link add link {} name {} type macvtap mod bridge",
&format!( phy_net, host_macvtap_name
"sudo ip link add link {} name {} type macvtap mod bridge", ))
phy_net, host_macvtap_name .success());
),
])
.status()
.expect("Expected 'ip link add' to work");
// Use default mask "255.255.255.0" // Use default mask "255.255.255.0"
std::process::Command::new("bash") assert!(exec_host_command_status(&format!(
.args(&[ "sudo ip address add {}/24 dev {}",
"-c", guest.network.host_ip, host_macvtap_name
&format!( ))
"sudo ip address add {}/24 dev {}", .success());
guest.network.host_ip, host_macvtap_name assert!(exec_host_command_status(&format!(
), "sudo ip link set dev {} up",
]) host_macvtap_name
.status() ))
.expect("Expected 'ip address add' to work"); .success());
std::process::Command::new("bash")
.args(&[
"-c",
&format!("sudo ip link set dev {} up", host_macvtap_name),
])
.status()
.expect("Expected 'ip link set dev up' to work");
let mut child = GuestCommand::new(&guest) let mut child = GuestCommand::new(&guest)
.args(&["--cpus", "boot=1"]) .args(&["--cpus", "boot=1"])
@ -5208,14 +5186,8 @@ mod tests {
let _ = child.kill(); let _ = child.kill();
std::process::Command::new("bash") exec_host_command_status(&format!("sudo ip link del {}", guest_macvtap_name));
.args(&["-c", &format!("sudo ip link del {}", guest_macvtap_name)]) exec_host_command_status(&format!("sudo ip link del {}", host_macvtap_name));
.status()
.expect("Expected 'ip link del' to work");
std::process::Command::new("bash")
.args(&["-c", &format!("sudo ip link del {}", host_macvtap_name)])
.status()
.expect("Expected 'ip link del' to work");
let output = child.wait_with_output().unwrap(); let output = child.wait_with_output().unwrap();