use super::boot_blocking;
use crate::boot::*;
use crate::dogs::DogSpec;
use crate::fake::ProcScript;
use shep_core::config::AppConfig;
use shep_core::protocol::{DogSource, ProcessInfo};
use std::collections::BTreeMap;
fn built_in(name: &str) -> DogSpec {
DogSpec {
name: name.to_string(),
source: DogSource::BuiltIn {
grants: None,
events: Vec::new(),
},
env: BTreeMap::new(),
}
}
fn boot_over_a_saved_metrics(
scripts: Vec<ProcScript>,
options: BootOptions,
) -> (Vec<ProcessInfo>, String) {
boot_blocking(
vec![AppConfig::minimal("metrics", "./srv")],
scripts,
BootOptions {
restore: true,
..options
},
)
}
#[test]
fn a_saved_sheep_named_after_a_listed_built_in_survives_and_the_built_in_is_not_registered() {
let (flock, logs) = boot_over_a_saved_metrics(
vec![ProcScript::never_exits()],
BootOptions {
known_dogs: vec![built_in("metrics"), built_in("bark")],
..BootOptions::default()
},
);
let held: Vec<&ProcessInfo> = flock.iter().filter(|info| info.name == "metrics").collect();
assert_eq!(held.len(), 1, "one name is one entry: {flock:?}");
assert!(held[0].dog.is_none(), "the sheep keeps the name: {flock:?}");
assert!(
flock
.iter()
.any(|info| info.name == "bark" && info.dog.is_some()),
"the other built-in is still listed: {flock:?}"
);
assert!(
logs.contains("WARN") && logs.contains("rename"),
"the warning must say to rename one: {logs:?}"
);
}
#[test]
fn a_saved_sheep_named_after_an_enabled_built_in_survives_and_the_built_in_does_not_start() {
let (flock, logs) = boot_over_a_saved_metrics(
vec![ProcScript::never_exits()],
BootOptions {
dogs: vec![built_in("metrics")],
known_dogs: vec![built_in("metrics")],
..BootOptions::default()
},
);
assert_eq!(flock.len(), 1, "{flock:?}");
assert!(flock[0].dog.is_none(), "{flock:?}");
assert!(logs.contains("rename"), "{logs:?}");
}
#[test]
fn a_boot_first_dog_colliding_with_a_saved_sheep_keeps_the_sheep() {
let (flock, logs) = boot_over_a_saved_metrics(
vec![ProcScript::never_exits()],
BootOptions {
dogs: vec![built_in("metrics")],
known_dogs: vec![built_in("metrics")],
boot_first_dogs: vec!["metrics".to_string()],
..BootOptions::default()
},
);
assert_eq!(flock.len(), 1, "one name is one entry: {flock:?}");
assert!(
flock[0].dog.is_none(),
"the sheep holds the name: {flock:?}"
);
assert_eq!(
flock[0].status,
shep_core::status::ProcStatus::Online,
"the sheep was running when the roll was written: {flock:?}"
);
assert!(logs.contains("rename"), "{logs:?}");
}