use std::path::Path;
use std::time::Duration;
use isothrum_runner::{DownloadToAdopt, RunOptions, Runner};
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};
use super::{Progress, throwaway};
use crate::Context;
use crate::docker::{NoAnswer, daemon_info, docker};
pub const IMAGE: &str = "busybox:1.36";
const MARKER: &str = "isothrum-selftest-ok";
const RUN: &str = "engine-selftest";
const ANSWER_TRIES: u32 = 10;
const RETRY: Duration = Duration::from_millis(500);
const PULL_LIMIT: Duration = Duration::from_secs(300);
const REPLY_LIMIT: Duration = Duration::from_secs(3);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Step {
Engine,
Pull,
Start,
Network,
Port,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Found {
Version(String),
Image {
held: bool,
},
Nothing,
Reached,
Address(String),
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Failure {
Engine(NoAnswer),
Said(String),
}
impl std::fmt::Display for Failure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Failure::Engine(why) => why.fmt(f),
Failure::Said(said) => f.write_str(said),
}
}
}
pub type StepProgress = Progress<Step, Found, Failure>;
#[derive(Debug)]
#[non_exhaustive]
pub struct Ran {
pub passed: Result<(), Failure>,
pub cleaned_up: Result<(), String>,
pub pulled: Option<DownloadToAdopt>,
}
fn compose_file() -> String {
format!(
r#"services:
web:
image: {IMAGE}
hostname: {MARKER}
command: ["httpd", "-f", "-p", "80", "-h", "/etc"]
ports: ["127.0.0.1::80"]
probe:
image: {IMAGE}
command: ["sleep", "300"]
"#
)
}
async fn image_id(cx: &Context, context: Option<&str>) -> Option<String> {
let spec = docker(context, &["image", "inspect", "--format", "{{.Id}}", IMAGE]);
let out = cx.probe_within(cx.probe_timeout, &spec).await.ok()?.output().ok()?;
Some(out.trim().to_owned()).filter(|id| !id.is_empty())
}
pub async fn pull(cx: &Context, context: Option<&str>) -> Result<(bool, Option<DownloadToAdopt>), String> {
if image_id(cx, context).await.is_some() {
return Ok((true, None));
}
let spec = docker(context, &["pull", "--quiet", IMAGE]);
let pulled = cx.probe_within(PULL_LIMIT, &spec).await.map_err(|e| e.to_string())?;
pulled.output().map_err(|e| e.reason)?;
let image_id = image_id(cx, context).await;
Ok((
false,
Some(DownloadToAdopt::Image {
reference: IMAGE.to_owned(),
image_id,
}),
))
}
pub async fn run(cx: &Context, dir: &Path, context: Option<&str>, progress: &(dyn Fn(StepProgress) + Send + Sync)) -> Ran {
let mut pulled = None;
let made = throwaway(cx, dir, "compose.yaml", &compose_file());
let (runner, project) = match made {
Ok(test) => test,
Err(e) => {
return Ran {
passed: Err(Failure::Said(e)),
cleaned_up: Ok(()),
pulled,
};
}
};
let opts = context.map_or_else(RunOptions::new, |c| RunOptions::new().with_docker_context(c));
let test = Test {
cx,
runner: &runner,
project: &project,
context,
opts: &opts,
};
let passed = Box::pin(steps(&test, &mut pulled, progress)).await;
let cleaned_up = match super::open(&runner, &project, RUN) {
Ok(run) => run.down(&opts, &super::quiet).await.map_err(|e| e.to_string()),
Err(e) => Err(e),
};
Ran { passed, cleaned_up, pulled }
}
struct Test<'a> {
cx: &'a Context,
runner: &'a Runner,
project: &'a Path,
context: Option<&'a str>,
opts: &'a RunOptions,
}
async fn steps(test: &Test<'_>, pulled: &mut Option<DownloadToAdopt>, progress: &(dyn Fn(StepProgress) + Send + Sync)) -> Result<(), Failure> {
let Test {
cx,
runner,
project,
context,
opts,
} = *test;
let step = super::Steps(progress);
step.run(Step::Engine, async {
daemon_info(cx, "{{.ServerVersion}}", context)
.await
.map(|v| Found::Version(v.trim().to_owned()))
.map_err(Failure::Engine)
})
.await?;
step.run(Step::Pull, async {
let (held, image) = pull(cx, context).await.map_err(Failure::Said)?;
*pulled = image;
Ok(Found::Image { held })
})
.await?;
let run = super::open(runner, project, RUN).map_err(Failure::Said)?;
let up = Box::pin(async {
run.up(opts, &super::quiet)
.await
.map(|_| Found::Nothing)
.map_err(|e| Failure::Said(e.to_string()))
});
step.run(Step::Start, up).await?;
step.run(Step::Network, async { reach_by_name(&run, opts).await.map_err(Failure::Said) })
.await?;
step.run(Step::Port, async { reach_port(&run, opts).await.map_err(Failure::Said) }).await?;
Ok(())
}
async fn reach_by_name(run: &isothrum_runner::Running<'_>, opts: &RunOptions) -> Result<Found, String> {
let argv = ["wget", "-qO-", "-T", "2", "http://web/hostname"].map(str::to_owned);
let mut last = String::new();
for _ in 0..ANSWER_TRIES {
match run.exec("probe", &argv, opts, &super::quiet).await {
Ok(ran) if ran.stdout.contains(MARKER) => return Ok(Found::Reached),
Ok(ran) => format!("{} {}", ran.stdout.trim(), ran.stderr.trim()).trim().clone_into(&mut last),
Err(e) => last = e.to_string(),
}
tokio::time::sleep(RETRY).await;
}
Err(format!("probe could not reach web: {last}"))
}
async fn reach_port(run: &isothrum_runner::Running<'_>, opts: &RunOptions) -> Result<Found, String> {
let status = run.status_with(opts).await.map_err(|e| e.to_string())?;
let port = status
.machines
.iter()
.find(|m| m.name == "web")
.and_then(|m| m.published.first())
.map(|p| p.host_port.get());
let address = format!("127.0.0.1:{}", port.ok_or("web publishes no port")?);
let mut last = String::new();
for _ in 0..ANSWER_TRIES {
match get(&address).await {
Ok(body) if body.contains(MARKER) => return Ok(Found::Address(address)),
Ok(body) => last = body,
Err(e) => last = e,
}
tokio::time::sleep(RETRY).await;
}
Err(format!("http://{address} did not answer: {last}"))
}
async fn get(address: &str) -> Result<String, String> {
let ask = async {
let mut stream = tokio::net::TcpStream::connect(address).await?;
stream.write_all(b"GET /hostname HTTP/1.0\r\nHost: localhost\r\n\r\n").await?;
let mut reply = Vec::new();
stream.read_to_end(&mut reply).await?;
Ok::<_, std::io::Error>(String::from_utf8_lossy(&reply).into_owned())
};
match tokio::time::timeout(REPLY_LIMIT, ask).await {
Ok(Ok(reply)) => Ok(reply),
Ok(Err(e)) => Err(e.to_string()),
Err(_) => Err(format!("no reply within {}s", REPLY_LIMIT.as_secs())),
}
}
#[cfg(test)]
mod tests;