Skip to content

Commit

Permalink
Removes requirement of specific environment variables to be set durin…
Browse files Browse the repository at this point in the history
…g tests.

Signed-off-by: Ludvig Liljenberg <[email protected]>
  • Loading branch information
ludfjig committed Dec 12, 2024
1 parent 03a8a54 commit f35af55
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 149 deletions.
1 change: 0 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
64 changes: 3 additions & 61 deletions src/hyperlight_host/src/hypervisor/hyperv_linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestConfig> =
Lazy::new(|| match envy::from_env::<TestConfig>() {
Ok(config) => config,
Err(err) => panic!("error parsing config from env: {}", err),
});
pub(crate) static SHOULD_RUN_TEST: Lazy<bool> = 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] = [
Expand Down Expand Up @@ -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();
Expand Down
62 changes: 6 additions & 56 deletions src/hyperlight_host/src/hypervisor/kvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestConfig> =
Lazy::new(|| match envy::from_env::<TestConfig>() {
Ok(config) => config,
Err(err) => panic!("error parsing config from env: {}", err),
});
pub(crate) static SHOULD_RUN_TEST: Lazy<bool> = 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<Mutex<OutBHandler>> = {
let func: Box<dyn FnMut(u16, u64) -> Result<()> + Send> =
Box::new(|_, _| -> Result<()> { Ok(()) });
Arc::new(Mutex::new(OutBHandler::from(func)))
Expand Down
30 changes: 7 additions & 23 deletions src/hyperlight_host/src/sandbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -177,29 +171,19 @@ mod tests {
#[test]
// TODO: add support for testing on WHP
#[cfg(target_os = "linux")]
fn test_is_hypervisor_present() {
fn is_hypervisor_present() {
// TODO: Handle requiring a stable API
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());
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/tests/rust_guests/callbackguest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/tests/rust_guests/simpleguest/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f35af55

Please sign in to comment.