#![cfg(unix)]
use std::io::Read;
use std::os::unix::process::ExitStatusExt;
use std::time::Duration;
use crate::graceful_hooks::{at, release_at, HookPoint};
use crate::send_log::Capture;
use crate::test_child::foreign::{self, survivor_tree, tree_of};
use crate::test_child::{blocker, StdChild};
use crate::wait::fault;
fn spawn_term_ignoring_blocker() -> StdChild {
let mut child = StdChild::spawn(
std::process::Command::new("sh")
.args(["-c", "trap '' TERM; echo r; exec cat"])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped()),
);
let mut buf = [0u8; 1];
child
.stdout
.take()
.expect("piped stdout")
.read_exact(&mut buf)
.expect("readiness byte");
child
}
#[skuld::test]
fn foreign_graceful_lone_watch_error_still_escalates() {
let mut child = spawn_term_ignoring_blocker();
let p = crate::Process::open_pid(child.id()).expect("open").expect("running");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown(Duration::from_secs(30))
.expect_err("the watch error must surface");
assert!(
!fault::armed(),
"seam not consumed — the watch did not run on this thread"
);
assert!(matches!(err, crate::error::Error::Io(_)), "got {err:?}");
let status = child.wait().expect("reap");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"child must be force-killed despite the watch error, got {status:?}"
);
}
#[skuld::test]
fn foreign_graceful_tree_watch_error_still_sweeps() {
let mut child = spawn_term_ignoring_blocker();
let p = crate::Process::open_pid(child.id()).expect("open").expect("running");
fault::set_force_watch_error(true);
let err = p
.graceful_shutdown_tree(Duration::from_secs(30))
.expect_err("the watch error must surface");
assert!(
!fault::armed(),
"seam not consumed — the watch did not run on this thread"
);
assert!(matches!(err, crate::error::Error::Io(_)), "got {err:?}");
let status = child.wait().expect("reap");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"root must be swept despite the watch error, got {status:?}"
);
}
#[skuld::test]
fn foreign_terminate_tree_sigterms_the_root_and_its_descendant() {
let tree = foreign::blocked_tree();
let p = crate::Process::open_pid(tree.root.id())
.expect("open")
.expect("running");
let sends = Capture::start();
p.terminate_tree().expect("terminate_tree");
foreign::assert_tree_sigtermed(&sends, tree.root.id(), tree.grandchild);
tree.assert_died_of_sigterm();
}
#[skuld::test]
fn foreign_graceful_shutdown_tree_graceful_path() {
let tree = foreign::blocked_tree();
let (root, grandchild) = (tree.root.id(), tree.grandchild);
let p = crate::Process::open_pid(root).expect("open").expect("running");
let sends = Capture::start();
let _armed = at(HookPoint::AfterTerminate, move || {
foreign::assert_tree_sigtermed(&sends, root, grandchild)
});
p.graceful_shutdown_tree(Duration::from_secs(30))
.expect("graceful_shutdown_tree");
tree.assert_died_of_sigterm();
}
#[skuld::test]
fn foreign_graceful_shutdown_graceful_path() {
let mut child = blocker();
let p = crate::Process::open_pid(child.id()).expect("open").expect("running");
let stdin = child.stdin.take().expect("piped stdin");
let _release = release_at(HookPoint::AfterTerminate, stdin);
p.graceful_shutdown(Duration::from_secs(30)).expect("graceful_shutdown");
let status = child.wait().expect("reap");
assert_eq!(status.signal(), Some(libc::SIGTERM), "got {status:?}");
}
#[skuld::test]
fn foreign_graceful_shutdown_tree_kills_a_member_that_ignores_the_sigterm_after_the_root_exits() {
let mut tree = survivor_tree();
tree.process
.graceful_shutdown_tree(Duration::from_secs(30))
.expect("every member the walk found is killed");
for member in &tree.members {
assert_eq!(
member.is_alive(),
crate::identity::Liveness::Dead,
"{member:?} ignored the SIGTERM and is killed by identity"
);
}
let status = tree.child.wait().expect("reap the root");
assert_eq!(status.signal(), Some(libc::SIGTERM), "got {status:?}");
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn foreign_graceful_sweep_whose_walk_is_not_made_kills_the_members_its_sigterm_walk_held() {
use crate::identity::{FakeKernel, HostKernel};
let tree = survivor_tree();
let settling = tree.process.begin_graceful().expect("the SIGTERM walk");
crate::test_child::wait_until_zombie(tree.child.id());
let unlistable = FakeKernel::over(&HostKernel).on_pids(Err(libc::EIO));
let err = tree
.process
.kill_tree_settling("graceful_shutdown_tree", &unlistable, Some(&settling))
.expect_err("the walk was not made");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
for member in &tree.members {
assert_eq!(
member.is_alive(),
crate::identity::Liveness::Dead,
"{member:?} was held by the SIGTERM walk and is killed by identity"
);
}
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn foreign_graceful_sweep_whose_walk_is_not_made_over_a_live_root_kills_the_held_member_and_leaves_the_root() {
use crate::identity::{FakeKernel, HostKernel, Liveness};
let tree = tree_of(crate::test_child::ROOT_OUTLIVING_THE_GRACE_ARGV);
let middle = tree
.process
.id()
.children(crate::Recursive::No)
.expect("the root's children")
.pop()
.expect("the root's child");
let middle = crate::Process::open(middle)
.expect("open the middle")
.expect("the middle runs");
let settling = tree.process.begin_graceful().expect("the SIGTERM walk");
middle.wait().expect("the middle exits on the SIGTERM");
let running: Vec<_> = tree
.members
.iter()
.copied()
.filter(|member| member.is_alive() == Liveness::Alive)
.collect();
assert_eq!(running.len(), 1, "the member that ignores the SIGTERM runs, reparented");
let unlistable = FakeKernel::over(&HostKernel).on_pids(Err(libc::EIO));
let err = tree
.process
.kill_tree_settling("graceful_shutdown_tree", &unlistable, Some(&settling))
.expect_err("the walk was not made");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(
running[0].is_alive(),
Liveness::Dead,
"the reparented member was held by the SIGTERM walk and is killed by identity"
);
assert_eq!(tree.process.id().is_alive(), Liveness::Alive, "the root is not killed");
tree.process.kill().expect("end the root");
}
#[skuld::test]
fn foreign_graceful_shutdown_tree_kills_a_member_reparented_while_the_root_outlives_the_grace() {
let mut tree = tree_of(crate::test_child::ROOT_OUTLIVING_THE_GRACE_ARGV);
assert_eq!(tree.members.len(), 2, "the child and the member below it");
let reparented = crate::test_child::wait_for_the_middle_to_exit_after_the_term(tree.process.id());
tree.process
.graceful_shutdown_tree(Duration::ZERO)
.expect("the member its walk reached is killed by identity");
reparented.assert_fired();
for member in &tree.members {
assert_eq!(
member.is_alive(),
crate::identity::Liveness::Dead,
"{member:?} is killed or ended by the SIGTERM"
);
}
let status = tree.child.wait().expect("reap the root");
assert_eq!(
status.signal(),
Some(libc::SIGKILL),
"the sweep killed the live root: {status:?}"
);
}
#[skuld::test]
fn foreign_kill_tree_over_an_exited_unreaped_root_is_unassessable() {
let mut tree = survivor_tree();
tree.child.kill().expect("kill the root alone");
crate::test_child::wait_until_zombie(tree.child.id());
let err = tree
.process
.kill_tree()
.expect_err("a walk from an exited root finds no member");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
}
#[skuld::test]
fn foreign_terminate_tree_over_an_exited_root_with_an_empty_walk_is_unassessable_and_sends_nothing() {
let mut child = blocker();
let p = crate::Process::open_pid(child.id()).expect("open").expect("running");
child.kill().expect("kill the root");
crate::test_child::wait_until_zombie(child.id());
let sends = Capture::start();
let err = p
.terminate_tree()
.expect_err("a walk from an exited root finds no member");
assert!(matches!(err, crate::error::Error::Unassessable { .. }), "{err:?}");
assert_eq!(sends.entries(), vec![], "nothing was sent");
}