use crate::containment::cgroup::fault;
use crate::error::Error;
use crate::test_child::Console;
use crate::test_groups::{cgroup, Group};
use crate::{ContainMode, Containment};
fn writes_marker(marker: &std::path::Path, mode: ContainMode) -> crate::Command {
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["sh", "-c", "touch \"$0\""]).arg(marker);
cmd.contain_with(mode);
cmd
}
struct Forced;
impl Drop for Forced {
fn drop(&mut self) {
fault::take_force_placement_write_result();
fault::take_force_leaf_open_failure();
}
}
#[derive(Debug, Clone, Copy)]
enum Step {
Leaf,
Placement,
}
impl Step {
fn force(self) -> Forced {
match self {
Step::Leaf => fault::set_force_leaf_open_failure(true),
Step::Placement => fault::set_force_placement_write_result(fault::ForcedWrite::Returns0),
}
Forced
}
}
fn assert_strongest_fails(step: Step) {
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("ran");
let _forced = step.force();
let spawned = writes_marker(&marker, ContainMode::Strongest).spawn();
let Err(error) = spawned else {
panic!("{step:?}: a failed leaf step must fail a Strongest spawn");
};
assert!(matches!(error, Error::Containment { .. }), "{step:?}: {error:?}");
assert!(error.fate().is_none(), "{step:?}: the program did not start: {error:?}");
let said = crate::error::Report(&error).to_string();
assert!(said.contains("cgroup"), "{step:?}: the error names the cgroup: {said}");
assert!(!marker.exists(), "{step:?}: the program ran");
}
fn assert_best_effort_degrades(step: Step) {
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("ran");
let _forced = step.force();
let child = writes_marker(&marker, ContainMode::BestEffort)
.spawn()
.unwrap_or_else(|e| panic!("{step:?}: BestEffort degrades: {e}"));
assert_eq!(child.containment(), Containment::ProcessGroup, "{step:?}");
child.wait().expect("wait");
assert!(marker.exists(), "{step:?}: the program ran");
}
#[skuld::test]
fn a_strongest_spawn_on_a_host_with_a_delegated_cgroup_gets_a_leaf(#[fixture(cgroup)] _group: &Group) {
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("ran");
let child = writes_marker(&marker, ContainMode::Strongest)
.spawn()
.unwrap_or_else(|e| panic!("the spawn failed on a host with a delegated cgroup: {e}"));
assert_eq!(child.containment(), Containment::CgroupV2);
child.wait().expect("wait");
assert!(marker.exists(), "the program ran");
}
#[skuld::test(labels = [])]
fn a_strongest_spawn_on_a_host_offering_no_usable_cgroup_gets_a_process_group() {
let root = tempfile::tempdir().expect("tempdir");
fault::set_force_own_cgroup("0::/not/here\n", root.path());
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("ran");
let child = writes_marker(&marker, ContainMode::Strongest)
.spawn()
.unwrap_or_else(|e| panic!("a host offering no cgroup is a process group, not an error: {e}"));
assert_eq!(child.containment(), Containment::ProcessGroup);
child.wait().expect("wait");
assert!(marker.exists(), "the program ran");
}
#[skuld::test]
fn strongest_refuses_an_unplaced_child_as_not_started_with_fds_0_and_2_closed(#[fixture(cgroup)] _group: &Group) {
use crate::test_own_process::{own_process, test_path};
use crate::test_spawn::spawn;
use crate::test_stdio::RestoreStdio;
let Some(done) = own_process(
test_path!(strongest_refuses_an_unplaced_child_as_not_started_with_fds_0_and_2_closed),
spawn,
) else {
return;
};
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("ran");
let stderr_file = dir.path().join("stderr");
let null = || crate::stdio::Stdio::from_file(std::fs::File::open("/dev/null").expect("open /dev/null"));
let mut cmd = writes_marker(&marker, ContainMode::Strongest);
cmd.stdin(null()).expect("set stdin");
cmd.stdout(null()).expect("set stdout");
cmd.stderr(crate::stdio::Stdio::from_file(
std::fs::File::create(&stderr_file).expect("create the stderr file"),
))
.expect("set stderr");
let _forced = Step::Placement.force();
let restore = RestoreStdio::close(&done, &[0, 2]);
let spawned = cmd.spawn();
drop(restore);
let Err(error) = spawned else {
panic!("a refused placement must fail a Strongest spawn");
};
assert!(matches!(error, Error::Containment { .. }), "not started: {error:?}");
assert!(error.fate().is_none(), "the program did not start: {error:?}");
assert!(!marker.exists(), "the program ran");
assert_eq!(
std::fs::read(&stderr_file).expect("read the stderr file"),
b"",
"the child wrote into its stderr"
);
}
#[skuld::test]
fn strongest_refuses_an_unplaced_child_as_not_started_when_another_party_reaps_it(#[fixture(cgroup)] _group: &Group) {
use crate::test_own_process::{own_process, test_path};
use crate::test_spawn::spawn;
let Some(_done) = own_process(
test_path!(strongest_refuses_an_unplaced_child_as_not_started_when_another_party_reaps_it),
spawn,
) else {
return;
};
unsafe { libc::signal(libc::SIGCHLD, libc::SIG_IGN) };
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("ran");
let _forced = Step::Placement.force();
let spawned = writes_marker(&marker, ContainMode::Strongest).spawn();
let Err(error) = spawned else {
panic!("a refused placement must fail a Strongest spawn");
};
assert!(matches!(error, Error::Containment { .. }), "not started: {error:?}");
assert!(error.fate().is_none(), "the program did not start: {error:?}");
let said = crate::error::Report(&error).to_string();
assert!(
said.contains("could not be placed"),
"the error names the placement: {said}"
);
assert!(!marker.exists(), "the program ran");
}
#[skuld::test]
fn strongest_fails_the_spawn_when_its_leaf_cannot_be_made(#[fixture(cgroup)] _group: &Group) {
assert_strongest_fails(Step::Leaf);
}
#[skuld::test]
fn strongest_fails_the_spawn_when_its_child_cannot_be_placed(#[fixture(cgroup)] _group: &Group) {
assert_strongest_fails(Step::Placement);
}
#[skuld::test]
fn a_missing_program_is_not_found_before_any_leaf_is_made(#[fixture(cgroup)] _group: &Group) {
let _forced = Step::Leaf.force();
let mut cmd = crate::test_child::command(Console::Hidden);
cmd.args(["cosca-strongest-no-such-program"])
.env("PATH", "/usr/bin:/bin");
cmd.contain_with(ContainMode::Strongest);
let spawned = cmd.spawn();
let Err(error) = spawned else {
panic!("a program found nowhere must fail the spawn");
};
assert!(
matches!(&error, Error::Io(e) if e.kind() == std::io::ErrorKind::NotFound),
"not found, not a cgroup refusal: {error:?}"
);
assert!(fault::take_force_leaf_open_failure(), "a leaf was made for it");
}
#[skuld::test]
fn best_effort_degrades_when_its_leaf_cannot_be_made(#[fixture(cgroup)] _group: &Group) {
assert_best_effort_degrades(Step::Leaf);
}
#[skuld::test]
fn best_effort_degrades_when_its_child_cannot_be_placed(#[fixture(cgroup)] _group: &Group) {
assert_best_effort_degrades(Step::Placement);
}