Skip to content

Commit 288d6f9

Browse files
authored
Removes requirement of specific environment variables to be set during tests. (#108)
Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent a38be60 commit 288d6f9

File tree

4 files changed

+16
-142
lines changed

4 files changed

+16
-142
lines changed

.devcontainer/devcontainer.json

-1
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

+3-61
Original file line numberDiff line numberDiff line change
@@ -371,65 +371,11 @@ impl Drop for HypervLinuxDriver {
371371
}
372372
}
373373

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

434380
#[rustfmt::skip]
435381
const CODE: [u8; 12] = [
@@ -460,15 +406,11 @@ mod tests {
460406
Ok(Box::new(shared_mem))
461407
}
462408

463-
#[test]
464-
fn is_hypervisor_present() {
465-
let result = super::is_hypervisor_present();
466-
assert_eq!(result, TEST_CONFIG.hyperv_should_be_present);
467-
}
468-
469409
#[test]
470410
fn create_driver() {
471-
should_run_hyperv_linux_test!();
411+
if !super::is_hypervisor_present() {
412+
return;
413+
}
472414
const MEM_SIZE: usize = 0x3000;
473415
let gm = shared_mem_with_code(CODE.as_slice(), MEM_SIZE, 0).unwrap();
474416
let rsp_ptr = GuestPtr::try_from(0).unwrap();

src/hyperlight_host/src/hypervisor/kvm.rs

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

388335
use crate::hypervisor::handlers::{MemAccessHandler, OutBHandler};
389336
use crate::hypervisor::tests::test_initialise;
390-
use crate::{should_run_kvm_linux_test, Result};
337+
use crate::Result;
391338

392339
#[test]
393340
fn test_init() {
394-
should_run_kvm_linux_test!();
395-
let outb_handler = {
341+
if !super::is_hypervisor_present() {
342+
return;
343+
}
344+
345+
let outb_handler: Arc<Mutex<OutBHandler>> = {
396346
let func: Box<dyn FnMut(u16, u64) -> Result<()> + Send> =
397347
Box::new(|_, _| -> Result<()> { Ok(()) });
398348
Arc::new(Mutex::new(OutBHandler::from(func)))

src/hyperlight_host/src/sandbox/mod.rs

+7-24
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,6 @@ mod tests {
158158
use crossbeam_queue::ArrayQueue;
159159
use hyperlight_testing::simple_guest_as_string;
160160

161-
#[cfg(target_os = "linux")]
162-
use super::is_hypervisor_present;
163-
#[cfg(mshv)]
164-
use crate::hypervisor::hyperv_linux::test_cfg::TEST_CONFIG as HYPERV_TEST_CONFIG;
165-
#[cfg(kvm)]
166-
use crate::hypervisor::kvm::test_cfg::TEST_CONFIG as KVM_TEST_CONFIG;
167161
use crate::sandbox::uninitialized::GuestBinary;
168162
use crate::sandbox_state::sandbox::EvolvableSandbox;
169163
use crate::sandbox_state::transition::Noop;
@@ -172,29 +166,18 @@ mod tests {
172166
#[test]
173167
// TODO: add support for testing on WHP
174168
#[cfg(target_os = "linux")]
175-
fn test_is_hypervisor_present() {
176-
// TODO: Handle requiring a stable API
169+
fn is_hypervisor_present() {
170+
use std::path::Path;
171+
177172
cfg_if::cfg_if! {
178173
if #[cfg(all(kvm, mshv))] {
179-
if KVM_TEST_CONFIG.kvm_should_be_present || HYPERV_TEST_CONFIG.hyperv_should_be_present {
180-
assert!(is_hypervisor_present());
181-
} else {
182-
assert!(!is_hypervisor_present());
183-
}
174+
assert_eq!(Path::new("/dev/kvm").exists() || Path::new("/dev/mshv").exists(), super::is_hypervisor_present());
184175
} else if #[cfg(kvm)] {
185-
if KVM_TEST_CONFIG.kvm_should_be_present {
186-
assert!(is_hypervisor_present());
187-
} else {
188-
assert!(!is_hypervisor_present());
189-
}
176+
assert_eq!(Path::new("/dev/kvm").exists(), super::is_hypervisor_present());
190177
} else if #[cfg(mshv)] {
191-
if HYPERV_TEST_CONFIG.hyperv_should_be_present {
192-
assert!(is_hypervisor_present());
193-
} else {
194-
assert!(!is_hypervisor_present());
195-
}
178+
assert_eq!(Path::new("/dev/mshv").exists(), super::is_hypervisor_present());
196179
} else {
197-
assert!(!is_hypervisor_present());
180+
assert!(!super::is_hypervisor_present());
198181
}
199182
}
200183
}

0 commit comments

Comments
 (0)