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:
Zhongtao Hu 2023-08-15 19:23:27 +08:00
parent e44919f0da
commit d90f7ac689
6 changed files with 67 additions and 4 deletions

View File

@ -1385,6 +1385,7 @@ dependencies = [
"shim-interface",
"slog",
"slog-scope",
"tests_utils",
"thiserror",
"tokio",
"tracing",

View File

@ -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"

View File

@ -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)
}
}
}

View File

@ -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

View File

@ -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

View File

@ -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));
}