use std::time::Duration;
use crate::error::Error;
use crate::identity::{Liveness, ProcessId};
use crate::test_child::{wait_until_zombie, TERM_SURVIVOR_TREE_ARGV};
use crate::{ContainMode, Containment, Recursive};
const DROP_WARN: &str = "Child::drop: contained-tree teardown did not fully succeed";
fn survivor_tree() -> (crate::Child, crate::PipeWriter, Vec<ProcessId>) {
tree_of(TERM_SURVIVOR_TREE_ARGV)
}
fn tree_of(argv: &[&str]) -> (crate::Child, crate::PipeWriter, Vec<ProcessId>) {
let (child, stdin, stdout) = crate::test_child::ready_tree(argv, |cmd| {
cmd.contain_with(ContainMode::TreeWalk);
});
drop(stdout);
assert_eq!(child.containment(), Containment::TreeWalk);
let survivors = child
.id()
.expect("the root's identity")
.children(Recursive::Yes)
.expect("the root's descendants");
assert!(!survivors.is_empty(), "the subshell is a descendant of the root");
for survivor in &survivors {
assert_eq!(survivor.is_alive(), Liveness::Alive, "premise: {survivor:?} runs");
}
(child, stdin, survivors)
}
fn tree_with_exited_root() -> (crate::Child, crate::PipeWriter, Vec<ProcessId>) {
let (child, stdin, survivors) = survivor_tree();
child.kill().expect("kill the root alone");
wait_until_zombie(child.pid());
for survivor in &survivors {
assert_eq!(survivor.is_alive(), Liveness::Alive, "{survivor:?} outlives the root");
}
(child, stdin, survivors)
}
#[skuld::test]
fn kill_tree_over_an_exited_root_is_unassessable_and_names_why() {
let (child, stdin, _survivors) = tree_with_exited_root();
let err = child
.kill_tree()
.expect_err("a walk from an exited root finds no member");
let Error::Unassessable { detail, .. } = &err else {
panic!("expected Unassessable, got {err:?}");
};
assert!(
detail.contains(&format!("pid {}", child.pid()))
&& detail.contains("has exited")
&& detail.contains("reparented")
&& detail.contains("no walk ran"),
"{detail}"
);
drop(stdin);
child.wait().expect("reap the root");
}
#[skuld::test]
fn dropping_a_tree_walk_child_whose_root_exited_warns_once_naming_the_pid() {
crate::log_capture::install();
let (child, stdin, _survivors) = tree_with_exited_root();
let pid = child.pid();
let mark = crate::log_capture::mark();
drop(child);
let warns = crate::log_capture::records_since_on_current_thread(mark, DROP_WARN);
assert_eq!(warns.len(), 1, "exactly one warn, got {warns:?}");
let (level, text) = &warns[0];
assert_eq!(*level, log::Level::Warn);
assert!(text.contains(&format!("pid {pid}")), "the warn names the root: {text}");
drop(stdin);
}
fn assert_sigtermed(status: std::process::ExitStatus) {
use std::os::unix::process::ExitStatusExt as _;
assert_eq!(status.signal(), Some(libc::SIGTERM), "{status:?}");
}
fn drop_warns(child: crate::Child) -> Vec<String> {
crate::log_capture::install();
let mark = crate::log_capture::mark();
drop(child);
crate::log_capture::warns_since(mark, "Child::drop")
}
#[skuld::test]
fn graceful_shutdown_tree_kills_the_members_its_sigterm_walk_found_after_the_root_exits() {
let (child, stdin, survivors) = survivor_tree();
let status = child
.graceful_shutdown_tree(Duration::from_secs(30))
.expect("every member its walk found is killed");
assert_sigtermed(status);
for survivor in &survivors {
assert_eq!(
survivor.is_alive(),
Liveness::Dead,
"{survivor:?} ignored the SIGTERM and must be killed by identity"
);
}
assert_eq!(drop_warns(child), Vec::<String>::new());
drop(stdin);
}
#[skuld::test]
fn graceful_shutdown_tree_kills_a_member_reparented_while_the_root_outlives_the_grace() {
let (child, stdin, members) = tree_of(crate::test_child::ROOT_OUTLIVING_THE_GRACE_ARGV);
let reparented = crate::test_child::wait_for_the_middle_to_exit_after_the_term(child.id().expect("the root"));
let status = child
.graceful_shutdown_tree(Duration::ZERO)
.expect("every member the walk reached is killed");
reparented.assert_fired();
std::os::unix::process::ExitStatusExt::signal(&status)
.filter(|signal| *signal == libc::SIGKILL)
.unwrap_or_else(|| panic!("the sweep killed the live root: {status:?}"));
for member in &members {
assert_eq!(
member.is_alive(),
Liveness::Dead,
"{member:?} is killed or ended by the SIGTERM"
);
}
assert_eq!(drop_warns(child), Vec::<String>::new());
drop(stdin);
}
#[skuld::test]
fn graceful_shutdown_tree_of_a_tree_that_ends_on_the_sigterm_is_ok_with_the_roots_status() {
let (child, stdin, members) = tree_of(crate::test_child::CAT_TREE_ARGV);
let status = child
.graceful_shutdown_tree(Duration::from_secs(30))
.expect("the tree ended on the SIGTERM");
assert_sigtermed(status);
for member in &members {
assert_eq!(member.is_alive(), Liveness::Dead, "{member:?} ends on the SIGTERM");
}
assert_eq!(drop_warns(child), Vec::<String>::new());
drop(stdin);
}
#[skuld::test]
fn graceful_shutdown_tree_of_a_root_with_no_descendants_is_ok_with_its_status() {
let (child, stdin) = crate::test_child::held_treewalk_blocker();
let status = child
.graceful_shutdown_tree(Duration::from_secs(30))
.expect("the root ended on the SIGTERM");
assert_sigtermed(status);
assert_eq!(drop_warns(child), Vec::<String>::new());
drop(stdin);
}
#[skuld::test]
fn kill_tree_after_a_separate_terminate_tree_kills_the_held_members_and_is_unassessable() {
let (child, stdin, survivors) = survivor_tree();
child.terminate_tree().expect("the walk found the whole tree");
wait_until_zombie(child.pid());
let err = child.kill_tree().expect_err("the held members are all it can name");
assert!(matches!(err, Error::Unassessable { .. }), "{err:?}");
for survivor in &survivors {
assert_eq!(
survivor.is_alive(),
Liveness::Dead,
"{survivor:?} is killed by identity"
);
}
drop(stdin);
child.wait().expect("reap the root");
}
#[skuld::test]
fn a_retry_after_an_unwalked_kill_tree_over_a_running_root_reaches_what_a_held_member_forked() {
use crate::containment::dispatch::{Keeping, TreeKillOutcome};
use crate::containment::treewalk::Listing;
let (child, stdin, survivors) = survivor_tree();
let root = child.id().expect("the root's identity");
let [subshell] = root.children(Recursive::No).expect("the root's child")[..] else {
panic!("the root has one child, the subshell");
};
let leaf = *survivors.iter().find(|member| **member != subshell).expect("the leaf");
child.tree.hold_walked(
Listing {
members: vec![subshell],
complete: true,
},
None,
);
let unwalked = Error::Unassessable {
detail: "the process table could not be read".into(),
source: None,
};
let settled = child.tree.settle_for_kill_tree(
TreeKillOutcome::Unwalked(unwalked),
false,
Keeping::ForWalkFromRunningRoot,
);
assert!(
matches!(settled.answer, Err(Error::Unassessable { .. })),
"{:?}",
settled.answer
);
assert_eq!(settled.killed_again, [], "the held subshell is kept for the retry");
assert_eq!(subshell.is_alive(), Liveness::Alive, "the held subshell runs on");
child
.kill_tree()
.expect("the walk from the running root reaches the whole tree");
for member in [subshell, leaf] {
assert_eq!(
member.is_alive(),
Liveness::Dead,
"{member:?} is under the root, and killed"
);
}
drop(stdin);
child.wait().expect("reap the root");
}
#[skuld::test]
fn dropping_a_child_after_a_separate_terminate_tree_and_the_roots_exit_warns_once() {
let (child, stdin, _survivors) = survivor_tree();
child.terminate_tree().expect("the walk found the whole tree");
wait_until_zombie(child.pid());
assert_eq!(drop_warns(child).len(), 1);
drop(stdin);
}
#[skuld::test]
fn terminate_tree_over_an_exited_root_with_an_empty_walk_is_unassessable_and_sends_nothing() {
let (child, stdin) = crate::test_child::held_treewalk_blocker();
child.kill().expect("kill the root");
wait_until_zombie(child.pid());
let sends = crate::send_log::Capture::start();
let err = child
.terminate_tree()
.expect_err("a walk from an exited root finds no member");
assert!(matches!(err, Error::Unassessable { .. }), "{err:?}");
assert_eq!(sends.entries(), vec![], "nothing was sent");
drop(stdin);
child.wait().expect("reap the root");
}
#[skuld::test]
fn graceful_shutdown_tree_over_an_exited_root_kills_the_held_members() {
let (child, stdin, survivors) = survivor_tree();
child.terminate_tree().expect("the walk found the whole tree");
wait_until_zombie(child.pid());
let err = child
.graceful_shutdown_tree(Duration::from_secs(30))
.expect_err("a root that exited before the call cannot vouch for the tree");
assert!(matches!(err, Error::Unassessable { .. }), "{err:?}");
for survivor in &survivors {
assert_eq!(
survivor.is_alive(),
Liveness::Dead,
"{survivor:?} is killed by identity"
);
}
drop(stdin);
}