mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-07-07 10:15:45 +00:00
tests: Introduce new test methodology
Rather than using a credible TestBlock to capture the test assertions instead use a catch_unwind block to catch the panic and turn it into a Result<>. If block panicked or the child binary had non-zero exit then, and only then, print out the child output. This results in a clearer test output with no interleaving. Currently only test_counters is ported to this methodology to demonstrate its use. Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
parent
5da55f0f2e
commit
6cd31e7a4d
@ -102,6 +102,44 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_child_output(
|
||||||
|
r: Result<(), std::boxed::Box<dyn std::any::Any + std::marker::Send>>,
|
||||||
|
output: &std::process::Output,
|
||||||
|
) {
|
||||||
|
use std::os::unix::process::ExitStatusExt;
|
||||||
|
if r.is_ok() && output.status.success() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
match output.status.code() {
|
||||||
|
None => {
|
||||||
|
// Don't treat child.kill() as a problem
|
||||||
|
if output.status.signal() == Some(9) && r.is_ok() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
eprintln!(
|
||||||
|
"==== child killed by signal: {} ====",
|
||||||
|
output.status.signal().unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Some(code) => {
|
||||||
|
eprintln!("\n\n==== child exit code: {} ====", code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
eprintln!(
|
||||||
|
"\n\n==== Start child stdout ====\n\n{}\n\n==== End child stdout ====",
|
||||||
|
String::from_utf8_lossy(&output.stdout)
|
||||||
|
);
|
||||||
|
eprintln!(
|
||||||
|
"\n\n==== Start child stderr ====\n\n{}\n\n==== End child stderr ====",
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
);
|
||||||
|
|
||||||
|
panic!("Test failed")
|
||||||
|
}
|
||||||
|
|
||||||
fn rate_limited_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
|
fn rate_limited_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<u64> {
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
let free_bytes = unsafe {
|
let free_bytes = unsafe {
|
||||||
@ -4903,41 +4941,39 @@ mod tests {
|
|||||||
#[cfg_attr(not(feature = "mmio"), test)]
|
#[cfg_attr(not(feature = "mmio"), test)]
|
||||||
#[cfg(target_arch = "x86_64")]
|
#[cfg(target_arch = "x86_64")]
|
||||||
fn test_counters() {
|
fn test_counters() {
|
||||||
test_block!(tb, "", {
|
let mut focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string());
|
||||||
let mut focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string());
|
let guest = Guest::new(&mut focal);
|
||||||
let guest = Guest::new(&mut focal);
|
let api_socket = temp_api_path(&guest.tmp_dir);
|
||||||
let api_socket = temp_api_path(&guest.tmp_dir);
|
|
||||||
|
|
||||||
let mut child = GuestCommand::new(&guest)
|
let mut child = GuestCommand::new(&guest)
|
||||||
.args(&["--cpus", "boot=1"])
|
.args(&["--cpus", "boot=1"])
|
||||||
.args(&["--memory", "size=512M"])
|
.args(&["--memory", "size=512M"])
|
||||||
.args(&["--kernel", guest.fw_path.as_str()])
|
.args(&["--kernel", guest.fw_path.as_str()])
|
||||||
.default_disks()
|
.default_disks()
|
||||||
.args(&["--net", guest.default_net_string().as_str()])
|
.args(&["--net", guest.default_net_string().as_str()])
|
||||||
.args(&["--api-socket", &api_socket])
|
.args(&["--api-socket", &api_socket])
|
||||||
.spawn()
|
.capture_output()
|
||||||
.unwrap();
|
.spawn()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
thread::sleep(std::time::Duration::new(20, 0));
|
thread::sleep(std::time::Duration::new(20, 0));
|
||||||
|
|
||||||
|
let r = std::panic::catch_unwind(|| {
|
||||||
let orig_counters = get_counters(&api_socket);
|
let orig_counters = get_counters(&api_socket);
|
||||||
|
assert!(guest
|
||||||
aver!(
|
.ssh_command("dd if=/dev/zero of=test count=8 bs=1M")
|
||||||
tb,
|
.is_ok());
|
||||||
guest
|
|
||||||
.ssh_command("dd if=/dev/zero of=test count=8 bs=1M")
|
|
||||||
.is_ok()
|
|
||||||
);
|
|
||||||
|
|
||||||
let new_counters = get_counters(&api_socket);
|
let new_counters = get_counters(&api_socket);
|
||||||
|
|
||||||
// Check that all the counters have increased
|
// Check that all the counters have increased
|
||||||
aver!(tb, new_counters > orig_counters);
|
assert!(new_counters > orig_counters);
|
||||||
|
|
||||||
let _ = child.kill();
|
|
||||||
let _ = child.wait();
|
|
||||||
Ok(())
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let _ = child.kill();
|
||||||
|
let output = child.wait_with_output().unwrap();
|
||||||
|
|
||||||
|
handle_child_output(r, &output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user