Files
leleka/crates/deploy/tests/e2e/supervisor_lifecycle.rs
T

156 lines
5.7 KiB
Rust

//! The supervisor from start to ready to a clean stop, with each form of the archive.
use crate::harness::*;
use leleka_deploy::profile::{KarSource, Profile};
use std::{fs, os::unix::fs::PermissionsExt, time::Duration};
pub(super) fn lifecycle(source: KarSource) {
let fixture = Fixture::new(match source {
KarSource::Memfd => "memfd",
KarSource::File => "file",
KarSource::None => "none",
});
fixture.install_fake_binaries(&fixture.dirs.prefix.join("bin"));
let mut profile = Profile::builtin("rtx3080-16g").unwrap();
profile.kar_source = source;
let config = fixture.write_state(profile);
let paths = fixture.dirs.paths();
let memfd = (source == KarSource::Memfd).then(|| inheritable_sealed_memfd(&vec![7u8; 100_000]));
if source == KarSource::File {
fs::create_dir_all(&fixture.dirs.assets).unwrap();
fs::write(paths.kar(), b"archive").unwrap();
}
let fd = memfd.as_ref().map(std::os::fd::AsRawFd::as_raw_fd);
let mut supervisor = start_supervisor(&fixture, &config, fd);
let ready = fixture.wait_for("ready", Duration::from_secs(120), |s| s["phase"] == "ready");
let pids = check_ready(&fixture, &ready, source);
let again = check_engine_restart(&fixture, pids);
check_restart_calls(&fixture, source);
// The sealed descriptor was reused, not copied again (one process held it the whole time).
assert!(!supervisor.try_wait().unwrap().is_some());
check_clean_stop(&fixture, &mut supervisor, &again, pids);
}
/// The ready status: no restarts, the API answers, the archive form is in the engine log, the status file is private.
/// Returns the pids of the engine, the gateway and the API.
fn check_ready(fixture: &Fixture, ready: &serde_json::Value, source: KarSource) -> (u64, u64, u64) {
let paths = fixture.dirs.paths();
assert_eq!(ready["engine_restarts"], 0);
assert_eq!(fixture.http_status("/ready"), 200);
let engine_log = fs::read_to_string(paths.logs().join("engine.log")).unwrap();
match source {
KarSource::Memfd => assert!(engine_log.contains("kar fd "), "{engine_log}"),
KarSource::File => assert!(engine_log.contains("listening"), "{engine_log}"),
KarSource::None => {}
}
// The status file is private and never holds a secret.
let mode = fs::metadata(paths.status_file())
.unwrap()
.permissions()
.mode()
& 0o777;
assert_eq!(mode, 0o600);
assert!(
!fs::read_to_string(paths.status_file())
.unwrap()
.contains(KEY)
);
(
ready["engine_pid"].as_u64().unwrap(),
ready["gateway_pid"].as_u64().unwrap(),
ready["api_pid"].as_u64().unwrap(),
)
}
/// Kill the engine: the API reports not ready, the supervisor restarts it and loads again.
fn check_engine_restart(
fixture: &Fixture,
(engine_pid, gateway_pid, api_pid): (u64, u64, u64),
) -> serde_json::Value {
signal(engine_pid, "KILL");
let again = fixture.wait_for("engine restart", Duration::from_secs(60), |s| {
s["phase"] == "ready" && s["engine_restarts"] == 1
});
assert_ne!(again["engine_pid"].as_u64().unwrap(), engine_pid);
assert_eq!(
again["gateway_pid"].as_u64().unwrap(),
gateway_pid,
"the gateway must survive an engine restart"
);
assert_eq!(
again["api_pid"].as_u64().unwrap(),
api_pid,
"the API must survive an engine restart"
);
assert_eq!(fixture.http_status("/ready"), 200);
again
}
fn check_restart_calls(fixture: &Fixture, source: KarSource) {
let calls = fs::read_to_string(fixture.fake_dir().join("calls.log")).unwrap();
assert_eq!(calls.matches("leleka-inference ").count(), 2, "{calls}");
assert_eq!(calls.matches("load-mtp").count(), 2, "{calls}");
// The first engine hashed the sealed archive; the restarted one is told the same memfd was verified already
// (stamps are on by default), and both get the stamp directory.
let starts: Vec<&str> = calls
.lines()
.filter(|l| l.starts_with("leleka-inference "))
.collect();
assert!(
starts.iter().all(|l| l.contains("--verify-stamps")),
"{calls}"
);
assert!(
!starts[0].contains("--kar-attested-by-stamp"),
"{}",
starts[0]
);
assert_eq!(
starts[1].contains("--kar-attested-by-stamp"),
source == KarSource::Memfd,
"{}",
starts[1]
);
}
/// SIGTERM drains and stops everything with status 0.
fn check_clean_stop(
fixture: &Fixture,
supervisor: &mut std::process::Child,
again: &serde_json::Value,
(_, gateway_pid, api_pid): (u64, u64, u64),
) {
let paths = fixture.dirs.paths();
let new_engine = again["engine_pid"].as_u64().unwrap();
signal(u64::from(supervisor.id()), "TERM");
let status = wait_exit(supervisor, Duration::from_secs(60))
.expect("the supervisor must exit after SIGTERM");
assert!(status.success(), "exit {status:?}\n{}", fixture.logs());
assert_eq!(fixture.status().unwrap()["phase"], "stopped");
for pid in [new_engine, gateway_pid, api_pid] {
assert!(!pid_alive(pid), "pid {pid} outlived the supervisor");
}
assert_eq!(
fixture.http_status("/ready"),
0,
"the API port must be closed"
);
// No child ever saw the Hugging Face token (the fake engine exits 3 if it does).
assert!(
!fs::read_to_string(paths.logs().join("engine.log"))
.unwrap()
.contains("leaked")
);
}
#[test]
fn supervisor_lifecycle_with_an_inherited_sealed_descriptor() {
lifecycle(KarSource::Memfd);
}
#[test]
fn supervisor_lifecycle_with_a_read_only_archive_file() {
lifecycle(KarSource::File);
}