Skip to content

Commit

Permalink
test(fs-test): refactor new_file_test
Browse files Browse the repository at this point in the history
new_file_test now runs a kernel called open_close_file, which attempts
to open foo.txt, write to it, then closes the file, and attempts to
open it again so as to read from it. This is more of a workaround
because of the fact that we cannot read the temporary file easily,
but it should work just as well and expand the test's coverage to
"reading the contents of a file that is a temporary file on the
host filesystem".
  • Loading branch information
n0toose committed Nov 25, 2024
1 parent 0cf67ba commit c80cbe2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 1 addition & 4 deletions tests/fs-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ fn new_file_test() {
..Default::default()
};

let bin_path = build_hermit_bin("create_file");
let bin_path = build_hermit_bin("open_close_file");
let vm = UhyveVm::new(bin_path, params).unwrap();
let mut output_path = vm.get_tempdir().path().to_path_buf();
output_path.push("foo.txt");
let res = vm.run(None);
assert_eq!(res.code, 0);
verify_file_equals(&output_path, "Hello, world!");
}

#[test]
Expand Down
5 changes: 4 additions & 1 deletion tests/test-kernels/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "uhyve-test-kernels"
version = "0.1.0"
authors = ["Jonathan Klimt <[email protected]>"]
authors = [
"Jonathan Klimt <[email protected]>",
'Panagiotis "Ivory" Vasilopoulos <[email protected]>',
]
edition = "2021"
publish = false

Expand Down
20 changes: 20 additions & 0 deletions tests/test-kernels/src/bin/open_close_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::{
fs::{read_to_string, File},
io::prelude::*,
};

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

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

{
let mut file = File::create("/root/foo.txt").unwrap();
file.write_all(b"Hello, world!").unwrap();
}
{
let file_content = read_to_string("/root/foo.txt").unwrap();
assert_eq!(file_content, "Hello, world!".to_string());
}
}

0 comments on commit c80cbe2

Please sign in to comment.