use super::super::*;
use shep_core::config::{AppConfig, normalize};
use shep_core::status::ProcStatus;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::supervisor::spawn_supervisor;
use crate::testing::{roll_of, sorted_status, test_paths};
#[test]
fn restorable_keeps_every_member_and_starts_only_what_was_up() {
let mut stopped = AppConfig::minimal("stopped", "./s");
stopped.instances = 1;
let mut opted_out = AppConfig::minimal("manual", "./m");
opted_out.autostart = false;
let roll = FlockSnapshot::with_apps(vec![
SavedApp {
app: AppConfig::minimal("web", "./srv"),
instances_running: 2,
},
SavedApp {
app: stopped,
instances_running: 0,
},
SavedApp {
app: opted_out,
instances_running: 1,
},
]);
let restorable = restorable(roll);
let members: Vec<&str> = restorable
.members
.iter()
.map(|a| a.config().name.as_str())
.collect();
assert_eq!(
members,
["web", "stopped", "manual"],
"every entry that normalizes belongs to the flock, running or not"
);
let starting: Vec<&str> = restorable
.to_start
.iter()
.map(|a| a.config().name.as_str())
.collect();
assert_eq!(
starting,
["web"],
"only the sheep that was up and opts into autostart is started"
);
assert!(restorable.rejected.is_empty());
}
#[test]
fn restorable_reports_a_hand_edited_invalid_app_instead_of_aborting() {
let mut broken = AppConfig::minimal("broken", "./b");
broken.instances = 0; let roll = roll_of(vec![broken, AppConfig::minimal("web", "./srv")]);
let restorable = restorable(roll);
assert_eq!(
restorable.members.len(),
1,
"one bad entry must not sink the muster"
);
assert_eq!(
restorable.rejected,
vec![(
"broken".to_string(),
shep_core::config::NormalizeError::ZeroInstances
)]
);
}
#[tokio::test(start_paused = true)]
async fn muster_restores_both_and_starts_only_the_one_that_was_up() {
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
std::fs::create_dir_all(paths.snapshot.parent().unwrap()).unwrap();
let roll = FlockSnapshot::with_apps(vec![
SavedApp {
app: AppConfig::minimal("up", "./srv"),
instances_running: 1,
},
SavedApp {
app: AppConfig::minimal("down", "./srv"),
instances_running: 0,
},
]);
write_atomic(&paths.snapshot, &roll).unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
events.clone(),
);
let registry = FlockRegistry::new();
let restored = muster(&paths.snapshot, ®istry, &handle, &events, &[], &[])
.await
.unwrap();
assert_eq!(
restored,
vec!["up".to_string(), "down".to_string()],
"both are restored to the flock; only one of them runs"
);
assert_eq!(
sorted_status(&handle).await,
[
("down".to_string(), ProcStatus::Stopped),
("up".to_string(), ProcStatus::Online)
],
"the sheep that was down is listed and stopped, not missing"
);
handle.shutdown().await;
}
#[tokio::test(start_paused = true)]
async fn a_bad_saved_app_does_not_take_the_apps_after_it_down() {
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
std::fs::create_dir_all(paths.snapshot.parent().unwrap()).unwrap();
let app = |name: &str| AppConfig::minimal(name, "./srv");
let roll = roll_of(vec![app("a-good"), app("b-bad"), app("c-good")]);
write_atomic(&paths.snapshot, &roll).unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(
ScriptedRunner::new(vec![ProcScript::never_exits(); 2]).failing_to_spawn(&["b-bad"]),
paths.clone(),
events.clone(),
);
let registry = FlockRegistry::new();
let restored = muster(&paths.snapshot, ®istry, &handle, &events, &[], &[])
.await
.unwrap();
assert_eq!(
restored,
vec![
"a-good".to_string(),
"b-bad".to_string(),
"c-good".to_string()
]
);
assert_eq!(
sorted_status(&handle).await,
[
("a-good".to_string(), ProcStatus::Online),
("b-bad".to_string(), ProcStatus::Errored),
("c-good".to_string(), ProcStatus::Online),
],
"every app after the broken one must still get its turn, and the \
broken one must be visible rather than absent"
);
handle.shutdown().await;
}
#[tokio::test(start_paused = true)]
async fn one_unstartable_saved_app_does_not_keep_the_rest_of_the_flock_down() {
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
std::fs::create_dir_all(paths.snapshot.parent().unwrap()).unwrap();
let roll = roll_of(vec![
AppConfig::minimal("good", "./srv"),
AppConfig::minimal("gone", "./deleted-by-a-rebuild"),
]);
write_atomic(&paths.snapshot, &roll).unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(
ScriptedRunner::new(vec![ProcScript::never_exits()])
.refusing(&["gone"])
.failing_to_spawn(&["gone"]),
paths.clone(),
events.clone(),
);
let registry = FlockRegistry::new();
let restored = muster(&paths.snapshot, ®istry, &handle, &events, &[], &[])
.await
.unwrap();
assert_eq!(restored, vec!["good".to_string(), "gone".to_string()]);
assert_eq!(
sorted_status(&handle).await,
[
("gone".to_string(), ProcStatus::Errored),
("good".to_string(), ProcStatus::Online)
],
"the app that could still run must come up, and the one that \
could not must be visible rather than absent"
);
handle.shutdown().await;
}
#[tokio::test(start_paused = true)]
async fn muster_leaves_an_app_the_flock_already_has_where_it_stands() {
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
std::fs::create_dir_all(paths.snapshot.parent().unwrap()).unwrap();
let roll = roll_of(vec![AppConfig::minimal("web", "./srv")]);
write_atomic(&paths.snapshot, &roll).unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
events.clone(),
);
let registry = FlockRegistry::new();
let app = normalize(AppConfig::minimal("web", "./srv")).unwrap();
registry.record(std::slice::from_ref(&app));
handle.start(vec![app]).await.unwrap();
let restored = muster(&paths.snapshot, ®istry, &handle, &events, &[], &[])
.await
.unwrap();
assert_eq!(
restored,
vec!["web".to_string()],
"the roll's app is restored whether or not this call is what started it"
);
let listed = handle.list().await;
assert_eq!(listed.len(), 1, "one instance of a one-instance app");
assert_eq!(listed[0].status, ProcStatus::Online);
handle.shutdown().await;
}
#[tokio::test(start_paused = true)]
async fn a_missing_roll_restores_nothing_without_failing() {
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
std::fs::create_dir_all(paths.snapshot.parent().unwrap()).unwrap();
assert!(!paths.snapshot.exists());
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
events.clone(),
);
let registry = FlockRegistry::new();
assert_eq!(
muster(&paths.snapshot, ®istry, &handle, &events, &[], &[])
.await
.unwrap(),
Vec::<String>::new()
);
assert!(handle.list().await.is_empty());
handle.shutdown().await;
}