From c80cbe28af6742a6d5f9291f3bb7501627ddc2d6 Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Mon, 25 Nov 2024 16:43:35 +0100 Subject: [PATCH] test(fs-test): refactor new_file_test 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". --- tests/fs-test.rs | 5 +---- tests/test-kernels/Cargo.toml | 5 ++++- tests/test-kernels/src/bin/open_close_file.rs | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 tests/test-kernels/src/bin/open_close_file.rs diff --git a/tests/fs-test.rs b/tests/fs-test.rs index e30b5e12..1d7b84f3 100644 --- a/tests/fs-test.rs +++ b/tests/fs-test.rs @@ -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] diff --git a/tests/test-kernels/Cargo.toml b/tests/test-kernels/Cargo.toml index d452a458..3ae07a55 100644 --- a/tests/test-kernels/Cargo.toml +++ b/tests/test-kernels/Cargo.toml @@ -1,7 +1,10 @@ [package] name = "uhyve-test-kernels" version = "0.1.0" -authors = ["Jonathan Klimt "] +authors = [ + "Jonathan Klimt ", + 'Panagiotis "Ivory" Vasilopoulos ', +] edition = "2021" publish = false diff --git a/tests/test-kernels/src/bin/open_close_file.rs b/tests/test-kernels/src/bin/open_close_file.rs new file mode 100644 index 00000000..4215bc78 --- /dev/null +++ b/tests/test-kernels/src/bin/open_close_file.rs @@ -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()); + } +}