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,91 +4735,85 @@ mod tests {
#[test] #[test]
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
fn test_snapshot_restore() { fn test_snapshot_restore() {
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 mut workload_path = dirs::home_dir().unwrap();
let mut workload_path = dirs::home_dir().unwrap(); workload_path.push("workloads");
workload_path.push("workloads");
let mut kernel_path = workload_path; let mut kernel_path = workload_path;
kernel_path.push("bzImage"); kernel_path.push("bzImage");
let api_socket = temp_api_path(&guest.tmp_dir); let api_socket = temp_api_path(&guest.tmp_dir);
let net_id = "net123"; let net_id = "net123";
let net_params = format!( let net_params = format!(
"id={},tap=,mac={},ip={},mask=255.255.255.0", "id={},tap=,mac={},ip={},mask=255.255.255.0",
net_id, guest.network.guest_mac, guest.network.host_ip net_id, guest.network.guest_mac, guest.network.host_ip
); );
let cloudinit_params = if cfg!(feature = "mmio") { let cloudinit_params = if cfg!(feature = "mmio") {
format!(
"path={}",
guest.disk_config.disk(DiskType::CloudInit).unwrap()
)
} else {
format!(
"path={},iommu=on",
guest.disk_config.disk(DiskType::CloudInit).unwrap()
)
};
let socket = temp_vsock_path(&guest.tmp_dir);
let mut child = GuestCommand::new(&guest)
.args(&["--api-socket", &api_socket])
.args(&["--cpus", "boot=4"])
.args(&["--memory", "size=4G"])
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&[
"--disk",
format!( format!(
"path={}", "path={}",
guest.disk_config.disk(DiskType::CloudInit).unwrap() guest.disk_config.disk(DiskType::OperatingSystem).unwrap()
) )
} else { .as_str(),
format!( cloudinit_params.as_str(),
"path={},iommu=on", ])
guest.disk_config.disk(DiskType::CloudInit).unwrap() .args(&["--net", net_params.as_str()])
) .args(&["--vsock", format!("cid=3,socket={}", socket).as_str()])
}; .args(&["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE])
.capture_output()
.spawn()
.unwrap();
let socket = temp_vsock_path(&guest.tmp_dir); thread::sleep(std::time::Duration::new(20, 0));
let mut child = GuestCommand::new(&guest) let console_text = String::from("On a branch floating down river a cricket, singing.");
.args(&["--api-socket", &api_socket]) let console_cmd = format!("echo {} | sudo tee /dev/hvc0", console_text);
.args(&["--cpus", "boot=4"]) // Create the snapshot directory
.args(&["--memory", "size=4G"]) let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir);
.args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&[
"--disk",
format!(
"path={}",
guest.disk_config.disk(DiskType::OperatingSystem).unwrap()
)
.as_str(),
cloudinit_params.as_str(),
])
.args(&["--net", net_params.as_str()])
.args(&["--vsock", format!("cid=3,socket={}", socket).as_str()])
.args(&["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE])
.capture_output()
.spawn()
.unwrap();
thread::sleep(std::time::Duration::new(20, 0));
let r = std::panic::catch_unwind(|| {
// Check the number of vCPUs // 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 // 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 // Check block devices are readable
aver!( assert!(guest
tb, .ssh_command("dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024")
guest .is_ok());
.ssh_command("dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024") assert!(guest
.is_ok() .ssh_command("dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8")
); .is_ok());
aver!(
tb,
guest
.ssh_command("dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8")
.is_ok()
);
// Check if the rng device is readable // Check if the rng device is readable
aver!( assert!(guest
tb, .ssh_command("head -c 1000 /dev/hwrng > /dev/null")
guest .is_ok());
.ssh_command("head -c 1000 /dev/hwrng > /dev/null")
.is_ok()
);
// Check vsock // Check vsock
guest.check_vsock(socket.as_str()); guest.check_vsock(socket.as_str());
// Check if the console is usable // Check if the console is usable
let console_text =
String::from("On a branch floating down river a cricket, singing."); assert!(guest.ssh_command(&console_cmd).is_ok());
let console_cmd = format!("echo {} | sudo tee /dev/hvc0", console_text);
aver!(tb, guest.ssh_command(&console_cmd).is_ok());
// Only for PCI, we can check that removing and adding back the // Only for PCI, we can check that removing and adding back the
// virtio-net device does not break the snapshot/restore support // virtio-net device does not break the snapshot/restore support
@ -4829,113 +4823,93 @@ mod tests {
#[cfg(not(feature = "mmio"))] #[cfg(not(feature = "mmio"))]
{ {
// Unplug the virtio-net device // Unplug the virtio-net device
aver!( assert!(remote_command(&api_socket, "remove-device", Some(net_id),));
tb,
remote_command(&api_socket, "remove-device", Some(net_id),)
);
thread::sleep(std::time::Duration::new(10, 0)); thread::sleep(std::time::Duration::new(10, 0));
// Plug the virtio-net device again // Plug the virtio-net device again
aver!( assert!(remote_command(
tb, &api_socket,
remote_command(&api_socket, "add-net", Some(net_params.as_str()),) "add-net",
); Some(net_params.as_str()),
));
thread::sleep(std::time::Duration::new(10, 0)); thread::sleep(std::time::Duration::new(10, 0));
} }
// Pause the VM // Pause the VM
aver!(tb, remote_command(&api_socket, "pause", None)); assert!(remote_command(&api_socket, "pause", None));
// Create the snapshot directory
let snapshot_dir = temp_snapshot_dir_path(&guest.tmp_dir);
// Take a snapshot from the VM // Take a snapshot from the VM
aver!( assert!(remote_command(
tb, &api_socket,
remote_command( "snapshot",
&api_socket, Some(format!("file://{}", snapshot_dir).as_str()),
"snapshot", ));
Some(format!("file://{}", snapshot_dir).as_str()),
)
);
// Wait to make sure the snapshot is completed // Wait to make sure the snapshot is completed
thread::sleep(std::time::Duration::new(10, 0)); thread::sleep(std::time::Duration::new(10, 0));
});
// Shutdown the source VM and check console output // Shutdown the source VM and check console output
let _ = child.kill(); let _ = child.kill();
match child.wait_with_output() { let output = child.wait_with_output().unwrap();
Ok(out) => { handle_child_output(r, &output);
aver!(
tb,
String::from_utf8_lossy(&out.stdout).contains(&console_text)
);
}
Err(_) => aver!(tb, false),
}
// Remove the vsock socket file. let r = std::panic::catch_unwind(|| {
Command::new("rm") assert!(String::from_utf8_lossy(&output.stdout).contains(&console_text));
.arg("-f") });
.arg(socket.as_str())
.output()
.unwrap();
// Restore the VM from the snapshot handle_child_output(r, &output);
let mut child = GuestCommand::new(&guest)
.args(&["--api-socket", &api_socket])
.args(&[
"--restore",
format!("source_url=file://{}", snapshot_dir).as_str(),
])
.capture_output()
.spawn()
.unwrap();
// Wait for the VM to be restored // Remove the vsock socket file.
thread::sleep(std::time::Duration::new(10, 0)); Command::new("rm")
.arg("-f")
.arg(socket.as_str())
.output()
.unwrap();
// Restore the VM from the snapshot
let mut child = GuestCommand::new(&guest)
.args(&["--api-socket", &api_socket])
.args(&[
"--restore",
format!("source_url=file://{}", snapshot_dir).as_str(),
])
.capture_output()
.spawn()
.unwrap();
// Wait for the VM to be restored
thread::sleep(std::time::Duration::new(10, 0));
let r = std::panic::catch_unwind(|| {
// Resume the VM // 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 // Perform same checks to validate VM has been properly restored
aver_eq!(tb, guest.get_cpu_count().unwrap_or_default(), 4); assert_eq!(guest.get_cpu_count().unwrap_or_default(), 4);
aver!(tb, guest.get_total_memory().unwrap_or_default() > 3_840_000); assert!(guest.get_total_memory().unwrap_or_default() > 3_840_000);
aver!( assert!(guest
tb, .ssh_command("dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024")
guest .is_ok());
.ssh_command("dd if=/dev/vda of=/dev/null bs=1M iflag=direct count=1024") assert!(guest
.is_ok() .ssh_command("dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8")
); .is_ok());
aver!( assert!(guest
tb, .ssh_command("head -c 1000 /dev/hwrng > /dev/null")
guest .is_ok());
.ssh_command("dd if=/dev/vdb of=/dev/null bs=1M iflag=direct count=8")
.is_ok()
);
aver!(
tb,
guest
.ssh_command("head -c 1000 /dev/hwrng > /dev/null")
.is_ok()
);
guest.check_vsock(socket.as_str()); 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 // 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)] #[cfg_attr(not(feature = "mmio"), test)]