mirror of
https://github.com/marcoallegretti/WEFT_OS.git
synced 2026-11-22 08:13:59 +00:00
test(appd): give each test process its own runtime directory
Session sockets are named by session ID and every test registry starts at 1, so test processes running at the same time shared one socket path in the common test runtime directory and broke each other's sessions. Each test process now uses its own directory under the system temporary directory, and directories of ended test processes are removed. The test of a closed IPC connection connects to the socket itself instead of relying on python3 in a child script. A session whose component closes its IPC connection during startup now stops before it is reported ready. The weft runtime directory is created with mode 0700, and the socket is removed if its permissions cannot be restricted.
This commit is contained in:
parent
26bd5b5079
commit
ec00531c75
2 changed files with 65 additions and 34 deletions
|
|
@ -415,7 +415,13 @@ async fn handle_connection(
|
||||||
pub(crate) fn session_ipc_socket_path(session_id: u64) -> Option<PathBuf> {
|
pub(crate) fn session_ipc_socket_path(session_id: u64) -> Option<PathBuf> {
|
||||||
let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?;
|
let runtime_dir = std::env::var("XDG_RUNTIME_DIR").ok()?;
|
||||||
let dir = PathBuf::from(runtime_dir).join("weft");
|
let dir = PathBuf::from(runtime_dir).join("weft");
|
||||||
std::fs::create_dir_all(&dir).ok()?;
|
// Created accessible to the user only, so session sockets are private
|
||||||
|
// whatever the umask.
|
||||||
|
let mut builder = std::fs::DirBuilder::new();
|
||||||
|
builder.recursive(true);
|
||||||
|
#[cfg(unix)]
|
||||||
|
std::os::unix::fs::DirBuilderExt::mode(&mut builder, 0o700);
|
||||||
|
builder.create(&dir).ok()?;
|
||||||
Some(dir.join(format!("ipc-{session_id}.sock")))
|
Some(dir.join(format!("ipc-{session_id}.sock")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -634,11 +640,25 @@ mod tests {
|
||||||
|
|
||||||
static ENV_LOCK: std::sync::OnceLock<tokio::sync::Mutex<()>> = std::sync::OnceLock::new();
|
static ENV_LOCK: std::sync::OnceLock<tokio::sync::Mutex<()>> = std::sync::OnceLock::new();
|
||||||
|
|
||||||
/// Points XDG_RUNTIME_DIR at a private directory for the session's IPC
|
/// Points XDG_RUNTIME_DIR at a directory private to this test process,
|
||||||
/// socket. Callers hold env_lock.
|
/// since session sockets are named by session ID and every test registry
|
||||||
|
/// starts at 1. Directories of test processes that have ended are
|
||||||
|
/// removed. Callers hold env_lock.
|
||||||
fn use_test_runtime_dir() {
|
fn use_test_runtime_dir() {
|
||||||
let dir =
|
let base = std::env::temp_dir().join("weft-appd-tests");
|
||||||
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../target/test-runtime-dir");
|
if let Ok(entries) = std::fs::read_dir(&base) {
|
||||||
|
for entry in entries.flatten() {
|
||||||
|
let alive = entry
|
||||||
|
.file_name()
|
||||||
|
.to_str()
|
||||||
|
.and_then(|pid| pid.parse::<u32>().ok())
|
||||||
|
.is_some_and(|pid| std::path::Path::new(&format!("/proc/{pid}")).exists());
|
||||||
|
if !alive {
|
||||||
|
let _ = std::fs::remove_dir_all(entry.path());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let dir = base.join(std::process::id().to_string());
|
||||||
std::fs::create_dir_all(&dir).unwrap();
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
// SAFETY: env_lock serialises the tests that change the environment,
|
// SAFETY: env_lock serialises the tests that change the environment,
|
||||||
// and std serialises the environment accesses themselves.
|
// and std serialises the environment accesses themselves.
|
||||||
|
|
@ -1047,25 +1067,15 @@ mod tests {
|
||||||
use_test_runtime_dir();
|
use_test_runtime_dir();
|
||||||
let dir = std::env::temp_dir().join(format!("weft_test_ipc_close_{}", std::process::id()));
|
let dir = std::env::temp_dir().join(format!("weft_test_ipc_close_{}", std::process::id()));
|
||||||
std::fs::create_dir_all(&dir).unwrap();
|
std::fs::create_dir_all(&dir).unwrap();
|
||||||
// The runtime connects to its IPC socket ($4), closes the connection
|
// Both children report ready and keep running; the test plays the
|
||||||
// and keeps running; the app shell just reports ready.
|
// component, connecting to the session's IPC socket and closing it.
|
||||||
let runtime = dir.join("runtime.sh");
|
let child = dir.join("child.sh");
|
||||||
std::fs::write(
|
std::fs::write(
|
||||||
&runtime,
|
&child,
|
||||||
"#!/bin/sh\necho READY $WEFT_READY_TOKEN\n\
|
|
||||||
python3 -c 'import socket,sys; s=socket.socket(socket.AF_UNIX); \
|
|
||||||
s.connect(sys.argv[1]); s.close()' \"$4\"\nexec sleep 30\n",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let shell = dir.join("shell.sh");
|
|
||||||
std::fs::write(
|
|
||||||
&shell,
|
|
||||||
"#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 30\n",
|
"#!/bin/sh\necho READY $WEFT_READY_TOKEN\nexec sleep 30\n",
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
for script in [&runtime, &shell] {
|
std::fs::set_permissions(&child, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||||
std::fs::set_permissions(script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
|
||||||
}
|
|
||||||
let prior: Vec<_> = [
|
let prior: Vec<_> = [
|
||||||
"WEFT_RUNTIME_BIN",
|
"WEFT_RUNTIME_BIN",
|
||||||
"WEFT_APP_SHELL_BIN",
|
"WEFT_APP_SHELL_BIN",
|
||||||
|
|
@ -1076,25 +1086,38 @@ mod tests {
|
||||||
.collect();
|
.collect();
|
||||||
// SAFETY: env_lock serialises the tests that change the environment.
|
// SAFETY: env_lock serialises the tests that change the environment.
|
||||||
unsafe {
|
unsafe {
|
||||||
std::env::set_var("WEFT_RUNTIME_BIN", &runtime);
|
std::env::set_var("WEFT_RUNTIME_BIN", &child);
|
||||||
std::env::set_var("WEFT_APP_SHELL_BIN", &shell);
|
std::env::set_var("WEFT_APP_SHELL_BIN", &child);
|
||||||
std::env::set_var("WEFT_DISABLE_CGROUP", "1");
|
std::env::set_var("WEFT_DISABLE_CGROUP", "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
let registry = make_registry();
|
let registry = make_registry();
|
||||||
|
let mut rx = registry.lock().await.subscribe();
|
||||||
let session_id = registry.lock().await.launch("test.app");
|
let session_id = registry.lock().await.launch("test.app");
|
||||||
let abort_rx = registry.lock().await.register_abort(session_id);
|
let abort_rx = registry.lock().await.register_abort(session_id);
|
||||||
let finished = tokio::time::timeout(
|
let socket = session_ipc_socket_path(session_id).unwrap();
|
||||||
std::time::Duration::from_secs(15),
|
let component = async {
|
||||||
runtime::supervise(
|
// Connect once the session is running, then close the connection.
|
||||||
session_id,
|
loop {
|
||||||
"test.app",
|
if let Ok(Response::AppReady { session_id: id, .. }) = rx.recv().await
|
||||||
grants::SessionGrants::default(),
|
&& id == session_id
|
||||||
Arc::clone(®istry),
|
{
|
||||||
abort_rx,
|
break;
|
||||||
None,
|
}
|
||||||
),
|
}
|
||||||
)
|
drop(tokio::net::UnixStream::connect(&socket).await.unwrap());
|
||||||
|
};
|
||||||
|
let supervised = runtime::supervise(
|
||||||
|
session_id,
|
||||||
|
"test.app",
|
||||||
|
grants::SessionGrants::default(),
|
||||||
|
Arc::clone(®istry),
|
||||||
|
abort_rx,
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
let finished = tokio::time::timeout(std::time::Duration::from_secs(15), async {
|
||||||
|
tokio::join!(supervised, component)
|
||||||
|
})
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
for (key, value) in prior {
|
for (key, value) in prior {
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,12 @@ pub(crate) fn spawn_ipc_relay(
|
||||||
let listener = tokio::net::UnixListener::bind(&socket_path)?;
|
let listener = tokio::net::UnixListener::bind(&socket_path)?;
|
||||||
{
|
{
|
||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
std::fs::set_permissions(&socket_path, std::fs::Permissions::from_mode(0o600))?;
|
if let Err(e) =
|
||||||
|
std::fs::set_permissions(&socket_path, std::fs::Permissions::from_mode(0o600))
|
||||||
|
{
|
||||||
|
let _ = std::fs::remove_file(&socket_path);
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let (html_to_wasm_tx, mut html_to_wasm_rx) = tokio::sync::mpsc::channel::<String>(64);
|
let (html_to_wasm_tx, mut html_to_wasm_rx) = tokio::sync::mpsc::channel::<String>(64);
|
||||||
let task = tokio::spawn(async move {
|
let task = tokio::spawn(async move {
|
||||||
|
|
@ -397,6 +402,9 @@ pub(crate) async fn supervise(
|
||||||
status = session.runtime.as_mut().expect("runtime spawned").wait() => {
|
status = session.runtime.as_mut().expect("runtime spawned").wait() => {
|
||||||
Err(format!("runtime exited before the app shell was ready ({status:?})"))
|
Err(format!("runtime exited before the app shell was ready ({status:?})"))
|
||||||
}
|
}
|
||||||
|
_ = session.relay.as_mut().expect("relay opened").ended() => {
|
||||||
|
Err("component closed its IPC connection before the session was ready".to_owned())
|
||||||
|
}
|
||||||
};
|
};
|
||||||
let shell_stdout = match shell_stdout {
|
let shell_stdout = match shell_stdout {
|
||||||
Ok(reader) => reader,
|
Ok(reader) => reader,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue