use crate::boot::*;
use crate::dogs::DogSpec;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::testing::{capture_logs, roll_of, test_paths};
use shep_core::config::AppConfig;
use shep_core::protocol::DogSource;
use shep_core::status::ProcStatus;
#[tokio::test]
async fn boot_restores_the_flock_before_it_lets_the_dogs_out() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let roll = roll_of(vec![AppConfig::minimal("web", "./srv")]);
crate::snapshot::write_atomic(&paths.snapshot, &roll).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits(), ProcScript::never_exits()]),
paths.clone(),
BootOptions {
restore: true,
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn,
}],
..BootOptions::default()
},
)
.await
.unwrap();
let ctx = daemon.context();
let flock = ctx.supervisor.list_checked().await.unwrap();
assert_eq!(flock.len(), 2, "the sheep and the dog must both be up");
let sheep = flock
.iter()
.find(|p| p.name == "web")
.expect("the restored sheep must be present");
let dog = flock
.iter()
.find(|p| p.name == "metrics")
.expect("the dog must be present");
assert!(
sheep.dog.is_none(),
"the restored app must carry no dog marker"
);
assert_eq!(
dog.dog,
Some(DogSource::BuiltIn),
"the dog entry must carry its source"
);
assert!(
sheep.pid < dog.pid,
"the sheep must be spawned before the dog: sheep={:?} dog={:?}",
sheep.pid,
dog.pid
);
drop(daemon); }
#[test]
fn a_dog_that_will_not_start_does_not_fail_the_boot() {
let _guard = SIGNAL_TEST_LOCK.blocking_lock();
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let mut boot_result = None;
let logs = capture_logs(|| {
boot_result = Some(rt.block_on(boot_with_idle_sweep(
ScriptedRunner::new(vec![]),
paths.clone(),
BootOptions {
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn,
}],
..BootOptions::default()
},
)));
});
let daemon = boot_result
.unwrap()
.expect("a dog that will not start must not fail the boot");
let flock = rt
.block_on(daemon.context().supervisor.list_checked())
.unwrap();
let dog = flock
.iter()
.find(|p| p.name == "metrics")
.expect("the dog's entry must still be registered");
assert_eq!(
dog.status,
ProcStatus::Errored,
"a dog that could not spawn is errored, not silently absent"
);
assert!(
logs.contains("metrics"),
"the warning must name the dog that did not start: {logs:?}"
);
assert!(
logs.contains("WARN"),
"a dog failing to start is a warning, not silence: {logs:?}"
);
drop(daemon); }
#[test]
fn a_dog_enabled_under_a_sheeps_name_does_not_start_and_does_not_fail_the_boot() {
let _guard = SIGNAL_TEST_LOCK.blocking_lock();
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let roll = roll_of(vec![AppConfig::minimal("metrics", "./srv")]);
crate::snapshot::write_atomic(&paths.snapshot, &roll).unwrap();
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let mut boot_result = None;
let logs = capture_logs(|| {
boot_result = Some(rt.block_on(boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
BootOptions {
restore: true,
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn,
}],
..BootOptions::default()
},
)));
});
let daemon = boot_result
.unwrap()
.expect("a name collision must not fail the boot");
let flock = rt
.block_on(daemon.context().supervisor.list_checked())
.unwrap();
assert_eq!(
flock.len(),
1,
"the collision must not register a second entry: {flock:?}"
);
assert!(
flock[0].dog.is_none(),
"the sheep must not be relabeled as a dog by a same-named enable: {:?}",
flock[0]
);
assert!(
logs.contains("metrics"),
"the warning must name the collision: {logs:?}"
);
drop(daemon); }