use std::time::Duration;
use super::*;
use crate::process::capture;
#[cfg(unix)]
use crate::process::stopping::Stopping;
fn spec(program: &str, args: &[&str]) -> ProcessSpec {
ProcessSpec::new(program, args.iter().copied())
}
async fn gone(pid: &str) -> bool {
for _ in 0..60 {
let alive = if cfg!(windows) {
let out = std::process::Command::new("tasklist").args(["/FI", &format!("PID eq {pid}"), "/NH"]).output();
out.is_ok_and(|o| String::from_utf8_lossy(&o.stdout).contains(pid))
} else {
let out = std::process::Command::new("ps").args(["-o", "stat=", "-p", pid]).output();
out.is_ok_and(|o| {
let stat = String::from_utf8_lossy(&o.stdout);
!stat.trim().is_empty() && !stat.trim().starts_with('Z')
})
};
if !alive {
return true;
}
tokio::time::sleep(Duration::from_millis(100)).await;
}
false
}
#[tokio::test]
async fn a_missing_tool_is_named() {
let err = capture(&SystemProcesses::default(), &spec("isothrum-no-such-tool", &[])).await.unwrap_err();
assert!(
matches!(err, ProcessError::Missing { ref program } if program == "isothrum-no-such-tool"),
"{err:?}"
);
}
#[tokio::test]
async fn a_tool_cancelled_takes_what_it_started_with_it() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("pid");
let path = file.display().to_string();
let job = if cfg!(windows) {
let script = format!(
"$p = Start-Process -FilePath ping -ArgumentList '-n','120','127.0.0.1' -PassThru -NoNewWindow; Set-Content -Path '{path}' -Value $p.Id; Start-Sleep 120"
);
spec("powershell", &["-NoProfile", "-NonInteractive", "-Command", &script])
} else {
spec("sh", &["-c", &format!("sleep 120 & echo $! > '{path}'; wait")])
};
let runner = SystemProcesses::default();
let limit = Duration::from_secs(if cfg!(windows) { 10 } else { 2 });
assert!(tokio::time::timeout(limit, capture(&runner, &job)).await.is_err(), "cancelled while it ran");
let pid = std::fs::read_to_string(&file).unwrap().trim().to_owned();
assert!(gone(&pid).await, "the tool's child {pid} was left running");
}
#[cfg(unix)]
fn looping(pid: &std::path::Path, on_int: &str) -> ProcessSpec {
let script = format!("trap {on_int} INT; echo $$ > '{}'; while :; do sleep 0.1; done", pid.display());
spec("sh", &["-c", &script])
}
#[cfg(unix)]
fn pid_in(file: &std::path::Path) -> String {
std::fs::read_to_string(file).unwrap().trim().to_owned()
}
#[cfg(unix)]
#[tokio::test]
async fn a_tool_cut_short_is_asked_to_stop_before_it_is_ended() {
let dir = tempfile::tempdir().unwrap();
let (pid, said) = (dir.path().join("pid"), dir.path().join("said"));
let job = looping(&pid, &format!("\"echo stopped > '{}'; exit 0\"", said.display()));
let runner = SystemProcesses::default();
assert!(tokio::time::timeout(Duration::from_secs(2), capture(&runner, &job)).await.is_err(), "cut short");
tokio::time::timeout(Duration::from_secs(10), runner.stopped())
.await
.expect("it stopped on its own");
assert!(!runner.is_stopping());
assert_eq!(std::fs::read_to_string(&said).unwrap().trim(), "stopped", "its interrupt handler ran");
assert!(gone(&pid_in(&pid)).await);
}
#[cfg(unix)]
#[tokio::test]
async fn a_tool_that_ignores_the_request_is_ended_after_its_grace() {
let dir = tempfile::tempdir().unwrap();
let pid = dir.path().join("pid");
let runner = SystemProcesses {
stopping: Arc::new(Stopping::new(Duration::from_secs(1))),
..SystemProcesses::default()
};
assert!(
tokio::time::timeout(Duration::from_secs(2), capture(&runner, &looping(&pid, "''")))
.await
.is_err()
);
assert!(runner.is_stopping(), "given its grace");
tokio::time::timeout(Duration::from_secs(10), runner.stopped())
.await
.expect("ended after its grace");
assert!(gone(&pid_in(&pid)).await);
}
#[cfg(unix)]
#[tokio::test]
async fn the_tools_still_stopping_can_be_ended_at_once() {
let dir = tempfile::tempdir().unwrap();
let pid = dir.path().join("pid");
let runner = SystemProcesses::default();
assert!(
tokio::time::timeout(Duration::from_secs(2), capture(&runner, &looping(&pid, "''")))
.await
.is_err()
);
runner.end_stopping();
tokio::time::timeout(Duration::from_secs(5), runner.stopped())
.await
.expect("ended well before its 30 s");
assert!(gone(&pid_in(&pid)).await);
}
#[cfg(unix)]
#[tokio::test]
async fn output_that_isnt_utf8_is_read_lossily_and_the_tool_runs_on() {
let script = r"printf 'Cr\351ation\r\n'; printf 'err \377\n' >&2; sleep 0.2; printf 'done'";
let out = capture(&SystemProcesses::default(), &spec("sh", &["-c", script])).await.unwrap();
assert_eq!(out.stdout, ["Cr\u{fffd}ation", "done"]);
assert_eq!(out.stderr, ["err \u{fffd}"]);
assert!(out.success());
}
#[cfg(unix)]
#[tokio::test]
async fn a_tool_that_exited_is_not_waited_for_past_what_it_left_holding_its_output() {
let started = std::time::Instant::now();
let (runner, job) = (SystemProcesses::default(), spec("sh", &["-c", "sleep 20 & printf 'done'"]));
let run = capture(&runner, &job);
let out = tokio::time::timeout(Duration::from_secs(10), run).await.unwrap().unwrap();
assert_eq!(out.stdout, ["done"]);
assert!(started.elapsed() < Duration::from_secs(10));
}
#[cfg(unix)]
#[tokio::test]
async fn a_failure_keeps_its_exit_code_and_last_lines() {
let out = capture(
&SystemProcesses::default(),
&spec("sh", &["-c", "for i in $(seq 1 12); do echo e$i >&2; done; exit 4"]),
)
.await
.unwrap();
assert_eq!(out.exit, Exit { code: Some(4) });
assert_eq!(out.stderr.first().map(String::as_str), Some("e3"));
assert_eq!(out.stderr.len(), 10);
}
#[cfg(unix)]
#[tokio::test]
async fn stdin_comes_from_a_file_and_env_and_cwd_are_the_specs() {
use crate::events::Stream;
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("in.sh"), "echo \"$GREETING from $(basename \"$PWD\")\"\n").unwrap();
let mut job = spec("sh", &["-s"]);
job.stdin = Some(dir.path().join("in.sh"));
job.env = vec![("GREETING".into(), "hello".into())];
job.cwd = Some(dir.path().to_path_buf());
let mut lines = Vec::new();
let exit = SystemProcesses::default()
.run(&job, &mut |s: Stream, l: &str| {
lines.push((s, l.to_owned()));
})
.await
.unwrap();
let name = dir.path().file_name().unwrap().to_string_lossy().to_string();
assert!(exit.success());
assert_eq!(lines, [(Stream::Stdout, format!("hello from {name}"))]);
}
#[test]
fn only_the_windows_wrappers_go_through_cmd() {
let az = spec("az", &["account", "show"]);
let (program, args) = wrapped(&az);
if cfg!(windows) {
assert_eq!((program, args), ("cmd", vec!["/C", "az", "account", "show"]));
} else {
assert_eq!((program, args), ("az", vec!["account", "show"]));
}
assert_eq!(wrapped(&spec("docker", &["ps"])), ("docker", vec!["ps"]));
}
#[tokio::test]
async fn an_argument_cmd_would_interpret_is_refused_before_anything_starts() {
let err = capture(&SystemProcesses::default(), &spec("az", &["group", "create", "--name", "x & calc"]))
.await
.unwrap_err();
assert!(matches!(err, ProcessError::Refused { .. }), "{err:?}");
}
#[test]
fn the_docker_cli_config_is_read_from_docker_config_before_the_home() {
let (env_dir, home) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
let context = |name: &str| format!(r#"{{"currentContext":"{name}"}}"#);
std::fs::create_dir_all(home.path().join(".docker")).unwrap();
std::fs::write(home.path().join(".docker/config.json"), context("colima")).unwrap();
std::fs::write(env_dir.path().join("config.json"), context("default")).unwrap();
let env = env_dir.path().display().to_string();
let with = |docker_config: Option<&str>| {
SystemProcesses::for_host(
|v| (v == "DOCKER_CONFIG").then(|| docker_config.map(std::ffi::OsString::from)).flatten(),
Some(home.path()),
)
};
assert!(with(None).strip_engine_overrides, "the home's config picks a context");
assert!(!with(Some(&env)).strip_engine_overrides, "$DOCKER_CONFIG's config comes first");
assert!(!SystemProcesses::for_host(|_| None, None).strip_engine_overrides, "no config: nothing picked");
}
#[cfg(unix)]
#[test]
fn a_docker_config_that_isnt_utf8_is_still_the_one_read() {
use std::os::unix::ffi::OsStringExt;
let home = tempfile::tempdir().unwrap();
std::fs::create_dir_all(home.path().join(".docker")).unwrap();
std::fs::write(home.path().join(".docker/config.json"), r#"{"currentContext":"colima"}"#).unwrap();
let elsewhere = std::ffi::OsString::from_vec(b"/nowhere/bad\xff".to_vec());
let read = SystemProcesses::for_host(|v| (v == "DOCKER_CONFIG").then(|| elsewhere.clone()), Some(home.path()));
assert!(!read.strip_engine_overrides, "the home's config isn't the one DOCKER_CONFIG names");
}
#[tokio::test]
async fn a_docker_cli_config_that_cant_be_read_or_isnt_json_is_traced() {
let dir = tempfile::tempdir().unwrap();
let config = dir.path().join("config.json");
let docker_config = dir.path().as_os_str().to_owned();
let read = || SystemProcesses::for_host(|v| (v == "DOCKER_CONFIG").then(|| docker_config.clone()), None);
let (none, quiet) = crate::testing::traced(async { read() }).await;
assert!(!none.strip_engine_overrides && quiet.is_empty(), "none there is fine: {quiet}");
std::fs::create_dir(&config).unwrap();
let (unreadable, traced) = crate::testing::traced(async { read() }).await;
assert!(!unreadable.strip_engine_overrides);
assert!(
traced.contains("WARN") && traced.contains("config couldn't be read") && traced.contains("config.json"),
"{traced}"
);
std::fs::remove_dir(&config).unwrap();
std::fs::write(&config, r#"{"currentContext": "colima""#).unwrap();
let (cut_short, not_json) = crate::testing::traced(async { read() }).await;
assert!(!cut_short.strip_engine_overrides);
assert!(not_json.contains("WARN") && not_json.contains("config isn't JSON"), "{not_json}");
}