From 1e2aa769e5bc71b2751c9f9450184f20b5df67b1 Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Fri, 4 Jun 2021 10:09:10 +0200 Subject: [PATCH] test_infra, tests: Introduce exec_host_command_output() function This new function allows for better readability of the code by factorizing a few of lines of code into a single one. Signed-off-by: Sebastien Boeuf --- test_infra/src/lib.rs | 9 ++++++++- tests/integration.rs | 20 +++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/test_infra/src/lib.rs b/test_infra/src/lib.rs index bb888842f..a1846175a 100644 --- a/test_infra/src/lib.rs +++ b/test_infra/src/lib.rs @@ -11,7 +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::process::{ExitStatus, Output}; use std::str::FromStr; use std::thread; use vmm_sys_util::tempdir::TempDir; @@ -597,3 +597,10 @@ pub fn exec_host_command_status(command: &str) -> ExitStatus { .status() .expect(&format!("Expected '{}' to run", command)) } + +pub fn exec_host_command_output(command: &str) -> Output { + std::process::Command::new("bash") + .args(&["-c", command]) + .output() + .expect(&format!("Expected '{}' to run", command)) +} diff --git a/tests/integration.rs b/tests/integration.rs index 089de1f93..69f78f5c9 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -964,20 +964,14 @@ mod tests { guest.wait_vm_boot(None).unwrap(); if let Some(tap_name) = tap { - let tap_count = std::process::Command::new("bash") - .arg("-c") - .arg(format!("ip link | grep -c {}", tap_name)) - .output() - .expect("Expected checking of tap count to succeed"); + let tap_count = + exec_host_command_output(&format!("ip link | grep -c {}", tap_name)); assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); } if let Some(host_mac) = tap { - let mac_count = std::process::Command::new("bash") - .arg("-c") - .arg(format!("ip link | grep -c {}", host_mac)) - .output() - .expect("Expected checking of host mac to succeed"); + let mac_count = + exec_host_command_output(&format!("ip link | grep -c {}", host_mac)); assert_eq!(String::from_utf8_lossy(&mac_count.stdout).trim(), "1"); } @@ -2740,11 +2734,7 @@ mod tests { let r = std::panic::catch_unwind(|| { guest.wait_vm_boot(None).unwrap(); - let tap_count = std::process::Command::new("bash") - .arg("-c") - .arg("ip link | grep -c mytap1") - .output() - .expect("Expected checking of tap count to succeed"); + let tap_count = exec_host_command_output("ip link | grep -c mytap1"); assert_eq!(String::from_utf8_lossy(&tap_count.stdout).trim(), "1"); // 3 network interfaces + default localhost ==> 4 interfaces