use std::collections::BTreeMap;
use std::fs;
use std::io;
#[cfg(unix)]
use std::os::fd::AsRawFd;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt as _;
use std::path::PathBuf;
#[cfg(unix)]
use std::sync::Arc;
#[cfg(unix)]
use nix::sys::signal::Signal;
#[cfg(unix)]
use shep_core::signals::OperatorSignal;
#[cfg(unix)]
use tokio::io::DuplexStream;
use tokio::sync::{mpsc, oneshot};
use tokio::time::timeout;
#[cfg(unix)]
use crate::channel::{ChildMessage, ShepherdMessage};
#[cfg(unix)]
use crate::runner::RunnerError;
#[cfg(unix)]
use crate::runner::{AdoptSpec, LogCtl, ProcessRunner, RunningProcess};
use crate::runner::{Preflight, SpawnSpec, StdinWrite};
use super::super::CHANNEL_CAPACITY;
#[cfg(unix)]
use super::super::TokioRunner;
#[cfg(unix)]
use super::super::log_file::{LogSink, PipeFds};
#[cfg(unix)]
use super::super::pump::spawn_log_pump;
use super::super::pump::spawn_stdin_pump;
#[cfg(unix)]
use super::super::runner::signallable_pid;
use super::super::runner::{PATH_LIST_SEPARATOR, summarise_path, what_exec_will_find};
#[cfg(unix)]
use super::super::signal_group;
use super::*;
const STDIN_BUFFER: usize = 4;
#[tokio::test]
async fn a_line_whose_caller_stopped_waiting_is_dropped_rather_than_written_later() {
use tokio::io::AsyncReadExt as _;
let (mut child_end, daemon_end) = tokio::io::duplex(STDIN_BUFFER);
let (to_stdin, rx) = mpsc::channel(CHANNEL_CAPACITY);
spawn_stdin_pump(Some(daemon_end), rx);
let (first_done, first_ack) = oneshot::channel();
to_stdin
.send(StdinWrite {
line: "AAAAAAAA".to_string(),
done: first_done,
})
.await
.unwrap();
let (second_done, second_ack) = oneshot::channel();
to_stdin
.send(StdinWrite {
line: "BBBB".to_string(),
done: second_done,
})
.await
.unwrap();
drop(second_ack);
let (third_done, third_ack) = oneshot::channel();
to_stdin
.send(StdinWrite {
line: "CCCC".to_string(),
done: third_done,
})
.await
.unwrap();
drop(to_stdin);
let mut written = Vec::new();
timeout(PUMP_DEADLINE, child_end.read_to_end(&mut written))
.await
.expect("the pump must drain and close the pipe")
.expect("reading the pipe must succeed");
assert_eq!(
String::from_utf8(written).unwrap(),
"AAAAAAAA\nCCCC\n",
"the abandoned line must not reach the app, and the live one must"
);
assert!(
timeout(PUMP_DEADLINE, first_ack)
.await
.expect("the first write must be acknowledged")
.expect("its sender must outlive the write")
.is_ok()
);
assert!(
timeout(PUMP_DEADLINE, third_ack)
.await
.expect("the third write must be acknowledged")
.expect("its sender must outlive the write")
.is_ok()
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_report_names_the_stdin_write_end_it_was_told_about() {
let dir = tempfile::tempdir().unwrap();
let (_child_end, daemon_end) = std::io::pipe().unwrap();
let (_out_writer, out_reader) = tokio::io::duplex(STREAM_BUFFER);
let (logs_tx, _logs) = mpsc::channel(CHANNEL_CAPACITY);
let (ctl, ctl_rx) = mpsc::channel(CHANNEL_CAPACITY);
let pipes = PipeFds {
out: None,
err: None,
stdin: Some(daemon_end.as_raw_fd()),
channel: None,
};
spawn_log_pump(
Some(out_reader),
None::<DuplexStream>,
LogSink::Path(dir.path().join("out.log")),
LogSink::Path(dir.path().join("err.log")),
true,
logs_tx,
ctl_rx,
pipes,
);
let (done, ack) = oneshot::channel();
ctl.send(LogCtl::ReportFds { done }).await.unwrap();
let fds = timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
assert_eq!(fds.stdin, Some(daemon_end.as_raw_fd()));
}
#[cfg(unix)]
#[tokio::test]
async fn an_adopted_stdin_pipe_carries_a_line_to_the_child() {
let dir = tempfile::tempdir().unwrap();
let (mut child_end, daemon_end) = std::io::pipe().unwrap();
let daemon_end = tokio::net::unix::pipe::Sender::from_file(std::fs::File::from(
std::os::fd::OwnedFd::from(daemon_end),
))
.expect("the daemon's end of a stdin pipe is writable");
let (_proc, io) = TokioRunner::new()
.adopt(adopt_spec(&dir, Some(daemon_end), None))
.expect("the real runner must be able to adopt");
let (done, ack) = oneshot::channel();
io.to_stdin
.send(StdinWrite {
line: "whisper".to_string(),
done,
})
.await
.expect("an adopted sheep must still have a stdin pump");
timeout(PUMP_DEADLINE, ack)
.await
.expect("the write must be acknowledged")
.expect("the pump must outlive the write")
.expect("the write must succeed");
let mut buf = [0_u8; 8];
std::io::Read::read_exact(&mut child_end, &mut buf).expect("the child end must read");
assert_eq!(&buf, b"whisper\n");
}
#[cfg(unix)]
#[tokio::test]
async fn an_adopted_sheep_without_a_stdin_pipe_has_a_closed_channel() {
let dir = tempfile::tempdir().unwrap();
let (_proc, io) = TokioRunner::new()
.adopt(adopt_spec(&dir, None, None))
.expect("the real runner must be able to adopt");
assert!(io.to_stdin.is_closed());
}
#[cfg(unix)]
#[tokio::test]
async fn an_adopted_sheep_keeps_its_log_unstamped() {
let dir = tempfile::tempdir().unwrap();
let (mut child_out, out_pipe) = tokio::net::unix::pipe::pipe().unwrap();
let spec = AdoptSpec {
out_pipe: Some(out_pipe),
log_timestamps: false,
..adopt_spec(&dir, None, None)
};
let (_proc, mut io) = TokioRunner::new()
.adopt(spec)
.expect("the real runner must be able to adopt");
child_out.write_all(b"after the handover\n").await.unwrap();
timeout(PUMP_DEADLINE, io.logs.recv())
.await
.expect("the adopted pump must forward the line")
.expect("the pump must not end while its stream is open");
let (done, ack) = oneshot::channel();
io.log_ctl.send(LogCtl::Flush { done }).await.unwrap();
timeout(PUMP_DEADLINE, ack)
.await
.expect("a flush must be acknowledged")
.expect("the pump must answer")
.expect("the flush must succeed");
let written = fs::read_to_string(dir.path().join("out.log")).unwrap();
assert_eq!(written, "after the handover\n");
}
#[cfg(unix)]
#[tokio::test]
async fn a_report_names_the_shepherd_channel_it_was_told_about() {
let dir = tempfile::tempdir().unwrap();
let (daemon_end, _child_end) = std::os::unix::net::UnixStream::pair().unwrap();
let (_out_writer, out_reader) = tokio::io::duplex(STREAM_BUFFER);
let (logs_tx, _logs) = mpsc::channel(CHANNEL_CAPACITY);
let (ctl, ctl_rx) = mpsc::channel(CHANNEL_CAPACITY);
let pipes = PipeFds {
out: None,
err: None,
stdin: None,
channel: Some(daemon_end.as_raw_fd()),
};
spawn_log_pump(
Some(out_reader),
None::<DuplexStream>,
LogSink::Path(dir.path().join("out.log")),
LogSink::Path(dir.path().join("err.log")),
true,
logs_tx,
ctl_rx,
pipes,
);
let (done, ack) = oneshot::channel();
ctl.send(LogCtl::ReportFds { done }).await.unwrap();
let fds = timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
assert_eq!(fds.channel, Some(daemon_end.as_raw_fd()));
}
#[cfg(unix)]
#[tokio::test]
async fn an_adopted_shepherd_channel_carries_both_directions() {
use tokio::io::{AsyncBufReadExt as _, AsyncWriteExt as _};
let dir = tempfile::tempdir().unwrap();
let (daemon_end, child_end) = tokio::net::UnixStream::pair().unwrap();
let (child_read, mut child_write) = tokio::io::split(child_end);
let mut child = tokio::io::BufReader::new(child_read);
let (_proc, mut io) = TokioRunner::new()
.adopt(adopt_spec(&dir, None, Some(daemon_end)))
.expect("the real runner must be able to adopt");
io.to_child
.send(ShepherdMessage::Shutdown)
.await
.expect("an adopted sheep must still have a channel writer");
let mut line = String::new();
timeout(PUMP_DEADLINE, child.read_line(&mut line))
.await
.expect("the shepherd's message must reach the child's end")
.expect("the child end must read");
assert_eq!(line.trim_end(), r#"{"kind":"shutdown"}"#);
child_write
.write_all(b"{\"kind\":\"ready\"}\n")
.await
.unwrap();
let back = timeout(PUMP_DEADLINE, io.from_child.recv())
.await
.expect("an adopted sheep must still have a channel reader")
.expect("the reader must forward what the child said");
assert_eq!(back, ChildMessage::Ready);
}
#[cfg(unix)]
#[tokio::test]
async fn an_adopted_sheep_without_a_channel_has_closed_channel_ends() {
let dir = tempfile::tempdir().unwrap();
let (_proc, mut io) = TokioRunner::new()
.adopt(adopt_spec(&dir, None, None))
.expect("the real runner must be able to adopt");
assert!(io.to_child.is_closed());
assert!(
io.from_child.recv().await.is_none(),
"a sheep with no channel must report one that is over, not one that is quiet"
);
}
#[cfg(unix)]
fn adopt_spec(
dir: &tempfile::TempDir,
stdin_pipe: Option<tokio::net::unix::pipe::Sender>,
channel: Option<tokio::net::UnixStream>,
) -> AdoptSpec {
AdoptSpec {
pid: std::process::id(),
out_file: dir.path().join("out.log"),
err_file: dir.path().join("err.log"),
out_pipe: None,
err_pipe: None,
out_log: None,
err_log: None,
log_timestamps: true,
stdin_pipe,
channel,
reaper: Arc::new(crate::runner::AdoptedReaper::new()),
}
}
#[cfg(unix)]
fn child_spec(dir: &tempfile::TempDir, program: &str, args: &[&str], stdin: bool) -> SpawnSpec {
SpawnSpec {
name: "web".to_string(),
program: program.to_string(),
args: args.iter().map(|arg| (*arg).to_string()).collect(),
cwd: None,
env: BTreeMap::new(),
out_file: dir.path().join("out.log"),
err_file: dir.path().join("err.log"),
log_timestamps: true,
channel: false,
stdin,
credentials: None,
}
}
#[cfg(unix)]
#[tokio::test]
async fn a_spawn_reports_the_write_end_it_put_on_the_childs_stdin() {
use nix::fcntl::{FcntlArg, OFlag, fcntl};
let dir = tempfile::tempdir().unwrap();
let (_proc, io) = TokioRunner::new()
.spawn(&child_spec(&dir, "/bin/cat", &[], true))
.unwrap();
let (done, ack) = oneshot::channel();
io.log_ctl.send(LogCtl::ReportFds { done }).await.unwrap();
let fds = timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
let stdin = fds
.stdin
.expect("a sheep spawned with a stdin pipe carries its write end");
let flags = OFlag::from_bits_truncate(fcntl(stdin, FcntlArg::F_GETFL).unwrap());
assert_eq!(
flags & OFlag::O_ACCMODE,
OFlag::O_WRONLY,
"the number reported for stdin must be the end the daemon writes"
);
assert_ne!(Some(stdin), fds.out_pipe);
assert_ne!(Some(stdin), fds.err_pipe);
drop(io);
}
#[cfg(unix)]
#[tokio::test]
async fn a_spawn_without_stdin_reports_no_descriptor_for_it() {
let dir = tempfile::tempdir().unwrap();
let (mut proc, io) = TokioRunner::new()
.spawn(&child_spec(&dir, "/bin/sleep", &["30"], false))
.unwrap();
let (done, ack) = oneshot::channel();
io.log_ctl.send(LogCtl::ReportFds { done }).await.unwrap();
let fds = timeout(PUMP_DEADLINE, ack)
.await
.expect("a descriptor report must be acknowledged")
.expect("the pump must answer rather than drop the acknowledgement");
assert_eq!(fds.stdin, None);
proc.signal_process(OperatorSignal::Kill).unwrap();
drop(io);
}
fn preflight_spec(program: &str, cwd: Option<PathBuf>, path: Option<&str>) -> SpawnSpec {
let mut env = BTreeMap::new();
if let Some(path) = path {
env.insert("PATH".to_string(), path.to_string());
}
SpawnSpec {
name: "web".to_string(),
program: program.to_string(),
args: Vec::new(),
cwd,
env,
out_file: PathBuf::from("/dev/null"),
err_file: PathBuf::from("/dev/null"),
log_timestamps: true,
channel: false,
stdin: false,
credentials: None,
}
}
#[test]
fn a_program_that_is_really_there_is_nothing_to_report() {
let dir = tempfile::tempdir().unwrap();
let bin = dir.path().join("srv");
fs::write(&bin, "#!/bin/sh\n").unwrap();
let path_dir = dir.path().to_string_lossy().into_owned();
for spec in [
preflight_spec(&bin.to_string_lossy(), None, None),
preflight_spec("./srv", Some(dir.path().to_path_buf()), None),
preflight_spec("srv", None, Some(&path_dir)),
preflight_spec("srv", None, Some(&format!("/nonexistent:{path_dir}"))),
preflight_spec("./srv", None, None),
preflight_spec("srv", None, None),
preflight_spec("srv", None, Some("")),
preflight_spec("", None, None),
] {
assert_eq!(
what_exec_will_find(&spec),
Preflight::Unknown,
"reported something about a spec it cannot be certain about: {spec:?}"
);
}
}
#[test]
fn an_absent_path_is_impossible_and_names_the_path_that_was_tried() {
let dir = tempfile::tempdir().unwrap();
assert_eq!(
what_exec_will_find(&preflight_spec(
"./proto-enum-api",
Some(dir.path().to_path_buf()),
None,
)),
Preflight::Impossible(format!(
"no such file: {}",
dir.path().join("./proto-enum-api").display()
)),
);
let absent = if cfg!(windows) {
r"C:\nonexistent\srv"
} else {
"/nonexistent/srv"
};
assert_eq!(
what_exec_will_find(&preflight_spec(absent, None, None)),
Preflight::Impossible(format!("no such file: {absent}")),
);
}
#[test]
fn a_filesystem_error_that_is_not_absence_is_never_impossible() {
let dir = tempfile::tempdir().unwrap();
#[cfg(unix)]
let unreadable = {
let wall = dir.path().join("wall");
fs::write(&wall, "not a directory").unwrap();
wall.join("srv")
};
#[cfg(windows)]
let unreadable = dir.path().join("no<such<name");
let kind = fs::metadata(&unreadable).unwrap_err().kind();
assert_ne!(
kind,
io::ErrorKind::NotFound,
"the provocation must be an error OTHER than not-found, or this \
case proves nothing: {kind:?}"
);
assert_eq!(
what_exec_will_find(&preflight_spec(&unreadable.to_string_lossy(), None, None)),
Preflight::Unknown,
"a path shep could not read is a suspicion, not a certainty, and \
must never refuse a batch"
);
#[cfg(unix)]
{
if nix::unistd::Uid::effective().is_root() {
return;
}
let locked = dir.path().join("locked");
fs::create_dir(&locked).unwrap();
fs::set_permissions(&locked, fs::Permissions::from_mode(0o000)).unwrap();
let behind_the_wall = locked.join("srv");
let observed = fs::metadata(&behind_the_wall)
.map(|_| ())
.map_err(|e| e.kind());
let verdict = what_exec_will_find(&preflight_spec(
&behind_the_wall.to_string_lossy(),
None,
None,
));
fs::set_permissions(&locked, fs::Permissions::from_mode(0o755)).unwrap();
assert_eq!(
observed,
Err(io::ErrorKind::PermissionDenied),
"the chmod must actually bite, or the assertion below is vacuous"
);
assert_eq!(
verdict,
Preflight::Unknown,
"a permission error on the way to the program must not take the rest \
of the flock down with it"
);
}
}
#[test]
fn a_bare_command_off_the_path_is_only_ever_doubtful() {
let found = what_exec_will_find(&preflight_spec("node", None, Some("/nonexistent")));
assert_eq!(
found,
Preflight::Doubtful("`node` is not on the shepherd's PATH (/nonexistent)".to_string()),
);
assert!(
!matches!(found, Preflight::Impossible(_)),
"a claim about an environment must never refuse a batch"
);
}
#[test]
fn a_long_path_is_summarised_and_a_startup_units_own_path_is_not() {
let sep = PATH_LIST_SEPARATOR;
let join = |entries: &[&str]| entries.join(&sep.to_string());
let fallback = join(&["/usr/local/bin", "/usr/bin", "/bin"]);
assert_eq!(
summarise_path(&fallback),
fallback,
"the PATH a unit actually gets must print in full"
);
let long = join(&["/a", "/b", "/c", "/d", "/e", "/f"]);
assert_eq!(
summarise_path(&long),
format!("{} and 2 more entries", join(&["/a", "/b", "/c", "/d"]))
);
let capped = join(&["/a", "/b", "/c", "/d"]);
assert_eq!(summarise_path(&capped), capped);
}
#[cfg(unix)]
#[test]
fn a_zero_pid_is_refused_before_it_can_reach_the_daemons_own_group() {
let err = signal_group(0, Signal::SIGCONT).unwrap_err();
assert_eq!(
err.to_string(),
"signal delivery failed: pid 0 is not a signallable process id"
);
}
#[cfg(unix)]
#[test]
fn a_pid_above_i32_max_is_refused_as_not_signallable() {
let err = signallable_pid(u32::MAX).unwrap_err();
assert_eq!(
err,
RunnerError::SignalFailed("pid 4294967295 is not a signallable process id".to_string())
);
assert_eq!(
err.to_string(),
"signal delivery failed: pid 4294967295 is not a signallable process id"
);
}
#[cfg(unix)]
#[test]
fn a_signallable_pid_converts_unchanged() {
assert_eq!(signallable_pid(1), Ok(1));
assert_eq!(signallable_pid(i32::MAX as u32), Ok(i32::MAX));
}