Skip to content

Commit 5d61fb9

Browse files
committed
Added tests for removing files
1 parent 9448b19 commit 5d61fb9

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

tests/fs-test.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
mod common;
22

3-
use std::{fs::read_to_string, path::PathBuf};
3+
use std::{
4+
fs::{read_to_string, File},
5+
path::PathBuf,
6+
};
47

58
use byte_unit::{Byte, Unit};
6-
use common::{build_hermit_bin, check_result, remove_file_if_exists};
9+
use common::{build_hermit_bin, check_result, remove_file_if_exists, run_simple_vm};
710
use uhyvelib::{
811
params::{Output, Params},
912
vm::UhyveVm,
@@ -67,3 +70,42 @@ fn uhyvefilemap_test() {
6770
check_result(&res);
6871
verify_file_equals(&output_path, "Hello, world!");
6972
}
73+
74+
#[test]
75+
fn remove_file_test() {
76+
env_logger::try_init().ok();
77+
let output_path = PathBuf::from("/tmp/file_to_remove.txt");
78+
remove_file_if_exists(&output_path);
79+
File::create(&output_path).unwrap();
80+
81+
let bin_path = build_hermit_bin("remove_file");
82+
83+
let params = Params {
84+
cpu_count: 2.try_into().unwrap(),
85+
memory_size: Byte::from_u64_with_unit(64, Unit::MiB)
86+
.unwrap()
87+
.try_into()
88+
.unwrap(),
89+
file_mapping: vec!["/tmp/file_to_remove.txt:/root/file_to_remove.txt".to_string()],
90+
output: Output::Buffer,
91+
stats: true,
92+
..Default::default()
93+
};
94+
95+
assert!(output_path.exists());
96+
97+
let vm = UhyveVm::new(bin_path.clone(), params.clone()).unwrap();
98+
let res = vm.run(None);
99+
check_result(&res);
100+
assert!(!output_path.exists());
101+
}
102+
103+
#[test]
104+
fn remove_non_present_file_test() {
105+
// kernel tries to open a non-present file, so uhyve will reject the hypercall and the kernel
106+
// will panic.
107+
env_logger::try_init().ok();
108+
let bin_path = build_hermit_bin("remove_file");
109+
let res = run_simple_vm(bin_path);
110+
assert_ne!(res.code, 0);
111+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::fs;
2+
3+
#[cfg(target_os = "hermit")]
4+
use hermit as _;
5+
6+
fn main() {
7+
println!("Hello from remove_file!");
8+
9+
fs::remove_file("/root/file_to_remove.txt").unwrap();
10+
}

0 commit comments

Comments
 (0)