use crate::boot::*;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::testing::{AnnouncingRunner, test_paths};
use shep_core::config::AppConfig;
use shep_core::values::UpDuration;
use std::time::Duration;
use super::seed_roll;
#[tokio::test]
async fn boot_writes_readiness_to_the_callers_pipe_after_the_socket_is_bound() {
use std::io::Read;
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let (mut reader, writer) = std::io::pipe().unwrap();
let pipe = std::fs::File::from(std::os::fd::OwnedFd::from(writer));
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![]),
paths.clone(),
BootOptions {
ready_fd: Some(pipe),
..BootOptions::default()
},
)
.await
.unwrap();
assert!(
paths.socket.exists(),
"boot must bind the socket before it returns"
);
let mut line = String::new();
reader.read_to_string(&mut line).unwrap();
let ready: DaemonReady = serde_json::from_str(line.trim_end()).unwrap();
assert_eq!(ready.pid, std::process::id());
assert!(line.ends_with('\n'), "the parent reads a line: {line:?}");
drop(daemon); }
#[tokio::test]
async fn readiness_is_reported_only_once_the_roll_is_restored() {
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 notify_path = dir.path().join("n.sock");
let listener = std::os::unix::net::UnixDatagram::bind(¬ify_path).unwrap();
listener
.set_read_timeout(Some(Duration::from_secs(5)))
.unwrap();
let runner = AnnouncingRunner::new(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
¬ify_path,
);
let daemon = boot_with_idle_sweep(
runner,
paths,
BootOptions {
restore: true,
notify_socket: Some(notify_path.clone().into_os_string()),
..BootOptions::default()
},
)
.await
.unwrap();
let mut buf = [0u8; 64];
let read = listener.recv(&mut buf).unwrap();
assert!(buf[..read].starts_with(b"EXTEND_TIMEOUT_USEC="));
let read = listener.recv(&mut buf).unwrap();
assert_eq!(
&buf[..read],
b"SPAWNED\n",
"READY=1 arrived before the roll was restored: a unit that goes \
green at exec time reports a flock that is not up yet, and a \
restore that hangs reads as a healthy service supervising nothing"
);
let read = listener.recv(&mut buf).unwrap();
assert!(buf[..read].starts_with(b"EXTEND_TIMEOUT_USEC="));
let read = listener.recv(&mut buf).unwrap();
assert_eq!(&buf[..read], b"READY=1\n");
let ctx = daemon.context();
let flock = ctx.supervisor.list_checked().await.unwrap();
assert_eq!(flock.len(), 1, "the roll was actually restored");
assert_eq!(flock[0].name, "web");
ctx.shutdown();
daemon.run().await.unwrap();
}
#[tokio::test]
async fn each_restored_stage_extends_the_start_deadline_before_readiness() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let mut db = AppConfig::minimal("db", "./srv");
db.listen_timeout = UpDuration::from_millis(30_000);
let mut api = AppConfig::minimal("api", "./srv");
api.listen_timeout = UpDuration::from_millis(7_000);
api.depends_on = vec!["db".to_string()];
seed_roll(&paths, vec![db, api]);
let notify_path = dir.path().join("n.sock");
let listener = std::os::unix::net::UnixDatagram::bind(¬ify_path).unwrap();
listener
.set_read_timeout(Some(Duration::from_secs(5)))
.unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits(), ProcScript::never_exits()]),
paths,
BootOptions {
restore: true,
notify_socket: Some(notify_path.clone().into_os_string()),
..BootOptions::default()
},
)
.await
.unwrap();
let mut messages = Vec::new();
let mut buf = [0u8; 64];
loop {
let read = listener.recv(&mut buf).unwrap();
let message = String::from_utf8(buf[..read].to_vec()).unwrap();
let ready = message == "READY=1\n";
messages.push(message);
if ready {
break;
}
}
assert_eq!(
messages,
vec![
"EXTEND_TIMEOUT_USEC=35000000\n",
"EXTEND_TIMEOUT_USEC=35000000\n",
"EXTEND_TIMEOUT_USEC=12000000\n",
"EXTEND_TIMEOUT_USEC=5000000\n",
"READY=1\n",
]
);
daemon.context().shutdown();
daemon.run().await.unwrap();
}
#[tokio::test]
async fn a_readiness_datagram_that_cannot_be_delivered_does_not_fail_the_boot() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![]),
paths,
BootOptions {
notify_socket: Some(dir.path().join("nobody.sock").into_os_string()),
..BootOptions::default()
},
)
.await
.expect("a daemon nobody could be told about is still a daemon");
assert!(daemon.context().supervisor.list_checked().await.is_ok());
daemon.context().shutdown();
daemon.run().await.unwrap();
}