use super::*;
fn dog_row(listed: &[ProcessInfo], id: u32) -> ProcessInfo {
listed
.iter()
.find(|info| info.id == id)
.unwrap_or_else(|| panic!("id {id} left the flock: {listed:?}"))
.clone()
}
#[tokio::test(start_paused = true)]
async fn a_dog_that_restarts_is_still_a_dog() {
let dir = tempfile::tempdir().unwrap();
let (events, mut rx) = crate::bus::test_bus(64);
let runner = ScriptedRunner::new(vec![ProcScript::const_exit(1), ProcScript::never_exits()]);
let handle = spawn_supervisor(runner, test_paths(&dir), events);
let dog = handle
.start_dog(dog_app("bark"), DogSource::BuiltIn { grants: None })
.await
.unwrap();
assert_eq!(dog.dog, Some(DogSource::BuiltIn { grants: None }));
expect_event(&mut rx, dog.id, ProcessEventKind::Restart).await;
let after = dog_row(&handle.list().await, dog.id);
assert_eq!(
after.restarts, 1,
"the ordinary restart path, not a dog one"
);
assert_eq!(after.dog, Some(DogSource::BuiltIn { grants: None }));
}
#[tokio::test(start_paused = true)]
async fn a_dog_that_cannot_be_spawned_is_still_a_dog() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(ScriptedRunner::new(Vec::new()), test_paths(&dir), events);
let failed = handle
.start_dog(dog_app("bark"), DogSource::BuiltIn { grants: None })
.await
.expect_err("a spawn with no script behind it cannot succeed");
assert!(matches!(failed, SupervisorError::SpawnFailed(_)));
let listed = handle.list().await;
let errored = dog_row(&listed, 0);
assert_eq!(errored.status, ProcStatus::Errored);
assert_eq!(errored.dog, Some(DogSource::BuiltIn { grants: None }));
}
#[tokio::test(start_paused = true)]
async fn an_app_in_the_gate_set_holds_at_starting_without_a_probe() {
let dir = tempfile::tempdir().unwrap();
let (mut actor, _mailbox) = actor_with_one_online_sheep(&dir, vec![ProcScript::never_exits()]);
let (reply, rx) = oneshot::channel();
let mut app = AppConfig::minimal("db", "./db");
app.listen_timeout = UpDuration::from_millis(50);
actor.handle_command(Command::Start {
apps: vec![normalize(app).unwrap()],
policy: BatchPolicy::AllOrNothing,
gate: BTreeSet::from(["db".to_string()]),
reply,
});
let started = rx.await.unwrap().unwrap();
assert_eq!(started[0].status, ProcStatus::Starting);
}
#[tokio::test(start_paused = true)]
async fn an_app_outside_the_gate_set_is_online_at_spawn() {
let dir = tempfile::tempdir().unwrap();
let (mut actor, _mailbox) = actor_with_one_online_sheep(&dir, vec![ProcScript::never_exits()]);
let (reply, rx) = oneshot::channel();
actor.handle_command(Command::Start {
apps: vec![normalize(AppConfig::minimal("db", "./db")).unwrap()],
policy: BatchPolicy::AllOrNothing,
gate: BTreeSet::new(),
reply,
});
let started = rx.await.unwrap().unwrap();
assert_eq!(started[0].status, ProcStatus::Online);
}
#[cfg(unix)]
#[tokio::test(start_paused = true)]
async fn a_credential_failure_never_shifts_another_apps_identity() {
let dir = tempfile::tempdir().unwrap();
let (mut actor, _mailbox) =
actor_with_one_online_sheep(&dir, vec![ProcScript::never_exits(); 2]);
let own_uid = nix::unistd::geteuid();
let own_user = own_user_name();
let mut bad = AppConfig::minimal("a-bad", "./a");
bad.user = Some("definitely-not-a-real-shep-user".to_string());
let mut mine = AppConfig::minimal("b-mine", "./b");
mine.user = Some(own_user);
let plain = AppConfig::minimal("c-plain", "./c");
let (reply, answer) = oneshot::channel();
actor.handle_command(Command::Start {
apps: vec![
normalize(bad).unwrap(),
normalize(mine).unwrap(),
normalize(plain).unwrap(),
],
policy: BatchPolicy::PerApp,
gate: BTreeSet::new(),
reply,
});
let err = answer
.await
.expect("the actor answers every Start")
.expect_err("one app has no resolvable user");
assert!(
err.to_string().contains("a-bad"),
"the failure must name the app whose user could not be resolved: {err}"
);
let identity = |name: &str| {
actor
.sheep
.values()
.find(|slot| slot.entry.spec.config().name == name)
.map(|slot| slot.entry.credentials)
};
assert_eq!(
identity("a-bad"),
Some(SpawnIdentity::Unresolved),
"an app with no resolvable user is registered `Errored` so it is \
visible, and must hold NO identity: `Unresolved` is what stops a \
later restart reusing one, and it is also the proof this app did \
not inherit b-mine's"
);
assert_eq!(
identity("b-mine"),
Some(SpawnIdentity::Resolved(Some(Credentials {
uid: own_uid.as_raw(),
gid: None
}))),
"the app that asked for a user must run as that user"
);
assert_eq!(
identity("c-plain"),
Some(SpawnIdentity::Resolved(None)),
"the app that asked for no user must be registered, and must run \
as the daemon rather than as somebody else's uid. `Resolved(None)` \
rather than a bare `None`: this app was ASKED, and answered \
nobody, which is not the same fact as never having been asked"
);
}
#[tokio::test(start_paused = true)]
async fn an_all_or_nothing_start_stops_at_the_first_failed_spawn() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let handle = spawn_supervisor(
ScriptedRunner::new(vec![ProcScript::never_exits()]).failing_to_spawn(&["first"]),
test_paths(&dir),
events,
);
let err = handle
.start(vec![
normalize(AppConfig::minimal("first", "./first")).unwrap(),
normalize(AppConfig::minimal("second", "./second")).unwrap(),
])
.await
.expect_err("the first app cannot spawn");
assert!(matches!(err, SupervisorError::SpawnFailed(_)), "{err:?}");
let listed = handle.list().await;
assert_eq!(
listed
.iter()
.map(|i| (i.name.as_str(), i.status))
.collect::<Vec<_>>(),
vec![("first", ProcStatus::Errored)],
"an operator's `shep start` must not go on past a failure: the \
second app is never reached: {listed:?}"
);
handle.shutdown().await;
}
#[tokio::test(start_paused = true)]
async fn a_dog_that_cannot_spawn_is_registered_errored_rather_than_refused() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let runner = ScriptedRunner::new(Vec::new()).refusing(&["bark"]);
let handle = spawn_supervisor(runner, test_paths(&dir), events);
let err = handle
.start_dog(dog_app("bark"), DogSource::BuiltIn { grants: None })
.await
.expect_err("nothing can spawn against an empty script pool");
assert!(
matches!(err, SupervisorError::SpawnFailed(_)),
"a dog must reach its spawn and fail there, not be refused \
before it registers: {err:?}"
);
let listed = handle.list().await;
assert_eq!(
listed
.iter()
.map(|i| (i.name.as_str(), i.status, i.dog.clone()))
.collect::<Vec<_>>(),
vec![(
"bark",
ProcStatus::Errored,
Some(DogSource::BuiltIn { grants: None })
)],
"the dogs table must still show the dog, and show it broken"
);
handle.shutdown().await;
}
#[tokio::test(start_paused = true)]
async fn a_reloaded_dog_is_still_a_dog() {
let dir = tempfile::tempdir().unwrap();
let (events, mut rx) = crate::bus::test_bus(256);
let runner = ScriptedRunner::new(vec![ProcScript::never_exits(); 3]);
let handle = spawn_supervisor(runner, test_paths(&dir), events);
let dog = handle
.start_dog(dog_app("bark"), DogSource::BuiltIn { grants: None })
.await
.unwrap();
handle
.reload(ProcessSelector::Name("bark".to_string()))
.await
.expect("a reload that names the dog is accepted");
let replacement = dog.id + 1;
expect_event(&mut rx, replacement, ProcessEventKind::Reloaded).await;
let listed = handle.list().await;
assert_eq!(
listed.len(),
1,
"the swap is over, not in flight: {listed:?}"
);
assert_eq!(
dog_row(&listed, replacement).dog,
Some(DogSource::BuiltIn { grants: None }),
"the half that arrived is the same dog the half that left was"
);
}
#[tokio::test(start_paused = true)]
async fn a_dog_is_refused_once_a_shutdown_has_begun() {
let dir = tempfile::tempdir().unwrap();
let (mut actor, _mailbox) = actor_with_one_online_sheep(&dir, vec![ProcScript::never_exits()]);
actor.shutting_down = true;
let (reply, rx) = oneshot::channel();
actor.handle_command(Command::StartDog {
app: Box::new(dog_app("bark")),
source: DogSource::BuiltIn { grants: None },
reply,
});
assert_eq!(rx.await, Ok(Err(SupervisorError::EngineStopped)));
assert_eq!(actor.sheep.len(), 1, "nothing new was registered");
}
#[tokio::test(start_paused = true)]
async fn enabling_a_dog_twice_starts_one_process() {
let dir = tempfile::tempdir().unwrap();
let (events, _rx) = crate::bus::test_bus(64);
let runner = ScriptedRunner::new(vec![ProcScript::never_exits(); 2]);
let handle = spawn_supervisor(runner, test_paths(&dir), events);
let first = handle
.start_dog(dog_app("bark"), DogSource::BuiltIn { grants: None })
.await
.unwrap();
let second = handle
.start_dog(dog_app("bark"), DogSource::BuiltIn { grants: None })
.await
.unwrap();
assert_eq!(first.id, second.id);
assert_eq!(second.pid, first.pid, "the same process, not a fresh one");
let listed = handle.list().await;
assert_eq!(listed.iter().filter(|i| i.name == "bark").count(), 1);
}