test_infra: Add control of verbosity level

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2022-05-19 15:22:32 +01:00
parent 3538afc1fe
commit 38b8e387ea

View File

@ -9,6 +9,7 @@ extern crate lazy_static;
use ssh2::Session; use ssh2::Session;
use std::env; use std::env;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fmt::Debug;
use std::fs; use std::fs;
use std::io; use std::io;
use std::io::{Read, Write}; use std::io::{Read, Write};
@ -1220,11 +1221,35 @@ impl Guest {
} }
} }
pub enum VerbosityLevel {
Warn,
Info,
Debug,
}
impl Default for VerbosityLevel {
fn default() -> Self {
Self::Warn
}
}
impl ToString for VerbosityLevel {
fn to_string(&self) -> String {
use VerbosityLevel::*;
match self {
Warn => "".to_string(),
Info => "-v".to_string(),
Debug => "-vv".to_string(),
}
}
}
pub struct GuestCommand<'a> { pub struct GuestCommand<'a> {
command: Command, command: Command,
guest: &'a Guest, guest: &'a Guest,
capture_output: bool, capture_output: bool,
print_cmd: bool, print_cmd: bool,
verbosity: VerbosityLevel,
} }
impl<'a> GuestCommand<'a> { impl<'a> GuestCommand<'a> {
@ -1238,9 +1263,15 @@ impl<'a> GuestCommand<'a> {
guest, guest,
capture_output: false, capture_output: false,
print_cmd: true, print_cmd: true,
verbosity: VerbosityLevel::Info,
} }
} }
pub fn verbosity(&mut self, verbosity: VerbosityLevel) -> &mut Self {
self.verbosity = verbosity;
self
}
pub fn capture_output(&mut self) -> &mut Self { pub fn capture_output(&mut self) -> &mut Self {
self.capture_output = true; self.capture_output = true;
self self
@ -1252,6 +1283,17 @@ impl<'a> GuestCommand<'a> {
} }
pub fn spawn(&mut self) -> io::Result<Child> { pub fn spawn(&mut self) -> io::Result<Child> {
use VerbosityLevel::*;
match &self.verbosity {
Warn => {}
Info => {
self.command.arg("-v");
}
Debug => {
self.command.arg("-vv");
}
};
if self.print_cmd { if self.print_cmd {
println!( println!(
"\n\n==== Start cloud-hypervisor command-line ====\n\n\ "\n\n==== Start cloud-hypervisor command-line ====\n\n\
@ -1264,7 +1306,6 @@ impl<'a> GuestCommand<'a> {
if self.capture_output { if self.capture_output {
let child = self let child = self
.command .command
.arg("-v")
.stderr(Stdio::piped()) .stderr(Stdio::piped())
.stdout(Stdio::piped()) .stdout(Stdio::piped())
.spawn() .spawn()
@ -1284,7 +1325,7 @@ impl<'a> GuestCommand<'a> {
)) ))
} }
} else { } else {
self.command.arg("-v").spawn() self.command.spawn()
} }
} }