test_infra, tests: Introduce exec_host_command_status() function

This new function allows for better readability of the code by
factorizing a bunch of lines of code into a single one.

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
Sebastien Boeuf 2021-06-04 09:51:25 +02:00
parent 3d8728488d
commit eca1df4e2d
2 changed files with 41 additions and 151 deletions

View File

@ -11,6 +11,7 @@ use std::net::TcpListener;
use std::net::TcpStream;
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::process::ExitStatus;
use std::str::FromStr;
use std::thread;
use vmm_sys_util::tempdir::TempDir;
@ -589,3 +590,10 @@ pub fn ssh_command_ip(
timeout,
)
}
pub fn exec_host_command_status(command: &str) -> ExitStatus {
std::process::Command::new("bash")
.args(&["-c", command])
.status()
.expect(&format!("Expected '{}' to run", command))
}

View File

@ -643,17 +643,10 @@ mod tests {
thread::sleep(std::time::Duration::new(10, 0));
// Write something to vsock from the host
Command::new("bash")
.arg("-c")
.arg(
format!(
"echo -e \"CONNECT 16\\nHelloWorld!\" | socat - UNIX-CONNECT:{}",
socket
)
.as_str(),
)
.output()
.unwrap();
exec_host_command_status(&format!(
"echo -e \"CONNECT 16\\nHelloWorld!\" | socat - UNIX-CONNECT:{}",
socket
));
// Wait for the thread to terminate.
listen_socat.join().unwrap();
@ -1828,114 +1821,34 @@ mod tests {
// We reserve a different IP class for it: 172.18.0.0/24.
fn setup_vfio_network_interfaces() {
// 'vfio-br0'
Command::new("bash")
.arg("-c")
.arg("sudo ip link add name vfio-br0 type bridge")
.output()
.expect("Failed to create 'vfio-br0'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-br0 up")
.output()
.expect("Failed to create 'vfio-br0'");
Command::new("bash")
.arg("-c")
.arg("sudo ip addr add 172.18.0.1/24 dev vfio-br0")
.output()
.expect("Failed to create 'vfio-br0'");
exec_host_command_status("sudo ip link add name vfio-br0 type bridge");
exec_host_command_status("sudo ip link set vfio-br0 up");
exec_host_command_status("sudo ip addr add 172.18.0.1/24 dev vfio-br0");
// 'vfio-tap0'
Command::new("bash")
.arg("-c")
.arg("sudo ip tuntap add vfio-tap0 mode tap")
.output()
.expect("Failed to create 'vfio-tap0'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap0 master vfio-br0")
.output()
.expect("Failed to create 'vfio-tap0'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap0 up")
.output()
.expect("Failed to create 'vfio-tap0'");
exec_host_command_status("sudo ip tuntap add vfio-tap0 mode tap");
exec_host_command_status("sudo ip link set vfio-tap0 master vfio-br0");
exec_host_command_status("sudo ip link set vfio-tap0 up");
// 'vfio-tap1'
Command::new("bash")
.arg("-c")
.arg("sudo ip tuntap add vfio-tap1 mode tap")
.output()
.expect("Failed to create 'vfio-tap1'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap1 master vfio-br0")
.output()
.expect("Failed to create 'vfio-tap1'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap1 up")
.output()
.expect("Failed to create 'vfio-tap1'");
exec_host_command_status("sudo ip tuntap add vfio-tap1 mode tap");
exec_host_command_status("sudo ip link set vfio-tap1 master vfio-br0");
exec_host_command_status("sudo ip link set vfio-tap1 up");
// 'vfio-tap2'
Command::new("bash")
.arg("-c")
.arg("sudo ip tuntap add vfio-tap2 mode tap")
.output()
.expect("Failed to create 'vfio-tap2'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap2 master vfio-br0")
.output()
.expect("Failed to create 'vfio-tap2'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap2 up")
.output()
.expect("Failed to create 'vfio-tap2'");
exec_host_command_status("sudo ip tuntap add vfio-tap2 mode tap");
exec_host_command_status("sudo ip link set vfio-tap2 master vfio-br0");
exec_host_command_status("sudo ip link set vfio-tap2 up");
// 'vfio-tap3'
Command::new("bash")
.arg("-c")
.arg("sudo ip tuntap add vfio-tap3 mode tap")
.output()
.expect("Failed to create 'vfio-tap3'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap3 master vfio-br0")
.output()
.expect("Failed to create 'vfio-tap3'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link set vfio-tap3 up")
.output()
.expect("Failed to create 'vfio-tap3'");
exec_host_command_status("sudo ip tuntap add vfio-tap3 mode tap");
exec_host_command_status("sudo ip link set vfio-tap3 master vfio-br0");
exec_host_command_status("sudo ip link set vfio-tap3 up");
}
// Tear VFIO test network down
fn cleanup_vfio_network_interfaces() {
Command::new("bash")
.arg("-c")
.arg("sudo ip link del vfio-br0")
.output()
.expect("Failed to delete 'vfio-br0'");
Command::new("bash")
.arg("-c")
.arg("sudo ip link del vfio-tap0")
.output()
.expect("Failed to delete ''");
Command::new("bash")
.arg("-c")
.arg("sudo ip link del vfio-tap1")
.output()
.expect("Failed to delete ''");
Command::new("bash")
.arg("-c")
.arg("sudo ip link del vfio-tap2")
.output()
.expect("Failed to delete ''");
Command::new("bash")
.arg("-c")
.arg("sudo ip link del vfio-tap3")
.output()
.expect("Failed to delete ''");
exec_host_command_status("sudo ip link del vfio-br0");
exec_host_command_status("sudo ip link del vfio-tap0");
exec_host_command_status("sudo ip link del vfio-tap1");
exec_host_command_status("sudo ip link del vfio-tap2");
exec_host_command_status("sudo ip link del vfio-tap3");
}
mod parallel {
@ -5192,35 +5105,13 @@ mod tests {
#[test]
fn test_ovs_dpdk() {
// Create OVS-DPDK bridge and ports
std::process::Command::new("bash")
.args(&[
"-c",
"ovs-vsctl add-br ovsbr0 -- set bridge ovsbr0 datapath_type=netdev",
])
.status()
.expect("Expected 'ovs-vsctl add-br ovsbr0 -- set bridge ovsbr0 datapath_type=netdev' to work");
std::process::Command::new("bash")
.args(&[
"-c",
"ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1",
])
.status()
.expect("Expected 'ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1' to work");
std::process::Command::new("bash")
.args(&[
"-c",
"ovs-vsctl add-port ovsbr0 vhost-user2 -- set Interface vhost-user2 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient2",
])
.status()
.expect("Expected 'ovs-vsctl add-port ovsbr0 vhost-user2 -- set Interface vhost-user2 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient2' to work");
std::process::Command::new("bash")
.args(&["-c", "ip link set up dev ovsbr0"])
.status()
.expect("Expected 'ip link set up dev ovsbr0' to work");
std::process::Command::new("bash")
.args(&["-c", "service openvswitch-switch restart"])
.status()
.expect("Expected 'service openvswitch-switch restart' to work");
exec_host_command_status(
"ovs-vsctl add-br ovsbr0 -- set bridge ovsbr0 datapath_type=netdev",
);
exec_host_command_status("ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1");
exec_host_command_status("ovs-vsctl add-port ovsbr0 vhost-user2 -- set Interface vhost-user2 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient2");
exec_host_command_status("ip link set up dev ovsbr0");
exec_host_command_status("service openvswitch-switch restart");
let focal1 = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string());
let guest1 = Guest::new(Box::new(focal1));
@ -5303,10 +5194,7 @@ mod tests {
guest2.ssh_command("nc -vz 172.100.0.1 12345").unwrap();
// Remove one of the two ports from the OVS bridge
std::process::Command::new("bash")
.args(&["-c", "ovs-vsctl del-port vhost-user1"])
.status()
.expect("Expected 'ovs-vsctl del-port vhost-user1' to work");
exec_host_command_status("ovs-vsctl del-port vhost-user1");
// Spawn a new netcat listener in the first VM
let guest_ip = guest1.network.guest_ip.clone();
@ -5324,13 +5212,7 @@ mod tests {
assert!(guest2.ssh_command("nc -vz 172.100.0.1 12345").is_err());
// Add the OVS port back
std::process::Command::new("bash")
.args(&[
"-c",
"ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1",
])
.status()
.expect("Expected 'ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1' to work");
exec_host_command_status("ovs-vsctl add-port ovsbr0 vhost-user1 -- set Interface vhost-user1 type=dpdkvhostuserclient options:vhost-server-path=/tmp/dpdkvhostclient1");
// And finally check the connection is functional again
guest2.ssh_command("nc -vz 172.100.0.1 12345").unwrap();