diff --git a/src/runtime-rs/Cargo.lock b/src/runtime-rs/Cargo.lock index 90f29db44b..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", @@ -2762,6 +2763,7 @@ dependencies = [ "slog-scope", "tempfile", "test-utils", + "tests_utils", "tokio", "tracing", "uuid", @@ -3325,6 +3327,8 @@ dependencies = [ name = "tests_utils" version = "0.1.0" dependencies = [ + "anyhow", + "kata-types", "rand 0.8.5", ] 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/Cargo.toml b/src/runtime-rs/crates/resource/Cargo.toml index 62f1c2fd04..78d203d4a8 100644 --- a/src/runtime-rs/crates/resource/Cargo.toml +++ b/src/runtime-rs/crates/resource/Cargo.toml @@ -11,6 +11,7 @@ tempfile = "3.2.0" [dependencies] anyhow = "^1.0" +actix-rt = "2.7.0" async-trait = "0.1.48" bitflags = "1.2.1" byte-unit = "4.0.14" @@ -40,7 +41,7 @@ kata-types = { path = "../../../libs/kata-types" } kata-sys-util = { path = "../../../libs/kata-sys-util" } logging = { path = "../../../libs/logging" } oci = { path = "../../../libs/oci" } -actix-rt = "2.7.0" persist = { path = "../persist"} +tests_utils = { path = "../../tests/utils" } [features] diff --git a/src/runtime-rs/crates/resource/src/manager_inner.rs b/src/runtime-rs/crates/resource/src/manager_inner.rs index f4e17ce748..cadc75f67e 100644 --- a/src/runtime-rs/crates/resource/src/manager_inner.rs +++ b/src/runtime-rs/crates/resource/src/manager_inner.rs @@ -11,7 +11,7 @@ use anyhow::{anyhow, Context, Ok, Result}; use async_trait::async_trait; use hypervisor::{ device::{ - device_manager::{do_handle_device, DeviceManager}, + device_manager::{do_handle_device, get_block_driver, DeviceManager}, util::{get_host_path, DEVICE_TYPE_CHAR}, DeviceConfig, DeviceType, }, @@ -277,13 +277,15 @@ impl ResourceManagerInner { for d in linux.devices.iter() { match d.r#type.as_str() { "b" => { + let block_driver = get_block_driver(&self.device_manager).await; let dev_info = DeviceConfig::BlockCfg(BlockConfig { major: d.major, minor: d.minor, + driver_option: block_driver, ..Default::default() }); - let device_info = do_handle_device(&self.device_manager.clone(), &dev_info) + let device_info = do_handle_device(&self.device_manager, &dev_info) .await .context("do handle device")?; 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 0a7b545f29..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 @@ -6,11 +6,12 @@ #[cfg(test)] mod tests { - use std::{fs, path::Path, sync::Arc}; + use std::sync::Arc; use anyhow::{anyhow, Context, Result}; use netlink_packet_route::MACVLAN_MODE_PRIVATE; use scopeguard::defer; + use tests_utils::load_test_config; use tokio::sync::RwLock; use crate::network::{ @@ -24,22 +25,10 @@ mod tests { utils::link::net_test_utils::delete_link, }; use hypervisor::{device::device_manager::DeviceManager, qemu::Qemu}; - use kata_types::config::{QemuConfig, TomlConfig}; async fn get_device_manager() -> Result>> { - let path = env!("CARGO_MANIFEST_DIR"); - let path = Path::new(path) - .join("../../../libs/kata-types/tests/texture/configuration-anno-0.toml"); - - let content = fs::read_to_string(path).context("read configuration failed")?; - // just for test, use x/kata-types/tests/texture/configuration-anno-0.toml as - // the test configuration.toml which is for qemu. let hypervisor_name: &str = "qemu"; - - let qemu = QemuConfig::new(); - qemu.register(); - - let toml_config = TomlConfig::load(&content).context("load toml config failed")?; + let toml_config = load_test_config(hypervisor_name.to_owned())?; let hypervisor_config = toml_config .hypervisor .get(hypervisor_name) diff --git a/src/runtime-rs/tests/texture/configuration-qemu.toml b/src/runtime-rs/tests/texture/configuration-qemu.toml new file mode 100644 index 0000000000..b8d876f89e --- /dev/null +++ b/src/runtime-rs/tests/texture/configuration-qemu.toml @@ -0,0 +1,90 @@ +[hypervisor.qemu] +path = "/usr/bin/lsns" +valid_hypervisor_paths = ["/usr/bin/qemu*", "/opt/qemu?","/usr/bin/ls*","./hypervisor_path"] +valid_jailer_paths = ["/usr/lib/rust","./test_jailer_path"] +ctlpath = "/usr/bin/" +valid_ctlpaths = ["/usr/lib/jvm","usr/bin/qemu-io","./jvm"] +disable_nesting_checks = true +enable_iothreads = true +jailer_path = "/usr/local" +kernel = "/usr/bin/../bin/zcmp" +image = "/usr/bin/./tabs" +kernel_params = "ro" +firmware = "/etc/hostname" + +cpu_features="pmu=off,vmx=off" +default_vcpus = 2 +default_maxvcpus = 64 + +machine_type = "q35" +confidential_guest = true +rootless = true +enable_annotations = ["shared_fs","path", "ctlpath","jailer_path","enable_iothreads","default_memory","memory_slots","enable_mem_prealloc","enable_hugepages","file_mem_backend","enable_virtio_mem","enable_swap","enable_guest_swap","default_vcpus","virtio_fs_extra_args","block_device_driver","vhost_user_store_path","kernel","guest_hook_path","block_device_cache_noflush","virtio_fs_daemon"] +machine_accelerators="noapic" +default_bridges = 2 +default_memory = 128 +memory_slots = 128 +memory_offset = 0x100000 +enable_virtio_mem = true +disable_block_device_use = false +shared_fs = "virtio-fs" +virtio_fs_daemon = "/usr/bin/uptime" +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-pci" +block_device_cache_set = true +block_device_cache_direct = true +block_device_cache_noflush = true +enable_mem_prealloc = true +enable_hugepages = true +enable_vhost_user_store = true +vhost_user_store_path = "/tmp" +valid_vhost_user_store_paths = ["/var/kata/vhost-user-store*", "/tmp/kata?","/var/tmp","./store_path"] +enable_iommu = true +enable_iommu_platform = true +file_mem_backend = "/dev/shm" +valid_file_mem_backends = ["/dev/shm","/dev/snd","./test_file_backend_mem_root"] +enable_swap = true +pflashes = ["/proc/mounts"] +enable_debug = true +msize_9p = 16384 +disable_image_nvdimm = true +hotplug_vfio_on_root_bus = true +pcie_root_port = 2 +disable_vhost_net = true +entropy_source= "/dev/urandom" +valid_entropy_sources = ["/dev/urandom", "/dev/random"] +guest_hook_path = "/usr/share" +rx_rate_limiter_max_rate = 10000 +tx_rate_limiter_max_rate = 10000 +guest_memory_dump_path="/var/crash/kata" +guest_memory_dump_paging = true +enable_guest_swap = true + +[agent.agent0] +enable_tracing = true +debug_console_enabled = true +debug = true +dial_timeout = 1 +kernel_modules = ["e1000e InterruptThrottleRate=3000,3000,3000 EEE=1","i915_enabled_ppgtt=0"] +container_pipe_size = 2 +[runtime] +enable_debug = true +internetworking_model="macvtap" +disable_guest_seccomp=false +enable_tracing = true +jaeger_endpoint = "localhost:1234" +jaeger_user = "user" +jaeger_password = "pw" +disable_new_netns = true +sandbox_cgroup_only=true +sandbox_bind_mounts=["/proc/self"] +vfio_mode="vfio" +experimental=["a", "b"] +enable_pprof = true +hypervisor_name = "qemu" +agent_name = "agent0" + + diff --git a/src/runtime-rs/tests/texture/kata-containers-configuration.toml b/src/runtime-rs/tests/texture/kata-containers-configuration.toml deleted file mode 100644 index 9116080ddf..0000000000 --- a/src/runtime-rs/tests/texture/kata-containers-configuration.toml +++ /dev/null @@ -1,11 +0,0 @@ -[runtime] -enable_debug = true - -[hypervisor] - -[hypervisor.dragonball] -default_vcpus = 2 - -[hypervisor.qemu] -default_vcpus = 4 - diff --git a/src/runtime-rs/tests/utils/Cargo.toml b/src/runtime-rs/tests/utils/Cargo.toml index c4fc094719..d35892da5d 100644 --- a/src/runtime-rs/tests/utils/Cargo.toml +++ b/src/runtime-rs/tests/utils/Cargo.toml @@ -8,4 +8,6 @@ license = "Apache-2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "^1.0" rand = "0.8.4" +kata-types = { path = "../../../libs/kata-types" } diff --git a/src/runtime-rs/tests/utils/src/lib.rs b/src/runtime-rs/tests/utils/src/lib.rs index b3a4b35174..9349682341 100644 --- a/src/runtime-rs/tests/utils/src/lib.rs +++ b/src/runtime-rs/tests/utils/src/lib.rs @@ -6,17 +6,20 @@ // This crate is used to share code among tests -use std::path::PathBuf; +use anyhow::{anyhow, Result}; +use kata_types::config::{QemuConfig, TomlConfig}; +use std::{fs, path::PathBuf}; use rand::{ distributions::Alphanumeric, {thread_rng, Rng}, }; -pub fn get_kata_config_file() -> PathBuf { +fn get_kata_config_file(hypervisor_name: String) -> PathBuf { let target = format!( - "{}/../texture/kata-containers-configuration.toml", - env!("CARGO_MANIFEST_DIR") + "{}/../texture/configuration-{}.toml", + env!("CARGO_MANIFEST_DIR"), + hypervisor_name ); std::fs::canonicalize(target).unwrap() } @@ -33,3 +36,19 @@ pub fn gen_id(len: usize) -> String { .map(char::from) .collect() } + +pub fn load_test_config(hypervisor_name: String) -> Result { + match hypervisor_name.as_str() { + "qemu" => { + let qemu = QemuConfig::new(); + qemu.register(); + } + // TODO add other hypervisor test config + _ => { + return Err(anyhow!("invalid hypervisor {}", hypervisor_name)); + } + } + + let content = fs::read_to_string(get_kata_config_file(hypervisor_name))?; + Ok(TomlConfig::load(&content)?) +}