Skip to content

Commit f35af55

Browse files
committed
Removes requirement of specific environment variables to be set during tests.
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 03a8a54 commit f35af55

File tree

6 files changed

+24
-149
lines changed

6 files changed

+24
-149
lines changed

.devcontainer/devcontainer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// Environment for the container also used by the `postCreateCommand`
1010
"containerEnv": {
1111
"DEVICE": "/dev/kvm",
12-
"KVM_SHOULD_BE_PRESENT": "true",
1312
"REMOTE_USER": "vscode",
1413
"REMOTE_GROUP": "vscode"
1514
},

src/hyperlight_host/src/hypervisor/hyperv_linux.rs

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -374,65 +374,11 @@ impl Drop for HypervLinuxDriver {
374374
}
375375
}
376376

377-
#[cfg(test)]
378-
pub(crate) mod test_cfg {
379-
use once_cell::sync::Lazy;
380-
use serde::Deserialize;
381-
382-
pub(crate) static TEST_CONFIG: Lazy<TestConfig> =
383-
Lazy::new(|| match envy::from_env::<TestConfig>() {
384-
Ok(config) => config,
385-
Err(err) => panic!("error parsing config from env: {}", err),
386-
});
387-
pub(crate) static SHOULD_RUN_TEST: Lazy<bool> = Lazy::new(is_hyperv_present);
388-
389-
fn is_hyperv_present() -> bool {
390-
println!(
391-
"HYPERV_SHOULD_BE_PRESENT is {}",
392-
TEST_CONFIG.hyperv_should_be_present
393-
);
394-
let is_present = super::is_hypervisor_present();
395-
if (is_present && !TEST_CONFIG.hyperv_should_be_present)
396-
|| (!is_present && TEST_CONFIG.hyperv_should_be_present)
397-
{
398-
panic!(
399-
"WARNING Hyper-V is present returned {}, should be present is: {}",
400-
is_present, TEST_CONFIG.hyperv_should_be_present
401-
);
402-
}
403-
is_present
404-
}
405-
406-
fn hyperv_should_be_present_default() -> bool {
407-
false
408-
}
409-
410-
#[derive(Deserialize, Debug)]
411-
pub(crate) struct TestConfig {
412-
#[serde(default = "hyperv_should_be_present_default")]
413-
// Set env var HYPERV_SHOULD_BE_PRESENT to require hyperv to be present for the tests.
414-
pub(crate) hyperv_should_be_present: bool,
415-
}
416-
417-
#[macro_export]
418-
macro_rules! should_run_hyperv_linux_test {
419-
() => {{
420-
if !(*SHOULD_RUN_TEST) {
421-
println! {"Not Running Test SHOULD_RUN_TEST is false"}
422-
return;
423-
}
424-
println! {"Running Test SHOULD_RUN_TEST is true"}
425-
}};
426-
}
427-
}
428-
429377
#[cfg(test)]
430378
mod tests {
431-
use super::test_cfg::{SHOULD_RUN_TEST, TEST_CONFIG};
432379
use super::*;
433380
use crate::mem::memory_region::MemoryRegionVecBuilder;
434381
use crate::mem::shared_mem::{ExclusiveSharedMemory, SharedMemory};
435-
use crate::should_run_hyperv_linux_test;
436382

437383
#[rustfmt::skip]
438384
const CODE: [u8; 12] = [
@@ -463,15 +409,11 @@ mod tests {
463409
Ok(Box::new(shared_mem))
464410
}
465411

466-
#[test]
467-
fn is_hypervisor_present() {
468-
let result = super::is_hypervisor_present();
469-
assert_eq!(result, TEST_CONFIG.hyperv_should_be_present);
470-
}
471-
472412
#[test]
473413
fn create_driver() {
474-
should_run_hyperv_linux_test!();
414+
if !super::is_hypervisor_present() {
415+
return;
416+
}
475417
const MEM_SIZE: usize = 0x3000;
476418
let gm = shared_mem_with_code(CODE.as_slice(), MEM_SIZE, 0).unwrap();
477419
let rsp_ptr = GuestPtr::try_from(0).unwrap();

src/hyperlight_host/src/hypervisor/kvm.rs

Lines changed: 6 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -331,71 +331,21 @@ impl Hypervisor for KVMDriver {
331331
&self.mem_regions
332332
}
333333
}
334-
335-
#[cfg(test)]
336-
pub(crate) mod test_cfg {
337-
use once_cell::sync::Lazy;
338-
use serde::Deserialize;
339-
340-
pub(crate) static TEST_CONFIG: Lazy<TestConfig> =
341-
Lazy::new(|| match envy::from_env::<TestConfig>() {
342-
Ok(config) => config,
343-
Err(err) => panic!("error parsing config from env: {}", err),
344-
});
345-
pub(crate) static SHOULD_RUN_TEST: Lazy<bool> = Lazy::new(is_kvm_present);
346-
347-
fn is_kvm_present() -> bool {
348-
println!(
349-
"KVM_SHOULD_BE_PRESENT is {}",
350-
TEST_CONFIG.kvm_should_be_present
351-
);
352-
let is_present = super::is_hypervisor_present();
353-
if (is_present && !TEST_CONFIG.kvm_should_be_present)
354-
|| (!is_present && TEST_CONFIG.kvm_should_be_present)
355-
{
356-
println!(
357-
"WARNING: KVM is-present returned {}, should be present is: {}",
358-
is_present, TEST_CONFIG.kvm_should_be_present
359-
);
360-
}
361-
is_present
362-
}
363-
364-
fn kvm_should_be_present_default() -> bool {
365-
false
366-
}
367-
368-
#[derive(Deserialize, Debug)]
369-
pub(crate) struct TestConfig {
370-
#[serde(default = "kvm_should_be_present_default")]
371-
// Set env var KVM_SHOULD_BE_PRESENT to require kvm to be present for the tests.
372-
pub(crate) kvm_should_be_present: bool,
373-
}
374-
375-
#[macro_export]
376-
macro_rules! should_run_kvm_linux_test {
377-
() => {{
378-
if !(*$crate::hypervisor::kvm::test_cfg::SHOULD_RUN_TEST) {
379-
println! {"Not Running KVM Test - SHOULD_RUN_TEST is false"}
380-
return;
381-
}
382-
println! {"Running Test - SHOULD_RUN_TEST is true"}
383-
}};
384-
}
385-
}
386-
387334
#[cfg(test)]
388335
mod tests {
389336
use std::sync::{Arc, Mutex};
390337

391338
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
392339
use crate::hypervisor::tests::test_initialise;
393-
use crate::{should_run_kvm_linux_test, Result};
340+
use crate::Result;
394341

395342
#[test]
396343
fn test_init() {
397-
should_run_kvm_linux_test!();
398-
let outb_handler = {
344+
if !super::is_hypervisor_present() {
345+
return;
346+
}
347+
348+
let outb_handler: Arc<Mutex<OutBHandler>> = {
399349
let func: Box<dyn FnMut(u16, u64) -> Result<()> + Send> =
400350
Box::new(|_, _| -> Result<()> { Ok(()) });
401351
Arc::new(Mutex::new(OutBHandler::from(func)))

src/hyperlight_host/src/sandbox/mod.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,6 @@ mod tests {
163163
use crossbeam_queue::ArrayQueue;
164164
use hyperlight_testing::simple_guest_as_string;
165165

166-
#[cfg(target_os = "linux")]
167-
use super::is_hypervisor_present;
168-
#[cfg(mshv)]
169-
use crate::hypervisor::hyperv_linux::test_cfg::TEST_CONFIG as HYPERV_TEST_CONFIG;
170-
#[cfg(kvm)]
171-
use crate::hypervisor::kvm::test_cfg::TEST_CONFIG as KVM_TEST_CONFIG;
172166
use crate::sandbox::uninitialized::GuestBinary;
173167
use crate::sandbox_state::sandbox::EvolvableSandbox;
174168
use crate::sandbox_state::transition::Noop;
@@ -177,29 +171,19 @@ mod tests {
177171
#[test]
178172
// TODO: add support for testing on WHP
179173
#[cfg(target_os = "linux")]
180-
fn test_is_hypervisor_present() {
174+
fn is_hypervisor_present() {
181175
// TODO: Handle requiring a stable API
176+
use std::path::Path;
177+
182178
cfg_if::cfg_if! {
183179
if #[cfg(all(kvm, mshv))] {
184-
if KVM_TEST_CONFIG.kvm_should_be_present || HYPERV_TEST_CONFIG.hyperv_should_be_present {
185-
assert!(is_hypervisor_present());
186-
} else {
187-
assert!(!is_hypervisor_present());
188-
}
180+
assert_eq!(Path::new("/dev/kvm").exists() || Path::new("/dev/mshv").exists(), super::is_hypervisor_present());
189181
} else if #[cfg(kvm)] {
190-
if KVM_TEST_CONFIG.kvm_should_be_present {
191-
assert!(is_hypervisor_present());
192-
} else {
193-
assert!(!is_hypervisor_present());
194-
}
182+
assert_eq!(Path::new("/dev/kvm").exists(), super::is_hypervisor_present());
195183
} else if #[cfg(mshv)] {
196-
if HYPERV_TEST_CONFIG.hyperv_should_be_present {
197-
assert!(is_hypervisor_present());
198-
} else {
199-
assert!(!is_hypervisor_present());
200-
}
184+
assert_eq!(Path::new("/dev/mshv").exists(), super::is_hypervisor_present());
201185
} else {
202-
assert!(!is_hypervisor_present());
186+
assert!(!super::is_hypervisor_present());
203187
}
204188
}
205189
}

src/tests/rust_guests/callbackguest/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tests/rust_guests/simpleguest/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)