diff --git a/src/runtime-rs/Cargo.lock b/src/runtime-rs/Cargo.lock index e4485e26c0..68872f7f0b 100644 --- a/src/runtime-rs/Cargo.lock +++ b/src/runtime-rs/Cargo.lock @@ -1385,6 +1385,7 @@ dependencies = [ "shim-interface", "slog", "slog-scope", + "tests_utils", "thiserror", "tokio", "tracing", diff --git a/src/runtime-rs/crates/hypervisor/Cargo.toml b/src/runtime-rs/crates/hypervisor/Cargo.toml index 85a4666ea2..f8e5497f5d 100644 --- a/src/runtime-rs/crates/hypervisor/Cargo.toml +++ b/src/runtime-rs/crates/hypervisor/Cargo.toml @@ -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" diff --git a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs index 76f62feed8..c1e6509142 100644 --- a/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs +++ b/src/runtime-rs/crates/hypervisor/src/device/device_manager.rs @@ -470,3 +470,66 @@ pub async fn do_handle_device( pub async fn get_block_driver(d: &RwLock) -> 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>> { + 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) + } + } +} diff --git a/src/runtime-rs/crates/resource/src/network/endpoint/endpoints_test.rs b/src/runtime-rs/crates/resource/src/network/endpoint/endpoints_test.rs index 2cf87e7879..7bfb429621 100644 --- a/src/runtime-rs/crates/resource/src/network/endpoint/endpoints_test.rs +++ b/src/runtime-rs/crates/resource/src/network/endpoint/endpoints_test.rs @@ -28,9 +28,6 @@ mod tests { async fn get_device_manager() -> Result>> { 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 diff --git a/src/runtime-rs/tests/texture/configuration-qemu.toml b/src/runtime-rs/tests/texture/configuration-qemu.toml index 807de57b69..b8d876f89e 100644 --- a/src/runtime-rs/tests/texture/configuration-qemu.toml +++ b/src/runtime-rs/tests/texture/configuration-qemu.toml @@ -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 diff --git a/src/runtime-rs/tests/utils/src/lib.rs b/src/runtime-rs/tests/utils/src/lib.rs index bd5246778e..9349682341 100644 --- a/src/runtime-rs/tests/utils/src/lib.rs +++ b/src/runtime-rs/tests/utils/src/lib.rs @@ -43,6 +43,7 @@ pub fn load_test_config(hypervisor_name: String) -> Result { let qemu = QemuConfig::new(); qemu.register(); } + // TODO add other hypervisor test config _ => { return Err(anyhow!("invalid hypervisor {}", hypervisor_name)); }