From b8813398669eac2ecdebf12362cc7de6a3d52cdd Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 19 Oct 2021 10:46:52 +0100 Subject: [PATCH] tests: Add test_net_multi_segment_hotplug Refactor the existing net hotplug test to support controlling the PCI segment the device should be added to and use this for a multiple segment test. Signed-off-by: Rob Bradford --- tests/integration.rs | 92 ++++++++++++++++++++++++++++++-------------- 1 file changed, 64 insertions(+), 28 deletions(-) diff --git a/tests/integration.rs b/tests/integration.rs index aa435246b..668e0c7b2 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -4931,6 +4931,16 @@ mod tests { #[test] fn test_net_hotplug() { + _test_net_hotplug(None) + } + + #[cfg(target_arch = "x86_64")] + #[test] + fn test_net_multi_segment_hotplug() { + _test_net_hotplug(Some(15)) + } + + fn _test_net_hotplug(pci_segment: Option) { let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); let guest = Guest::new(Box::new(focal)); @@ -4942,16 +4952,21 @@ mod tests { let api_socket = temp_api_path(&guest.tmp_dir); // Boot without network - let mut child = GuestCommand::new(&guest) - .args(&["--api-socket", &api_socket]) + let mut cmd = GuestCommand::new(&guest); + + cmd.args(&["--api-socket", &api_socket]) .args(&["--cpus", "boot=1"]) .args(&["--memory", "size=512M"]) .args(&["--kernel", kernel_path.to_str().unwrap()]) .args(&["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) .default_disks() - .capture_output() - .spawn() - .unwrap(); + .capture_output(); + + if pci_segment.is_some() { + cmd.args(&["--platform", "num_pci_segments=16"]); + } + + let mut child = cmd.spawn().unwrap(); thread::sleep(std::time::Duration::new(20, 0)); @@ -4960,16 +4975,30 @@ mod tests { let (cmd_success, cmd_output) = remote_command_w_output( &api_socket, "add-net", - Some(guest.default_net_string().as_str()), + Some( + format!( + "{}{},id=test0", + guest.default_net_string(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ) + .as_str(), + ), ); assert!(cmd_success); - #[cfg(target_arch = "x86_64")] - assert!(String::from_utf8_lossy(&cmd_output) - .contains("{\"id\":\"_net2\",\"bdf\":\"0000:00:05.0\"}")); - #[cfg(target_arch = "aarch64")] - assert!(String::from_utf8_lossy(&cmd_output) - .contains("{\"id\":\"_net0\",\"bdf\":\"0000:00:05.0\"}")); + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"test0\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test0\",\"bdf\":\"0000:00:05.0\"}")); + } thread::sleep(std::time::Duration::new(5, 0)); @@ -4985,29 +5014,36 @@ mod tests { ); // Remove network - assert!(remote_command( - &api_socket, - "remove-device", - #[cfg(target_arch = "x86_64")] - Some("_net2"), - #[cfg(target_arch = "aarch64")] - Some("_net0") - )); + assert!(remote_command(&api_socket, "remove-device", Some("test0"),)); thread::sleep(std::time::Duration::new(5, 0)); - // Add network again let (cmd_success, cmd_output) = remote_command_w_output( &api_socket, "add-net", - Some(guest.default_net_string().as_str()), + Some( + format!( + "{}{},id=test1", + guest.default_net_string(), + if let Some(pci_segment) = pci_segment { + format!(",pci_segment={}", pci_segment) + } else { + "".to_owned() + } + ) + .as_str(), + ), ); assert!(cmd_success); - #[cfg(target_arch = "x86_64")] - assert!(String::from_utf8_lossy(&cmd_output) - .contains("{\"id\":\"_net3\",\"bdf\":\"0000:00:05.0\"}")); - #[cfg(target_arch = "aarch64")] - assert!(String::from_utf8_lossy(&cmd_output) - .contains("{\"id\":\"_net1\",\"bdf\":\"0000:00:05.0\"}")); + + if let Some(pci_segment) = pci_segment { + assert!(String::from_utf8_lossy(&cmd_output).contains(&format!( + "{{\"id\":\"test1\",\"bdf\":\"{:04x}:00:01.0\"}}", + pci_segment + ))); + } else { + assert!(String::from_utf8_lossy(&cmd_output) + .contains("{\"id\":\"test1\",\"bdf\":\"0000:00:05.0\"}")); + } thread::sleep(std::time::Duration::new(5, 0));