diff --git a/src/agent/src/device.rs b/src/agent/src/device.rs index 6e3b8e0e82..63134eb49c 100644 --- a/src/agent/src/device.rs +++ b/src/agent/src/device.rs @@ -11,91 +11,6 @@ fn sl() -> slog::Logger { #[cfg(test)] mod tests { use super::*; - - #[tokio::test] - #[allow(clippy::redundant_clone)] - async fn test_virtio_blk_matcher() { - let root_bus = create_pci_root_bus_path(); - let devname = "vda"; - - let mut uev_a = crate::uevent::Uevent::default(); - let relpath_a = "/0000:00:0a.0"; - uev_a.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); - uev_a.subsystem = BLOCK.to_string(); - uev_a.devname = devname.to_string(); - uev_a.devpath = format!("{}{}/virtio4/block/{}", root_bus, relpath_a, devname); - let matcher_a = VirtioBlkPciMatcher::new(relpath_a); - - let mut uev_b = uev_a.clone(); - let relpath_b = "/0000:00:0a.0/0000:00:0b.0"; - uev_b.devpath = format!("{}{}/virtio0/block/{}", root_bus, relpath_b, devname); - let matcher_b = VirtioBlkPciMatcher::new(relpath_b); - - assert!(matcher_a.is_match(&uev_a)); - assert!(matcher_b.is_match(&uev_b)); - assert!(!matcher_b.is_match(&uev_a)); - assert!(!matcher_a.is_match(&uev_b)); - } - - #[cfg(target_arch = "s390x")] - #[tokio::test] - async fn test_virtio_blk_ccw_matcher() { - let root_bus = CCW_ROOT_BUS_PATH; - let subsystem = "block"; - let devname = "vda"; - let relpath = "0.0.0002"; - - let mut uev = crate::uevent::Uevent::default(); - uev.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); - uev.subsystem = subsystem.to_string(); - uev.devname = devname.to_string(); - uev.devpath = format!( - "{}/0.0.0001/{}/virtio1/{}/{}", - root_bus, relpath, subsystem, devname - ); - - // Valid path - let device = ccw::Device::from_str(relpath).unwrap(); - let matcher = VirtioBlkCCWMatcher::new(root_bus, &device); - assert!(matcher.is_match(&uev)); - - // Invalid paths - uev.devpath = format!( - "{}/0.0.0001/0.0.0003/virtio1/{}/{}", - root_bus, subsystem, devname - ); - assert!(!matcher.is_match(&uev)); - - uev.devpath = format!("0.0.0001/{}/virtio1/{}/{}", relpath, subsystem, devname); - assert!(!matcher.is_match(&uev)); - - uev.devpath = format!( - "{}/0.0.0001/{}/virtio/{}/{}", - root_bus, relpath, subsystem, devname - ); - assert!(!matcher.is_match(&uev)); - - uev.devpath = format!("{}/0.0.0001/{}/virtio1", root_bus, relpath); - assert!(!matcher.is_match(&uev)); - - uev.devpath = format!( - "{}/1.0.0001/{}/virtio1/{}/{}", - root_bus, relpath, subsystem, devname - ); - assert!(!matcher.is_match(&uev)); - - uev.devpath = format!( - "{}/0.4.0001/{}/virtio1/{}/{}", - root_bus, relpath, subsystem, devname - ); - assert!(!matcher.is_match(&uev)); - - uev.devpath = format!( - "{}/0.0.10000/{}/virtio1/{}/{}", - root_bus, relpath, subsystem, devname - ); - assert!(!matcher.is_match(&uev)); - } #[tokio::test] #[allow(clippy::redundant_clone)] @@ -150,69 +65,6 @@ mod tests { assert!(!matcher_a.is_match(&uev_b)); } - #[tokio::test] - #[allow(clippy::redundant_clone)] - async fn test_net_pci_matcher() { - let root_bus = create_pci_root_bus_path(); - let relpath_a = "/0000:00:02.0/0000:01:01.0"; - - let mut uev_a = crate::uevent::Uevent::default(); - uev_a.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); - uev_a.devpath = format!("{}{}", root_bus, relpath_a); - uev_a.subsystem = String::from("net"); - uev_a.interface = String::from("eth0"); - let matcher_a = NetPciMatcher::new(relpath_a); - println!("Matcher a : {}", matcher_a.devpath); - - let relpath_b = "/0000:00:02.0/0000:01:02.0"; - let mut uev_b = uev_a.clone(); - uev_b.devpath = format!("{}{}", root_bus, relpath_b); - let matcher_b = NetPciMatcher::new(relpath_b); - - assert!(matcher_a.is_match(&uev_a)); - assert!(matcher_b.is_match(&uev_b)); - assert!(!matcher_b.is_match(&uev_a)); - assert!(!matcher_a.is_match(&uev_b)); - - let relpath_c = "/0000:00:02.0/0000:01:03.0"; - let net_substr = "/net/eth0"; - let mut uev_c = uev_a.clone(); - uev_c.devpath = format!("{}{}{}", root_bus, relpath_c, net_substr); - let matcher_c = NetPciMatcher::new(relpath_c); - - assert!(matcher_c.is_match(&uev_c)); - assert!(!matcher_a.is_match(&uev_c)); - assert!(!matcher_b.is_match(&uev_c)); - } - - #[tokio::test] - #[allow(clippy::redundant_clone)] - async fn test_mmio_block_matcher() { - let devname_a = "vda"; - let devname_b = "vdb"; - let mut uev_a = crate::uevent::Uevent::default(); - uev_a.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); - uev_a.subsystem = BLOCK.to_string(); - uev_a.devname = devname_a.to_string(); - uev_a.devpath = format!( - "/sys/devices/virtio-mmio-cmdline/virtio-mmio.0/virtio0/block/{}", - devname_a - ); - let matcher_a = MmioBlockMatcher::new(devname_a); - - let mut uev_b = uev_a.clone(); - uev_b.devpath = format!( - "/sys/devices/virtio-mmio-cmdline/virtio-mmio.4/virtio4/block/{}", - devname_b - ); - let matcher_b = MmioBlockMatcher::new(devname_b); - - assert!(matcher_a.is_match(&uev_a)); - assert!(matcher_b.is_match(&uev_b)); - assert!(!matcher_b.is_match(&uev_a)); - assert!(!matcher_a.is_match(&uev_b)); - } - #[test] fn test_split_vfio_pci_option() { assert_eq!( diff --git a/src/agent/src/device/block_device_handler.rs b/src/agent/src/device/block_device_handler.rs index 914b9a25e6..d518f9d56a 100644 --- a/src/agent/src/device/block_device_handler.rs +++ b/src/agent/src/device/block_device_handler.rs @@ -222,3 +222,121 @@ impl UeventMatcher for VirtioBlkCCWMatcher { uev.action == "add" && self.rex.is_match(&uev.devpath) && !uev.devname.is_empty() } } + +#[cfg(test)] +mod tests { + use super::*; + + #[tokio::test] + #[allow(clippy::redundant_clone)] + async fn test_virtio_blk_matcher() { + let root_bus = create_pci_root_bus_path(); + let devname = "vda"; + + let mut uev_a = crate::uevent::Uevent::default(); + let relpath_a = "/0000:00:0a.0"; + uev_a.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); + uev_a.subsystem = BLOCK.to_string(); + uev_a.devname = devname.to_string(); + uev_a.devpath = format!("{}{}/virtio4/block/{}", root_bus, relpath_a, devname); + let matcher_a = VirtioBlkPciMatcher::new(relpath_a); + + let mut uev_b = uev_a.clone(); + let relpath_b = "/0000:00:0a.0/0000:00:0b.0"; + uev_b.devpath = format!("{}{}/virtio0/block/{}", root_bus, relpath_b, devname); + let matcher_b = VirtioBlkPciMatcher::new(relpath_b); + + assert!(matcher_a.is_match(&uev_a)); + assert!(matcher_b.is_match(&uev_b)); + assert!(!matcher_b.is_match(&uev_a)); + assert!(!matcher_a.is_match(&uev_b)); + } + + #[cfg(target_arch = "s390x")] + #[tokio::test] + async fn test_virtio_blk_ccw_matcher() { + let root_bus = CCW_ROOT_BUS_PATH; + let subsystem = "block"; + let devname = "vda"; + let relpath = "0.0.0002"; + + let mut uev = crate::uevent::Uevent::default(); + uev.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); + uev.subsystem = subsystem.to_string(); + uev.devname = devname.to_string(); + uev.devpath = format!( + "{}/0.0.0001/{}/virtio1/{}/{}", + root_bus, relpath, subsystem, devname + ); + + // Valid path + let device = ccw::Device::from_str(relpath).unwrap(); + let matcher = VirtioBlkCCWMatcher::new(root_bus, &device); + assert!(matcher.is_match(&uev)); + + // Invalid paths + uev.devpath = format!( + "{}/0.0.0001/0.0.0003/virtio1/{}/{}", + root_bus, subsystem, devname + ); + assert!(!matcher.is_match(&uev)); + + uev.devpath = format!("0.0.0001/{}/virtio1/{}/{}", relpath, subsystem, devname); + assert!(!matcher.is_match(&uev)); + + uev.devpath = format!( + "{}/0.0.0001/{}/virtio/{}/{}", + root_bus, relpath, subsystem, devname + ); + assert!(!matcher.is_match(&uev)); + + uev.devpath = format!("{}/0.0.0001/{}/virtio1", root_bus, relpath); + assert!(!matcher.is_match(&uev)); + + uev.devpath = format!( + "{}/1.0.0001/{}/virtio1/{}/{}", + root_bus, relpath, subsystem, devname + ); + assert!(!matcher.is_match(&uev)); + + uev.devpath = format!( + "{}/0.4.0001/{}/virtio1/{}/{}", + root_bus, relpath, subsystem, devname + ); + assert!(!matcher.is_match(&uev)); + + uev.devpath = format!( + "{}/0.0.10000/{}/virtio1/{}/{}", + root_bus, relpath, subsystem, devname + ); + assert!(!matcher.is_match(&uev)); + } + + #[tokio::test] + #[allow(clippy::redundant_clone)] + async fn test_virtio_blk_mmio_matcher() { + let devname_a = "vda"; + let devname_b = "vdb"; + let mut uev_a = crate::uevent::Uevent::default(); + uev_a.action = crate::linux_abi::U_EVENT_ACTION_ADD.to_string(); + uev_a.subsystem = BLOCK.to_string(); + uev_a.devname = devname_a.to_string(); + uev_a.devpath = format!( + "/sys/devices/virtio-mmio-cmdline/virtio-mmio.0/virtio0/block/{}", + devname_a + ); + let matcher_a = VirtioBlkMmioMatcher::new(devname_a); + + let mut uev_b = uev_a.clone(); + uev_b.devpath = format!( + "/sys/devices/virtio-mmio-cmdline/virtio-mmio.4/virtio4/block/{}", + devname_b + ); + let matcher_b = VirtioBlkMmioMatcher::new(devname_b); + + assert!(matcher_a.is_match(&uev_a)); + assert!(matcher_b.is_match(&uev_b)); + assert!(!matcher_b.is_match(&uev_a)); + assert!(!matcher_a.is_match(&uev_b)); + } +}