use crate::graceful_hooks::{at, HookPoint};
use crate::send_log::{Capture, GroupStep, Route};
use crate::test_child::Console;
pub(crate) fn zombie_group_root() -> crate::Child {
let (stdin, writer) = crate::test_child::held_writer_stdin();
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
cmd.stdin(stdin).expect("set stdin");
cmd.contain_with(crate::ContainMode::Session);
#[cfg(target_os = "macos")]
cmd.suppress_fd_marker();
let child = cmd.spawn().expect("spawn");
drop(writer);
crate::test_child::wait_until_zombie(child.pid());
child
}
pub(crate) fn reaped_after_the_look<T>(pid: u32, op: impl FnOnce() -> T) -> (Vec<GroupStep>, T) {
#[cfg(target_os = "linux")]
let _old_kernel = crate::containment::unix::fault::refuse_pidfd_group_flag();
let _hook = at(HookPoint::AfterRootLook, move || {
crate::test_child::reaped_elsewhere::reap_behind(pid);
});
let capture = Capture::start();
let answer = op();
(capture.group_steps(), answer)
}
#[track_caller]
pub(crate) fn assert_nothing_by_number(steps: &[GroupStep]) {
assert_eq!(
steps.last(),
Some(&GroupStep::Check),
"the last step is the check: {steps:?}"
);
assert!(
!steps.iter().any(|s| matches!(
s,
GroupStep::Group(Route::Number, _) | GroupStep::Leader(Route::Number, _) | GroupStep::Member(..)
)),
"nothing went by the group's number: {steps:?}"
);
}
#[track_caller]
pub(crate) fn assert_reaped_elsewhere(err: &crate::error::Error) {
match err {
crate::error::Error::Unassessable { detail, .. } => assert!(
detail.contains(crate::test_child::reaped_elsewhere::REAPED_ELSEWHERE),
"{detail}"
),
other => panic!("expected Unassessable, got {other:?}"),
}
}
#[skuld::test]
fn kill_tree_checks_the_root_again_before_a_send_by_number() {
let child = zombie_group_root();
let (steps, result) = reaped_after_the_look(child.pid(), || child.kill_tree());
assert_nothing_by_number(&steps);
assert_reaped_elsewhere(&result.expect_err("a root reaped elsewhere is not assessable"));
}
#[skuld::test]
fn terminate_tree_checks_the_root_again_before_a_send_by_number() {
let child = zombie_group_root();
let (steps, result) = reaped_after_the_look(child.pid(), || child.terminate_tree());
assert_nothing_by_number(&steps);
assert_reaped_elsewhere(&result.expect_err("a root reaped elsewhere is not assessable"));
}
#[skuld::test]
fn graceful_shutdown_tree_checks_the_root_again_before_a_send_by_number() {
let child = zombie_group_root();
let (steps, result) =
reaped_after_the_look(child.pid(), || child.graceful_shutdown_tree(std::time::Duration::ZERO));
assert_nothing_by_number(&steps);
assert_reaped_elsewhere(&result.expect_err("a root reaped elsewhere is not assessable"));
}
#[skuld::test]
fn a_drop_checks_the_root_again_before_a_send_by_number() {
let child = zombie_group_root();
let (steps, ()) = reaped_after_the_look(child.pid(), || drop(child));
assert_nothing_by_number(&steps);
}
#[skuld::test]
fn finish_elevated_checks_the_root_again_before_a_send_by_number() {
let child = zombie_group_root();
let (steps, failed) = reaped_after_the_look(child.pid(), || {
crate::child::spawn::finish_elevated(
child,
Err(crate::error::Error::Io(std::io::Error::other("no password"))),
)
});
assert_nothing_by_number(&steps);
assert!(failed.is_err(), "the password write failed");
}