Skip to content

Commit

Permalink
test(isolation): introduce UhyveFileMap integration tests
Browse files Browse the repository at this point in the history
Introduces a test for UhyveFileMap. Some refactoring was done so
as to keep the two tests, fs-test and uhyvefilemap, in separate
files. Some filesystem-related functions were moved into common.rs,
because:
- we anticipate that they will be necessary for further
  filesystem-related tests
- putting two test functions in a single test (e.g. fs-test)
  causes the second test to hang for some mysterious reason
- more descriptive errors
  • Loading branch information
n0toose committed Nov 12, 2024
1 parent f50841f commit ca0e3c4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
57 changes: 52 additions & 5 deletions tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
env,
fs::{read, remove_file},
path::{Path, PathBuf},
process::Command,
};
Expand Down Expand Up @@ -36,9 +37,12 @@ pub fn build_hermit_bin(kernel: impl AsRef<Path>) -> 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(),
Expand All @@ -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<String> 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<String>) -> 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());
}
}
18 changes: 5 additions & 13 deletions tests/fs-test.rs
Original file line number Diff line number Diff line change
@@ -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()));
}
2 changes: 1 addition & 1 deletion tests/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
22 changes: 22 additions & 0 deletions tests/uhyvefilemap.rs
Original file line number Diff line number Diff line change
@@ -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()));
}

0 comments on commit ca0e3c4

Please sign in to comment.