tests: Port test_snapshot_restore to new methodology

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-08-10 12:41:16 +01:00 committed by Sebastien Boeuf
parent 6cd31e7a4d
commit 070f47246c

View File

@ -4735,7 +4735,6 @@ mod tests {
#[test]
#[cfg(target_arch = "x86_64")]
fn test_snapshot_restore() {
test_block!(tb, "", {
let mut focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string());
let guest = Guest::new(&mut focal);
let mut workload_path = dirs::home_dir().unwrap();
@ -4789,37 +4788,32 @@ mod tests {
thread::sleep(std::time::Duration::new(20, 0));
let console_text = String::from("On a branch floating down river a cricket, singing.");
let console_cmd = format!("echo {} | sudo tee /dev/hvc0", console_text);
// Create the snapshot directory
let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir);
let r = std::panic::catch_unwind(|| {
// Check the number of vCPUs
aver_eq!(tb, guest.get_cpu_count().unwrap_or_default(), 4);
assert_eq!(guest.get_cpu_count().unwrap_or_default(), 4);
// Check the guest RAM
aver!(tb, guest.get_total_memory().unwrap_or_default() > 3_840_000);
assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000);
// Check block devices are readable
aver!(
tb,
guest
assert!(guest
.ssh_command("dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024")
.is_ok()
);
aver!(
tb,
guest
.is_ok());
assert!(guest
.ssh_command("dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8")
.is_ok()
);
.is_ok());
// Check if the rng device is readable
aver!(
tb,
guest
assert!(guest
.ssh_command("head -c 1000 /dev/hwrng > /dev/null")
.is_ok()
);
.is_ok());
// Check vsock
guest.check_vsock(socket.as_str());
// Check if the console is usable
let console_text =
String::from("On a branch floating down river a cricket, singing.");
let console_cmd = format!("echo {} | sudo tee /dev/hvc0", console_text);
aver!(tb, guest.ssh_command(&console_cmd).is_ok());
assert!(guest.ssh_command(&console_cmd).is_ok());
// Only for PCI, we can check that removing and adding back the
// virtio-net device does not break the snapshot/restore support
@ -4829,50 +4823,42 @@ mod tests {
#[cfg(not(feature = "mmio"))]
{
// Unplug the virtio-net device
aver!(
tb,
remote_command(&api_socket, "remove-device", Some(net_id),)
);
assert!(remote_command(&api_socket, "remove-device", Some(net_id),));
thread::sleep(std::time::Duration::new(10, 0));
// Plug the virtio-net device again
aver!(
tb,
remote_command(&api_socket, "add-net", Some(net_params.as_str()),)
);
assert!(remote_command(
&api_socket,
"add-net",
Some(net_params.as_str()),
));
thread::sleep(std::time::Duration::new(10, 0));
}
// Pause the VM
aver!(tb, remote_command(&api_socket, "pause", None));
// Create the snapshot directory
let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir);
assert!(remote_command(&api_socket, "pause", None));
// Take a snapshot from the VM
aver!(
tb,
remote_command(
assert!(remote_command(
&api_socket,
"snapshot",
Some(format!("file://{}", snapshot_dir).as_str()),
)
);
));
// Wait to make sure the snapshot is completed
thread::sleep(std::time::Duration::new(10, 0));
});
// Shutdown the source VM and check console output
let _ = child.kill();
match child.wait_with_output() {
Ok(out) => {
aver!(
tb,
String::from_utf8_lossy(&out.stdout).contains(&console_text)
);
}
Err(_) => aver!(tb, false),
}
let output = child.wait_with_output().unwrap();
handle_child_output(r, &output);
let r = std::panic::catch_unwind(|| {
assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text));
});
handle_child_output(r, &output);
// Remove the vsock socket file.
Command::new("rm")
@ -4895,47 +4881,35 @@ mod tests {
// Wait for the VM to be restored
thread::sleep(std::time::Duration::new(10, 0));
let r = std::panic::catch_unwind(|| {
// Resume the VM
aver!(tb, remote_command(&api_socket, "resume", None));
assert!(remote_command(&api_socket, "resume", None));
// Perform same checks to validate VM has been properly restored
aver_eq!(tb, guest.get_cpu_count().unwrap_or_default(), 4);
aver!(tb, guest.get_total_memory().unwrap_or_default() > 3_840_000);
aver!(
tb,
guest
assert_eq!(guest.get_cpu_count().unwrap_or_default(), 4);
assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000);
assert!(guest
.ssh_command("dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024")
.is_ok()
);
aver!(
tb,
guest
.is_ok());
assert!(guest
.ssh_command("dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8")
.is_ok()
);
aver!(
tb,
guest
.is_ok());
assert!(guest
.ssh_command("head -c 1000 /dev/hwrng > /dev/null")
.is_ok()
);
.is_ok());
guest.check_vsock(socket.as_str());
aver!(tb, guest.ssh_command(&console_cmd).is_ok());
assert!(guest.ssh_command(&console_cmd).is_ok());
// Shutdown the target VM and check console output
let _ = child.kill();
match child.wait_with_output() {
Ok(out) => {
aver!(
tb,
String::from_utf8_lossy(&out.stdout).contains(&console_text)
);
}
Err(_) => aver!(tb, false),
}
Ok(())
});
let _ = child.kill();
let output = child.wait_with_output().unwrap();
handle_child_output(r, &output);
let r = std::panic::catch_unwind(|| {
assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text));
});
handle_child_output(r, &output);
}
#[cfg_attr(not(feature = "mmio"), test)]