diff --git a/src/runtime-rs/crates/hypervisor/src/ch/inner.rs b/src/runtime-rs/crates/hypervisor/src/ch/inner.rs index 734f36795b..869a1200dc 100644 --- a/src/runtime-rs/crates/hypervisor/src/ch/inner.rs +++ b/src/runtime-rs/crates/hypervisor/src/ch/inner.rs @@ -188,3 +188,43 @@ impl Persist for CloudHypervisorInner { Ok(ch) } } + +#[cfg(test)] +mod tests { + use super::*; + use kata_sys_util::protection::TDXDetails; + + #[actix_rt::test] + async fn test_save_clh() { + let mut clh = CloudHypervisorInner::new(); + clh.id = String::from("123456"); + clh.netns = Some(String::from("/var/run/netns/testnet")); + clh.vm_path = String::from("/opt/kata/bin/cloud-hypervisor"); + clh.run_dir = String::from("/var/run/kata-containers/") + &clh.id; + + let details = TDXDetails { + major_version: 1, + minor_version: 0, + }; + + clh.guest_protection_to_use = GuestProtection::Tdx(details); + + let state = clh.save().await.unwrap(); + assert_eq!(state.id, clh.id); + assert_eq!(state.netns, clh.netns); + assert_eq!(state.vm_path, clh.vm_path); + assert_eq!(state.run_dir, clh.run_dir); + assert_eq!(state.guest_protection_to_use, clh.guest_protection_to_use); + assert_eq!(state.jailed, false); + assert_eq!(state.hypervisor_type, HYPERVISOR_NAME_CH.to_string()); + + let clh = CloudHypervisorInner::restore((), state.clone()) + .await + .unwrap(); + assert_eq!(clh.id, state.id); + assert_eq!(clh.netns, state.netns); + assert_eq!(clh.vm_path, state.vm_path); + assert_eq!(clh.run_dir, state.run_dir); + assert_eq!(clh.guest_protection_to_use, state.guest_protection_to_use); + } +}