mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-10-22 20:39:41 +00:00
Merge pull request #11868 from burgerdev/serial-tests
kata-sys-util: use a tempdir per test case
This commit is contained in:
@@ -401,7 +401,11 @@ impl Handle {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let RouteAttribute::Oif(index) = attribute {
|
if let RouteAttribute::Oif(index) = attribute {
|
||||||
route.device = self.find_link(LinkFilter::Index(*index)).await?.name();
|
route.device = self
|
||||||
|
.find_link(LinkFilter::Index(*index))
|
||||||
|
.await
|
||||||
|
.context(format!("error looking up device {index}"))?
|
||||||
|
.name();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -973,10 +977,12 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn list_routes() {
|
async fn list_routes() {
|
||||||
|
let devices: Vec<Interface> = Handle::new().unwrap().list_interfaces().await.unwrap();
|
||||||
let all = Handle::new()
|
let all = Handle::new()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.list_routes()
|
.list_routes()
|
||||||
.await
|
.await
|
||||||
|
.context(format!("available devices: {:?}", devices))
|
||||||
.expect("Failed to list routes");
|
.expect("Failed to list routes");
|
||||||
|
|
||||||
assert_ne!(all.len(), 0);
|
assert_ne!(all.len(), 0);
|
||||||
@@ -1102,9 +1108,9 @@ mod tests {
|
|||||||
.output()
|
.output()
|
||||||
.expect("failed to add dummy interface");
|
.expect("failed to add dummy interface");
|
||||||
|
|
||||||
// ip addr add 192.168.0.2/16 dev dummy
|
// ip addr add 192.0.2.2/24 dev dummy
|
||||||
Command::new("ip")
|
Command::new("ip")
|
||||||
.args(["addr", "add", "192.168.0.2/16", "dev", dummy_name])
|
.args(["addr", "add", "192.0.2.2/24", "dev", dummy_name])
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to add ip for dummy");
|
.expect("failed to add ip for dummy");
|
||||||
|
|
||||||
@@ -1120,7 +1126,7 @@ mod tests {
|
|||||||
skip_if_not_root!();
|
skip_if_not_root!();
|
||||||
|
|
||||||
let mac = "6a:92:3a:59:70:aa";
|
let mac = "6a:92:3a:59:70:aa";
|
||||||
let to_ip = "169.254.1.1";
|
let to_ip = "192.0.2.127";
|
||||||
let dummy_name = "dummy_for_arp";
|
let dummy_name = "dummy_for_arp";
|
||||||
|
|
||||||
prepare_env_for_test_add_one_arp_neighbor(dummy_name, to_ip);
|
prepare_env_for_test_add_one_arp_neighbor(dummy_name, to_ip);
|
||||||
@@ -1141,13 +1147,19 @@ mod tests {
|
|||||||
.expect("Failed to add ARP neighbor");
|
.expect("Failed to add ARP neighbor");
|
||||||
|
|
||||||
// ip neigh show dev dummy ip
|
// ip neigh show dev dummy ip
|
||||||
let stdout = Command::new("ip")
|
let output = Command::new("ip")
|
||||||
.args(["neigh", "show", "dev", dummy_name, to_ip])
|
.args(["neigh", "show", "dev", dummy_name, to_ip])
|
||||||
.output()
|
.output()
|
||||||
.expect("failed to show neigh")
|
.expect("failed to show neigh");
|
||||||
.stdout;
|
|
||||||
|
|
||||||
let stdout = std::str::from_utf8(&stdout).expect("failed to convert stdout");
|
let stdout = std::str::from_utf8(&output.stdout).expect("failed to convert stdout");
|
||||||
|
let stderr = std::str::from_utf8(&output.stderr).expect("failed to convert stderr");
|
||||||
|
assert!(
|
||||||
|
output.status.success(),
|
||||||
|
"`ip neigh show` returned exit code {:?}. stderr: {:?}",
|
||||||
|
output.status.code(),
|
||||||
|
stderr
|
||||||
|
);
|
||||||
assert_eq!(stdout.trim(), format!("{} lladdr {} PERMANENT", to_ip, mac));
|
assert_eq!(stdout.trim(), format!("{} lladdr {} PERMANENT", to_ip, mac));
|
||||||
|
|
||||||
clean_env_for_test_add_one_arp_neighbor(dummy_name, to_ip);
|
clean_env_for_test_add_one_arp_neighbor(dummy_name, to_ip);
|
||||||
|
@@ -297,18 +297,16 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const MOCK_PCI_DEVICES_ROOT: &str = "tests/mock_devices";
|
|
||||||
// domain number
|
// domain number
|
||||||
const TEST_PCI_DEV_DOMAIN: &str = "0000";
|
const TEST_PCI_DEV_DOMAIN: &str = "0000";
|
||||||
// sysfs path
|
|
||||||
const MOCK_SYS_BUS_PCI_DEVICES: &str = "/tmp/bus/pci/devices";
|
|
||||||
|
|
||||||
// Mock data
|
// Mock data
|
||||||
fn setup_mock_device_files() {
|
fn setup_mock_device_files() -> tempfile::TempDir {
|
||||||
|
let dir = tempfile::tempdir().expect("tempdir should not fail");
|
||||||
// Create mock path and files for PCI devices
|
// Create mock path and files for PCI devices
|
||||||
let device_path = PathBuf::from(MOCK_PCI_DEVICES_ROOT).join("0000:ff:1f.0");
|
let device_path = dir.path().join("0000:ff:1f.0");
|
||||||
fs::create_dir_all(&device_path).unwrap();
|
fs::create_dir_all(&device_path).unwrap();
|
||||||
fs::write(device_path.join("vendor"), "0x8086").unwrap();
|
fs::write(device_path.join("vendor"), "0x8086").unwrap();
|
||||||
fs::write(device_path.join("device"), "0x1234").unwrap();
|
fs::write(device_path.join("device"), "0x1234").unwrap();
|
||||||
@@ -319,13 +317,7 @@ mod tests {
|
|||||||
"0x00000000 0x0000ffff 0x00000404\n",
|
"0x00000000 0x0000ffff 0x00000404\n",
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
dir
|
||||||
// Mock data
|
|
||||||
fn cleanup_mock_device_files() {
|
|
||||||
// Create mock path and files for PCI devices
|
|
||||||
let device_path = PathBuf::from(MOCK_PCI_DEVICES_ROOT).join("0000:ff:1f.0");
|
|
||||||
// Clean up
|
|
||||||
let _ = fs::remove_file(device_path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -380,10 +372,10 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_get_all_devices() {
|
fn test_get_all_devices() {
|
||||||
// Setup mock data
|
// Setup mock data
|
||||||
setup_mock_device_files();
|
let tmpdir = setup_mock_device_files();
|
||||||
|
|
||||||
// Initialize PCI device manager with the mock path
|
// Initialize PCI device manager with the mock path
|
||||||
let manager = PCIDeviceManager::new(MOCK_PCI_DEVICES_ROOT);
|
let manager = PCIDeviceManager::new(&tmpdir.path().to_string_lossy());
|
||||||
|
|
||||||
// Get all devices
|
// Get all devices
|
||||||
let devices_result = manager.get_all_devices(None);
|
let devices_result = manager.get_all_devices(None);
|
||||||
@@ -396,17 +388,14 @@ mod tests {
|
|||||||
assert_eq!(device.vendor, 0x8086);
|
assert_eq!(device.vendor, 0x8086);
|
||||||
assert_eq!(device.device, 0x1234);
|
assert_eq!(device.device, 0x1234);
|
||||||
assert_eq!(device.class, 0x060100);
|
assert_eq!(device.class, 0x060100);
|
||||||
|
|
||||||
// Cleanup mock data
|
|
||||||
cleanup_mock_device_files()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_resources() {
|
fn test_parse_resources() {
|
||||||
setup_mock_device_files();
|
let tmpdir = setup_mock_device_files();
|
||||||
|
|
||||||
let manager = PCIDeviceManager::new(MOCK_PCI_DEVICES_ROOT);
|
let manager = PCIDeviceManager::new(&tmpdir.path().to_string_lossy());
|
||||||
let device_path = PathBuf::from(MOCK_PCI_DEVICES_ROOT).join("0000:ff:1f.0");
|
let device_path = tmpdir.path().join("0000:ff:1f.0");
|
||||||
|
|
||||||
let resources_result = manager.parse_resources(&device_path);
|
let resources_result = manager.parse_resources(&device_path);
|
||||||
assert!(resources_result.is_ok());
|
assert!(resources_result.is_ok());
|
||||||
@@ -418,17 +407,14 @@ mod tests {
|
|||||||
assert_eq!(resource.start, 0x00000000);
|
assert_eq!(resource.start, 0x00000000);
|
||||||
assert_eq!(resource.end, 0x0000ffff);
|
assert_eq!(resource.end, 0x0000ffff);
|
||||||
assert_eq!(resource.flags, 0x00000404);
|
assert_eq!(resource.flags, 0x00000404);
|
||||||
|
|
||||||
cleanup_mock_device_files();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_pcie_device() {
|
fn test_is_pcie_device() {
|
||||||
// Create a mock PCI device config file
|
// Create a mock PCI device config file
|
||||||
let bdf = format!("{}:ff:00.0", TEST_PCI_DEV_DOMAIN);
|
let bdf = format!("{}:ff:00.0", TEST_PCI_DEV_DOMAIN);
|
||||||
let config_path = Path::new(MOCK_SYS_BUS_PCI_DEVICES)
|
let tmpdir = tempfile::tempdir().expect("tempdir should not fail");
|
||||||
.join(&bdf)
|
let config_path = tmpdir.path().join(&bdf).join("config");
|
||||||
.join("config");
|
|
||||||
let _ = fs::create_dir_all(config_path.parent().unwrap());
|
let _ = fs::create_dir_all(config_path.parent().unwrap());
|
||||||
|
|
||||||
// Write a file with a size larger than PCI_CONFIG_SPACE_SZ
|
// Write a file with a size larger than PCI_CONFIG_SPACE_SZ
|
||||||
@@ -437,9 +423,6 @@ mod tests {
|
|||||||
file.write_all(&vec![0; 512]).unwrap();
|
file.write_all(&vec![0; 512]).unwrap();
|
||||||
|
|
||||||
// It should be true
|
// It should be true
|
||||||
assert!(is_pcie_device("ff:00.0", MOCK_SYS_BUS_PCI_DEVICES));
|
assert!(is_pcie_device("ff:00.0", &tmpdir.path().to_string_lossy()));
|
||||||
|
|
||||||
// Clean up
|
|
||||||
let _ = fs::remove_file(config_path);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user