use super::{Skip, Why};
use crate::containment::dispatch::TreeKillOutcome;
use crate::containment::{Refusal, Tree};
const PID: u32 = 999_999;
fn skip(why: Why) -> Skip {
Skip {
action: "kill its process group".to_owned(),
why,
root_alone: false,
holders: None,
}
}
fn killed(settled: bool) -> Tree {
let tree = Tree::none();
if settled {
tree.settle(TreeKillOutcome::Ran(Ok(())), true)
.answer
.expect("a kill that left nothing");
}
assert_eq!(tree.is_killed(), settled);
tree
}
#[skuld::test]
fn a_skip_over_an_unsure_handle_answers_with_its_error_as_the_source() {
use super::SkipCall;
use crate::containment::dispatch::TreeSignal;
use crate::error::Error;
crate::log_capture::install();
let mark = crate::log_capture::mark();
let os_error = std::io::Error::from_raw_os_error(libc::EPERM).to_string();
let call = SkipCall {
op: "skip_cause_probe_7e1",
pid: PID,
signal: TreeSignal::Term,
earlier: None,
};
let why = Why::MayNameAnother(Refusal::Unsure(
PID,
std::sync::Arc::new(std::io::Error::from_raw_os_error(libc::EPERM)),
));
let answer = call
.answer(&killed(false), skip(why), None)
.answer
.expect_err("a skipped call is no success");
let Error::Unassessable { detail, .. } = &answer else {
panic!("got {answer:?}");
};
assert_eq!(crate::error::first_os_code(&answer), Some(libc::EPERM), "{answer:?}");
assert!(!detail.contains(&os_error), "{detail}");
let warns = crate::log_capture::warns_since(mark, "skip_cause_probe_7e1");
assert_eq!(warns.len(), 1, "{warns:?}");
assert!(warns[0].contains(&os_error), "{warns:?}");
}
#[skuld::test]
fn a_sweep_that_skipped_has_the_handles_error_as_its_source_unless_the_holders_failed() {
use crate::error::Error;
let os_error = std::io::Error::from_raw_os_error(libc::EPERM).to_string();
let why = || {
Why::MayNameAnother(Refusal::Unsure(
PID,
std::sync::Arc::new(std::io::Error::from_raw_os_error(libc::EPERM)),
))
};
let answer = skip(why()).sweep_error();
let Error::Unassessable { detail, .. } = &answer else {
panic!("got {answer:?}");
};
assert_eq!(crate::error::first_os_code(&answer), Some(libc::EPERM), "{answer:?}");
assert!(!detail.contains(&os_error), "{detail}");
let own = Error::Containment {
detail: "the holders' sweep failed".into(),
source: None,
};
let answer = Skip {
holders: Some(Err(own)),
..skip(why())
}
.sweep_error();
assert!(matches!(answer, Error::Containment { .. }), "{answer:?}");
assert_eq!(
crate::error::first_os_code(&answer),
Some(libc::EPERM),
"the skip's reason is below the holders' error: {answer:?}"
);
}
#[skuld::test]
fn a_skip_is_moot_only_over_a_killed_tree_after_the_own_reap() {
type Case = (&'static str, fn() -> Why, bool);
let whys: [Case; 6] = [
(
"own reap, pid gone",
|| Why::MayNameAnother(Refusal::ReapedPidGone(PID)),
true,
),
(
"own reap, pid unread",
|| Why::MayNameAnother(Refusal::ReapedPidUnread(PID)),
true,
),
(
"own reap, pid taken",
|| Why::MayNameAnother(Refusal::ReapedPidTaken(PID)),
false,
),
(
"reaped elsewhere",
|| Why::MayNameAnother(Refusal::ReapedElsewhere(PID)),
false,
),
(
"handle could not say",
|| {
Why::MayNameAnother(Refusal::Unsure(
PID,
std::sync::Arc::new(std::io::Error::other("forced")),
))
},
false,
),
(
"untrusted",
|| Why::Untrusted {
why: "forced".to_owned(),
cause: None,
},
false,
),
];
for (name, why, moot) in whys {
assert_eq!(skip(why()).is_moot(&killed(true)), moot, "{name}, tree killed");
assert!(!skip(why()).is_moot(&killed(false)), "{name}, tree never killed");
}
}
fn unsure(code: i32) -> Why {
Why::MayNameAnother(Refusal::Unsure(
PID,
std::sync::Arc::new(std::io::Error::from_raw_os_error(code)),
))
}
fn term_call(op: &'static str) -> super::SkipCall<'static> {
super::SkipCall {
op,
pid: PID,
signal: crate::containment::dispatch::TreeSignal::Term,
earlier: None,
}
}
fn codes_below(error: &crate::error::Error) -> Vec<Option<i32>> {
use std::error::Error as _;
let source = error.source().and_then(|s| s.downcast_ref::<crate::error::Error>());
let Some(crate::error::Error::Several(list)) = source else {
panic!("expected a Several below the variant: {error:?}");
};
list.iter()
.map(|member| crate::error::first_os_code(&**member))
.collect()
}
#[skuld::test]
fn a_skipped_call_whose_holders_and_root_both_failed_keeps_both_and_the_reason() {
use crate::error::Error;
let holders = Error::Containment {
detail: "the holders' sweep failed".into(),
source: None,
};
let root = Error::Unassessable {
detail: "the root's signal failed".into(),
source: Some(Error::io_cause(std::io::Error::from_raw_os_error(libc::EPERM))),
};
let answer = term_call("skip_peers_probe_a1")
.answer(
&killed(false),
Skip {
holders: Some(Err(holders)),
..skip(unsure(libc::EACCES))
},
Some(Err(root)),
)
.answer
.expect_err("a skipped call is no success");
let Error::Containment { detail, .. } = &answer else {
panic!("the holders' variant is kept: {answer:?}");
};
assert_eq!(detail, "the holders' sweep failed");
assert_eq!(codes_below(&answer), [Some(libc::EPERM), Some(libc::EACCES)]);
}
#[skuld::test]
fn a_skipped_calls_root_failure_takes_the_reason_as_a_peer() {
use crate::error::Error;
let root = Error::Unassessable {
detail: "the root's signal failed".into(),
source: Some(Error::io_cause(std::io::Error::from_raw_os_error(libc::EPERM))),
};
let answer = term_call("skip_peers_probe_a2")
.answer(&killed(false), skip(unsure(libc::EACCES)), Some(Err(root)))
.answer
.expect_err("a skipped call is no success");
assert!(matches!(&answer, Error::Unassessable { detail, .. } if detail == "the root's signal failed"));
assert_eq!(codes_below(&answer), [Some(libc::EPERM), Some(libc::EACCES)]);
}
#[skuld::test]
fn a_skipped_calls_io_failure_goes_below_the_unassessable_after_the_reason() {
use crate::error::Error;
let eperm = || Error::Io(std::io::Error::from_raw_os_error(libc::EPERM));
let answer = term_call("skip_peers_probe_a3")
.answer(&killed(false), skip(unsure(libc::EACCES)), Some(Err(eperm())))
.answer
.expect_err("a skipped call is no success");
assert!(matches!(&answer, Error::Unassessable { .. }), "{answer:?}");
assert_eq!(codes_below(&answer), [Some(libc::EACCES), Some(libc::EPERM)]);
let alone = term_call("skip_peers_probe_a4")
.answer(
&killed(false),
skip(Why::MayNameAnother(Refusal::ReapedElsewhere(PID))),
Some(Err(eperm())),
)
.answer
.expect_err("a skipped call is no success");
assert!(matches!(&alone, Error::Unassessable { .. }), "{alone:?}");
assert_eq!(crate::error::first_os_code(&alone), Some(libc::EPERM), "{alone:?}");
}
#[skuld::test]
fn an_untrusted_roots_cause_joins_the_failed_holders_sweep() {
use crate::error::Error;
let untrusted = Why::Untrusted {
why: "its state could not be read".to_owned(),
cause: Some(std::sync::Arc::new(std::io::Error::from_raw_os_error(libc::EACCES))),
};
let holders = Error::Containment {
detail: "the holders' sweep failed".into(),
source: Some(Error::io_cause(std::io::Error::from_raw_os_error(libc::EPERM))),
};
let answer = term_call("skip_peers_probe_b1")
.answer(
&killed(false),
Skip {
holders: Some(Err(holders)),
..skip(untrusted)
},
None,
)
.answer
.expect_err("a skipped call is no success");
assert!(matches!(&answer, Error::Containment { detail, .. } if detail == "the holders' sweep failed"));
assert_eq!(codes_below(&answer), [Some(libc::EPERM), Some(libc::EACCES)]);
}
#[skuld::test]
fn a_holders_only_kill_that_failed_takes_the_term_and_the_reason_as_peers() {
use crate::containment::dispatch::Unkilled;
use crate::error::Error;
let kill = Error::Containment {
detail: "a holder refused the kill".into(),
source: Some(Error::io_cause(std::io::Error::from_raw_os_error(libc::EPERM))),
};
let term_failed = || Error::Containment {
detail: "a holder refused the SIGTERM".into(),
source: Some(Error::io_cause(std::io::Error::from_raw_os_error(libc::EIO))),
};
let settled = term_call("skip_peers_probe_c1").holders_settled(
&killed(false),
Skip {
holders: Some(Err(term_failed())),
..skip(unsure(libc::EACCES))
},
TreeKillOutcome::Ran(Err(Unkilled::from(kill))),
);
assert!(matches!(&settled.answer, Error::Containment { detail, .. } if detail == "a holder refused the kill"));
assert_eq!(
codes_below(&settled.answer),
[Some(libc::EPERM), Some(libc::EIO), Some(libc::EACCES)]
);
let settled = term_call("skip_peers_probe_c2").holders_settled(
&killed(false),
Skip {
holders: Some(Err(term_failed())),
..skip(unsure(libc::EACCES))
},
TreeKillOutcome::Ran(Ok(())),
);
assert!(
matches!(&settled.answer, Error::Unassessable { .. }),
"{:?}",
settled.answer
);
assert_eq!(codes_below(&settled.answer), [Some(libc::EACCES), Some(libc::EIO)]);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_pidfd_shutdown_that_failed_takes_the_reason_as_a_peer() {
use crate::child::graceful::PidfdShutdown;
use crate::elevation::front::Gate;
use crate::error::Error;
let failed = |term: Error| {
PidfdShutdown::start(Gate::Open, || Err(term))
.expect("the gate is open")
.finish(
&term_call("skip_peers_probe_d1"),
&skip(unsure(libc::EACCES)),
None,
|| Gate::Open,
|| Ok(crate::signal::Sent::Delivered),
)
};
let answer = failed(Error::Unassessable {
detail: "the SIGTERM failed".into(),
source: Some(Error::io_cause(std::io::Error::from_raw_os_error(libc::EPERM))),
});
assert!(matches!(&answer, Error::Unassessable { detail, .. } if detail == "the SIGTERM failed"));
assert_eq!(codes_below(&answer), [Some(libc::EPERM), Some(libc::EACCES)]);
let answer = failed(Error::Io(std::io::Error::from_raw_os_error(libc::EPERM)));
assert!(matches!(&answer, Error::Unassessable { .. }), "{answer:?}");
assert_eq!(codes_below(&answer), [Some(libc::EACCES), Some(libc::EPERM)]);
}