use super::adopt_fixtures::{
AdoptingRunner, adoptable_child, carried, carried_marked, counters, flock_until, respawnable,
without_handles,
};
use super::*;
use crate::supervisor::error::AdoptError;
use shep_core::config::NormalizeError;
mod disabled;
mod dogs;
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn an_adopted_sheep_that_was_still_starting_reaches_online() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let sup = SupervisorBuilder::new(AdoptingRunner, test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried("web", 7, Some(4242), |entry| {
let mut app = AppConfig::minimal("web", "./srv");
app.autorestart = false;
app.wait_ready = true;
entry.spec = normalize(app).unwrap();
entry.status = ProcStatus::Starting;
}))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
assert_eq!(
sup.list().await[0].status,
ProcStatus::Starting,
"the blob said this sheep was still starting, so the successor must agree"
);
flock_until(
&sup,
|info| info[0].status == ProcStatus::Online,
"an adopted sheep left `Starting` must still have a readiness wait over it",
)
.await;
}
#[cfg(unix)]
fn by_id(mut info: Vec<ProcessInfo>) -> Vec<ProcessInfo> {
info.sort_unstable_by_key(|row| row.id);
info
}
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn an_adopted_flock_keeps_its_pids_and_ids() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let sup = SupervisorBuilder::new(AdoptingRunner, test_paths(&dir), events)
.spawn_adopted(
vec![
without_handles(carried("web", 7, Some(4242), |_| {})),
without_handles(carried("api", 8, Some(4243), |_| {})),
],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
let info = by_id(sup.list().await);
assert_eq!(
info.iter().map(|row| row.id).collect::<Vec<_>>(),
vec![7, 8],
"the successor reissued ids instead of carrying them"
);
assert_eq!(
info.iter().map(|row| row.pid).collect::<Vec<_>>(),
vec![Some(4242), Some(4243)],
"a sheep whose pid moved was respawned"
);
}
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn an_adopted_dog_keeps_its_marker_and_stays_out_of_a_wildcard() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let sup = SupervisorBuilder::new(AdoptingRunner, test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried(
"log-rotate",
7,
Some(4242),
|entry| {
entry.dog = Some(DogSource::Adopted {
grants: None,
path: "/opt/bin/shep-log-rotate".to_string(),
channel: false,
events: Vec::new(),
});
},
))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
let info = sup.list().await;
assert_eq!(
info[0].dog,
Some(DogSource::Adopted {
grants: None,
path: "/opt/bin/shep-log-rotate".to_string(),
channel: false,
events: Vec::new(),
}),
"a dog adopted as an ordinary sheep is one `shep dogs` has lost"
);
let swept = tokio::time::timeout(Duration::from_secs(5), sup.stop(ProcessSelector::All))
.await
.expect("a wildcard over a flock of nothing but dogs must answer rather than hang");
assert!(
matches!(swept, Err(SupervisorError::NotFound)),
"`all` is the flock, not the kennel: {swept:?}"
);
}
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn an_adopted_sheep_keeps_its_counters_and_last_exit() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let sup = SupervisorBuilder::new(AdoptingRunner, test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried("web", 7, Some(4242), |entry| {
entry.restarts = 4;
entry.last_exit = Some(ExitInfo {
code: Some(2),
signal: None,
});
}))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
let info = sup.list().await;
assert_eq!(info[0].restarts, 4, "the restart count was reset");
assert_eq!(
info[0].last_exit,
Some(ExitInfo {
code: Some(2),
signal: None
}),
"the last exit was lost"
);
}
#[cfg(unix)]
#[tokio::test]
#[expect(
clippy::zombie_processes,
reason = "the adopted flock's reaper collects this status; a Child::wait would take it first"
)]
async fn an_adopted_sheeps_exit_flows_through_the_ordinary_path() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let child = std::process::Command::new("/bin/sh")
.args(["-c", "sleep 30"])
.spawn()
.expect("a test host can spawn a shell");
let pid = child.id();
let sup = SupervisorBuilder::new(TokioRunner::new(), test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried("web", 7, Some(pid), |_| {}))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
nix::sys::signal::kill(
nix::unistd::Pid::from_raw(i32::try_from(pid).unwrap()),
nix::sys::signal::Signal::SIGKILL,
)
.expect("the adopted child is signalable");
let info = flock_until(
&sup,
|info| info[0].status == ProcStatus::Stopped,
"an adopted sheep's exit reaches the actor",
)
.await;
assert_eq!(
info[0].last_exit,
Some(ExitInfo {
code: None,
signal: Some(9)
}),
"the exit must be recorded, not lost"
);
}
#[cfg(unix)]
#[tokio::test]
#[expect(
clippy::zombie_processes,
reason = "the adopted flock's reaper collects this status; a Child::wait would take it first"
)]
async fn a_carried_pending_delete_deregisters_on_the_next_exit() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let child = std::process::Command::new("/bin/sh")
.args(["-c", "sleep 30"])
.spawn()
.expect("a test host can spawn a shell");
let pid = child.id();
let sup = SupervisorBuilder::new(TokioRunner::new(), test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried_marked(
"web",
7,
Some(pid),
true,
None,
false,
|_| {},
))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
nix::sys::signal::kill(
nix::unistd::Pid::from_raw(i32::try_from(pid).unwrap()),
nix::sys::signal::Signal::SIGKILL,
)
.expect("the adopted child is signalable");
let info = flock_until(
&sup,
|info| info.is_empty(),
"a carried pending delete must deregister the sheep on its next exit, not merely \
stop it",
)
.await;
assert!(
info.is_empty(),
"the deleted sheep must be gone, not just stopped"
);
}
#[cfg(unix)]
#[tokio::test]
async fn a_carried_manual_stop_stops_the_sheep_instead_of_respawning_it() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let pid = adoptable_child("sleep 30");
let sup = SupervisorBuilder::new(TokioRunner::new(), test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried_marked(
"web",
7,
Some(pid),
false,
Some(PendingManual {
kind: ManualKind::Stop,
origin: CommandOrigin::Operator,
}),
false,
respawnable(|app| app.autorestart = true),
))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
let info = flock_until(
&sup,
|info| info[0].status == ProcStatus::Stopped,
"a carried manual stop must still stop the sheep after the exec",
)
.await;
assert_eq!(
info[0].restarts, 0,
"a stop must not come back as a respawn"
);
assert_eq!(
info[0].last_exit,
Some(ExitInfo {
code: None,
signal: Some(15)
}),
"the re-armed ladder starts at its polite rung, not at SIGKILL"
);
assert_eq!(info[0].pid, None, "a stopped sheep holds no pid");
}
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn an_adopted_flock_announces_none_of_the_tables_it_carried() {
let dir = tempfile::tempdir().unwrap();
let (events, mut rx) = crate::bus::test_bus(64);
let with_jobs = |name: &str| {
let mut app = AppConfig::minimal(name, "./srv");
app.autorestart = false;
let mut table = serde_json::Map::new();
table.insert("concurrency".to_string(), serde_json::json!(2));
app.dogs.insert("jobs".to_string(), DogTable::from(table));
normalize(app).unwrap()
};
let sup = SupervisorBuilder::new(AdoptingRunner, test_paths(&dir), events)
.spawn_adopted(
vec![without_handles(carried("web", 7, Some(4242), |entry| {
entry.spec = with_jobs("web");
}))],
counters(9),
Vec::new(),
)
.expect("a carried flock installs");
let started = tokio::time::timeout(Duration::from_secs(5), sup.start(vec![with_jobs("api")]))
.await
.expect("a start must answer rather than hang");
assert!(started.is_ok(), "{started:?}");
let fence = tokio::time::timeout(Duration::from_secs(5), sup.list_checked()).await;
assert!(matches!(fence, Ok(Ok(_))), "{fence:?}");
let mut announced = Vec::new();
while let Ok(event) = rx.try_recv() {
if let BusEvent::DogSheepSettingsChanged { dog, sheep } = event.to_event() {
announced.push((dog, sheep));
}
}
assert_eq!(
announced,
[("jobs".to_string(), "api".to_string())],
"web's table crossed the handover and is not news"
);
}
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn a_carried_sheep_that_sets_the_removed_sandbox_key_is_refused_not_unsandboxed() {
let mut blob = serde_json::to_value(carried("boxed", 0, Some(4242), |_| {})).unwrap();
blob["app"]["sandbox"] = serde_json::json!("box.json");
let crossed: CarriedSheep = serde_json::from_value(blob).unwrap();
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let refused = SupervisorBuilder::new(AdoptingRunner, test_paths(&dir), events)
.spawn_adopted(vec![without_handles(crossed)], counters(1), Vec::new())
.expect_err("a carried sheep that sets the removed key must be refused");
assert!(
matches!(
&refused,
AdoptError::Spec { sheep, source }
if sheep == "boxed"
&& matches!(source, NormalizeError::SandboxKeyRemoved { .. })
),
"{refused:?}"
);
}