From 87c5823c4bbc2848cbfaf614c88027f5d7368d56 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Tue, 9 Feb 2021 12:05:48 +1100 Subject: [PATCH] agent/device: Add unit test for pcipath_to_sysfs() Port this test from the Kata 1 Go agent to the Kata 2 Rust agent. Signed-off-by: David Gibson --- src/agent/src/device.rs | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/agent/src/device.rs b/src/agent/src/device.rs index b4d10a36d7..f69fa5c98f 100644 --- a/src/agent/src/device.rs +++ b/src/agent/src/device.rs @@ -435,6 +435,7 @@ pub fn update_device_cgroup(spec: &mut Spec) -> Result<()> { mod tests { use super::*; use oci::Linux; + use tempfile::tempdir; #[test] fn test_update_device_cgroup() { @@ -714,4 +715,68 @@ mod tests { assert_eq!(Some(host_major), specresources.devices[1].major); assert_eq!(Some(host_minor), specresources.devices[1].minor); } + + #[test] + fn test_pcipath_to_sysfs() { + let testdir = tempdir().expect("failed to create tmpdir"); + let rootbuspath = testdir.path().to_str().unwrap(); + + let path2 = pci::Path::from_str("02").unwrap(); + let path23 = pci::Path::from_str("02/03").unwrap(); + let path234 = pci::Path::from_str("02/03/04").unwrap(); + + let relpath = pcipath_to_sysfs(rootbuspath, &path2); + assert_eq!(relpath.unwrap(), "/0000:00:02.0"); + + let relpath = pcipath_to_sysfs(rootbuspath, &path23); + assert!(relpath.is_err()); + + let relpath = pcipath_to_sysfs(rootbuspath, &path234); + assert!(relpath.is_err()); + + // Create mock sysfs files for the device at 0000:00:02.0 + let bridge2path = format!("{}{}", rootbuspath, "/0000:00:02.0"); + + fs::create_dir_all(&bridge2path).unwrap(); + + let relpath = pcipath_to_sysfs(rootbuspath, &path2); + assert_eq!(relpath.unwrap(), "/0000:00:02.0"); + + let relpath = pcipath_to_sysfs(rootbuspath, &path23); + assert!(relpath.is_err()); + + let relpath = pcipath_to_sysfs(rootbuspath, &path234); + assert!(relpath.is_err()); + + // Create mock sysfs files to indicate that 0000:00:02.0 is a bridge to bus 01 + let bridge2bus = "0000:01"; + let bus2path = format!("{}/pci_bus/{}", bridge2path, bridge2bus); + + fs::create_dir_all(bus2path).unwrap(); + + let relpath = pcipath_to_sysfs(rootbuspath, &path2); + assert_eq!(relpath.unwrap(), "/0000:00:02.0"); + + let relpath = pcipath_to_sysfs(rootbuspath, &path23); + assert_eq!(relpath.unwrap(), "/0000:00:02.0/0000:01:03.0"); + + let relpath = pcipath_to_sysfs(rootbuspath, &path234); + assert!(relpath.is_err()); + + // Create mock sysfs files for a bridge at 0000:01:03.0 to bus 02 + let bridge3path = format!("{}/0000:01:03.0", bridge2path); + let bridge3bus = "0000:02"; + let bus3path = format!("{}/pci_bus/{}", bridge3path, bridge3bus); + + fs::create_dir_all(bus3path).unwrap(); + + let relpath = pcipath_to_sysfs(rootbuspath, &path2); + assert_eq!(relpath.unwrap(), "/0000:00:02.0"); + + let relpath = pcipath_to_sysfs(rootbuspath, &path23); + assert_eq!(relpath.unwrap(), "/0000:00:02.0/0000:01:03.0"); + + let relpath = pcipath_to_sysfs(rootbuspath, &path234); + assert_eq!(relpath.unwrap(), "/0000:00:02.0/0000:01:03.0/0000:02:04.0"); + } }