Skip to content

Commit

Permalink
Added tests for removing files
Browse files Browse the repository at this point in the history
  • Loading branch information
jounathaen committed Feb 5, 2025
1 parent 9448b19 commit 5d61fb9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
46 changes: 44 additions & 2 deletions tests/fs-test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mod common;

use std::{fs::read_to_string, path::PathBuf};
use std::{
fs::{read_to_string, File},
path::PathBuf,
};

use byte_unit::{Byte, Unit};
use common::{build_hermit_bin, check_result, remove_file_if_exists};
use common::{build_hermit_bin, check_result, remove_file_if_exists, run_simple_vm};
use uhyvelib::{
params::{Output, Params},
vm::UhyveVm,
Expand Down Expand Up @@ -67,3 +70,42 @@ fn uhyvefilemap_test() {
check_result(&res);
verify_file_equals(&output_path, "Hello, world!");
}

#[test]
fn remove_file_test() {
env_logger::try_init().ok();
let output_path = PathBuf::from("/tmp/file_to_remove.txt");
remove_file_if_exists(&output_path);
File::create(&output_path).unwrap();

let bin_path = build_hermit_bin("remove_file");

let params = Params {
cpu_count: 2.try_into().unwrap(),
memory_size: Byte::from_u64_with_unit(64, Unit::MiB)
.unwrap()
.try_into()
.unwrap(),
file_mapping: vec!["/tmp/file_to_remove.txt:/root/file_to_remove.txt".to_string()],
output: Output::Buffer,
stats: true,
..Default::default()
};

assert!(output_path.exists());

let vm = UhyveVm::new(bin_path.clone(), params.clone()).unwrap();
let res = vm.run(None);
check_result(&res);
assert!(!output_path.exists());
}

#[test]
fn remove_non_present_file_test() {
// kernel tries to open a non-present file, so uhyve will reject the hypercall and the kernel
// will panic.
env_logger::try_init().ok();
let bin_path = build_hermit_bin("remove_file");
let res = run_simple_vm(bin_path);
assert_ne!(res.code, 0);
}
10 changes: 10 additions & 0 deletions tests/test-kernels/src/bin/remove_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::fs;

#[cfg(target_os = "hermit")]
use hermit as _;

fn main() {
println!("Hello from remove_file!");

fs::remove_file("/root/file_to_remove.txt").unwrap();
}

0 comments on commit 5d61fb9

Please sign in to comment.