diff --git a/tests/common.rs b/tests/common.rs index a8c4cf64..11c21b8c 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,5 +1,6 @@ use std::{ env, + fs::{read, remove_file}, path::{Path, PathBuf}, process::Command, }; @@ -36,9 +37,12 @@ pub fn build_hermit_bin(kernel: impl AsRef) -> PathBuf { .collect() } -/// Small wrapper around [`Uhyve::run`] with default parameters for a small and -/// simple Uhyve vm -pub fn run_simple_vm(kernel_path: PathBuf) { +/// Wrapper around [`UhyveVm::new`] with default parameters for a small and +/// simple UhyveVM. +/// +/// `kernel_path` - Location of the kernel +#[allow(dead_code)] +pub fn run_simple_vm(kernel_path: PathBuf) -> i32 { let params = Params { verbose: true, cpu_count: 2.try_into().unwrap(), @@ -48,6 +52,49 @@ pub fn run_simple_vm(kernel_path: PathBuf) { .unwrap(), ..Default::default() }; - let code = UhyveVm::new(kernel_path, params).unwrap().run(None); - assert_eq!(0, code); + + UhyveVm::new(kernel_path, params).unwrap().run(None) +} + +/// Small wrapper around [`UhyveVm::new`] that also accepts a filemap. +/// +/// `kernel_path` - Location of the kernel +/// `file_map` - Vec containing Strings of the format `host.txt:guest.txt`. +#[allow(dead_code)] +pub fn run_vm_with_file_map(kernel_path: PathBuf, file_map: Vec) -> i32 { + let params = Params { + verbose: true, + cpu_count: 2.try_into().unwrap(), + memory_size: Byte::from_u64_with_unit(32, Unit::MiB) + .unwrap() + .try_into() + .unwrap(), + file_map: Some(file_map), + ..Default::default() + }; + + UhyveVm::new(kernel_path, params).unwrap().run(None) +} + +/// Creates a file on the host OS, while attempting to remove the the file if +/// it already exists. +#[allow(dead_code)] +pub fn remove_file_if_exists(path: &PathBuf) { + if path.exists() { + println!("Removing existing file {}", path.display()); + remove_file(path).unwrap_or_else(|_| panic!("Can't remove {}", path.display())); + } +} + +/// Verifies that the file was successfully created on the host OS and contains +/// the right content. +#[allow(dead_code)] +pub fn verify_file_contents(testfile: &Path, exists: bool) { + if exists { + assert!(testfile.exists()); + let file_content = read(testfile.to_str().unwrap()).unwrap(); + assert_eq!(file_content, "Hello, world!".as_bytes()); + } else { + assert!(!testfile.exists()); + } } diff --git a/tests/fs-test.rs b/tests/fs-test.rs index c1c29b25..f7261615 100644 --- a/tests/fs-test.rs +++ b/tests/fs-test.rs @@ -1,24 +1,16 @@ mod common; -use std::{ - fs::{read, remove_file}, - path::PathBuf, -}; +use std::{fs::remove_file, path::PathBuf}; -use common::{build_hermit_bin, run_simple_vm}; +use common::{build_hermit_bin, remove_file_if_exists, run_simple_vm, verify_file_contents}; #[test] fn new_file_test() { let testfile = PathBuf::from("foo.txt"); - if testfile.exists() { - println!("Removing existing file {}", testfile.display()); - remove_file(&testfile).unwrap_or_else(|_| panic!("Can't remove {}", testfile.display())); - } + remove_file_if_exists(&testfile); let bin_path = build_hermit_bin("create_file"); - run_simple_vm(bin_path); - assert!(testfile.exists()); - let file_content = read("foo.txt").unwrap(); - assert_eq!(file_content, "Hello, world!".as_bytes()); + assert_eq!(0, run_simple_vm(bin_path)); + verify_file_contents(&testfile, true); remove_file(&testfile).unwrap_or_else(|_| panic!("Can't remove {}", testfile.display())); } diff --git a/tests/serial.rs b/tests/serial.rs index b764fdea..d1bd6fcd 100644 --- a/tests/serial.rs +++ b/tests/serial.rs @@ -6,5 +6,5 @@ use common::{build_hermit_bin, run_simple_vm}; fn serial_buffer_test() { // TODO: Check the output once https://github.com/hermit-os/uhyve/issues/528 is resolved let bin_path = build_hermit_bin("serial"); - run_simple_vm(bin_path); + assert_eq!(0, run_simple_vm(bin_path)); } diff --git a/tests/uhyvefilemap.rs b/tests/uhyvefilemap.rs new file mode 100644 index 00000000..13ba5045 --- /dev/null +++ b/tests/uhyvefilemap.rs @@ -0,0 +1,22 @@ +mod common; + +use std::{fs::remove_file, path::PathBuf}; + +use common::{build_hermit_bin, remove_file_if_exists, run_vm_with_file_map, verify_file_contents}; + +#[test] +fn uhyvefilemap_test() { + let testfile = PathBuf::from("foo.txt"); + remove_file_if_exists(&testfile); + let bin_path = build_hermit_bin("create_file"); + + // The file should not exist on the host OS. + let mut code = run_vm_with_file_map(bin_path.clone(), vec!["foo.txt:wrong.txt".to_string()]); + assert_eq!(-1, code); + verify_file_contents(&testfile, false); + + code = run_vm_with_file_map(bin_path, vec!["foo.txt:foo.txt".to_string()]); + assert_eq!(0, code); + verify_file_contents(&testfile, true); + remove_file(&testfile).unwrap_or_else(|_| panic!("Can't remove {}", testfile.display())); +}