mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-07-04 19:16:23 +00:00
kata-sys-utils: Add test cases for devices
In this, the crate mockall is introduced to help mock get_all_devices. Fixes #10556 Signed-off-by: alex.lyn <alex.lyn@antgroup.com>
This commit is contained in:
parent
f714b6c049
commit
3e77377be0
77
src/libs/Cargo.lock
generated
77
src/libs/Cargo.lock
generated
@ -37,6 +37,12 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9"
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.57"
|
||||
@ -472,6 +478,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "downcast"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
@ -510,6 +522,12 @@ version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
|
||||
|
||||
[[package]]
|
||||
name = "fragile"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa"
|
||||
|
||||
[[package]]
|
||||
name = "funty"
|
||||
version = "2.0.0"
|
||||
@ -826,6 +844,7 @@ dependencies = [
|
||||
"kata-types",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"mockall",
|
||||
"nix 0.24.2",
|
||||
"num_cpus",
|
||||
"oci-spec",
|
||||
@ -968,6 +987,32 @@ dependencies = [
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mockall"
|
||||
version = "0.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39a6bfcc6c8c7eed5ee98b9c3e33adc726054389233e201c95dab2d41a3839d2"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"downcast",
|
||||
"fragile",
|
||||
"mockall_derive",
|
||||
"predicates",
|
||||
"predicates-tree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mockall_derive"
|
||||
version = "0.13.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25ca3004c2efe9011bd4e461bd8256445052b9615405b4f7ea43fc8ca5c20898"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.66",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multimap"
|
||||
version = "0.8.3"
|
||||
@ -1224,6 +1269,32 @@ version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||
|
||||
[[package]]
|
||||
name = "predicates"
|
||||
version = "3.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7e9086cc7640c29a356d1a29fd134380bee9d8f79a17410aa76e7ad295f42c97"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"predicates-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "predicates-core"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae8177bee8e75d6846599c6b9ff679ed51e882816914eec639944d7c9aa11931"
|
||||
|
||||
[[package]]
|
||||
name = "predicates-tree"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41b740d195ed3166cd147c8047ec98db0e22ec019eb8eeb76d343b795304fb13"
|
||||
dependencies = [
|
||||
"predicates-core",
|
||||
"termtree",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "0.1.5"
|
||||
@ -1945,6 +2016,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termtree"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76"
|
||||
|
||||
[[package]]
|
||||
name = "test-utils"
|
||||
version = "0.1.0"
|
||||
|
@ -29,6 +29,7 @@ rand = "0.8.5"
|
||||
thiserror = "1.0.30"
|
||||
hex = "0.4.3"
|
||||
pci-ids = "0.2.5"
|
||||
mockall = "0.13.1"
|
||||
|
||||
kata-types = { path = "../kata-types" }
|
||||
oci-spec = { version = "0.6.8", features = ["runtime"] }
|
||||
|
@ -76,3 +76,85 @@ pub fn get_bars_max_addressable_memory() -> (u64, u64) {
|
||||
|
||||
(max_32bit, max_64bit)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use super::*;
|
||||
use crate::pcilibs::pci_manager::{
|
||||
MemoryResource, MemoryResources, MockPCIDevices, PCI_BASE_ADDRESS_MEM_TYPE32,
|
||||
PCI_BASE_ADDRESS_MEM_TYPE64,
|
||||
};
|
||||
use mockall::predicate::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_bars_max_addressable_memory() {
|
||||
let pci_device = PCIDevice {
|
||||
device_path: PathBuf::new(),
|
||||
address: "0000:00:00.0".to_string(),
|
||||
vendor: PCI_NVIDIA_VENDOR_ID,
|
||||
class: PCI3D_CONTROLLER_CLASS,
|
||||
class_name: "3D Controller".to_string(),
|
||||
device: 0x1c82,
|
||||
device_name: "NVIDIA Device".to_string(),
|
||||
driver: "nvidia".to_string(),
|
||||
iommu_group: 0,
|
||||
numa_node: 0,
|
||||
resources: MemoryResources::default(),
|
||||
};
|
||||
let devices = vec![pci_device.clone()];
|
||||
|
||||
// Mock PCI device manager and devices
|
||||
let mut mock_pci_manager = MockPCIDevices::default();
|
||||
// Setting up Mock to return a device
|
||||
mock_pci_manager
|
||||
.expect_get_pci_devices()
|
||||
.with(eq(Some(PCI_NVIDIA_VENDOR_ID)))
|
||||
.returning(move |_| devices.clone());
|
||||
|
||||
// Create NvidiaPCIDevice
|
||||
let nvidia_device = NvidiaPCIDevice::new(PCI_NVIDIA_VENDOR_ID, PCI3D_CONTROLLER_CLASS);
|
||||
|
||||
// Prepare memory resources
|
||||
let mut resources: MemoryResources = HashMap::new();
|
||||
// resource0 memsz = end - start => 1024
|
||||
resources.insert(
|
||||
0,
|
||||
MemoryResource {
|
||||
start: 0,
|
||||
end: 1023,
|
||||
flags: PCI_BASE_ADDRESS_MEM_TYPE32,
|
||||
path: PathBuf::from("/fake/path/resource0"),
|
||||
},
|
||||
);
|
||||
// resource1 memsz = end - start => 1024
|
||||
resources.insert(
|
||||
1,
|
||||
MemoryResource {
|
||||
start: 1024,
|
||||
end: 2047,
|
||||
flags: PCI_BASE_ADDRESS_MEM_TYPE64,
|
||||
path: PathBuf::from("/fake/path/resource1"),
|
||||
},
|
||||
);
|
||||
|
||||
let pci_device_with_resources = PCIDevice {
|
||||
resources: resources.clone(),
|
||||
..pci_device
|
||||
};
|
||||
|
||||
mock_pci_manager
|
||||
.expect_get_pci_devices()
|
||||
.with(eq(Some(PCI_NVIDIA_VENDOR_ID)))
|
||||
.returning(move |_| vec![pci_device_with_resources.clone()]);
|
||||
|
||||
// Call the function under test
|
||||
let (max_32bit, max_64bit) = nvidia_device.get_bars_max_addressable_memory();
|
||||
|
||||
// Assert the results
|
||||
assert_eq!(max_32bit, 2 * 2 * 1024 * 1024);
|
||||
assert_eq!(max_64bit, 2 * 1024 * 1024);
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,13 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mockall::automock;
|
||||
use pci_ids::{Classes, Vendors};
|
||||
|
||||
const PCI_DEV_DOMAIN: &str = "0000";
|
||||
@ -96,6 +98,7 @@ impl MemoryResourceTrait for MemoryResources {
|
||||
}
|
||||
}
|
||||
|
||||
#[automock]
|
||||
pub trait PCIDevices {
|
||||
fn get_pci_devices(&self, vendor: Option<u16>) -> Vec<PCIDevice>;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user