use std::time::Duration;
use isothrum_runner::fake::FakeProcesses;
use isothrum_runner::{BoxFuture, Exit, OnLine, ProcessError, ProcessRunner, ProcessSpec};
pub(crate) struct Stuck;
impl ProcessRunner for Stuck {
fn run<'a>(&'a self, _spec: &'a ProcessSpec, _on_line: OnLine<'a>) -> BoxFuture<'a, Result<Exit, ProcessError>> {
Box::pin(async {
tokio::time::sleep(Duration::from_secs(3600)).await;
Ok(Exit::new(Some(0)))
})
}
}
pub(crate) struct StuckOn {
call: String,
rest: FakeProcesses,
}
impl StuckOn {
pub(crate) fn new(call: &str, rest: FakeProcesses) -> StuckOn {
StuckOn { call: call.to_owned(), rest }
}
}
impl ProcessRunner for StuckOn {
fn run<'a>(&'a self, spec: &'a ProcessSpec, on_line: OnLine<'a>) -> BoxFuture<'a, Result<Exit, ProcessError>> {
if spec.line() == self.call {
Stuck.run(spec, on_line)
} else {
self.rest.run(spec, on_line)
}
}
}
pub(crate) fn write(path: &std::path::Path, text: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}
pub(crate) fn kvm_holding(host: &crate::host::Host, modules: &str) {
write(&host.devices.proc_modules, modules);
write(&host.devices.kvm_virt_at_load, "Y\n");
}
pub(crate) fn on_apt(tools: FakeProcesses) -> crate::Context {
let mut host = linux_host(std::path::Path::new("/nonexistent/isothrum-machine-test"));
host.packages = Some(crate::host::Packages::Linux(crate::host::LinuxPackages::Apt));
crate::Context::new(std::sync::Arc::new(tools), host)
}
pub(crate) fn linux_host(root: &std::path::Path) -> crate::host::Host {
crate::host::Host {
os: isothrum_runner::HostOs::Linux,
arch: crate::host::Arch::Amd64,
packages: None,
devices: crate::host::Devices::under(root),
}
}
pub(crate) fn install_virtiofsd(devices: &crate::host::Devices) {
let write = |path: &std::path::Path, text: &str| {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
};
let binary = &devices.virtiofsd[0];
write(binary, "#!/bin/sh\n");
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(binary, std::fs::Permissions::from_mode(0o755)).unwrap();
}
let descriptor = serde_json::json!({ "description": "virtiofsd", "type": "fs", "binary": binary });
write(&devices.vhost_user[0].join("50-virtiofsd.json"), &descriptor.to_string());
}
pub(crate) fn answered_by(runner: impl isothrum_runner::ProcessRunner + 'static) -> crate::Context {
shared(std::sync::Arc::new(runner))
}
pub(crate) fn shared(runner: std::sync::Arc<dyn isothrum_runner::ProcessRunner>) -> crate::Context {
crate::Context::new(runner, linux_host(std::path::Path::new("/nonexistent/isothrum-machine-test")))
}
#[derive(Debug)]
pub(crate) struct Out {
pub(crate) status: isothrum_runner::Exit,
#[cfg(unix)]
pub(crate) stdout: Vec<u8>,
#[cfg(unix)]
pub(crate) stderr: Vec<u8>,
}
pub(crate) fn run(program: &str, args: &[&str]) -> Option<Out> {
run_with(program, args, &[])
}
#[cfg(target_os = "macos")]
pub(crate) fn run_env(program: &str, args: &[&str], env: &[(&str, &str)]) -> Option<Out> {
run_with(program, args, env)
}
fn run_with(program: &str, args: &[&str], env: &[(&str, &str)]) -> Option<Out> {
std::thread::scope(|scope| scope.spawn(|| run_here(program, args, env)).join().expect("the program's thread ends"))
}
fn run_here(program: &str, args: &[&str], env: &[(&str, &str)]) -> Option<Out> {
let mut spec = isothrum_runner::ProcessSpec::new(program, args.iter().copied());
spec.env = env.iter().map(|(k, v)| ((*k).to_owned(), (*v).to_owned())).collect();
let runtime = tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap();
let mut stdout = Vec::new();
let mut stderr = Vec::new();
let exit = runtime.block_on(async {
let mut keep = |stream: isothrum_runner::Stream, line: &str| {
let to = if stream == isothrum_runner::Stream::Stdout {
&mut stdout
} else {
&mut stderr
};
to.extend_from_slice(line.as_bytes());
to.push(b'\n');
};
isothrum_runner::ProcessRunner::run(&isothrum_runner::SystemProcesses::default(), &spec, &mut keep).await
});
exit.ok().map(|status| Out {
status,
#[cfg(unix)]
stdout,
#[cfg(unix)]
stderr,
})
}
#[cfg(unix)]
pub(crate) fn sh(script: &str, env: &[(&str, &str)]) -> Out {
run_with("sh", &["-c", script], env).expect("sh starts")
}
pub(crate) fn sh_parses(script: &str) -> bool {
match run("sh", &["-n", "-c", script]) {
Some(out) => out.status.success(),
None => cfg!(windows),
}
}
#[cfg(unix)]
pub(crate) fn write_executable(path: impl AsRef<std::path::Path>, contents: impl AsRef<str>) {
let path = path.as_ref();
std::fs::write(path, contents.as_ref()).unwrap();
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt as _;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
}
}
pub(crate) fn fresh_dir(tag: &str) -> std::path::PathBuf {
tempfile::Builder::new().prefix(&format!("isothrum-machine-{tag}-")).tempdir().unwrap().keep()
}