Skip to content

Commit c250584

Browse files
authored
Tests: provides easy mount of temp fs (#7249)
1 parent 3891ee1 commit c250584

File tree

8 files changed

+82
-26
lines changed

8 files changed

+82
-26
lines changed

tests/by-util/test_base64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn test_manpage() {
232232

233233
let test_scenario = TestScenario::new("");
234234

235-
let child = Command::new(test_scenario.bin_path)
235+
let child = Command::new(&test_scenario.bin_path)
236236
.arg("manpage")
237237
.arg("base64")
238238
.stdin(Stdio::piped())

tests/by-util/test_cp.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,7 @@ fn test_cp_one_file_system() {
22882288
use crate::common::util::AtPath;
22892289
use walkdir::WalkDir;
22902290

2291-
let scene = TestScenario::new(util_name!());
2291+
let mut scene = TestScenario::new(util_name!());
22922292
let at = &scene.fixtures;
22932293

22942294
// Test must be run as root (or with `sudo -E`)
@@ -2304,14 +2304,8 @@ fn test_cp_one_file_system() {
23042304
let mountpoint_path = &at_src.plus_as_string(TEST_MOUNT_MOUNTPOINT);
23052305

23062306
scene
2307-
.cmd("mount")
2308-
.arg("-t")
2309-
.arg("tmpfs")
2310-
.arg("-o")
2311-
.arg("size=640k") // ought to be enough
2312-
.arg("tmpfs")
2313-
.arg(mountpoint_path)
2314-
.succeeds();
2307+
.mount_temp_fs(mountpoint_path)
2308+
.expect("mounting tmpfs failed");
23152309

23162310
at_src.touch(TEST_MOUNT_OTHER_FILESYSTEM_FILE);
23172311

@@ -2324,7 +2318,7 @@ fn test_cp_one_file_system() {
23242318
.succeeds();
23252319

23262320
// Ditch the mount before the asserts
2327-
scene.cmd("umount").arg(mountpoint_path).succeeds();
2321+
scene.umount_temp_fs();
23282322

23292323
assert!(!at_dst.file_exists(TEST_MOUNT_OTHER_FILESYSTEM_FILE));
23302324
// Check if the other files were copied from the source folder hierarchy

tests/by-util/test_dd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1683,7 +1683,7 @@ fn test_reading_partial_blocks_from_fifo() {
16831683
fn test_reading_partial_blocks_from_fifo_unbuffered() {
16841684
// Create the FIFO.
16851685
let ts = TestScenario::new(util_name!());
1686-
let at = ts.fixtures;
1686+
let at = &ts.fixtures;
16871687
at.mkfifo("fifo");
16881688
let fifoname = at.plus_as_string("fifo");
16891689

tests/by-util/test_du.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ fn test_du_time() {
655655
#[cfg(feature = "touch")]
656656
fn birth_supported() -> bool {
657657
let ts = TestScenario::new(util_name!());
658-
let m = match std::fs::metadata(ts.fixtures.subdir) {
658+
let m = match std::fs::metadata(&ts.fixtures.subdir) {
659659
Ok(m) => m,
660660
Err(e) => panic!("{}", e),
661661
};

tests/by-util/test_env.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn test_debug_2() {
137137
let result = ts
138138
.ucmd()
139139
.arg("-vv")
140-
.arg(ts.bin_path)
140+
.arg(&ts.bin_path)
141141
.args(&["echo", "hello2"])
142142
.succeeds();
143143
result.stderr_matches(
@@ -165,7 +165,7 @@ fn test_debug1_part_of_string_arg() {
165165
let result = ts
166166
.ucmd()
167167
.arg("-vS FOO=BAR")
168-
.arg(ts.bin_path)
168+
.arg(&ts.bin_path)
169169
.args(&["echo", "hello1"])
170170
.succeeds();
171171
result.stderr_matches(
@@ -186,7 +186,7 @@ fn test_debug2_part_of_string_arg() {
186186
let result = ts
187187
.ucmd()
188188
.arg("-vvS FOO=BAR")
189-
.arg(ts.bin_path)
189+
.arg(&ts.bin_path)
190190
.args(&["echo", "hello2"])
191191
.succeeds();
192192
result.stderr_matches(

tests/by-util/test_pwd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn test_deleted_dir() {
3131
use std::process::Command;
3232

3333
let ts = TestScenario::new(util_name!());
34-
let at = ts.fixtures;
34+
let at = &ts.fixtures;
3535
let output = Command::new("sh")
3636
.arg("-c")
3737
.arg(format!(

tests/common/util.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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

11741176
impl 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
}

tests/test_util_name.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn execution_phrase_single() {
3535
use std::process::Command;
3636

3737
let scenario = TestScenario::new("ls");
38-
symlink_file(scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
38+
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
3939
let output = Command::new(scenario.fixtures.plus("uu-ls"))
4040
.arg("--some-invalid-arg")
4141
.output()
@@ -56,7 +56,7 @@ fn util_name_double() {
5656
};
5757

5858
let scenario = TestScenario::new("sort");
59-
let mut child = Command::new(scenario.bin_path)
59+
let mut child = Command::new(&scenario.bin_path)
6060
.arg("sort")
6161
.stdin(Stdio::piped())
6262
.stderr(Stdio::piped())
@@ -78,7 +78,7 @@ fn util_name_single() {
7878
};
7979

8080
let scenario = TestScenario::new("sort");
81-
symlink_file(scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
81+
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
8282
let mut child = Command::new(scenario.fixtures.plus("uu-sort"))
8383
.stdin(Stdio::piped())
8484
.stderr(Stdio::piped())
@@ -102,7 +102,7 @@ fn util_invalid_name_help() {
102102
};
103103

104104
let scenario = TestScenario::new("invalid_name");
105-
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
105+
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
106106
let child = Command::new(scenario.fixtures.plus("invalid_name"))
107107
.arg("--help")
108108
.stdin(Stdio::piped())
@@ -138,7 +138,7 @@ fn util_non_utf8_name_help() {
138138

139139
let scenario = TestScenario::new("invalid_name");
140140
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff"));
141-
symlink_file(scenario.bin_path, &non_utf8_path).unwrap();
141+
symlink_file(&scenario.bin_path, &non_utf8_path).unwrap();
142142
let child = Command::new(&non_utf8_path)
143143
.arg("--help")
144144
.stdin(Stdio::piped())
@@ -166,7 +166,7 @@ fn util_invalid_name_invalid_command() {
166166
};
167167

168168
let scenario = TestScenario::new("invalid_name");
169-
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
169+
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
170170
let child = Command::new(scenario.fixtures.plus("invalid_name"))
171171
.arg("definitely_invalid")
172172
.stdin(Stdio::piped())
@@ -192,7 +192,7 @@ fn util_completion() {
192192
};
193193

194194
let scenario = TestScenario::new("completion");
195-
let child = Command::new(scenario.bin_path)
195+
let child = Command::new(&scenario.bin_path)
196196
.arg("completion")
197197
.arg("true")
198198
.arg("powershell")
@@ -220,7 +220,7 @@ fn util_manpage() {
220220
};
221221

222222
let scenario = TestScenario::new("completion");
223-
let child = Command::new(scenario.bin_path)
223+
let child = Command::new(&scenario.bin_path)
224224
.arg("manpage")
225225
.arg("true")
226226
.stdin(Stdio::piped())

0 commit comments

Comments
 (0)