Files
agent-manager/tests/process_test.rs
T

69 lines
1.7 KiB
Rust

//! Process lifecycle: background spawn, liveness, stop.
mod common;
use agent_manager::process;
use std::collections::BTreeMap;
fn sleep_cmd() -> (&'static str, Vec<String>) {
if cfg!(windows) {
(
"ping",
vec![
"-n".to_string(),
"40".to_string(),
"127.0.0.1".to_string(),
],
)
} else {
("sleep", vec!["40".to_string()])
}
}
#[test]
fn spawn_detect_stop_cycle() {
let app = common::test_app("proc", &["status"]);
let (prog, args) = sleep_cmd();
let env = BTreeMap::new();
let pid = process::spawn_background(
&app,
"test-agent",
std::path::Path::new(prog),
&args,
&env,
None,
)
.unwrap();
assert!(pid > 0);
assert!(process::is_running(pid));
assert!(process::stop_pid(pid, true, 5));
assert!(!process::is_running(pid));
// The log file was created.
assert!(process::agent_log_path(&app, "test-agent").exists());
}
#[test]
fn spawning_a_failing_program_reports_an_error() {
let app = common::test_app("procfail", &["status"]);
let env = BTreeMap::new();
// A command that exits immediately with an error.
let (prog, args) = if cfg!(windows) {
("cmd", vec!["/C".to_string(), "exit 7".to_string()])
} else {
("sh", vec!["-c".to_string(), "exit 7".to_string()])
};
let err = process::spawn_background(
&app,
"failing",
std::path::Path::new(prog),
&args,
&env,
None,
)
.unwrap_err();
assert!(
err.to_string().contains("exited immediately"),
"unexpected error: {err:#}"
);
}