@@ -12,6 +12,7 @@ use common::{
1212 build_hermit_bin, check_result, get_fs_fixture_path, remove_file_if_exists, run_vm_in_thread,
1313} ;
1414use rand:: { Rng , distr:: Alphanumeric } ;
15+ use tempfile:: TempDir ;
1516use uhyvelib:: { params:: Params , vm:: UhyveVm } ;
1617
1718/// Verifies successful file creation on the host OS and its contents.
@@ -39,13 +40,35 @@ impl AsStr for &str {
3940
4041/// Gets a "base" guest and host path, only useful for UhyveFileMap tests.
4142fn get_default_paths ( ) -> ( PathBuf , PathBuf ) {
42- let guest_dir_path: PathBuf = PathBuf :: from ( "/root/ " ) ;
43+ let guest_dir_path: PathBuf = PathBuf :: from ( "/uhyve_mount " ) ;
4344 let mut host_dir_path = get_fs_fixture_path ( ) ;
4445 host_dir_path. push ( "ignore_everything_here" ) ;
4546
4647 ( guest_dir_path, host_dir_path)
4748}
4849
50+ /// Creates a temporary directory for a uhyve mount and returns the path to it
51+ /// as well as the owning TempDir
52+ fn create_tmp_mount ( ) -> ( PathBuf , TempDir ) {
53+ let tempdir = TempDir :: new ( ) . unwrap ( ) ;
54+ let host_path = tempdir. path ( ) . to_path_buf ( ) ;
55+ ( host_path, tempdir)
56+ }
57+
58+ /// creates a tempfile from the given filename and returns a filemap, the guest
59+ /// filepath and a directory handle to the tempdir.
60+ fn create_filemap ( guest_filename : & str ) -> ( Vec < String > , PathBuf , TempDir ) {
61+ let guest_file_path = get_testname_derived_guest_path ( guest_filename) ;
62+ let ( mut host_path, tmpdir) = create_tmp_mount ( ) ;
63+ host_path. push ( guest_filename) ;
64+ let filemap_str = format ! (
65+ "{}:{}" ,
66+ host_path. as_os_str( ) . display( ) ,
67+ guest_file_path. as_os_str( ) . display( )
68+ ) ;
69+ ( vec ! [ filemap_str] , guest_file_path, tmpdir)
70+ }
71+
4972/// Generates a filename in the format of prefixab1cD23.txt
5073fn generate_filename ( prefix : & str ) -> String {
5174 let mut filename = prefix. to_owned ( ) ;
@@ -64,7 +87,7 @@ fn generate_filename(prefix: &str) -> String {
6487/// * `test_name` - Name of the test.
6588fn get_testname_derived_guest_path ( test_name : & str ) -> PathBuf {
6689 // Starting off with the "guest_dir_path".
67- let mut guest_file_path = PathBuf :: from ( "/root /" ) ;
90+ let mut guest_file_path = PathBuf :: from ( "/uhyve_mount /" ) ;
6891 guest_file_path. push ( generate_filename ( test_name) ) ;
6992 guest_file_path
7093}
@@ -272,8 +295,8 @@ fn fd_open_remove_close() {
272295 env_logger:: try_init ( ) . ok ( ) ;
273296
274297 let test_name: & ' static str = "fd_open_remove_close" ;
275- let guest_file_path = get_testname_derived_guest_path ( test_name) ;
276- let params = generate_params ( None , test_name, & guest_file_path) ;
298+ let ( filemap , guest_file_path, _tmpdir ) = create_filemap ( test_name) ;
299+ let params = generate_params ( Some ( filemap ) , test_name, & guest_file_path) ;
277300
278301 let bin_path: PathBuf = build_hermit_bin ( "fs_tests" ) ;
279302 let res = run_vm_in_thread ( bin_path, params) ;
@@ -317,8 +340,8 @@ fn open_read_only_write() {
317340 env_logger:: try_init ( ) . ok ( ) ;
318341
319342 let test_name: & ' static str = "open_read_only_write" ;
320- let guest_file_path = get_testname_derived_guest_path ( test_name) ;
321- let params = generate_params ( None , test_name, & guest_file_path) ;
343+ let ( filemap , guest_file_path, _tmpdir ) = create_filemap ( test_name) ;
344+ let params = generate_params ( Some ( filemap ) , test_name, & guest_file_path) ;
322345
323346 let bin_path: PathBuf = build_hermit_bin ( "fs_tests" ) ;
324347
@@ -356,13 +379,55 @@ fn fd_write_to_fd() {
356379 check_result ( & res) ;
357380}
358381
382+ #[ test]
383+ fn mounts_test ( ) {
384+ env_logger:: try_init ( ) . ok ( ) ;
385+
386+ let test_name: & ' static str = "mounts_test" ;
387+ let guest_dir_path: PathBuf = PathBuf :: from ( "/" ) ;
388+ let host_dir_path = get_fs_fixture_path ( ) ;
389+
390+ let uhyvefilemap_params = vec ! [
391+ format!(
392+ "{}/testdir1:{}testdir1" ,
393+ ( & host_dir_path) . as_str( ) ,
394+ ( & guest_dir_path) . as_str( )
395+ ) ,
396+ format!(
397+ "{}/testdir2:{}testdir2" ,
398+ ( & host_dir_path) . as_str( ) ,
399+ ( & guest_dir_path) . as_str( )
400+ ) ,
401+ format!(
402+ "{}/testdir3:{}testdir3/subdir1/subdir2/subdir3" ,
403+ ( & host_dir_path) . as_str( ) ,
404+ ( & guest_dir_path) . as_str( )
405+ ) ,
406+ format!(
407+ "{}/testdir2:{}testdir4" ,
408+ ( & host_dir_path) . as_str( ) ,
409+ ( & guest_dir_path) . as_str( )
410+ ) ,
411+ format!(
412+ "{}/testdir1/testfile_a.txt:/anothermountpoint/test_a.txt" ,
413+ ( & host_dir_path) . as_str( ) ,
414+ ) ,
415+ ] ;
416+ let guest_file_path = get_testname_derived_guest_path ( test_name) ;
417+ let params = generate_params ( uhyvefilemap_params. into ( ) , test_name, & guest_file_path) ;
418+
419+ let bin_path: PathBuf = build_hermit_bin ( "fs_tests" ) ;
420+ let res = run_vm_in_thread ( bin_path, params) ;
421+ check_result ( & res) ;
422+ }
423+
359424#[ test]
360425fn lseek_test ( ) {
361426 env_logger:: try_init ( ) . ok ( ) ;
362427
363428 let test_name: & ' static str = "lseek_file" ;
364- let guest_file_path = get_testname_derived_guest_path ( test_name) ;
365- let params = generate_params ( None , test_name, & guest_file_path) ;
429+ let ( filemap , guest_file_path, _tmpdir ) = create_filemap ( test_name) ;
430+ let params = generate_params ( Some ( filemap ) , test_name, & guest_file_path) ;
366431
367432 let bin_path: PathBuf = build_hermit_bin ( "fs_tests" ) ;
368433 let res = run_vm_in_thread ( bin_path, params) ;
0 commit comments