Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/process-tests-direct-exec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
---

test(services): invoke /bin/sh directly in the process tests

The `shim()` helper wrote an executable script and then executed it, so another
test forking in the interval could let the child inherit the write descriptor and
make the exec fail with `ETXTBSY`. The tests now pass their program text to
`/bin/sh -c`, so nothing is written before it is run. No runtime behaviour changes.
49 changes: 12 additions & 37 deletions crates/nostream-services/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,32 +271,8 @@ fn signal_name(_status: &std::process::ExitStatus) -> String {
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use std::os::unix::fs::PermissionsExt;

use super::*;

/// Writes an executable shell script into `directory` under `name`.
fn shim(directory: &std::path::Path, name: &str, body: &str) -> String {
use std::io::Write;

let path = directory.join(name);
// Write to a uniquely named temp file in the same directory, close the write descriptor,
// mark it executable, and rename it into place so the target path never exists while
// still open for writing (narrows the ETXTBSY fork/exec window across parallel tests).
let mut temp = tempfile::NamedTempFile::new_in(directory).expect("a temporary shim file");
temp.write_all(format!("#!/bin/bash\n{body}\n").as_bytes())
.expect("the shim is written");
let (file, temp_path) = temp.keep().expect("the temporary shim stays");
drop(file);
let mut permissions = std::fs::metadata(&temp_path)
.expect("the shim exists")
.permissions();
permissions.set_mode(0o755);
std::fs::set_permissions(&temp_path, permissions).expect("the shim is executable");
std::fs::rename(&temp_path, &path).expect("the shim is moved into place");
path.display().to_string()
}

fn argv(program: &str, arguments: &[&str]) -> Vec<String> {
std::iter::once(program.to_owned())
.chain(arguments.iter().map(|argument| (*argument).to_owned()))
Expand Down Expand Up @@ -343,10 +319,12 @@ mod tests {

#[tokio::test]
async fn a_slow_child_times_out() {
let directory = tempfile::tempdir().expect("a temporary directory");
let slow = shim(directory.path(), "slow", "sleep 30");
let started = std::time::Instant::now();
let captured = capture(&argv(&slow, &[]), Some(Duration::from_millis(150))).await;
let captured = capture(
&argv("/bin/sh", &["-c", "exec sleep 30"]),
Some(Duration::from_millis(150)),
)
.await;
assert_eq!(captured, CapturedResult::TimedOut);
assert!(
started.elapsed() < Duration::from_secs(5),
Expand All @@ -356,24 +334,21 @@ mod tests {

#[tokio::test]
async fn run_answers_the_child_exit_code_and_capture_answers_its_output() {
let directory = tempfile::tempdir().expect("a temporary directory");
// The environment the caller adds reaches the child: the shim answers 0 only when it
// The environment the caller adds reaches the child: the shell answers 0 only when it
// finds the variable it was handed.
let env_check = shim(
directory.path(),
"env-check",
"env | grep -q '^NOSTREAM_PROBE=yes$'",
);
let code = run(
&argv(&env_check, &[]),
&argv("/bin/sh", &["-c", "env | grep -q '^NOSTREAM_PROBE=yes$'"]),
&[("NOSTREAM_PROBE", "yes".to_owned())],
)
.await
.expect("the child runs");
assert_eq!(code, 0, "the added environment reaches the child");

let echo = shim(directory.path(), "echoer", "printf '%s\\n' \"$@\"");
let captured = capture(&argv(&echo, &["two"]), None).await;
let captured = capture(
&argv("/bin/sh", &["-c", "printf '%s\\n' \"$@\"", "sh", "two"]),
None,
)
.await;
let CapturedResult::Exited { code, stdout, .. } = captured else {
panic!("the child exits");
};
Expand Down
Loading