44// file that was distributed with this source code.
55
66//spell-checker: ignore (linux) rlimit prlimit coreutil ggroups uchild uncaptured scmd SHLVL canonicalized openpty
7- //spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus
7+ //spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus tmpfs
88
99#![ allow( dead_code) ]
1010#![ allow(
@@ -1169,6 +1169,8 @@ pub struct TestScenario {
11691169 pub util_name : String ,
11701170 pub fixtures : AtPath ,
11711171 tmpd : Rc < TempDir > ,
1172+ #[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
1173+ tmp_fs_mountpoint : Option < String > ,
11721174}
11731175
11741176impl TestScenario {
@@ -1182,6 +1184,8 @@ impl TestScenario {
11821184 util_name : util_name. as_ref ( ) . into ( ) ,
11831185 fixtures : AtPath :: new ( tmpd. as_ref ( ) . path ( ) ) ,
11841186 tmpd,
1187+ #[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
1188+ tmp_fs_mountpoint : None ,
11851189 } ;
11861190 let mut fixture_path_builder = env:: current_dir ( ) . unwrap ( ) ;
11871191 fixture_path_builder. push ( TESTS_DIR ) ;
@@ -1215,6 +1219,44 @@ impl TestScenario {
12151219 pub fn ccmd < S : AsRef < str > > ( & self , util_name : S ) -> UCommand {
12161220 UCommand :: with_util ( util_name, self . tmpd . clone ( ) )
12171221 }
1222+
1223+ /// Mounts a temporary filesystem at the specified mount point.
1224+ #[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
1225+ pub fn mount_temp_fs ( & mut self , mount_point : & str ) -> core:: result:: Result < ( ) , String > {
1226+ if self . tmp_fs_mountpoint . is_some ( ) {
1227+ return Err ( "already mounted" . to_string ( ) ) ;
1228+ }
1229+ let cmd_result = self
1230+ . cmd ( "mount" )
1231+ . arg ( "-t" )
1232+ . arg ( "tmpfs" )
1233+ . arg ( "-o" )
1234+ . arg ( "size=640k" ) // ought to be enough
1235+ . arg ( "tmpfs" )
1236+ . arg ( mount_point)
1237+ . run ( ) ;
1238+ if !cmd_result. succeeded ( ) {
1239+ return Err ( format ! ( "mount failed: {}" , cmd_result. stderr_str( ) ) ) ;
1240+ }
1241+ self . tmp_fs_mountpoint = Some ( mount_point. to_string ( ) ) ;
1242+ Ok ( ( ) )
1243+ }
1244+
1245+ #[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
1246+ /// Unmounts the temporary filesystem if it is currently mounted.
1247+ pub fn umount_temp_fs ( & mut self ) {
1248+ if let Some ( mount_point) = self . tmp_fs_mountpoint . as_ref ( ) {
1249+ self . cmd ( "umount" ) . arg ( mount_point) . succeeds ( ) ;
1250+ self . tmp_fs_mountpoint = None ;
1251+ }
1252+ }
1253+ }
1254+
1255+ impl Drop for TestScenario {
1256+ fn drop ( & mut self ) {
1257+ #[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
1258+ self . umount_temp_fs ( ) ;
1259+ }
12181260}
12191261
12201262#[ cfg( unix) ]
@@ -4007,4 +4049,24 @@ mod tests {
40074049 . stdout_is ( expected) ;
40084050 std:: assert_eq!( p_umask, get_umask( ) ) ; // make sure parent umask didn't change
40094051 }
4052+
4053+ #[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
4054+ #[ test]
4055+ fn test_mount_temp_fs ( ) {
4056+ let mut scene = TestScenario :: new ( "util" ) ;
4057+ let at = & scene. fixtures ;
4058+ // Test must be run as root (or with `sudo -E`)
4059+ if scene. cmd ( "whoami" ) . run ( ) . stdout_str ( ) != "root\n " {
4060+ return ;
4061+ }
4062+ at. mkdir ( "mountpoint" ) ;
4063+ let mountpoint = at. plus ( "mountpoint" ) ;
4064+ scene. mount_temp_fs ( mountpoint. to_str ( ) . unwrap ( ) ) . unwrap ( ) ;
4065+ scene
4066+ . cmd ( "df" )
4067+ . arg ( "-h" )
4068+ . arg ( mountpoint)
4069+ . succeeds ( )
4070+ . stdout_contains ( "tmpfs" ) ;
4071+ }
40104072}
0 commit comments