use crate::containment::dispatch::TreeKillOutcome;
use crate::containment::fake::FakeContains;
use crate::containment::treewalk::Walker;
use crate::containment::{Containment, Mechanism, Tree};
use crate::error::Error;
#[cfg(target_os = "linux")]
use crate::test_groups::cgroup;
#[skuld::test]
fn a_tree_reports_the_containment_of_what_holds_it() {
assert_eq!(Tree::none().containment(), Containment::None);
assert_eq!(Tree::delegated().containment(), Containment::Delegated);
let walk = Tree::contained(Mechanism::Walk(Walker::over(
crate::identity::ProcessId::current(),
crate::identity::HostKernel,
)));
assert_eq!(walk.containment(), Containment::TreeWalk);
#[cfg(unix)]
{
use crate::containment::unix::Group;
let group = Tree::contained(Mechanism::Group(Group::process_group(1)));
assert_eq!(group.containment(), Containment::ProcessGroup);
let session = Tree::contained(Mechanism::Group(Group::session(1)));
assert_eq!(session.containment(), Containment::Session);
}
#[cfg(target_os = "linux")]
{
let leaf = crate::containment::cgroup::CgroupLeaf::placeholder_for_test();
assert_eq!(
Tree::contained(Mechanism::Cgroup(leaf)).containment(),
Containment::CgroupV2
);
}
#[cfg(windows)]
{
let job = crate::containment::windows::JobHandle::create_empty_for_test();
assert_eq!(
Tree::contained(Mechanism::Job(job)).containment(),
Containment::JobObject
);
}
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
#[skuld::test]
fn only_a_cgroup_leaf_has_awaitable_release_steps() {
use crate::containment::mechanism::Contains;
use crate::containment::unix::Group;
let (fake, _) = FakeContains::new(Containment::ProcessGroup, false);
let others = [
Mechanism::Walk(Walker::over(
crate::identity::ProcessId::current(),
crate::identity::HostKernel,
)),
Mechanism::Group(Group::process_group(1)),
Mechanism::Fake(fake),
];
for other in &others {
assert!(other.awaitable_release().is_none(), "{other:?}");
}
let leaf = Mechanism::Cgroup(crate::containment::cgroup::CgroupLeaf::placeholder_for_test());
assert!(leaf.awaitable_release().is_some());
}
#[skuld::test]
fn a_consumer_drives_a_tree_only_through_what_its_mechanism_answers() {
let (fake, sends) = FakeContains::new(Containment::ProcessGroup, true);
#[cfg_attr(not(unix), allow(unused_mut, reason = "only a Unix tree is released"))]
let mut tree = Tree::contained(Mechanism::Fake(fake));
assert_eq!(tree.containment(), Containment::ProcessGroup);
tree.require_contained().expect("a contained tree is actionable");
assert!(matches!(tree.kill(None).outcome(), TreeKillOutcome::Ran(Ok(()))));
assert_eq!(*sends.lock().unwrap(), ["kill"]);
assert!(tree.drain_edge().is_none(), "the fake states no drain edge");
let err = tree.wait_drained(None).expect_err("no edge to wait on");
assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");
#[cfg(unix)]
assert!(tree.would_reach(1, None).expect("stated"));
tree.disarm().expect("nothing to neutralize");
#[cfg(unix)]
tree.release().wait();
#[cfg(unix)]
assert_eq!(*sends.lock().unwrap(), ["kill", "disarm", "release"]);
#[cfg(not(unix))]
assert_eq!(*sends.lock().unwrap(), ["kill", "disarm"]);
}
#[cfg(unix)]
#[skuld::test]
fn a_tree_reaches_only_what_its_mechanism_says_it_reaches() {
use crate::child::owned::teardown::Reach;
use crate::os_process::fake::FakeProcess;
use crate::test_child::over_fake::child_over;
let child = child_over(&FakeProcess::new([]));
for tree in [Tree::none(), Tree::delegated()] {
assert!(!tree.would_reach(1, None).expect("a tree with no mechanism answers"));
assert!(!tree.reached(&child.proc).expect("a tree with no mechanism answers"));
}
let (fake, _) = FakeContains::new(Containment::ProcessGroup, false);
let tree = Tree::contained(Mechanism::Fake(fake));
assert!(!tree.would_reach(1, None).expect("stated"));
assert!(!tree.reached(&child.proc).expect("stated"));
}
#[cfg(unix)]
#[skuld::test]
fn a_teardown_names_its_tree_as_it_always_did() {
assert_eq!(Tree::none().teardown_subject(), "None");
assert_eq!(Tree::delegated().teardown_subject(), "Delegated");
let id = crate::identity::ProcessId::current();
let walk = Tree::contained(Mechanism::Walk(Walker::over(id, crate::identity::HostKernel)));
assert_eq!(walk.teardown_subject(), format!("TreeWalk({id:?})"));
{
use crate::containment::unix::Group;
let group = Tree::contained(Mechanism::Group(Group::process_group(123)));
assert_eq!(group.teardown_subject(), "ProcessGroup(123)");
let session = Tree::contained(Mechanism::Group(Group::session(123)));
assert_eq!(session.teardown_subject(), "ProcessGroup(123)");
}
#[cfg(target_os = "linux")]
{
let leaf = crate::containment::cgroup::CgroupLeaf::placeholder_for_test();
let path = leaf.path().display().to_string();
let leaf = Tree::contained(Mechanism::Cgroup(leaf));
assert_eq!(leaf.teardown_subject(), format!("cgroup leaf {path}"));
}
}
#[cfg(unix)]
#[skuld::test]
fn a_released_tree_keeps_what_its_kills_settled() {
let (fake, _) = FakeContains::new(Containment::ProcessGroup, false);
let mut tree = Tree::contained(Mechanism::Fake(fake));
tree.settle(TreeKillOutcome::Ran(Ok(())), true)
.answer
.expect("a kill that left nothing");
assert!(tree.is_killed());
tree.release().wait();
assert!(tree.is_killed(), "the release reset the settlement");
}
#[cfg(unix)]
#[skuld::test]
fn a_released_tree_keeps_its_kind() {
use crate::containment::dispatch::Unkilled;
use crate::containment::unix::Group;
let (fake, _) = FakeContains::new(Containment::Session, false);
let trees = [
(
Tree::contained(Mechanism::Group(Group::process_group(1))),
Containment::ProcessGroup,
),
(Tree::contained(Mechanism::Fake(fake)), Containment::Session),
];
for (mut tree, kind) in trees {
tree.release().wait();
assert_eq!(tree.containment(), kind);
tree.settle(TreeKillOutcome::Ran(Ok(())), true)
.answer
.expect("a kill that left nothing");
}
let mut walk = Tree::contained(Mechanism::Walk(Walker::over(
crate::identity::ProcessId::current(),
crate::identity::HostKernel,
)));
let left_running = || {
TreeKillOutcome::Ran(Err(Unkilled {
members: vec![Error::MemberNotKilled {
pid: 8,
source: std::sync::Arc::new(Error::Unassessable {
detail: "its identity could not be read".into(),
source: None,
}),
}],
root: None,
resolved: Vec::new(),
}))
};
walk.settle(left_running(), true)
.answer
.expect_err("a member is running");
walk.release().wait();
assert_eq!(walk.containment(), Containment::TreeWalk);
walk.settle(TreeKillOutcome::Ran(Ok(())), true)
.answer
.expect_err("the walk still holds the member it left running");
}
#[cfg(unix)]
#[skuld::test]
fn a_released_tree_is_not_actionable_and_has_no_drain_edge() {
use crate::containment::unix::Group;
let trees = [
Tree::contained(Mechanism::Group(Group::process_group(1))),
Tree::contained(Mechanism::Walk(Walker::over(
crate::identity::ProcessId::current(),
crate::identity::HostKernel,
))),
];
for mut tree in trees {
tree.release().wait();
let err = tree.require_contained().expect_err("a released tree is not actionable");
assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");
assert!(tree.drain_edge().is_none());
}
}
#[cfg(all(unix, debug_assertions))]
#[skuld::test]
fn a_send_on_a_released_tree_that_kept_its_mechanism_asserts() {
use std::panic::{catch_unwind, AssertUnwindSafe};
use crate::containment::unix::Group;
let mut tree = Tree::contained(Mechanism::Group(Group::process_group(1)));
tree.release().wait();
let asserts = |send: &dyn Fn()| catch_unwind(AssertUnwindSafe(send)).is_err();
assert!(asserts(&|| _ = tree.kill(None)), "kill did not assert");
assert!(
asserts(&|| _ = tree.terminate(1, None, None, crate::containment::TreeStop::default())),
"terminate did not assert"
);
assert!(asserts(&|| _ = tree.would_reach(1, None)), "would_reach did not assert");
assert!(asserts(&|| _ = tree.disarm()), "disarm did not assert");
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_released_cgroup_leaf_leaves_a_tree_that_still_reports_and_settles() {
let leaf = crate::containment::cgroup::CgroupLeaf::placeholder_for_test();
let mut tree = Tree::contained(Mechanism::Cgroup(leaf));
tree.release().wait();
assert_eq!(tree.containment(), Containment::CgroupV2);
tree.settle(TreeKillOutcome::Ran(Ok(())), true)
.answer
.expect("a kill that left nothing");
#[cfg(feature = "tokio")]
::tokio::runtime::Builder::new_current_thread()
.build()
.expect("a runtime")
.block_on(tree.await_release_steps(1));
}
#[skuld::test]
fn a_child_over_a_stated_mechanism_reports_and_signals_through_it() {
use crate::os_process::fake::FakeProcess;
use crate::test_child::over_fake::child_over;
let (fake, sends) = FakeContains::new(Containment::Session, false);
let mut child = child_over(&FakeProcess::new([]));
child.tree = Tree::contained(Mechanism::Fake(fake));
assert_eq!(child.containment(), Containment::Session);
child.terminate_tree().expect("the fake's terminate succeeds");
assert_eq!(*sends.lock().unwrap(), ["terminate"]);
let err = child.wait_tree().expect_err("the fake states no drain edge");
assert!(matches!(err, Error::Unsupported { .. }), "{err:?}");
}
#[cfg(unix)]
fn terminate_sends_the_signal_it_is_given(mode: crate::ContainMode, gives: Containment) {
use crate::containment::{TerminateOutcome, TreeStop};
use crate::signal::Signal;
let (child, stdin, stdout) =
crate::test_child::ready_tree(crate::test_child::signal_reporting_tree_argv(), |cmd| {
cmd.contain_with(mode);
});
assert_eq!(child.containment(), gives);
{
let root = child.proc.hold(child.pid());
let (outcome, _) = child
.tree
.terminate(child.pid(), Some(&root), None, TreeStop::of(Signal::Usr1));
let TerminateOutcome::Sent {
result: Ok(()),
earlier,
} = outcome
else {
panic!("the terminate sent: {outcome:?}");
};
assert_eq!(earlier.as_deref(), Some("the SIGUSR1"));
}
drop(stdin);
assert_eq!(
crate::test_child::signal_reports_to_end(stdout),
["leaf SIGUSR1", "root SIGUSR1"]
);
child.wait().expect("the root reported the signal and exited");
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_cgroup_leaf_terminate_sends_the_signal_it_is_given(#[fixture(cgroup)] _group: &crate::test_groups::Group) {
terminate_sends_the_signal_it_is_given(crate::ContainMode::Strongest, Containment::CgroupV2);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_session_terminate_sends_the_signal_it_is_given() {
terminate_sends_the_signal_it_is_given(crate::ContainMode::Session, Containment::Session);
}
#[cfg(target_os = "linux")]
#[skuld::test]
fn a_tree_walk_terminate_sends_the_signal_it_is_given() {
terminate_sends_the_signal_it_is_given(crate::ContainMode::TreeWalk, Containment::TreeWalk);
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn an_fd_marker_terminate_sends_the_signal_it_is_given() {
terminate_sends_the_signal_it_is_given(crate::ContainMode::Strongest, Containment::FdMarker);
}