diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ebfed8c51..6db35bc47 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -9,7 +9,6 @@ // Environment for the container also used by the `postCreateCommand` "containerEnv": { "DEVICE": "/dev/kvm", - "KVM_SHOULD_BE_PRESENT": "true", "REMOTE_USER": "vscode", "REMOTE_GROUP": "vscode" }, diff --git a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs index 224428eb2..db00a2c8c 100644 --- a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs +++ b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs @@ -371,65 +371,11 @@ impl Drop for HypervLinuxDriver { } } -#[cfg(test)] -pub(crate) mod test_cfg { - use once_cell::sync::Lazy; - use serde::Deserialize; - - pub(crate) static TEST_CONFIG: Lazy = - Lazy::new(|| match envy::from_env::() { - Ok(config) => config, - Err(err) => panic!("error parsing config from env: {}", err), - }); - pub(crate) static SHOULD_RUN_TEST: Lazy = Lazy::new(is_hyperv_present); - - fn is_hyperv_present() -> bool { - println!( - "HYPERV_SHOULD_BE_PRESENT is {}", - TEST_CONFIG.hyperv_should_be_present - ); - let is_present = super::is_hypervisor_present(); - if (is_present && !TEST_CONFIG.hyperv_should_be_present) - || (!is_present && TEST_CONFIG.hyperv_should_be_present) - { - panic!( - "WARNING Hyper-V is present returned {}, should be present is: {}", - is_present, TEST_CONFIG.hyperv_should_be_present - ); - } - is_present - } - - fn hyperv_should_be_present_default() -> bool { - false - } - - #[derive(Deserialize, Debug)] - pub(crate) struct TestConfig { - #[serde(default = "hyperv_should_be_present_default")] - // Set env var HYPERV_SHOULD_BE_PRESENT to require hyperv to be present for the tests. - pub(crate) hyperv_should_be_present: bool, - } - - #[macro_export] - macro_rules! should_run_hyperv_linux_test { - () => {{ - if !(*SHOULD_RUN_TEST) { - println! {"Not Running Test SHOULD_RUN_TEST is false"} - return; - } - println! {"Running Test SHOULD_RUN_TEST is true"} - }}; - } -} - #[cfg(test)] mod tests { - use super::test_cfg::{SHOULD_RUN_TEST, TEST_CONFIG}; use super::*; use crate::mem::memory_region::MemoryRegionVecBuilder; use crate::mem::shared_mem::{ExclusiveSharedMemory, SharedMemory}; - use crate::should_run_hyperv_linux_test; #[rustfmt::skip] const CODE: [u8; 12] = [ @@ -460,15 +406,11 @@ mod tests { Ok(Box::new(shared_mem)) } - #[test] - fn is_hypervisor_present() { - let result = super::is_hypervisor_present(); - assert_eq!(result, TEST_CONFIG.hyperv_should_be_present); - } - #[test] fn create_driver() { - should_run_hyperv_linux_test!(); + if !super::is_hypervisor_present() { + return; + } const MEM_SIZE: usize = 0x3000; let gm = shared_mem_with_code(CODE.as_slice(), MEM_SIZE, 0).unwrap(); let rsp_ptr = GuestPtr::try_from(0).unwrap(); diff --git a/src/hyperlight_host/src/hypervisor/kvm.rs b/src/hyperlight_host/src/hypervisor/kvm.rs index 0d459827b..e21c6a35a 100644 --- a/src/hyperlight_host/src/hypervisor/kvm.rs +++ b/src/hyperlight_host/src/hypervisor/kvm.rs @@ -328,71 +328,21 @@ impl Hypervisor for KVMDriver { &self.mem_regions } } - -#[cfg(test)] -pub(crate) mod test_cfg { - use once_cell::sync::Lazy; - use serde::Deserialize; - - pub(crate) static TEST_CONFIG: Lazy = - Lazy::new(|| match envy::from_env::() { - Ok(config) => config, - Err(err) => panic!("error parsing config from env: {}", err), - }); - pub(crate) static SHOULD_RUN_TEST: Lazy = Lazy::new(is_kvm_present); - - fn is_kvm_present() -> bool { - println!( - "KVM_SHOULD_BE_PRESENT is {}", - TEST_CONFIG.kvm_should_be_present - ); - let is_present = super::is_hypervisor_present(); - if (is_present && !TEST_CONFIG.kvm_should_be_present) - || (!is_present && TEST_CONFIG.kvm_should_be_present) - { - println!( - "WARNING: KVM is-present returned {}, should be present is: {}", - is_present, TEST_CONFIG.kvm_should_be_present - ); - } - is_present - } - - fn kvm_should_be_present_default() -> bool { - false - } - - #[derive(Deserialize, Debug)] - pub(crate) struct TestConfig { - #[serde(default = "kvm_should_be_present_default")] - // Set env var KVM_SHOULD_BE_PRESENT to require kvm to be present for the tests. - pub(crate) kvm_should_be_present: bool, - } - - #[macro_export] - macro_rules! should_run_kvm_linux_test { - () => {{ - if !(*$crate::hypervisor::kvm::test_cfg::SHOULD_RUN_TEST) { - println! {"Not Running KVM Test - SHOULD_RUN_TEST is false"} - return; - } - println! {"Running Test - SHOULD_RUN_TEST is true"} - }}; - } -} - #[cfg(test)] mod tests { use std::sync::{Arc, Mutex}; use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler}; use crate::hypervisor::tests::test_initialise; - use crate::{should_run_kvm_linux_test, Result}; + use crate::Result; #[test] fn test_init() { - should_run_kvm_linux_test!(); - let outb_handler = { + if !super::is_hypervisor_present() { + return; + } + + let outb_handler: Arc> = { let func: Box Result<()> + Send> = Box::new(|_, _| -> Result<()> { Ok(()) }); Arc::new(Mutex::new(OutBHandler::from(func))) diff --git a/src/hyperlight_host/src/sandbox/mod.rs b/src/hyperlight_host/src/sandbox/mod.rs index a8e70e995..a88556878 100644 --- a/src/hyperlight_host/src/sandbox/mod.rs +++ b/src/hyperlight_host/src/sandbox/mod.rs @@ -158,12 +158,6 @@ mod tests { use crossbeam_queue::ArrayQueue; use hyperlight_testing::simple_guest_as_string; - #[cfg(target_os = "linux")] - use super::is_hypervisor_present; - #[cfg(mshv)] - use crate::hypervisor::hyperv_linux::test_cfg::TEST_CONFIG as HYPERV_TEST_CONFIG; - #[cfg(kvm)] - use crate::hypervisor::kvm::test_cfg::TEST_CONFIG as KVM_TEST_CONFIG; use crate::sandbox::uninitialized::GuestBinary; use crate::sandbox_state::sandbox::EvolvableSandbox; use crate::sandbox_state::transition::Noop; @@ -172,29 +166,18 @@ mod tests { #[test] // TODO: add support for testing on WHP #[cfg(target_os = "linux")] - fn test_is_hypervisor_present() { - // TODO: Handle requiring a stable API + fn is_hypervisor_present() { + use std::path::Path; + cfg_if::cfg_if! { if #[cfg(all(kvm, mshv))] { - if KVM_TEST_CONFIG.kvm_should_be_present || HYPERV_TEST_CONFIG.hyperv_should_be_present { - assert!(is_hypervisor_present()); - } else { - assert!(!is_hypervisor_present()); - } + assert_eq!(Path::new("/dev/kvm").exists() || Path::new("/dev/mshv").exists(), super::is_hypervisor_present()); } else if #[cfg(kvm)] { - if KVM_TEST_CONFIG.kvm_should_be_present { - assert!(is_hypervisor_present()); - } else { - assert!(!is_hypervisor_present()); - } + assert_eq!(Path::new("/dev/kvm").exists(), super::is_hypervisor_present()); } else if #[cfg(mshv)] { - if HYPERV_TEST_CONFIG.hyperv_should_be_present { - assert!(is_hypervisor_present()); - } else { - assert!(!is_hypervisor_present()); - } + assert_eq!(Path::new("/dev/mshv").exists(), super::is_hypervisor_present()); } else { - assert!(!is_hypervisor_present()); + assert!(!super::is_hypervisor_present()); } } }