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 <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2021-10-19 10:46:52 +01:00
parent 0afa619d2c
commit b881339866

View File

@ -4931,6 +4931,16 @@ mod tests {
#[test] #[test]
fn test_net_hotplug() { 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<u16>) {
let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string()); let focal = UbuntuDiskConfig::new(FOCAL_IMAGE_NAME.to_string());
let guest = Guest::new(Box::new(focal)); let guest = Guest::new(Box::new(focal));
@ -4942,16 +4952,21 @@ mod tests {
let api_socket = temp_api_path(&guest.tmp_dir); let api_socket = temp_api_path(&guest.tmp_dir);
// Boot without network // Boot without network
let mut child = GuestCommand::new(&guest) let mut cmd = GuestCommand::new(&guest);
.args(&["--api-socket", &api_socket])
cmd.args(&["--api-socket", &api_socket])
.args(&["--cpus", "boot=1"]) .args(&["--cpus", "boot=1"])
.args(&["--memory", "size=512M"]) .args(&["--memory", "size=512M"])
.args(&["--kernel", kernel_path.to_str().unwrap()]) .args(&["--kernel", kernel_path.to_str().unwrap()])
.args(&["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE]) .args(&["--cmdline", DIRECT_KERNEL_BOOT_CMDLINE])
.default_disks() .default_disks()
.capture_output() .capture_output();
.spawn()
.unwrap(); 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)); thread::sleep(std::time::Duration::new(20, 0));
@ -4960,16 +4975,30 @@ mod tests {
let (cmd_success, cmd_output) = remote_command_w_output( let (cmd_success, cmd_output) = remote_command_w_output(
&api_socket, &api_socket,
"add-net", "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); assert!(cmd_success);
#[cfg(target_arch = "x86_64")] if let Some(pci_segment) = pci_segment {
assert!(String::from_utf8_lossy(&cmd_output) assert!(String::from_utf8_lossy(&cmd_output).contains(&format!(
.contains("{\"id\":\"_net2\",\"bdf\":\"0000:00:05.0\"}")); "{{\"id\":\"test0\",\"bdf\":\"{:04x}:00:01.0\"}}",
#[cfg(target_arch = "aarch64")] pci_segment
assert!(String::from_utf8_lossy(&cmd_output) )));
.contains("{\"id\":\"_net0\",\"bdf\":\"0000:00:05.0\"}")); } else {
assert!(String::from_utf8_lossy(&cmd_output)
.contains("{\"id\":\"test0\",\"bdf\":\"0000:00:05.0\"}"));
}
thread::sleep(std::time::Duration::new(5, 0)); thread::sleep(std::time::Duration::new(5, 0));
@ -4985,29 +5014,36 @@ mod tests {
); );
// Remove network // Remove network
assert!(remote_command( assert!(remote_command(&api_socket, "remove-device", Some("test0"),));
&api_socket,
"remove-device",
#[cfg(target_arch = "x86_64")]
Some("_net2"),
#[cfg(target_arch = "aarch64")]
Some("_net0")
));
thread::sleep(std::time::Duration::new(5, 0)); thread::sleep(std::time::Duration::new(5, 0));
// Add network again
let (cmd_success, cmd_output) = remote_command_w_output( let (cmd_success, cmd_output) = remote_command_w_output(
&api_socket, &api_socket,
"add-net", "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); assert!(cmd_success);
#[cfg(target_arch = "x86_64")]
assert!(String::from_utf8_lossy(&cmd_output) if let Some(pci_segment) = pci_segment {
.contains("{\"id\":\"_net3\",\"bdf\":\"0000:00:05.0\"}")); assert!(String::from_utf8_lossy(&cmd_output).contains(&format!(
#[cfg(target_arch = "aarch64")] "{{\"id\":\"test1\",\"bdf\":\"{:04x}:00:01.0\"}}",
assert!(String::from_utf8_lossy(&cmd_output) pci_segment
.contains("{\"id\":\"_net1\",\"bdf\":\"0000:00:05.0\"}")); )));
} else {
assert!(String::from_utf8_lossy(&cmd_output)
.contains("{\"id\":\"test1\",\"bdf\":\"0000:00:05.0\"}"));
}
thread::sleep(std::time::Duration::new(5, 0)); thread::sleep(std::time::Duration::new(5, 0));