use crate::boot::*;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::testing::{AnnouncingRunner, roll_of, test_paths};
use shep_core::config::AppConfig;
use std::time::Duration;
#[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(
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);
init_dirs(&paths).unwrap();
crate::snapshot::write_atomic(
&paths.snapshot,
&roll_of(vec![AppConfig::minimal("web", "./srv")]),
)
.unwrap();
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(
runner,
paths.clone(),
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_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_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 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(
ScriptedRunner::new(vec![]),
paths.clone(),
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();
}