use super::is_nested;
#[test]
fn is_nested_without_marker_is_false() {
assert!(!is_nested(false));
}
#[test]
fn is_nested_with_marker_is_true() {
assert!(is_nested(true));
}
#[cfg(unix)]
#[test]
fn unix_setup_for_session_selects_setsid() {
use super::{unix_setup_for, UnixSetup};
use crate::containment::ContainMode;
assert_eq!(unix_setup_for(Some(ContainMode::Session)), UnixSetup::Session);
}
#[cfg(unix)]
#[test]
fn unix_setup_for_strongest_selects_process_group() {
use super::{unix_setup_for, UnixSetup};
use crate::containment::ContainMode;
assert_eq!(unix_setup_for(Some(ContainMode::Strongest)), UnixSetup::ProcessGroup);
}
#[cfg(unix)]
#[test]
fn unix_setup_for_treewalk_selects_none() {
use super::{unix_setup_for, UnixSetup};
use crate::containment::ContainMode;
assert_eq!(unix_setup_for(Some(ContainMode::TreeWalk)), UnixSetup::None);
}
#[cfg(unix)]
#[test]
fn unix_setup_for_none_mode_selects_process_group() {
use super::{unix_setup_for, UnixSetup};
assert_eq!(unix_setup_for(None), UnixSetup::ProcessGroup);
}
#[test]
fn attached_is_actionable() {
use super::Attached;
assert!(!Attached::None.is_actionable()); assert!(!Attached::Delegated.is_actionable());
assert!(Attached::TreeWalk(crate::identity::ProcessId::current()).is_actionable());
#[cfg(unix)]
assert!(Attached::ProcessGroup(0).is_actionable());
#[cfg(target_os = "linux")]
assert!(Attached::Cgroup(crate::containment::cgroup::CgroupLeaf::placeholder_for_test()).is_actionable());
#[cfg(windows)]
assert!(Attached::JobObject(crate::containment::windows::JobHandle::create_empty_for_test()).is_actionable());
}
#[test]
fn nested_attach_is_delegated() {
use super::{attach, Attached, Prepared};
use crate::containment::{ContainMode, Containment};
fn spawn_trivial() -> std::process::Child {
#[cfg(unix)]
return std::process::Command::new("true").spawn().expect("spawn true");
#[cfg(windows)]
return std::process::Command::new("cmd")
.args(["/C", "exit"])
.spawn()
.expect("spawn cmd");
}
for mode in [ContainMode::Strongest, ContainMode::TreeWalk] {
let mut child = spawn_trivial();
let prepared = Prepared {
mode: Some(mode),
is_root: false, #[cfg(target_os = "linux")]
cgroup_leaf: None,
};
#[cfg(windows)]
let proc_handle = {
use std::os::windows::io::AsRawHandle;
child.as_raw_handle()
};
let (containment, attached) = attach(
child.id(),
#[cfg(windows)]
proc_handle,
prepared,
)
.expect("attach nested");
let _ = child.kill();
let _ = child.wait();
assert_eq!(
containment,
Containment::Delegated,
"nested member ({mode:?}) must report Containment::Delegated, got {containment:?}"
);
assert!(
matches!(attached, Attached::Delegated),
"nested member ({mode:?}) must be Attached::Delegated, got {attached:?}"
);
}
}
#[test]
fn sync_kill_tree_backstop_is_load_bearing() {
use super::super::treewalk::fault;
let mut cmd = crate::Command::new();
#[cfg(unix)]
cmd.args(["sleep", "30"]);
#[cfg(windows)]
cmd.args(["ping", "-n", "30", "127.0.0.1"]);
cmd.contain_with(crate::ContainMode::TreeWalk);
let child = cmd.spawn().expect("spawn");
fault::set_force_root_kill_noop(true);
let result = child.kill_tree();
assert!(
!fault::armed(),
"seam not consumed — hard_kill did not run on the arming thread"
);
result.expect("kill_tree via backstop");
let status = child.wait().expect("reap");
assert!(
!status.success(),
"the handle backstop must be what killed the root, got {status:?}"
);
}
#[cfg(feature = "tokio")]
#[tokio::test]
async fn async_kill_tree_backstop_is_load_bearing() {
use super::super::treewalk::fault;
let mut cmd = crate::tokio::Command::new();
#[cfg(unix)]
cmd.args(["sleep", "30"]);
#[cfg(windows)]
cmd.args(["ping", "-n", "30", "127.0.0.1"]);
cmd.contain_with(crate::ContainMode::TreeWalk);
let mut child = cmd.spawn().expect("spawn");
fault::set_force_root_kill_noop(true);
let result = child.kill_tree();
assert!(
!fault::armed(),
"seam not consumed — hard_kill did not run on the arming thread"
);
result.expect("kill_tree via backstop");
let status = child.wait().await.expect("reap");
assert!(
!status.success(),
"the handle backstop must be what killed the root, got {status:?}"
);
}