From e8ba7d708672add009d131ad916432e6f9e855c1 Mon Sep 17 00:00:00 2001 From: Ludvig Liljenberg Date: Thu, 12 Dec 2024 09:57:47 -0800 Subject: [PATCH] Removes requirement of specific environment variables to be set during tests. Signed-off-by: Ludvig Liljenberg --- .devcontainer/devcontainer.json | 1 - .../src/hypervisor/hyperv_linux.rs | 64 +------------------ src/hyperlight_host/src/hypervisor/kvm.rs | 62 ++---------------- src/hyperlight_host/src/sandbox/mod.rs | 31 ++------- .../rust_guests/callbackguest/Cargo.lock | 8 +-- src/tests/rust_guests/simpleguest/Cargo.lock | 8 +-- 6 files changed, 24 insertions(+), 150 deletions(-) diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index ebfed8c5..6db35bc4 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 b8a18303..e554f348 100644 --- a/src/hyperlight_host/src/hypervisor/hyperv_linux.rs +++ b/src/hyperlight_host/src/hypervisor/hyperv_linux.rs @@ -374,65 +374,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] = [ @@ -463,15 +409,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 991e1e24..4c72bbf4 100644 --- a/src/hyperlight_host/src/hypervisor/kvm.rs +++ b/src/hyperlight_host/src/hypervisor/kvm.rs @@ -331,71 +331,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 baf1360c..bdd48bff 100644 --- a/src/hyperlight_host/src/sandbox/mod.rs +++ b/src/hyperlight_host/src/sandbox/mod.rs @@ -163,12 +163,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; @@ -177,29 +171,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()); } } } diff --git a/src/tests/rust_guests/callbackguest/Cargo.lock b/src/tests/rust_guests/callbackguest/Cargo.lock index ff2522cc..b1243f25 100644 --- a/src/tests/rust_guests/callbackguest/Cargo.lock +++ b/src/tests/rust_guests/callbackguest/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "anyhow" -version = "1.0.91" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "autocfg" @@ -39,9 +39,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.31" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" +checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" dependencies = [ "shlex", ] diff --git a/src/tests/rust_guests/simpleguest/Cargo.lock b/src/tests/rust_guests/simpleguest/Cargo.lock index f8df7b2a..a554764d 100644 --- a/src/tests/rust_guests/simpleguest/Cargo.lock +++ b/src/tests/rust_guests/simpleguest/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "anyhow" -version = "1.0.91" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" [[package]] name = "autocfg" @@ -31,9 +31,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.31" +version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" +checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d" dependencies = [ "shlex", ]