use std::time::Duration;
use super::ctrl_break_tests::{Call, CALLS};
use crate::error::Error;
use crate::stop::proxy::protocol::{CtrlEvent, ProxyAnswer};
use crate::test_child::ctrl_break::{
self, assert_acked_event, assert_in_our_console, assert_proxy_answered, connect_reporter, kill_release_reap,
report_done, spawn_member, spawn_uncontained_member, Handler, STATUS_CONTROL_C_EXIT,
};
use crate::test_groups::{ctrl_proxy, Group};
use crate::ContainMode;
fn ending(call: Call, child: &crate::Child) -> std::process::ExitStatus {
let pid = child.pid();
let answered = move || assert_proxy_answered(pid, CtrlEvent::CtrlBreak, ProxyAnswer::Sent);
if call.escalates() {
let check = crate::graceful_hooks::at_checked(crate::graceful_hooks::HookPoint::AfterTerminate, answered);
let status = call.run(child, Duration::from_secs(30));
check.assert_fired();
status
.unwrap_or_else(|e| panic!("{call:?}: {e:?}"))
.expect("a graceful call answers a status")
} else {
call.run(child, Duration::ZERO)
.unwrap_or_else(|e| panic!("{call:?}: {e:?}"));
answered();
child.wait().expect("reap the root")
}
}
fn assert_default_stops_through_the_proxy(mode: ContainMode) {
for call in CALLS {
let (child, _sock, _) = spawn_member(Handler::Default, None, mode, |cmd| _ = cmd.no_window());
let status = ending(call, &child);
assert_eq!(
status.code(),
Some(STATUS_CONTROL_C_EXIT),
"{mode:?} {call:?}: the root must die to the break, got {status:?}"
);
}
}
#[skuld::test]
fn a_no_window_contained_root_is_stopped_through_the_proxy(#[fixture(ctrl_proxy)] _group: &Group) {
assert_default_stops_through_the_proxy(ContainMode::Strongest);
}
#[skuld::test]
fn a_treewalk_contained_root_is_stopped_through_the_proxy(#[fixture(ctrl_proxy)] _group: &Group) {
assert_default_stops_through_the_proxy(ContainMode::TreeWalk);
}
#[skuld::test]
fn an_uncontained_no_window_child_is_stopped_through_the_proxy(#[fixture(ctrl_proxy)] _group: &Group) {
let (child, mut sock) = spawn_uncontained_member(Handler::AckEvent, |cmd| _ = cmd.no_window());
child.terminate().expect("terminate through the proxy");
assert_proxy_answered(child.pid(), CtrlEvent::CtrlBreak, ProxyAnswer::Sent);
assert_acked_event(&mut sock, "the member", CtrlEvent::CtrlBreak);
kill_release_reap(&child, sock, false);
}
#[skuld::test]
fn a_child_without_a_group_is_refused_by_the_real_proxy_when_asked(#[fixture(ctrl_proxy)] _group: &Group) {
let (child, sock, _) = spawn_member(Handler::Ignore, None, ContainMode::Strongest, |cmd| {
_ = cmd.new_process_group(false);
cmd.graceful_stop(|target| crate::stop::send_console_event_via_proxy(target, CtrlEvent::CtrlBreak));
});
assert_in_our_console(child.pid(), "the member");
let err = child.terminate().expect_err("the child is on our console");
let Error::Unsupported { detail, .. } = &err else {
panic!("{err:?}")
};
assert!(detail.contains("Command::new_process_group"), "{detail}");
assert_proxy_answered(child.pid(), CtrlEvent::CtrlBreak, ProxyAnswer::ConsoleIsCallers);
kill_release_reap(&child, sock, false);
}
#[skuld::test]
fn a_detached_contained_root_is_refused_for_having_no_console(#[fixture(ctrl_proxy)] _group: &Group) {
for call in CALLS {
let (child, sock, _) = spawn_member(Handler::Default, None, ContainMode::Strongest, |cmd| _ = cmd.detached());
let err = call
.run(&child, Duration::ZERO)
.expect_err("a detached root has no console");
assert!(matches!(err, Error::Unsupported { .. }), "{call:?}: {err:?}");
assert_proxy_answered(child.pid(), CtrlEvent::CtrlBreak, ProxyAnswer::TargetHasNoConsole);
assert_eq!(
child.is_alive(),
crate::identity::Liveness::Alive,
"{call:?}: the call ended with the root running"
);
kill_release_reap(&child, sock, false);
}
}
const CONSOLE_LESS_TEST: &str = "child::graceful::ctrl_break_proxy_tests::fixture_console_less_default_reporter";
const CONSOLE_LESS_ADDR_ENV: &str = "COSCA_FIXTURE_CONSOLE_LESS_DEFAULT_ADDR";
#[skuld::test]
fn a_console_less_callers_default_reaches_its_contained_child(#[fixture(ctrl_proxy)] _group: &Group) {
ctrl_break::drive_reporter(CONSOLE_LESS_TEST, CONSOLE_LESS_ADDR_ENV, |cmd| _ = cmd.detached());
}
#[skuld::test]
fn fixture_console_less_default_reporter() {
let Some(mut report) = connect_reporter(CONSOLE_LESS_ADDR_ENV) else {
return;
};
ctrl_break::console_less::assert_no_console();
assert_default_stops_through_the_proxy(ContainMode::Strongest);
report_done(&mut report);
}