mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-04-28 03:42:09 +00:00
runtime-rs: add unit test for block driver
add unit test for block driver Fixes:#7539 Signed-off-by: Zhongtao Hu <zhongtaohu.tim@linux.alibaba.com>
This commit is contained in:
parent
e44919f0da
commit
d90f7ac689
1
src/runtime-rs/Cargo.lock
generated
1
src/runtime-rs/Cargo.lock
generated
@ -1385,6 +1385,7 @@ dependencies = [
|
||||
"shim-interface",
|
||||
"slog",
|
||||
"slog-scope",
|
||||
"tests_utils",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tracing",
|
||||
|
@ -38,6 +38,7 @@ shim-interface = { path = "../../../libs/shim-interface" }
|
||||
dragonball = { path = "../../../dragonball", features = ["atomic-guest-memory", "virtio-vsock", "hotplug", "virtio-blk", "virtio-net", "virtio-fs", "dbs-upcall"] }
|
||||
|
||||
ch-config = { path = "ch-config", optional = true }
|
||||
tests_utils = { path = "../../tests/utils" }
|
||||
|
||||
futures = "0.3.25"
|
||||
safe-path = "0.1.0"
|
||||
|
@ -470,3 +470,66 @@ pub async fn do_handle_device(
|
||||
pub async fn get_block_driver(d: &RwLock<DeviceManager>) -> String {
|
||||
d.read().await.get_block_driver().await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::DeviceManager;
|
||||
use crate::{
|
||||
device::{device_manager::get_block_driver, DeviceConfig, DeviceType},
|
||||
qemu::Qemu,
|
||||
BlockConfig, KATA_BLK_DEV_TYPE,
|
||||
};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use std::sync::Arc;
|
||||
use tests_utils::load_test_config;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
async fn new_device_manager() -> Result<Arc<RwLock<DeviceManager>>> {
|
||||
let hypervisor_name: &str = "qemu";
|
||||
let toml_config = load_test_config(hypervisor_name.to_owned())?;
|
||||
let hypervisor_config = toml_config
|
||||
.hypervisor
|
||||
.get(hypervisor_name)
|
||||
.ok_or_else(|| anyhow!("failed to get hypervisor for {}", &hypervisor_name))?;
|
||||
|
||||
let mut hypervisor = Qemu::new();
|
||||
hypervisor
|
||||
.set_hypervisor_config(hypervisor_config.clone())
|
||||
.await;
|
||||
|
||||
let dm = Arc::new(RwLock::new(
|
||||
DeviceManager::new(Arc::new(hypervisor))
|
||||
.await
|
||||
.context("device manager")?,
|
||||
));
|
||||
|
||||
Ok(dm)
|
||||
}
|
||||
|
||||
#[actix_rt::test]
|
||||
async fn test_new_block_device() {
|
||||
let dm = new_device_manager().await;
|
||||
assert!(dm.is_ok());
|
||||
|
||||
let d = dm.unwrap();
|
||||
let block_driver = get_block_driver(&d).await;
|
||||
let dev_info = DeviceConfig::BlockCfg(BlockConfig {
|
||||
path_on_host: "/dev/dddzzz".to_string(),
|
||||
driver_option: block_driver,
|
||||
..Default::default()
|
||||
});
|
||||
let new_device_result = d.write().await.new_device(&dev_info).await;
|
||||
assert!(new_device_result.is_ok());
|
||||
|
||||
let device_id = new_device_result.unwrap();
|
||||
let devices_info_result = d.read().await.get_device_info(&device_id).await;
|
||||
assert!(devices_info_result.is_ok());
|
||||
|
||||
let device_info = devices_info_result.unwrap();
|
||||
if let DeviceType::Block(device) = device_info {
|
||||
assert_eq!(device.config.driver_option, KATA_BLK_DEV_TYPE);
|
||||
} else {
|
||||
assert_eq!(1, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,9 +28,6 @@ mod tests {
|
||||
|
||||
async fn get_device_manager() -> Result<Arc<RwLock<DeviceManager>>> {
|
||||
let hypervisor_name: &str = "qemu";
|
||||
if let Err(e) = load_test_config(hypervisor_name.to_owned()) {
|
||||
println!("Test failed with error: {}", e);
|
||||
}
|
||||
let toml_config = load_test_config(hypervisor_name.to_owned())?;
|
||||
let hypervisor_config = toml_config
|
||||
.hypervisor
|
||||
|
@ -33,7 +33,7 @@ valid_virtio_fs_daemon_paths = ["/usr/local/bin/virtiofsd*","./virtio_fs"]
|
||||
virtio_fs_cache_size = 512
|
||||
virtio_fs_extra_args = ["-o", "arg1=xxx,arg2", "-o", "hello world", "--arg3=yyy"]
|
||||
virtio_fs_cache = "always"
|
||||
block_device_driver = "virtio-blk"
|
||||
block_device_driver = "virtio-blk-pci"
|
||||
block_device_cache_set = true
|
||||
block_device_cache_direct = true
|
||||
block_device_cache_noflush = true
|
||||
|
@ -43,6 +43,7 @@ pub fn load_test_config(hypervisor_name: String) -> Result<TomlConfig> {
|
||||
let qemu = QemuConfig::new();
|
||||
qemu.register();
|
||||
}
|
||||
// TODO add other hypervisor test config
|
||||
_ => {
|
||||
return Err(anyhow!("invalid hypervisor {}", hypervisor_name));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user