use super::{boot_blocking, seed_roll};
use crate::boot::*;
use crate::dogs::DogSpec;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::testing::test_paths;
use shep_core::config::AppConfig;
use shep_core::protocol::DogSource;
use shep_core::status::ProcStatus;
use std::collections::BTreeMap;
#[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);
seed_roll(&paths, vec![AppConfig::minimal("web", "./srv")]);
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits(), ProcScript::never_exits()]),
paths,
BootOptions {
restore: true,
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn {
grants: None,
events: Vec::new(),
},
env: BTreeMap::new(),
}],
..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 {
grants: None,
events: Vec::new()
}),
"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 (flock, logs) = boot_blocking(
vec![],
vec![],
BootOptions {
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn {
grants: None,
events: Vec::new(),
},
env: BTreeMap::new(),
}],
..BootOptions::default()
},
);
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:?}"
);
}
#[test]
fn a_dog_enabled_under_a_sheeps_name_does_not_start_and_does_not_fail_the_boot() {
let (flock, logs) = boot_blocking(
vec![AppConfig::minimal("metrics", "./srv")],
vec![ProcScript::never_exits()],
BootOptions {
restore: true,
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn {
grants: None,
events: Vec::new(),
},
env: BTreeMap::new(),
}],
..BootOptions::default()
},
);
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:?}"
);
}