tests: Port test_vfio to new test methodology

Now the testing aspects are run inside a panic handler block rather than
inside a credibility TestBlock. If the test fails then the output from
the cloud-hypervisor binary is then presented.

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-08-12 10:18:31 +01:00 committed by Sebastien Boeuf
parent 4ef702ea76
commit d749aa2e6b

View File

@ -3237,102 +3237,103 @@ mod tests {
// The third device is added to validate that hotplug works correctly since // The third device is added to validate that hotplug works correctly since
// it is being added to the L2 VM through hotplugging mechanism. // it is being added to the L2 VM through hotplugging mechanism.
fn test_vfio() { fn test_vfio() {
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_from_ip_range(&mut focal, "172.17", 0);
let guest = Guest::new_from_ip_range(&mut focal, "172.17", 0);
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.clone(); let mut kernel_path = workload_path.clone();
kernel_path.push("bzImage"); kernel_path.push("bzImage");
let mut vfio_path = workload_path.clone(); let mut vfio_path = workload_path.clone();
vfio_path.push("vfio"); vfio_path.push("vfio");
let mut cloud_init_vfio_base_path = vfio_path.clone(); let mut cloud_init_vfio_base_path = vfio_path.clone();
cloud_init_vfio_base_path.push("cloudinit.img"); cloud_init_vfio_base_path.push("cloudinit.img");
// We copy our cloudinit into the vfio mount point, for the nested // We copy our cloudinit into the vfio mount point, for the nested
// cloud-hypervisor guest to use. // cloud-hypervisor guest to use.
rate_limited_copy( rate_limited_copy(
&guest.disk_config.disk(DiskType::CloudInit).unwrap(), &guest.disk_config.disk(DiskType::CloudInit).unwrap(),
&cloud_init_vfio_base_path, &cloud_init_vfio_base_path,
) )
.expect("copying of cloud-init disk failed"); .expect("copying of cloud-init disk failed");
let mut vfio_disk_path = workload_path; let mut vfio_disk_path = workload_path;
vfio_disk_path.push("vfio.img"); vfio_disk_path.push("vfio.img");
// Create the vfio disk image // Create the vfio disk image
let output = Command::new("mkfs.ext4") let output = Command::new("mkfs.ext4")
.arg("-d") .arg("-d")
.arg(vfio_path.to_str().unwrap()) .arg(vfio_path.to_str().unwrap())
.arg(vfio_disk_path.to_str().unwrap()) .arg(vfio_disk_path.to_str().unwrap())
.arg("2g") .arg("2g")
.output() .output()
.unwrap(); .unwrap();
if !output.status.success() { if !output.status.success() {
eprintln!("{}", String::from_utf8_lossy(&output.stderr)); eprintln!("{}", String::from_utf8_lossy(&output.stderr));
panic!("mkfs.ext4 command generated an error"); panic!("mkfs.ext4 command generated an error");
} }
let vfio_tap0 = "vfio-tap0"; let vfio_tap0 = "vfio-tap0";
let vfio_tap1 = "vfio-tap1"; let vfio_tap1 = "vfio-tap1";
let vfio_tap2 = "vfio-tap2"; let vfio_tap2 = "vfio-tap2";
let vfio_tap3 = "vfio-tap3"; let vfio_tap3 = "vfio-tap3";
let mut child = GuestCommand::new(&guest) let mut child = GuestCommand::new(&guest)
.args(&["--cpus", "boot=4"]) .args(&["--cpus", "boot=4"])
.args(&["--memory", "size=2G,hugepages=on,shared=on"]) .args(&["--memory", "size=2G,hugepages=on,shared=on"])
.args(&["--kernel", kernel_path.to_str().unwrap()]) .args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&[ .args(&[
"--disk", "--disk",
format!( format!(
"path={}", "path={}",
guest.disk_config.disk(DiskType::OperatingSystem).unwrap() guest.disk_config.disk(DiskType::OperatingSystem).unwrap()
) )
.as_str(), .as_str(),
format!( format!(
"path={}", "path={}",
guest.disk_config.disk(DiskType::CloudInit).unwrap() guest.disk_config.disk(DiskType::CloudInit).unwrap()
) )
.as_str(), .as_str(),
format!("path={}", vfio_disk_path.to_str().unwrap()).as_str(), format!("path={}", vfio_disk_path.to_str().unwrap()).as_str(),
]) ])
.args(&[ .args(&[
"--cmdline", "--cmdline",
format!( format!(
"{} kvm-intel.nested=1 vfio_iommu_type1.allow_unsafe_interrupts", "{} kvm-intel.nested=1 vfio_iommu_type1.allow_unsafe_interrupts",
DIRECT_KERNEL_BOOT_CMDLINE DIRECT_KERNEL_BOOT_CMDLINE
) )
.as_str(), .as_str(),
]) ])
.args(&[ .args(&[
"--net", "--net",
format!("tap={},mac={}", vfio_tap0, guest.network.guest_mac).as_str(), format!("tap={},mac={}", vfio_tap0, guest.network.guest_mac).as_str(),
format!( format!(
"tap={},mac={},iommu=on", "tap={},mac={},iommu=on",
vfio_tap1, guest.network.l2_guest_mac1 vfio_tap1, guest.network.l2_guest_mac1
) )
.as_str(), .as_str(),
format!( format!(
"tap={},mac={},iommu=on", "tap={},mac={},iommu=on",
vfio_tap2, guest.network.l2_guest_mac2 vfio_tap2, guest.network.l2_guest_mac2
) )
.as_str(), .as_str(),
format!( format!(
"tap={},mac={},iommu=on", "tap={},mac={},iommu=on",
vfio_tap3, guest.network.l2_guest_mac3 vfio_tap3, guest.network.l2_guest_mac3
) )
.as_str(), .as_str(),
]) ])
.spawn() .capture_output()
.unwrap(); .spawn()
.unwrap();
thread::sleep(std::time::Duration::new(30, 0)); thread::sleep(std::time::Duration::new(30, 0));
guest.ssh_command_l1("sudo systemctl start vfio")?; let r = std::panic::catch_unwind(|| {
guest.ssh_command_l1("sudo systemctl start vfio").unwrap();
thread::sleep(std::time::Duration::new(120, 0)); thread::sleep(std::time::Duration::new(120, 0));
// We booted our cloud hypervisor L2 guest with a "VFIOTAG" tag // We booted our cloud hypervisor L2 guest with a "VFIOTAG" tag
@ -3340,8 +3341,7 @@ mod tests {
// Let's ssh into it and verify that it's there. If it is it means // Let's ssh into it and verify that it's there. If it is it means
// we're in the right guest (The L2 one) because the QEMU L1 guest // we're in the right guest (The L2 one) because the QEMU L1 guest
// does not have this command line tag. // does not have this command line tag.
aver_eq!( assert_eq!(
tb,
guest guest
.ssh_command_l2_1("grep -c VFIOTAG /proc/cmdline") .ssh_command_l2_1("grep -c VFIOTAG /proc/cmdline")
.unwrap_or_default() .unwrap_or_default()
@ -3353,8 +3353,7 @@ mod tests {
// Let's also verify from the second virtio-net device passed to // Let's also verify from the second virtio-net device passed to
// the L2 VM. // the L2 VM.
aver_eq!( assert_eq!(
tb,
guest guest
.ssh_command_l2_2("grep -c VFIOTAG /proc/cmdline") .ssh_command_l2_2("grep -c VFIOTAG /proc/cmdline")
.unwrap_or_default() .unwrap_or_default()
@ -3365,8 +3364,7 @@ mod tests {
); );
// Check the amount of PCI devices appearing in L2 VM. // Check the amount of PCI devices appearing in L2 VM.
aver_eq!( assert_eq!(
tb,
guest guest
.ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l")
.unwrap_or_default() .unwrap_or_default()
@ -3379,17 +3377,20 @@ mod tests {
// Hotplug an extra virtio-net device through L2 VM. // Hotplug an extra virtio-net device through L2 VM.
guest.ssh_command_l1( guest.ssh_command_l1(
"echo 0000:00:08.0 | sudo tee /sys/bus/pci/devices/0000:00:08.0/driver/unbind", "echo 0000:00:08.0 | sudo tee /sys/bus/pci/devices/0000:00:08.0/driver/unbind",
)?; ).unwrap();
guest.ssh_command_l1( guest
"echo 1af4 1041 | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id", .ssh_command_l1(
)?; "echo 1af4 1041 | sudo tee /sys/bus/pci/drivers/vfio-pci/new_id",
let vfio_hotplug_output = guest.ssh_command_l1( )
"sudo /mnt/ch-remote \ .unwrap();
let vfio_hotplug_output = guest
.ssh_command_l1(
"sudo /mnt/ch-remote \
--api-socket=/tmp/ch_api.sock \ --api-socket=/tmp/ch_api.sock \
add-device path=/sys/bus/pci/devices/0000:00:08.0,id=vfio123", add-device path=/sys/bus/pci/devices/0000:00:08.0,id=vfio123",
)?; )
aver!( .unwrap();
tb, assert!(
vfio_hotplug_output.contains("{\"id\":\"vfio123\",\"bdf\":\"0000:00:07.0\"}") vfio_hotplug_output.contains("{\"id\":\"vfio123\",\"bdf\":\"0000:00:07.0\"}")
); );
@ -3398,8 +3399,7 @@ mod tests {
// Let's also verify from the third virtio-net device passed to // Let's also verify from the third virtio-net device passed to
// the L2 VM. This third device has been hotplugged through the L2 // the L2 VM. This third device has been hotplugged through the L2
// VM, so this is our way to validate hotplug works for VFIO PCI. // VM, so this is our way to validate hotplug works for VFIO PCI.
aver_eq!( assert_eq!(
tb,
guest guest
.ssh_command_l2_3("grep -c VFIOTAG /proc/cmdline") .ssh_command_l2_3("grep -c VFIOTAG /proc/cmdline")
.unwrap_or_default() .unwrap_or_default()
@ -3412,8 +3412,7 @@ mod tests {
// Check the amount of PCI devices appearing in L2 VM. // Check the amount of PCI devices appearing in L2 VM.
// There should be one more device than before, raising the count // There should be one more device than before, raising the count
// up to 8 PCI devices. // up to 8 PCI devices.
aver_eq!( assert_eq!(
tb,
guest guest
.ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l")
.unwrap_or_default() .unwrap_or_default()
@ -3426,17 +3425,18 @@ mod tests {
// Let's now verify that we can correctly remove the virtio-net // Let's now verify that we can correctly remove the virtio-net
// device through the "remove-device" command responsible for // device through the "remove-device" command responsible for
// unplugging VFIO devices. // unplugging VFIO devices.
guest.ssh_command_l1( guest
"sudo /mnt/ch-remote \ .ssh_command_l1(
"sudo /mnt/ch-remote \
--api-socket=/tmp/ch_api.sock \ --api-socket=/tmp/ch_api.sock \
remove-device vfio123", remove-device vfio123",
)?; )
.unwrap();
thread::sleep(std::time::Duration::new(10, 0)); thread::sleep(std::time::Duration::new(10, 0));
// Check the amount of PCI devices appearing in L2 VM is back down // Check the amount of PCI devices appearing in L2 VM is back down
// to 7 devices. // to 7 devices.
aver_eq!( assert_eq!(
tb,
guest guest
.ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l") .ssh_command_l2_1("ls /sys/bus/pci/devices | wc -l")
.unwrap_or_default() .unwrap_or_default()
@ -3450,28 +3450,24 @@ mod tests {
// up as expected. In order to check, we will use the virtio-net // up as expected. In order to check, we will use the virtio-net
// device already passed through L2 as a VFIO device, this will // device already passed through L2 as a VFIO device, this will
// verify that VFIO devices are functional with memory hotplug. // verify that VFIO devices are functional with memory hotplug.
aver!( assert!(guest.get_total_memory_l2().unwrap_or_default() > 480_000);
tb,
guest.get_total_memory_l2().unwrap_or_default() > 480_000
);
guest.ssh_command_l2_1( guest.ssh_command_l2_1(
"sudo bash -c 'echo online > /sys/devices/system/memory/auto_online_blocks'", "sudo bash -c 'echo online > /sys/devices/system/memory/auto_online_blocks'",
)?; ).unwrap();
guest.ssh_command_l1( guest
"sudo /mnt/ch-remote \ .ssh_command_l1(
"sudo /mnt/ch-remote \
--api-socket=/tmp/ch_api.sock \ --api-socket=/tmp/ch_api.sock \
resize --memory=1073741824", resize --memory=1073741824",
)?; )
aver!( .unwrap();
tb, assert!(guest.get_total_memory_l2().unwrap_or_default() > 960_000);
guest.get_total_memory_l2().unwrap_or_default() > 960_000
);
let _ = child.kill();
let _ = child.wait();
Ok(())
}); });
let _ = child.kill();
let output = child.wait_with_output().unwrap();
handle_child_output(r, &output);
} }
#[cfg_attr(feature = "mmio", test)] #[cfg_attr(feature = "mmio", test)]