use std::os::fd::OwnedFd;
use super::super::log::Log;
use super::{kill, reap};
use crate::os_process::linux::fork::{NewChild, Path};
fn not_a_process() -> NewChild {
let not_a_pidfd: OwnedFd = std::fs::File::open("/dev/null").unwrap().into();
NewChild {
pid: 0,
pidfd: not_a_pidfd,
path: Path::Clone3,
}
}
fn collected_process() -> NewChild {
let mut command = std::process::Command::new("true");
let mut child = crate::test_spawn::spawn(&mut command).expect("true starts");
let pid = rustix::process::Pid::from_raw(child.id() as i32).unwrap();
let pidfd = rustix::process::pidfd_open(pid, rustix::process::PidfdFlags::empty()).unwrap();
child.wait().unwrap();
NewChild {
pid: pid.as_raw_nonzero().get(),
pidfd,
path: Path::Clone3,
}
}
#[skuld::test]
fn a_kill_of_a_collected_process_is_expected() {
kill(&collected_process(), &Log::new(None));
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "pidfd_send_signal(SIGKILL)")]
fn a_kill_that_fails_otherwise_is_a_bug() {
kill(¬_a_process(), &Log::new(None));
}
#[cfg(not(debug_assertions))]
#[skuld::test]
fn a_kill_that_fails_otherwise_is_logged_and_goes_on() {
kill(¬_a_process(), &Log::new(None));
}
#[skuld::test]
fn a_status_collected_by_someone_else_is_lost_not_a_bug() {
assert_eq!(reap(&collected_process(), false, &Log::new(None)), None);
}
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "waitid on the program's pidfd")]
fn a_wait_that_fails_other_than_echild_is_a_bug() {
reap(¬_a_process(), false, &Log::new(None));
}
#[cfg(not(debug_assertions))]
#[skuld::test]
fn a_wait_that_fails_other_than_echild_is_logged_and_lost() {
assert_eq!(reap(¬_a_process(), false, &Log::new(None)), None);
}