From 140f72d46438a5bf7915a383f6a62d54e189292b Mon Sep 17 00:00:00 2001 From: "Panagiotis \"Ivory\" Vasilopoulos" Date: Thu, 21 Nov 2024 15:20:27 +0100 Subject: [PATCH] test: add fs-related helper functions This should help us keep up with the increasing complexity of the tests (including for features currently being developed). verify_file_equals is not particularly useful right now, but I intend on using it more intensively for the UhyveFileMap feature much more extensively. --- tests/common.rs | 9 +++++++++ tests/fs-test.rs | 24 +++++++++++++----------- tests/serial.rs | 15 ++++----------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/tests/common.rs b/tests/common.rs index edabb7fe..20638361 100644 --- a/tests/common.rs +++ b/tests/common.rs @@ -1,5 +1,6 @@ use std::{ env, + fs::remove_file, path::{Path, PathBuf}, process::Command, }; @@ -57,3 +58,11 @@ pub fn run_simple_vm(kernel_path: PathBuf) -> VmResult { }; UhyveVm::new(kernel_path, params).unwrap().run(None) } + +/// Removes a file if it already exists on the host OS. +pub fn remove_file_if_exists(path: &PathBuf) { + if path.exists() { + println!("Removing existing directory {}", path.display()); + remove_file(path).unwrap_or_else(|_| panic!("Can't remove {}", path.display())); + } +} diff --git a/tests/fs-test.rs b/tests/fs-test.rs index c1c29b25..15119918 100644 --- a/tests/fs-test.rs +++ b/tests/fs-test.rs @@ -1,24 +1,26 @@ mod common; use std::{ - fs::{read, remove_file}, + fs::{read_to_string, remove_file}, path::PathBuf, }; -use common::{build_hermit_bin, run_simple_vm}; +use common::{build_hermit_bin, remove_file_if_exists, run_simple_vm}; + +/// Verifies successful file creation on the host OS and its contents. +pub fn verify_file_equals(testfile: &PathBuf, contents: &str) { + assert!(testfile.exists()); + let file_content = read_to_string(testfile).unwrap(); + assert_eq!(file_content, contents.to_string()); +} #[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())); - } + let output_path = PathBuf::from("foo.txt"); + remove_file_if_exists(&output_path); 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()); - remove_file(&testfile).unwrap_or_else(|_| panic!("Can't remove {}", testfile.display())); + verify_file_equals(&output_path, "Hello, world!"); + remove_file(&output_path).unwrap_or_else(|_| panic!("Can't remove {}", output_path.display())); } diff --git a/tests/serial.rs b/tests/serial.rs index 0976a833..4017f471 100644 --- a/tests/serial.rs +++ b/tests/serial.rs @@ -1,12 +1,9 @@ mod common; -use std::{ - fs::{read_to_string, remove_file}, - path::PathBuf, -}; +use std::{fs::read_to_string, path::PathBuf}; use byte_unit::{Byte, Unit}; -use common::{build_hermit_bin, run_simple_vm}; +use common::{build_hermit_bin, remove_file_if_exists, run_simple_vm}; use uhyvelib::{ params::{Output, Params}, vm::UhyveVm, @@ -30,11 +27,7 @@ fn serial_file_output_test() { env_logger::try_init().ok(); let bin_path = build_hermit_bin("serial"); let output_path: PathBuf = "testserialout.txt".into(); - if output_path.exists() { - println!("Removing existing file {}", output_path.display()); - remove_file(&output_path) - .unwrap_or_else(|_| panic!("Can't remove {}", output_path.display())); - } + remove_file_if_exists(&output_path); println!("Launching kernel {}", bin_path.display()); let params = Params { @@ -55,5 +48,5 @@ fn serial_file_output_test() { let file_content = read_to_string(&output_path).unwrap(); assert!(file_content.contains("Hello from serial!\nABCD\n1234ASDF!@#$\n")); - remove_file(&output_path).unwrap_or_else(|_| panic!("Can't remove {}", output_path.display())); + remove_file_if_exists(&output_path); }