#![cfg(target_os = "linux")]
use std::{
ffi::OsString,
fs,
io::Write,
os::unix::process::ExitStatusExt,
path::{Path, PathBuf},
process::{Child, Command, ExitStatus, Stdio},
thread,
time::{Duration, Instant},
};
#[path = "support/executable.rs"]
mod executable;
#[path = "support/temp_dir.rs"]
mod test_dir;
use test_dir::TempDir as Scratch;
fn wait(child: &mut Child, limit: Duration) -> ExitStatus {
let start = Instant::now();
loop {
if let Some(status) = child.try_wait().unwrap() {
return status;
}
if start.elapsed() > limit {
let _ = child.kill();
let _ = child.wait();
panic!("process did not exit within {limit:?}");
}
thread::sleep(Duration::from_millis(10));
}
}
fn exec_sort(parent: &str, root: &str) -> std::process::Output {
Command::new(executable::supervisor())
.args(["--exec-sort", parent, root, "0", "9", "1", "1"])
.env_clear()
.stdin(Stdio::null())
.output()
.unwrap()
}
#[test]
fn exec_sort_accepts_only_its_own_temporary_root() {
let hex = "0123456789abcdef0123456789abcdef";
let parent = std::process::id().to_string();
for root in [
format!("tmp/fastmash-sort-{hex}"),
format!("/tmp//fastmash-sort-{hex}"),
format!("/tmp/fastmash-sort-{}", &hex[..31]),
format!("/tmp/fastmash-sort-{hex}0"),
format!("/tmp/fastmash-sort-{}/..", &hex[..29]),
"/tmp/fastmash-sort-0123456789abcdef0123456789abcdeg".to_string(),
"/tmp".to_string(),
] {
let output = exec_sort(&parent, &root);
assert_eq!(output.status.code(), Some(77), "{root}");
assert_eq!(
output.stderr, b"fastmash-sort-supervisor: invalid supervisor arguments\n",
"{root}"
);
}
let output = exec_sort("1", &format!("/tmp/fastmash-sort-{hex}"));
assert_eq!(output.status.code(), Some(77));
assert_eq!(
output.stderr,
b"fastmash-sort-supervisor: invalid supervisor arguments\n"
);
for base in ["/tmp", "/var/tmp"] {
let output = exec_sort(&parent, &format!("{base}/fastmash-sort-{hex}"));
assert!(output.status.success(), "{output:?}");
assert!(output.stdout.is_empty());
}
}
#[test]
fn the_exec_role_exits_77_whatever_its_standard_error() {
use std::os::unix::process::CommandExt;
let parent = std::process::id().to_string();
let root = "/tmp/fastmash-sort-0123456789abcdef0123456789abcdef";
let keys = format!("1{}", ",1".repeat(19999));
let rejected = ["--exec-sort", &parent, "/tmp", "0", "9", "1", "1"];
let unexecutable = ["--exec-sort", &parent, root, "0", "9", &keys, "1"];
let run = |args: &[&str], stderr: Stdio| {
let mut command = Command::new(executable::supervisor());
command
.args(args)
.env_clear()
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(stderr);
unsafe { command.pre_exec(small_stack) };
command.output().unwrap()
};
let full = || {
let device = fs::OpenOptions::new().write(true).open("/dev/full");
Stdio::from(device.unwrap())
};
let unread = || {
let (reader, writer) = std::io::pipe().unwrap();
drop(reader);
Stdio::from(writer)
};
for (args, reason) in [
(&rejected[..], "invalid supervisor arguments"),
(&unexecutable, "Argument list too long (os error 7)"),
] {
let output = run(args, Stdio::piped());
assert_eq!(output.status.code(), Some(77), "{reason}");
assert_eq!(
String::from_utf8_lossy(&output.stderr),
format!("fastmash-sort-supervisor: {reason}\n")
);
for (sink, stderr) in [("full", full()), ("unread", unread())] {
let status = run(args, stderr).status;
assert_eq!(status.code(), Some(77), "{reason}, {sink}: {status:?}");
}
}
}
fn small_stack() -> std::io::Result<()> {
let mut limit = libc::rlimit {
rlim_cur: 0,
rlim_max: 0,
};
let set = unsafe {
libc::getrlimit(libc::RLIMIT_STACK, &mut limit) == 0 && {
limit.rlim_cur = limit.rlim_max.min(512 << 10);
libc::setrlimit(libc::RLIMIT_STACK, &limit) == 0
}
};
if set {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
fn start_external(tmpdir: Option<&std::ffi::OsStr>, cwd: &std::path::Path) -> Child {
let mut command = Command::new(executable::fastmash());
command
.args(["-s", "-g", "1", "geomean", "2"])
.env_clear()
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.current_dir(cwd)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
if let Some(tmpdir) = tmpdir {
command.env("TMPDIR", tmpdir);
}
let mut child = command.spawn().unwrap();
child
.stdin
.as_mut()
.unwrap()
.write_all(b"b\t2\na\t1\n")
.unwrap();
child
}
fn running_root(fastmash: &mut Child) -> PathBuf {
let start = Instant::now();
loop {
if let Some(root) = sort_root(fastmash.id()).filter(|root| root.exists()) {
return root;
}
if fastmash.try_wait().unwrap().is_some() {
panic!("fastmash exited early");
}
assert!(
start.elapsed() < Duration::from_secs(10),
"the external sort did not start"
);
thread::sleep(Duration::from_millis(5));
}
}
#[test]
fn scratch_rejects_a_preexisting_directory() {
let parent = Scratch::new("collision-parent");
let occupied = parent.0.join("occupied");
fs::create_dir(&occupied).unwrap();
fs::write(occupied.join("sentinel"), b"untouched").unwrap();
assert!(matches!(
Scratch::create(occupied.clone()),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists
));
assert_eq!(fs::read(occupied.join("sentinel")).unwrap(), b"untouched");
}
#[test]
fn scratch_rejects_a_preexisting_symlink() {
use std::os::unix::fs::symlink;
let parent = Scratch::new("symlink-parent");
let target = parent.0.join("target");
fs::create_dir(&target).unwrap();
fs::write(target.join("sentinel"), b"untouched").unwrap();
let occupied = parent.0.join("occupied");
symlink(&target, &occupied).unwrap();
assert!(matches!(
Scratch::create(occupied.clone()),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists
));
assert_eq!(fs::read_link(occupied).unwrap(), target);
assert_eq!(fs::read(target.join("sentinel")).unwrap(), b"untouched");
}
#[test]
fn external_sort_uses_tmpdir_and_cleans_it() {
let base = Scratch::new("tmpdir");
let cwd = Scratch::new("cwd");
let (base, cwd) = (&base.0, &cwd.0);
fs::create_dir(cwd.join("relative")).unwrap();
let parent_name = cwd.file_name().unwrap().to_str().unwrap();
let spelled = |s: String| std::ffi::OsString::from(s);
for (tmpdir, expected) in [
(Some(base.clone().into_os_string()), base.clone()),
(Some(spelled("relative".into())), cwd.join("relative")),
(Some(spelled("./relative/".into())), cwd.join("relative")),
(Some(spelled(format!("{}//", base.display()))), base.clone()),
(Some(spelled(format!("{}/.", base.display()))), base.clone()),
(
Some(spelled(format!("../{parent_name}/relative"))),
cwd.join(format!("../{parent_name}/relative")),
),
(None, PathBuf::from("/tmp")),
] {
let mut fastmash = start_external(tmpdir.as_deref(), cwd);
let root = running_root(&mut fastmash);
assert_eq!(root.parent(), Some(expected.as_path()), "{tmpdir:?}");
drop(fastmash.stdin.take());
let status = wait(&mut fastmash, Duration::from_secs(10));
assert!(status.success(), "{tmpdir:?}: {status:?}");
let mut output = String::new();
std::io::Read::read_to_string(fastmash.stdout.as_mut().unwrap(), &mut output).unwrap();
assert_eq!(output, "a\t1\nb\t2\n", "{tmpdir:?}");
assert!(!root.exists(), "{root:?} remains");
}
assert_eq!(fs::read_dir(base).unwrap().count(), 0);
assert_eq!(fs::read_dir(cwd.join("relative")).unwrap().count(), 0);
}
fn sort_argument(fastmash: &mut Child, prefix: &str) -> String {
let start = Instant::now();
loop {
for supervisor in children(fastmash.id()) {
for sort in children(supervisor) {
let cmdline = fs::read(format!("/proc/{sort}/cmdline")).unwrap_or_default();
for arg in cmdline.split(|b| *b == 0) {
let arg = String::from_utf8_lossy(arg);
if arg.starts_with(prefix) {
return arg.into_owned();
}
}
}
}
assert!(
fastmash.try_wait().unwrap().is_none(),
"fastmash exited early"
);
assert!(
start.elapsed() < Duration::from_secs(10),
"the external sort did not start"
);
thread::sleep(Duration::from_millis(5));
}
}
#[test]
fn external_sort_threads_follow_gnu_sort_defaults() {
let available = std::thread::available_parallelism()
.map_or(1, |n| n.get())
.min(8);
for (omp, expected) in [
(vec![], available),
(vec![("OMP_NUM_THREADS", "3")], 3),
(vec![("OMP_NUM_THREADS", "5"), ("OMP_THREAD_LIMIT", "2")], 2),
] {
let mut command = Command::new(executable::fastmash());
command
.args(["-s", "-g", "1", "geomean", "2"])
.env_clear()
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.envs(omp.iter().copied())
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::null());
let mut fastmash = command.spawn().unwrap();
fastmash
.stdin
.as_mut()
.unwrap()
.write_all(b"b\t2\na\t1\n")
.unwrap();
let parallel = sort_argument(&mut fastmash, "--parallel=");
assert_eq!(parallel, format!("--parallel={expected}"), "{omp:?}");
drop(fastmash.stdin.take());
assert!(wait(&mut fastmash, Duration::from_secs(10)).success());
}
}
#[test]
fn unusable_tmpdir_is_a_clear_error() {
let cwd = Scratch::new("unusable");
for tmpdir in ["/nonexistent/fastmash", ""] {
let output = run_in(
Path::new(&executable::fastmash()),
&cwd.0,
["-s", "-g", "1", "geomean", "2"],
&[("TMPDIR", tmpdir), ("FASTMASH_SORT_MEMORY_BYTES", "1")],
b"b\t2\na\t1\n",
);
assert_eq!(output.status.code(), Some(1), "{tmpdir:?}");
assert!(output.stdout.is_empty());
assert_eq!(
String::from_utf8_lossy(&output.stderr),
format!(
"{}: sort temporary I/O error: No such file or directory (os error 2)\n",
executable::fastmash().to_string_lossy()
),
"{tmpdir:?}"
);
}
}
#[test]
fn unusable_tmpdir_sorts_small_input_in_process() {
use std::os::unix::fs::PermissionsExt;
let cwd = Scratch::new("small-unusable");
let read_only = Scratch::new("read-only-tmpdir");
fs::set_permissions(&read_only.0, fs::Permissions::from_mode(0o500)).unwrap();
let file = cwd.0.join("file");
fs::write(&file, b"").unwrap();
for tmpdir in [
read_only.0.to_str().unwrap(),
"/nonexistent/fastmash",
file.to_str().unwrap(),
"",
] {
let output = run_in(
Path::new(&executable::fastmash()),
&cwd.0,
["-s", "-g", "1", "geomean", "2"],
&[("TMPDIR", tmpdir)],
b"b\t2\na\t1\nb\t8\n",
);
assert!(output.status.success(), "{tmpdir:?}: {output:?}");
assert_eq!(output.stdout, b"a\t1\nb\t4\n", "{tmpdir:?}");
assert!(output.stderr.is_empty(), "{tmpdir:?}: {output:?}");
}
assert_eq!(fs::read_dir(&read_only.0).unwrap().count(), 0);
}
#[test]
fn a_separator_byte_from_0x80_sorts_in_process() {
use std::os::unix::ffi::OsStringExt;
let cwd = Scratch::new("high-separator");
for (job, expected) in [
(&["-s", "-g", "1", "rms", "2"][..], &b"a\xa72\nb\xa75\n"[..]),
(&["-s", "rmdup", "1"], b"a\xa72\nb\xa71\n"),
] {
let mut args = vec![OsString::from("-t"), OsString::from_vec(vec![0xa7])];
args.extend(job.iter().map(OsString::from));
let output = run_in(
Path::new(&executable::fastmash()),
&cwd.0,
&args,
&[],
b"b\xa71\na\xa72\nb\xa77\n",
);
assert!(output.status.success(), "{job:?}: {output:?}");
assert_eq!(output.stdout, expected, "{job:?}");
assert!(output.stderr.is_empty(), "{job:?}: {output:?}");
}
}
#[test]
fn a_missing_supervisor_sorts_in_process() {
use std::os::unix::fs::PermissionsExt;
let parent = Scratch::new("no-supervisor");
let occupied = parent.0.join("old-root");
fs::create_dir(&occupied).unwrap();
let marker = parent.0.join("executed");
let stale_supervisor = occupied.join("fastmash-sort-supervisor");
let script = format!(
"#!/bin/sh\nprintf stale > '{}'\nexit 77\n",
marker.display()
);
fs::write(&stale_supervisor, &script).unwrap();
fs::set_permissions(&stale_supervisor, fs::Permissions::from_mode(0o700)).unwrap();
assert!(matches!(
Scratch::create(occupied),
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists
));
let dir = Scratch::create(parent.0.join("fresh-root")).unwrap();
let fastmash = dir.0.join("fastmash");
fs::copy(executable::fastmash(), &fastmash).unwrap();
for (job, expected) in [
(&["-s", "-g", "1", "rms", "2"][..], "a\t2\nb\t5\n"),
(&["-s", "rmdup", "1"], "a\t2\nb\t1\n"),
] {
let output = run_in(&fastmash, &dir.0, job, &[], b"b\t1\na\t2\nb\t7\n");
assert!(output.status.success(), "{job:?}: {output:?}");
assert_eq!(output.stdout, expected.as_bytes(), "{job:?}");
assert!(output.stderr.is_empty(), "{job:?}: {output:?}");
}
assert!(!marker.exists(), "the stale supervisor ran");
assert_eq!(fs::read(stale_supervisor).unwrap(), script.as_bytes());
}
#[test]
fn without_close_range_cloexec_jobs_sort_in_process() {
use std::os::unix::process::CommandExt;
let cwd = Scratch::new("no-close-range");
let input = cwd.0.join("input");
fs::write(&input, b"b\t1\na\t2\nb\t7\n").unwrap();
for errno in [libc::EINVAL, libc::ENOSYS] {
for (job, expected) in [
(&["-s", "-g", "1", "rms", "2"][..], "a\t2\nb\t5\n"),
(&["-s", "rmdup", "1"], "a\t2\nb\t1\n"),
] {
let mut command = Command::new(executable::fastmash());
command
.args(job)
.env_clear()
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.current_dir(&cwd.0)
.stdin(fs::File::open(&input).unwrap());
unsafe { command.pre_exec(move || deny_close_range(errno)) };
let output = command.output().unwrap();
assert!(output.status.success(), "{errno} {job:?}: {output:?}");
assert_eq!(output.stdout, expected.as_bytes(), "{errno} {job:?}");
assert!(output.stderr.is_empty(), "{errno} {job:?}: {output:?}");
}
}
}
fn deny_close_range(errno: i32) -> std::io::Result<()> {
const LOAD: u16 = 0x20; const EQUAL: u16 = 0x15; const RETURN: u16 = 0x06; const X86_64: u32 = 0xc000_003e; let step = |code, jt, jf, k| libc::sock_filter { code, jt, jf, k };
let filter = [
step(LOAD, 0, 0, 4),
step(EQUAL, 0, 3, X86_64),
step(LOAD, 0, 0, 0),
step(EQUAL, 0, 1, libc::SYS_close_range as u32),
step(RETURN, 0, 0, libc::SECCOMP_RET_ERRNO | errno as u32),
step(RETURN, 0, 0, libc::SECCOMP_RET_ALLOW),
];
let program = libc::sock_fprog {
len: filter.len() as u16,
filter: filter.as_ptr().cast_mut(),
};
let (one, zero): (libc::c_ulong, libc::c_ulong) = (1, 0);
let mode = libc::c_ulong::from(libc::SECCOMP_MODE_FILTER);
let installed = unsafe {
libc::prctl(libc::PR_SET_NO_NEW_PRIVS, one, zero, zero, zero) == 0
&& libc::prctl(
libc::PR_SET_SECCOMP,
mode,
&program as *const libc::sock_fprog,
) == 0
};
if installed {
Ok(())
} else {
Err(std::io::Error::last_os_error())
}
}
fn children(pid: u32) -> Vec<u32> {
let mut found = Vec::new();
for entry in fs::read_dir("/proc").unwrap().flatten() {
let Some(child) = entry
.file_name()
.to_str()
.and_then(|n| n.parse::<u32>().ok())
else {
continue;
};
let Ok(stat) = fs::read_to_string(format!("/proc/{child}/stat")) else {
continue;
};
let after = &stat[stat.rfind(')').unwrap() + 2..];
if after.split(' ').nth(1).and_then(|p| p.parse().ok()) == Some(pid) {
found.push(child);
}
}
found
}
fn sort_root(fastmash: u32) -> Option<PathBuf> {
for supervisor in children(fastmash) {
for sort in children(supervisor) {
let cmdline = fs::read(format!("/proc/{sort}/cmdline")).ok()?;
for arg in cmdline.split(|b| *b == 0) {
if let Some(root) = arg.strip_prefix(b"--temporary-directory=") {
return Some(PathBuf::from(String::from_utf8_lossy(root).into_owned()));
}
}
}
}
None
}
#[test]
fn interrupted_external_sort_leaves_no_temporary_directory() {
let mut fastmash = Command::new(executable::fastmash())
.args(["-s", "-g", "1", "geomean", "2"])
.env_clear()
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.stdin(Stdio::piped())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.unwrap();
let mut input = fastmash.stdin.take().unwrap();
input.write_all(b"b\t2\na\t1\n").unwrap();
let start = Instant::now();
let root = loop {
if let Some(root) = sort_root(fastmash.id()).filter(|root| root.exists()) {
break root;
}
if fastmash.try_wait().unwrap().is_some() {
let output = fastmash.wait_with_output().unwrap();
panic!("fastmash exited early: {output:?}");
}
if start.elapsed() > Duration::from_secs(10) {
let _ = fastmash.kill();
panic!("the external sort did not start");
}
thread::sleep(Duration::from_millis(5));
};
assert_eq!(unsafe { libc::kill(fastmash.id() as i32, libc::SIGINT) }, 0);
let status = wait(&mut fastmash, Duration::from_secs(10));
assert_eq!(status.signal(), Some(libc::SIGINT), "{status:?}");
assert_eq!(root.parent(), Some(std::path::Path::new("/tmp")));
drop(input);
let start = Instant::now();
while root.exists() {
assert!(
start.elapsed() < Duration::from_secs(10),
"temporary directory remains: {root:?}"
);
thread::sleep(Duration::from_millis(10));
}
}
fn running_sort(fastmash: &mut Child) -> u32 {
let start = Instant::now();
loop {
for supervisor in children(fastmash.id()) {
for sort in children(supervisor) {
let cmdline = fs::read(format!("/proc/{sort}/cmdline")).unwrap_or_default();
if cmdline.starts_with(b"/usr/bin/sort\0") {
return sort;
}
}
}
assert!(
fastmash.try_wait().unwrap().is_none(),
"fastmash exited early"
);
assert!(
start.elapsed() < Duration::from_secs(10),
"the external sort did not start"
);
thread::sleep(Duration::from_millis(5));
}
}
#[test]
fn a_killed_sort_is_named_before_read_error_on_close() {
use std::io::Read;
let cwd = Scratch::new("killed-sort");
for (signal, named) in [(libc::SIGKILL, "Killed"), (libc::SIGTERM, "Terminated")] {
let mut fastmash = start_external(None, &cwd.0);
let sort = running_sort(&mut fastmash);
assert_eq!(unsafe { libc::kill(sort as i32, signal) }, 0);
drop(fastmash.stdin.take());
let status = wait(&mut fastmash, Duration::from_secs(10));
let mut stdout = Vec::new();
let mut stderr = String::new();
let mut child_stdout = fastmash.stdout.take().unwrap();
child_stdout.read_to_end(&mut stdout).unwrap();
let mut child_stderr = fastmash.stderr.take().unwrap();
child_stderr.read_to_string(&mut stderr).unwrap();
assert_eq!(status.code(), Some(1), "{named}: {stderr}");
assert!(stdout.is_empty(), "{named}");
assert_eq!(
stderr,
format!(
"{named}\n{}: read error (on close)\n",
executable::fastmash().to_string_lossy()
)
);
}
}
#[test]
fn a_long_header_in_a_file_is_read_in_chunks() {
use std::io::Read;
let dir = Scratch::new("long-header");
let path = dir.0.join("input");
let length = 200_000;
let rows = 50_000;
let mut input = vec![b'h'; length];
input.extend_from_slice(b"\tv\n");
for n in 0..rows {
input.extend_from_slice(format!("k{:05}\t1\n", n * 7919 % rows).as_bytes());
}
fs::write(&path, &input).unwrap();
let mut fastmash = Command::new(executable::fastmash())
.args(["-s", "--header-in", "-g", "1", "geomean", "2"])
.env_clear()
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.stdin(fs::File::open(&path).unwrap())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.unwrap();
let start = Instant::now();
while children(fastmash.id()).is_empty() {
assert!(
fastmash.try_wait().unwrap().is_none(),
"fastmash exited early"
);
assert!(
start.elapsed() < Duration::from_secs(10),
"the external sort did not start"
);
thread::sleep(Duration::from_millis(5));
}
let io = fs::read_to_string(format!("/proc/{}/io", fastmash.id())).unwrap();
let reads: usize = io
.lines()
.find_map(|line| line.strip_prefix("syscr: "))
.unwrap()
.parse()
.unwrap();
let mut stdout = Vec::new();
let mut output = fastmash.stdout.take().unwrap();
output.read_to_end(&mut stdout).unwrap();
assert!(wait(&mut fastmash, Duration::from_secs(20)).success());
let expected: String = (0..rows).map(|n| format!("k{n:05}\t1\n")).collect();
assert!(stdout == expected.as_bytes());
assert!(reads < length / 100, "{reads} read calls");
}
#[test]
fn ignored_cancellation_signals_fall_back_to_native_sorting() {
let input = b"b\t2\t9\na\t1\t3\nb\t4\t1\na\t3\t7\n";
for args in [
&["-s", "-g", "1", "rms", "2", "pcov", "2:3"][..],
&["-s", "-g", "1", "geomean", "2"],
&["-s", "rmdup", "1"],
] {
let run = |trap: &str| {
let script = format!("{trap}exec \"$0\" \"$@\"");
let mut child = Command::new("/bin/sh")
.arg("-c")
.arg(script)
.arg(executable::fastmash())
.args(args)
.env_clear()
.env("PATH", "/usr/bin:/bin")
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
child.stdin.take().unwrap().write_all(input).unwrap();
child.wait_with_output().unwrap()
};
let normal = run("");
assert!(normal.status.success(), "{args:?}: {normal:?}");
for trap in ["trap '' HUP; ", "trap '' INT; ", "trap '' HUP INT TERM; "] {
let fallback = run(trap);
assert_eq!(
fallback.status.code(),
Some(0),
"{args:?} {trap}: {fallback:?}"
);
assert_eq!(fallback.stdout, normal.stdout, "{args:?} {trap}");
assert!(fallback.stderr.is_empty(), "{args:?} {trap}: {fallback:?}");
}
}
}
fn run_in<I, S>(
fastmash: &Path,
dir: &Path,
args: I,
env: &[(&str, &str)],
input: &[u8],
) -> std::process::Output
where
I: IntoIterator<Item = S> + Clone,
S: AsRef<std::ffi::OsStr>,
{
let path = dir.join("input");
fs::write(&path, input).unwrap();
for _ in 0..50 {
match Command::new(fastmash)
.args(args.clone())
.env_clear()
.env("LC_ALL", "C")
.env("FASTMASH_GROUPING", "sort")
.env("PATH", "/usr/bin:/bin")
.envs(env.iter().copied())
.current_dir(dir)
.stdin(fs::File::open(&path).unwrap())
.output()
{
Err(error) if error.raw_os_error() == Some(26) => {
thread::sleep(Duration::from_millis(20));
}
output => return output.unwrap(),
}
}
panic!("{} stayed busy", fastmash.display())
}
fn with_companion(name: &str, script: &str) -> (PathBuf, std::process::Output) {
use std::os::unix::fs::PermissionsExt;
let dir = Scratch::new(name);
let fastmash = dir.0.join("fastmash");
fs::copy(executable::fastmash(), &fastmash).unwrap();
let companion = dir.0.join("fastmash-sort-supervisor");
fs::write(
&companion,
format!(
"#!/bin/sh\nREAL='{}'\n{script}\n",
executable::supervisor().to_string_lossy()
),
)
.unwrap();
fs::set_permissions(&companion, fs::Permissions::from_mode(0o755)).unwrap();
let output = run_in(
&fastmash,
&dir.0,
["-s", "-g", "1", "geomean", "2"],
&[],
b"b\t2\na\t1\n",
);
(fastmash, output)
}
#[test]
fn a_companion_from_another_release_is_named() {
for (name, script, hint) in [
(
"other-release",
"shift\nexec \"$REAL\" --supervise-999 \"$@\"",
"hint: install fastmash and fastmash-sort-supervisor from the same release\n",
),
("no-reply", "exit 77", ""),
] {
let (fastmash, output) = with_companion(name, script);
assert_eq!(output.status.code(), Some(77), "{name}: {output:?}");
assert!(output.stdout.is_empty(), "{name}");
assert_eq!(
String::from_utf8_lossy(&output.stderr),
format!(
"{}: unable to start sort supervisor\n{hint}",
fastmash.display()
),
"{name}"
);
}
let (_, output) = with_companion("same-companion", "exec \"$REAL\" \"$@\"");
assert!(output.status.success(), "{output:?}");
assert_eq!(output.stdout, b"a\t1\nb\t2\n");
}