mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 11:07:44 +00:00
Tests: provides easy mount of temp fs (#7249)
This commit is contained in:
parent
3891ee1159
commit
c2505841e0
8 changed files with 82 additions and 26 deletions
|
@ -232,7 +232,7 @@ fn test_manpage() {
|
||||||
|
|
||||||
let test_scenario = TestScenario::new("");
|
let test_scenario = TestScenario::new("");
|
||||||
|
|
||||||
let child = Command::new(test_scenario.bin_path)
|
let child = Command::new(&test_scenario.bin_path)
|
||||||
.arg("manpage")
|
.arg("manpage")
|
||||||
.arg("base64")
|
.arg("base64")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
|
|
@ -2288,7 +2288,7 @@ fn test_cp_one_file_system() {
|
||||||
use crate::common::util::AtPath;
|
use crate::common::util::AtPath;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
let scene = TestScenario::new(util_name!());
|
let mut scene = TestScenario::new(util_name!());
|
||||||
let at = &scene.fixtures;
|
let at = &scene.fixtures;
|
||||||
|
|
||||||
// Test must be run as root (or with `sudo -E`)
|
// Test must be run as root (or with `sudo -E`)
|
||||||
|
@ -2304,14 +2304,8 @@ fn test_cp_one_file_system() {
|
||||||
let mountpoint_path = &at_src.plus_as_string(TEST_MOUNT_MOUNTPOINT);
|
let mountpoint_path = &at_src.plus_as_string(TEST_MOUNT_MOUNTPOINT);
|
||||||
|
|
||||||
scene
|
scene
|
||||||
.cmd("mount")
|
.mount_temp_fs(mountpoint_path)
|
||||||
.arg("-t")
|
.expect("mounting tmpfs failed");
|
||||||
.arg("tmpfs")
|
|
||||||
.arg("-o")
|
|
||||||
.arg("size=640k") // ought to be enough
|
|
||||||
.arg("tmpfs")
|
|
||||||
.arg(mountpoint_path)
|
|
||||||
.succeeds();
|
|
||||||
|
|
||||||
at_src.touch(TEST_MOUNT_OTHER_FILESYSTEM_FILE);
|
at_src.touch(TEST_MOUNT_OTHER_FILESYSTEM_FILE);
|
||||||
|
|
||||||
|
@ -2324,7 +2318,7 @@ fn test_cp_one_file_system() {
|
||||||
.succeeds();
|
.succeeds();
|
||||||
|
|
||||||
// Ditch the mount before the asserts
|
// Ditch the mount before the asserts
|
||||||
scene.cmd("umount").arg(mountpoint_path).succeeds();
|
scene.umount_temp_fs();
|
||||||
|
|
||||||
assert!(!at_dst.file_exists(TEST_MOUNT_OTHER_FILESYSTEM_FILE));
|
assert!(!at_dst.file_exists(TEST_MOUNT_OTHER_FILESYSTEM_FILE));
|
||||||
// Check if the other files were copied from the source folder hierarchy
|
// Check if the other files were copied from the source folder hierarchy
|
||||||
|
|
|
@ -1683,7 +1683,7 @@ fn test_reading_partial_blocks_from_fifo() {
|
||||||
fn test_reading_partial_blocks_from_fifo_unbuffered() {
|
fn test_reading_partial_blocks_from_fifo_unbuffered() {
|
||||||
// Create the FIFO.
|
// Create the FIFO.
|
||||||
let ts = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = ts.fixtures;
|
let at = &ts.fixtures;
|
||||||
at.mkfifo("fifo");
|
at.mkfifo("fifo");
|
||||||
let fifoname = at.plus_as_string("fifo");
|
let fifoname = at.plus_as_string("fifo");
|
||||||
|
|
||||||
|
|
|
@ -655,7 +655,7 @@ fn test_du_time() {
|
||||||
#[cfg(feature = "touch")]
|
#[cfg(feature = "touch")]
|
||||||
fn birth_supported() -> bool {
|
fn birth_supported() -> bool {
|
||||||
let ts = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let m = match std::fs::metadata(ts.fixtures.subdir) {
|
let m = match std::fs::metadata(&ts.fixtures.subdir) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => panic!("{}", e),
|
Err(e) => panic!("{}", e),
|
||||||
};
|
};
|
||||||
|
|
|
@ -137,7 +137,7 @@ fn test_debug_2() {
|
||||||
let result = ts
|
let result = ts
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-vv")
|
.arg("-vv")
|
||||||
.arg(ts.bin_path)
|
.arg(&ts.bin_path)
|
||||||
.args(&["echo", "hello2"])
|
.args(&["echo", "hello2"])
|
||||||
.succeeds();
|
.succeeds();
|
||||||
result.stderr_matches(
|
result.stderr_matches(
|
||||||
|
@ -165,7 +165,7 @@ fn test_debug1_part_of_string_arg() {
|
||||||
let result = ts
|
let result = ts
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-vS FOO=BAR")
|
.arg("-vS FOO=BAR")
|
||||||
.arg(ts.bin_path)
|
.arg(&ts.bin_path)
|
||||||
.args(&["echo", "hello1"])
|
.args(&["echo", "hello1"])
|
||||||
.succeeds();
|
.succeeds();
|
||||||
result.stderr_matches(
|
result.stderr_matches(
|
||||||
|
@ -186,7 +186,7 @@ fn test_debug2_part_of_string_arg() {
|
||||||
let result = ts
|
let result = ts
|
||||||
.ucmd()
|
.ucmd()
|
||||||
.arg("-vvS FOO=BAR")
|
.arg("-vvS FOO=BAR")
|
||||||
.arg(ts.bin_path)
|
.arg(&ts.bin_path)
|
||||||
.args(&["echo", "hello2"])
|
.args(&["echo", "hello2"])
|
||||||
.succeeds();
|
.succeeds();
|
||||||
result.stderr_matches(
|
result.stderr_matches(
|
||||||
|
|
|
@ -31,7 +31,7 @@ fn test_deleted_dir() {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
let ts = TestScenario::new(util_name!());
|
let ts = TestScenario::new(util_name!());
|
||||||
let at = ts.fixtures;
|
let at = &ts.fixtures;
|
||||||
let output = Command::new("sh")
|
let output = Command::new("sh")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!(
|
.arg(format!(
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
|
|
||||||
//spell-checker: ignore (linux) rlimit prlimit coreutil ggroups uchild uncaptured scmd SHLVL canonicalized openpty
|
//spell-checker: ignore (linux) rlimit prlimit coreutil ggroups uchild uncaptured scmd SHLVL canonicalized openpty
|
||||||
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus
|
//spell-checker: ignore (linux) winsize xpixel ypixel setrlimit FSIZE SIGBUS SIGSEGV sigbus tmpfs
|
||||||
|
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
#![allow(
|
#![allow(
|
||||||
|
@ -1169,6 +1169,8 @@ pub struct TestScenario {
|
||||||
pub util_name: String,
|
pub util_name: String,
|
||||||
pub fixtures: AtPath,
|
pub fixtures: AtPath,
|
||||||
tmpd: Rc<TempDir>,
|
tmpd: Rc<TempDir>,
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
|
||||||
|
tmp_fs_mountpoint: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TestScenario {
|
impl TestScenario {
|
||||||
|
@ -1182,6 +1184,8 @@ impl TestScenario {
|
||||||
util_name: util_name.as_ref().into(),
|
util_name: util_name.as_ref().into(),
|
||||||
fixtures: AtPath::new(tmpd.as_ref().path()),
|
fixtures: AtPath::new(tmpd.as_ref().path()),
|
||||||
tmpd,
|
tmpd,
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
|
||||||
|
tmp_fs_mountpoint: None,
|
||||||
};
|
};
|
||||||
let mut fixture_path_builder = env::current_dir().unwrap();
|
let mut fixture_path_builder = env::current_dir().unwrap();
|
||||||
fixture_path_builder.push(TESTS_DIR);
|
fixture_path_builder.push(TESTS_DIR);
|
||||||
|
@ -1215,6 +1219,44 @@ impl TestScenario {
|
||||||
pub fn ccmd<S: AsRef<str>>(&self, util_name: S) -> UCommand {
|
pub fn ccmd<S: AsRef<str>>(&self, util_name: S) -> UCommand {
|
||||||
UCommand::with_util(util_name, self.tmpd.clone())
|
UCommand::with_util(util_name, self.tmpd.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mounts a temporary filesystem at the specified mount point.
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
|
||||||
|
pub fn mount_temp_fs(&mut self, mount_point: &str) -> core::result::Result<(), String> {
|
||||||
|
if self.tmp_fs_mountpoint.is_some() {
|
||||||
|
return Err("already mounted".to_string());
|
||||||
|
}
|
||||||
|
let cmd_result = self
|
||||||
|
.cmd("mount")
|
||||||
|
.arg("-t")
|
||||||
|
.arg("tmpfs")
|
||||||
|
.arg("-o")
|
||||||
|
.arg("size=640k") // ought to be enough
|
||||||
|
.arg("tmpfs")
|
||||||
|
.arg(mount_point)
|
||||||
|
.run();
|
||||||
|
if !cmd_result.succeeded() {
|
||||||
|
return Err(format!("mount failed: {}", cmd_result.stderr_str()));
|
||||||
|
}
|
||||||
|
self.tmp_fs_mountpoint = Some(mount_point.to_string());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
|
||||||
|
/// Unmounts the temporary filesystem if it is currently mounted.
|
||||||
|
pub fn umount_temp_fs(&mut self) {
|
||||||
|
if let Some(mount_point) = self.tmp_fs_mountpoint.as_ref() {
|
||||||
|
self.cmd("umount").arg(mount_point).succeeds();
|
||||||
|
self.tmp_fs_mountpoint = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for TestScenario {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
|
||||||
|
self.umount_temp_fs();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
|
@ -4007,4 +4049,24 @@ mod tests {
|
||||||
.stdout_is(expected);
|
.stdout_is(expected);
|
||||||
std::assert_eq!(p_umask, get_umask()); // make sure parent umask didn't change
|
std::assert_eq!(p_umask, get_umask()); // make sure parent umask didn't change
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
|
||||||
|
#[test]
|
||||||
|
fn test_mount_temp_fs() {
|
||||||
|
let mut scene = TestScenario::new("util");
|
||||||
|
let at = &scene.fixtures;
|
||||||
|
// Test must be run as root (or with `sudo -E`)
|
||||||
|
if scene.cmd("whoami").run().stdout_str() != "root\n" {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
at.mkdir("mountpoint");
|
||||||
|
let mountpoint = at.plus("mountpoint");
|
||||||
|
scene.mount_temp_fs(mountpoint.to_str().unwrap()).unwrap();
|
||||||
|
scene
|
||||||
|
.cmd("df")
|
||||||
|
.arg("-h")
|
||||||
|
.arg(mountpoint)
|
||||||
|
.succeeds()
|
||||||
|
.stdout_contains("tmpfs");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ fn execution_phrase_single() {
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
let scenario = TestScenario::new("ls");
|
let scenario = TestScenario::new("ls");
|
||||||
symlink_file(scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
|
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-ls")).unwrap();
|
||||||
let output = Command::new(scenario.fixtures.plus("uu-ls"))
|
let output = Command::new(scenario.fixtures.plus("uu-ls"))
|
||||||
.arg("--some-invalid-arg")
|
.arg("--some-invalid-arg")
|
||||||
.output()
|
.output()
|
||||||
|
@ -56,7 +56,7 @@ fn util_name_double() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let scenario = TestScenario::new("sort");
|
let scenario = TestScenario::new("sort");
|
||||||
let mut child = Command::new(scenario.bin_path)
|
let mut child = Command::new(&scenario.bin_path)
|
||||||
.arg("sort")
|
.arg("sort")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
|
@ -78,7 +78,7 @@ fn util_name_single() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let scenario = TestScenario::new("sort");
|
let scenario = TestScenario::new("sort");
|
||||||
symlink_file(scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
|
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap();
|
||||||
let mut child = Command::new(scenario.fixtures.plus("uu-sort"))
|
let mut child = Command::new(scenario.fixtures.plus("uu-sort"))
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stderr(Stdio::piped())
|
.stderr(Stdio::piped())
|
||||||
|
@ -102,7 +102,7 @@ fn util_invalid_name_help() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let scenario = TestScenario::new("invalid_name");
|
let scenario = TestScenario::new("invalid_name");
|
||||||
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
|
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
|
||||||
let child = Command::new(scenario.fixtures.plus("invalid_name"))
|
let child = Command::new(scenario.fixtures.plus("invalid_name"))
|
||||||
.arg("--help")
|
.arg("--help")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
@ -138,7 +138,7 @@ fn util_non_utf8_name_help() {
|
||||||
|
|
||||||
let scenario = TestScenario::new("invalid_name");
|
let scenario = TestScenario::new("invalid_name");
|
||||||
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff"));
|
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff"));
|
||||||
symlink_file(scenario.bin_path, &non_utf8_path).unwrap();
|
symlink_file(&scenario.bin_path, &non_utf8_path).unwrap();
|
||||||
let child = Command::new(&non_utf8_path)
|
let child = Command::new(&non_utf8_path)
|
||||||
.arg("--help")
|
.arg("--help")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
@ -166,7 +166,7 @@ fn util_invalid_name_invalid_command() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let scenario = TestScenario::new("invalid_name");
|
let scenario = TestScenario::new("invalid_name");
|
||||||
symlink_file(scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
|
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap();
|
||||||
let child = Command::new(scenario.fixtures.plus("invalid_name"))
|
let child = Command::new(scenario.fixtures.plus("invalid_name"))
|
||||||
.arg("definitely_invalid")
|
.arg("definitely_invalid")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
@ -192,7 +192,7 @@ fn util_completion() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let scenario = TestScenario::new("completion");
|
let scenario = TestScenario::new("completion");
|
||||||
let child = Command::new(scenario.bin_path)
|
let child = Command::new(&scenario.bin_path)
|
||||||
.arg("completion")
|
.arg("completion")
|
||||||
.arg("true")
|
.arg("true")
|
||||||
.arg("powershell")
|
.arg("powershell")
|
||||||
|
@ -220,7 +220,7 @@ fn util_manpage() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let scenario = TestScenario::new("completion");
|
let scenario = TestScenario::new("completion");
|
||||||
let child = Command::new(scenario.bin_path)
|
let child = Command::new(&scenario.bin_path)
|
||||||
.arg("manpage")
|
.arg("manpage")
|
||||||
.arg("true")
|
.arg("true")
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue