use serde::{Deserialize, Serialize};
use std::path::Path;
use std::time::{Duration, Instant};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Status {
Ok,
Warn,
Fail,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Check {
pub id: String,
pub label: String,
pub status: Status,
pub detail: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fix: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub diagnostics: Option<String>,
}
pub const FIX_RESET_HUB: &str = "reset_hub";
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Report {
pub env: String,
pub api_url: String,
pub os: String,
pub arch: String,
pub checks: Vec<Check>,
}
impl Report {
pub fn ok(&self) -> bool {
self.checks.iter().all(|c| c.status != Status::Fail)
}
}
fn check(id: &str, label: impl Into<String>, status: Status, detail: impl Into<String>) -> Check {
Check {
id: id.into(),
label: label.into(),
status,
detail: detail.into(),
fix: None,
diagnostics: None,
}
}
impl Check {
fn with_fix(mut self, fix: &str) -> Self {
self.fix = Some(fix.to_string());
self
}
fn with_diagnostics(mut self, d: impl Into<String>) -> Self {
let d = d.into();
self.diagnostics = (!d.is_empty()).then_some(d);
self
}
}
pub fn total_memory() -> Option<u64> {
#[cfg(target_os = "linux")]
{
let meminfo = std::fs::read_to_string("/proc/meminfo").ok()?;
let read = |p: &str| std::fs::read_to_string(p).ok();
let own = read("/proc/self/cgroup").unwrap_or_default();
Some(effective_memory(
meminfo_total(&meminfo)?,
cgroup_memory_limit(&own, &read),
))
}
#[cfg(target_os = "macos")]
{
let out = std::process::Command::new("/usr/sbin/sysctl")
.args(["-n", "hw.memsize"])
.output()
.ok()?;
String::from_utf8_lossy(&out.stdout).trim().parse().ok()
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
None
}
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn meminfo_total(meminfo: &str) -> Option<u64> {
let kb: u64 = meminfo
.lines()
.find(|l| l.starts_with("MemTotal:"))?
.split_whitespace()
.nth(1)?
.parse()
.ok()?;
Some(kb * 1024)
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn cgroup_memory_limit(
self_cgroup: &str,
read: &dyn Fn(&str) -> Option<String>,
) -> Option<u64> {
let parse = |v: Option<String>| -> Option<u64> {
let v = v?;
let v = v.trim();
if v == "max" {
return None;
}
v.parse::<u64>().ok()
};
if let Some(path) = self_cgroup.lines().find_map(|l| l.strip_prefix("0::")) {
let mut dir = path.trim().trim_end_matches('/').to_string();
let mut lowest: Option<u64> = None;
loop {
if let Some(v) = parse(read(&format!("/sys/fs/cgroup{dir}/memory.max"))) {
lowest = Some(lowest.map_or(v, |l| l.min(v)));
}
if dir.is_empty() {
break;
}
dir = match dir.rfind('/') {
Some(i) => dir[..i].to_string(),
None => String::new(),
};
}
if lowest.is_some() {
return lowest;
}
}
parse(read("/sys/fs/cgroup/memory/memory.limit_in_bytes"))
}
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub fn effective_memory(meminfo_total: u64, cgroup_limit: Option<u64>) -> u64 {
match cgroup_limit {
Some(l) if l > 0 => meminfo_total.min(l),
_ => meminfo_total,
}
}
pub fn free_disk(path: &Path) -> Option<u64> {
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
let c = std::ffi::CString::new(path.as_os_str().as_bytes()).ok()?;
let mut st: libc::statvfs = unsafe { std::mem::zeroed() };
if unsafe { libc::statvfs(c.as_ptr(), &mut st) } != 0 {
return None;
}
#[allow(clippy::unnecessary_cast)]
Some(st.f_bavail as u64 * st.f_frsize as u64)
}
#[cfg(not(unix))]
{
let _ = path;
None
}
}
const GB: f64 = 1024.0 * 1024.0 * 1024.0;
const MIN_FREE_DISK_GB: f64 = 20.0;
const MIN_MEMORY_GB: f64 = 4.0;
pub fn cpu_check(cpus: usize) -> Check {
let lend = (cpus / 2).max(1);
check(
"cpu",
format!("{cpus} CPUs"),
if cpus >= 2 { Status::Ok } else { Status::Warn },
format!("lends up to {lend} by default (half, at least one)"),
)
}
pub fn memory_check(bytes: Option<u64>) -> Check {
match bytes {
Some(b) => {
let gb = b as f64 / GB;
check(
"memory",
format!("{gb:.0} GB memory"),
if gb >= MIN_MEMORY_GB {
Status::Ok
} else {
Status::Warn
},
if gb >= MIN_MEMORY_GB {
"enough for public jobs".to_string()
} else {
format!("under {MIN_MEMORY_GB:.0} GB: few jobs will fit")
},
)
}
None => check("memory", "Memory", Status::Warn, "could not read the total"),
}
}
pub fn disk_check(bytes: Option<u64>) -> Check {
match bytes {
Some(b) => {
let gb = b as f64 / GB;
check(
"disk",
format!("{gb:.0} GB free disk"),
if gb >= MIN_FREE_DISK_GB {
Status::Ok
} else {
Status::Warn
},
if gb >= MIN_FREE_DISK_GB {
"room for job images".to_string()
} else {
format!("under {MIN_FREE_DISK_GB:.0} GB: job images may not fit")
},
)
}
None => check(
"disk",
"Free disk",
Status::Warn,
"could not read free space",
),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomHub {
pub origin: &'static str,
pub fixable: bool,
}
impl CustomHub {
pub fn place(&self) -> &'static str {
match self.origin {
"ZAKURO_API_URL" => "the environment Zakuro was started with",
file => file,
}
}
}
fn same_url(a: &str, b: &str) -> bool {
a.trim().trim_end_matches('/') == b.trim().trim_end_matches('/')
}
pub fn custom_hub(
api_url: &str,
env: crate::profile::Env,
credentials: Option<&str>,
env_file: Option<&str>,
) -> Option<CustomHub> {
if same_url(api_url, env.profile().api_url) {
return None;
}
let stored = |text: Option<&str>, key: &str| {
text.and_then(|t| crate::credentials::parse(t).remove(key))
.map(|u| crate::credentials::migrate_legacy_api_url(&u))
.is_some_and(|u| same_url(&u, api_url))
};
Some(if stored(credentials, "api_url") {
CustomHub {
origin: "~/.zakuro/credentials",
fixable: true,
}
} else if stored(env_file, "ZAKURO_API_URL") {
CustomHub {
origin: "~/.zakuro/env",
fixable: true,
}
} else {
CustomHub {
origin: "ZAKURO_API_URL",
fixable: false,
}
})
}
fn is_loopback(host: &str) -> bool {
let h = host.rsplit_once(':').map_or(host, |(h, port)| {
if port.chars().all(|c| c.is_ascii_digit()) {
h
} else {
host
}
});
let h = h.trim_start_matches('[').trim_end_matches(']');
h.eq_ignore_ascii_case("localhost")
|| h == "::1"
|| h == "0.0.0.0"
|| h.parse::<std::net::Ipv4Addr>()
.is_ok_and(|a| a.is_loopback())
}
const HUB_ATTEMPTS: usize = 3;
const HUB_BACKOFF: [Duration; 2] = [Duration::from_millis(500), Duration::from_millis(1500)];
fn hub_check(api_url: &str, env: crate::profile::Env, custom: Option<&CustomHub>) -> Check {
let url = format!("{}/api/health", api_url.trim_end_matches('/'));
let agent = ureq::Agent::new_with_config(
ureq::Agent::config_builder()
.http_status_as_error(false)
.timeout_global(Some(Duration::from_secs(5)))
.build(),
);
hub_check_with(
api_url,
env,
custom,
|| {
let t = Instant::now();
agent
.get(&url)
.call()
.map(|r| (r.status().as_u16(), t.elapsed()))
.map_err(|e| e.to_string())
},
std::thread::sleep,
)
}
fn hub_check_with(
api_url: &str,
env: crate::profile::Env,
custom: Option<&CustomHub>,
mut get: impl FnMut() -> Result<(u16, Duration), String>,
mut sleep: impl FnMut(Duration),
) -> Check {
let host = host_of(api_url);
let mut last = String::new();
for attempt in 0..HUB_ATTEMPTS {
if attempt > 0 {
sleep(HUB_BACKOFF[attempt - 1]);
}
match get() {
Ok((s, t)) if (200..300).contains(&s) => {
return check(
"hub",
"Hub reachable",
Status::Ok,
format!("{host} · {} ms", t.as_millis()),
)
}
Ok((s, _)) => {
return check(
"hub",
"Hub reachable",
Status::Fail,
format!("{host} is having trouble right now; try again in a minute"),
)
.with_diagnostics(format!("GET {host}/api/health: HTTP {s}"))
}
Err(e) => last = e,
}
}
match custom {
Some(c) if is_loopback(host) => {
let detail = format!(
"nothing is answering at {host}, a development hub set in {}; \
this machine's {} hub is {}",
c.place(),
env.name(),
host_of(env.profile().api_url)
);
let fail = check("hub", "Hub not reachable", Status::Fail, detail)
.with_diagnostics(format!("GET {host}/api/health: {last}"));
if c.fixable {
fail.with_fix(FIX_RESET_HUB)
} else {
fail
}
}
_ => check(
"hub",
"Hub not reachable",
Status::Fail,
format!("can't reach {host}; check this machine's internet connection"),
)
.with_diagnostics(format!("GET {host}/api/health: {last}")),
}
}
fn without_keys(text: &str, keys: &[&str]) -> String {
text.lines()
.filter(|l| {
let l = l.trim();
let l = l.strip_prefix("export ").unwrap_or(l);
!l.split_once('=')
.is_some_and(|(k, _)| keys.contains(&k.trim()))
})
.map(|l| format!("{l}\n"))
.collect()
}
pub fn reset_hub(state_dir: &Path) -> std::io::Result<()> {
for (name, keys) in [
("credentials", &["api_url", "api_key"][..]),
("env", &["ZAKURO_API_URL", "ZAKURO_API_KEY"][..]),
] {
let p = state_dir.join(name);
let text = match std::fs::read_to_string(&p) {
Ok(t) => t,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => return Err(e),
};
std::fs::write(&p, without_keys(&text, keys))?;
}
Ok(())
}
pub fn run_fix(id: &str) -> Result<(), String> {
match id {
FIX_RESET_HUB => {
let dir = crate::credentials::dir().ok_or("no home directory")?;
reset_hub(&dir).map_err(|e| format!("could not reset the hub address: {e}"))
}
other => Err(format!("unknown fix {other:?}")),
}
}
fn host_of(url: &str) -> &str {
let s = url.split("://").nth(1).unwrap_or(url);
s.split('/').next().unwrap_or(s)
}
pub fn sandbox_check(sel: &crate::sandbox::Selection, macos: bool) -> Check {
use crate::sandbox::RuntimeKind;
let name = |k: RuntimeKind| match k {
RuntimeKind::Lima => "Lima VM",
RuntimeKind::Colima => "Colima VM",
RuntimeKind::Lxc => "LXC container",
RuntimeKind::Docker => "Docker",
RuntimeKind::Host => "host",
};
let skipped: Vec<String> = sel
.tried
.iter()
.take_while(|p| Some(p.kind) != sel.chosen)
.filter(|p| !p.available)
.map(|p| format!("{}: {}", p.kind.as_str(), p.detail))
.collect();
match sel.chosen {
Some(k) if k != RuntimeKind::Host => check(
"runtime",
format!("Sandbox: {}", name(k)),
Status::Ok,
"jobs run isolated",
)
.with_diagnostics(if skipped.is_empty() {
String::new()
} else {
format!("stronger isolation skipped ({})", skipped.join("; "))
}),
_ => {
let too_old = sel
.tried
.iter()
.any(|p| p.detail.contains("is too old for Lima's vz"));
let (detail, hint) = if macos && too_old {
(
"this version of macOS can't run Zakuro's sandbox (macOS 13.5 or newer is needed), so jobs that need isolation can't run on this Mac",
"update macOS",
)
} else if macos {
(
"Zakuro's sandbox is not set up yet, so jobs that need isolation can't run on this Mac",
"install Lima 2 (`brew install lima`) or Colima (`brew install colima`)",
)
} else {
(
"Incus is not set up, so jobs that need isolation can't run on this machine",
"install Incus (`apt install incus && incus admin init --minimal`) or Docker",
)
};
let mut diag = format!("host workers only: {hint}");
if !skipped.is_empty() {
diag.push_str(&format!(" ({})", skipped.join("; ")));
}
check("runtime", "No isolating sandbox", Status::Warn, detail).with_diagnostics(diag)
}
}
}
pub fn signed_in_check(file_key: bool, env_key: bool) -> Check {
if file_key || env_key {
check(
"signed_in",
"Signed in",
Status::Ok,
if file_key {
"key in ~/.zakuro/credentials"
} else {
"key from the environment Zakuro was started with"
},
)
.with_diagnostics(if file_key { "" } else { "ZAKURO_API_KEY" })
} else {
check(
"signed_in",
"Not signed in",
Status::Fail,
"sign in to continue",
)
.with_diagnostics("zc login")
}
}
pub fn profile_check(
env: crate::profile::Env,
api_url: &str,
unknown_env: Option<&str>,
custom: Option<&CustomHub>,
) -> Check {
if let Some(v) = unknown_env {
return check(
"profile",
format!("Profile {}", env.name()),
Status::Warn,
"unknown environment setting; using prod",
)
.with_diagnostics(format!("ZAKURO_ENV={v:?} is not lab|staging|prod"));
}
match custom {
Some(c) => {
let warn = check(
"profile",
format!("Custom hub, not the {} hub", env.name()),
Status::Warn,
format!(
"{} set in {}; the {} hub is {}",
host_of(api_url),
c.place(),
env.name(),
host_of(env.profile().api_url)
),
)
.with_diagnostics(format!("{api_url} from {}", c.origin));
if c.fixable {
warn.with_fix(FIX_RESET_HUB)
} else {
warn
}
}
None => check(
"profile",
format!("Profile {}", env.name()),
Status::Ok,
host_of(api_url).to_string(),
),
}
}
fn hub_action_kids(api_url: &str) -> Option<Vec<String>> {
let url = format!("{}/api/broker/action/pubkey", api_url.trim_end_matches('/'));
let agent = ureq::Agent::new_with_config(
ureq::Agent::config_builder()
.timeout_global(Some(Duration::from_secs(5)))
.build(),
);
let body = agent
.get(&url)
.call()
.ok()?
.body_mut()
.read_to_string()
.ok()?;
parse_action_kids(&body)
}
fn parse_action_kids(body: &str) -> Option<Vec<String>> {
let v: serde_json::Value = serde_json::from_str(body).ok()?;
v.get("keys")?
.as_array()?
.iter()
.map(|k| {
let b64 = k.get("pubkey").or_else(|| k.get("x"))?.as_str()?;
crate::deeplink::TrustedKey::from_b64(b64)
.ok()
.map(|t| t.kid)
})
.collect()
}
pub fn action_key_check(
env: crate::profile::Env,
local: Result<Vec<crate::deeplink::TrustedKey>, String>,
hub_kids: Option<&[String]>,
) -> Check {
let local = match local {
Ok(k) => k,
Err(e) => {
return check(
"action_key",
"Open in Zakuro links",
Status::Fail,
"the links key setting is not valid",
)
.with_diagnostics(e)
}
};
let hub_signs = hub_kids.filter(|k| !k.is_empty());
let trusted = |kid: &String| local.iter().any(|t| &t.kid == kid);
match hub_signs {
Some(kids) if !kids.iter().any(trusted) => check(
"action_key",
"Open in Zakuro links off",
Status::Warn,
format!(
"the {} hub signs links with key {}, which this version of Zakuro \
does not know yet; update Zakuro",
env.name(),
kids[0]
),
),
Some(kids) => check(
"action_key",
"Open in Zakuro links",
Status::Ok,
format!("hub key {}", kids.iter().find(|k| trusted(k)).unwrap()),
),
None if hub_kids.is_some() => check(
"action_key",
"Open in Zakuro links",
Status::Ok,
format!("the {} hub does not sign links yet", env.name()),
),
None => match local.first() {
Some(k) => check(
"action_key",
"Open in Zakuro links",
Status::Ok,
format!("hub key {}", k.kid),
),
None => check(
"action_key",
"Open in Zakuro links",
Status::Ok,
format!("no {} hub key in this version", env.name()),
),
},
}
}
pub fn machine_report(with_network: bool) -> Report {
let resolved = crate::profile::resolve();
let api_url = resolved.api_url.0.clone();
let env = crate::deeplink::env_for_api_url(Some(&api_url));
let state_dir = crate::credentials::dir();
let file_key = crate::credentials::path()
.and_then(|p| std::fs::read_to_string(p).ok())
.map(|t| crate::credentials::parse(&t))
.and_then(|m| m.get("api_key").cloned())
.is_some_and(|k| !k.trim().is_empty());
let env_key = std::env::var("ZAKURO_API_KEY").is_ok_and(|k| !k.trim().is_empty());
let read = |name: &str| {
state_dir
.as_ref()
.and_then(|d| std::fs::read_to_string(d.join(name)).ok())
};
let custom = custom_hub(
&api_url,
env,
read("credentials").as_deref(),
read("env").as_deref(),
);
let mut checks = vec![profile_check(
env,
&api_url,
resolved.unknown_env.as_deref(),
custom.as_ref(),
)];
checks.push(signed_in_check(file_key, env_key));
if with_network {
checks.push(hub_check(&api_url, env, custom.as_ref()));
}
let cpus = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
checks.push(cpu_check(cpus));
checks.push(memory_check(total_memory()));
let home = state_dir
.clone()
.or_else(|| std::env::var_os("HOME").map(Into::into))
.unwrap_or_else(|| "/".into());
let probe_dir = if home.exists() { home } else { "/".into() };
checks.push(disk_check(free_disk(&probe_dir)));
let active = crate::sandbox::engine::active();
checks.push(sandbox_check(&active.selection, cfg!(target_os = "macos")));
let node_key = crate::broker::node_identity::NodeKey::load_in(state_dir);
checks.push(match node_key {
Some(k) => check(
"node_key",
"Node identity",
Status::Ok,
format!("zc://node-{}", k.fingerprint()),
),
None => check(
"node_key",
"Node identity",
Status::Ok,
"created when sharing starts",
),
});
let hub_kids = if with_network {
hub_action_kids(&api_url)
} else {
None
};
checks.push(action_key_check(
env,
crate::deeplink::keys_for(env),
hub_kids.as_deref(),
));
Report {
env: env.name().to_string(),
api_url,
os: std::env::consts::OS.to_string(),
arch: std::env::consts::ARCH.to_string(),
checks,
}
}
pub fn format_check(c: &Check) -> String {
let mark = match c.status {
Status::Ok => "✓",
Status::Warn => "!",
Status::Fail => "✗",
};
match &c.diagnostics {
Some(d) => format!(" {mark} {:<30} {} ({d})", c.label, c.detail),
None => format!(" {mark} {:<30} {}", c.label, c.detail),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn thresholds_warn_but_never_fail_on_hardware() {
assert_eq!(cpu_check(12).status, Status::Ok);
assert!(cpu_check(12).detail.contains("up to 6"));
assert_eq!(cpu_check(1).status, Status::Warn);
assert_eq!(
memory_check(Some(36 * 1024 * 1024 * 1024)).label,
"36 GB memory"
);
assert_eq!(
memory_check(Some(2 * 1024 * 1024 * 1024)).status,
Status::Warn
);
assert_eq!(
disk_check(Some(10 * 1024 * 1024 * 1024)).status,
Status::Warn
);
assert_eq!(disk_check(None).status, Status::Warn);
assert_eq!(signed_in_check(false, false).status, Status::Fail);
assert_eq!(
signed_in_check(false, true).diagnostics.as_deref(),
Some("ZAKURO_API_KEY")
);
}
#[test]
fn a_leftover_dev_hub_is_named_and_offered_a_reset() {
use crate::profile::Env;
let creds = "api_key=zk_dev\napi_url=http://localhost:8000\n";
let c = custom_hub("http://localhost:8000", Env::Prod, Some(creds), None).unwrap();
assert_eq!(c.origin, "~/.zakuro/credentials");
assert!(c.fixable);
let p = profile_check(Env::Prod, "http://localhost:8000", None, Some(&c));
assert_eq!(p.status, Status::Warn);
assert_eq!(p.label, "Custom hub, not the prod hub");
assert!(p
.detail
.contains("localhost:8000 set in ~/.zakuro/credentials"));
assert!(p.detail.contains("hub.zakuro-ai.com"));
assert_eq!(p.fix.as_deref(), Some(FIX_RESET_HUB));
let mut slept = vec![];
let h = hub_check_with(
"http://localhost:8000",
Env::Prod,
Some(&c),
|| Err("io: Connection refused".into()),
|d| slept.push(d),
);
assert_eq!(h.status, Status::Fail);
assert!(h
.detail
.starts_with("nothing is answering at localhost:8000"));
assert_eq!(h.fix.as_deref(), Some(FIX_RESET_HUB));
assert_eq!(slept, HUB_BACKOFF.to_vec(), "3 attempts, backed off");
}
#[test]
fn the_environments_own_hubs_are_not_custom() {
use crate::profile::Env;
let prod = crate::credentials::PROD_API_URL;
let stg = crate::credentials::STAGING_API_URL;
assert_eq!(custom_hub(prod, Env::Prod, None, None), None);
assert_eq!(custom_hub(&format!("{prod}/"), Env::Prod, None, None), None);
let creds = format!("api_url={stg}\n");
assert_eq!(custom_hub(stg, Env::Staging, Some(&creds), None), None);
let p = profile_check(Env::Prod, prod, None, None);
assert_eq!((p.status, p.fix), (Status::Ok, None));
let c = custom_hub("http://127.0.0.1:9", Env::Prod, None, None).unwrap();
assert_eq!((c.origin, c.fixable), ("ZAKURO_API_URL", false));
let p = profile_check(Env::Prod, "http://127.0.0.1:9", None, Some(&c));
assert_eq!(p.fix, None);
let env_file = "ZAKURO_API_URL=https://my.zakuro-ai.com\n";
let c = custom_hub("http://10.0.0.5:8000", Env::Prod, None, Some(env_file));
assert_eq!(c.unwrap().origin, "ZAKURO_API_URL");
}
#[test]
fn the_hub_check_rides_out_a_blip_and_names_http_errors() {
use crate::profile::Env;
let mut calls = 0;
let h = hub_check_with(
"https://hub.zakuro-ai.com",
Env::Prod,
None,
|| {
calls += 1;
if calls < 3 {
Err("timeout".into())
} else {
Ok((200, Duration::from_millis(42)))
}
},
|_| {},
);
assert_eq!(h.status, Status::Ok);
assert_eq!(h.detail, "hub.zakuro-ai.com · 42 ms");
let mut calls = 0;
let h = hub_check_with(
"https://hub.zakuro-ai.com",
Env::Prod,
None,
|| {
calls += 1;
Ok((502, Duration::ZERO))
},
|_| {},
);
assert_eq!((h.status, calls), (Status::Fail, 1));
assert_eq!(
h.detail,
"hub.zakuro-ai.com is having trouble right now; try again in a minute"
);
assert_eq!(
h.diagnostics.as_deref(),
Some("GET hub.zakuro-ai.com/api/health: HTTP 502")
);
let h = hub_check_with(
"https://hub.zakuro-ai.com",
Env::Prod,
None,
|| Err("dns failed".into()),
|_| {},
);
assert_eq!(
h.detail,
"can't reach hub.zakuro-ai.com; check this machine's internet connection"
);
assert_eq!(
h.diagnostics.as_deref(),
Some("GET hub.zakuro-ai.com/api/health: dns failed")
);
assert_eq!(h.fix, None);
}
#[test]
fn loopback_hosts() {
for h in [
"localhost:8000",
"127.0.0.1",
"127.0.0.1:8000",
"[::1]:8000",
"0.0.0.0:80",
] {
assert!(is_loopback(h), "{h}");
}
for h in [
"hub.zakuro-ai.com",
"10.13.13.1:9000",
"localhost.example.com",
] {
assert!(!is_loopback(h), "{h}");
}
}
#[test]
fn reset_hub_drops_the_custom_hub_and_its_key_only() {
let dir = std::env::temp_dir().join(format!("zc-reset-hub-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(
dir.join("credentials"),
"# zc\napi_key=zk_dev\napi_url=http://localhost:8000\nenv=prod\n",
)
.unwrap();
std::fs::write(
dir.join("env"),
"export ZAKURO_API_URL=http://localhost:8000\nZAKURO_LOG=debug\n",
)
.unwrap();
reset_hub(&dir).unwrap();
assert_eq!(
std::fs::read_to_string(dir.join("credentials")).unwrap(),
"# zc\nenv=prod\n"
);
assert_eq!(
std::fs::read_to_string(dir.join("env")).unwrap(),
"ZAKURO_LOG=debug\n"
);
reset_hub(&dir.join("missing")).unwrap();
let _ = std::fs::remove_dir_all(&dir);
assert_eq!(run_fix("nope"), Err("unknown fix \"nope\"".into()));
}
const PROD_PUBKEY_BODY: &str = r#"{"alg":"EdDSA","kid":null,"pubkey":null,"keys":[]}"#;
const STG_PUBKEY_BODY: &str = r#"{"alg":"EdDSA","kid":"bde74a18996820b2","pubkey":"dOhGMGFe0iJu4g6KvZKsG8MwK/W0i8WX4l3tXbsmL04=","keys":[{"kty":"OKP","crv":"Ed25519","alg":"EdDSA","use":"sig","kid":"bde74a18996820b2","x":"dOhGMGFe0iJu4g6KvZKsG8MwK_W0i8WX4l3tXbsmL04","pubkey":"dOhGMGFe0iJu4g6KvZKsG8MwK/W0i8WX4l3tXbsmL04="}]}"#;
#[test]
fn a_hub_that_signs_no_links_is_not_a_warning() {
use crate::profile::Env;
let kids = parse_action_kids(PROD_PUBKEY_BODY).unwrap();
assert!(kids.is_empty());
let c = action_key_check(Env::Prod, Ok(vec![]), Some(&kids));
assert_eq!(c.status, Status::Ok);
assert_eq!(c.detail, "the prod hub does not sign links yet");
assert!(!c.detail.contains("ZAKURO_"), "{}", c.detail);
}
#[test]
fn the_staging_hub_key_is_trusted_by_kid() {
use crate::profile::Env;
let kids = parse_action_kids(STG_PUBKEY_BODY).unwrap();
assert_eq!(kids, vec!["bde74a18996820b2".to_string()]);
let local = crate::deeplink::keys_for(Env::Staging);
let c = action_key_check(Env::Staging, local, Some(&kids));
assert_eq!(
(c.status, c.detail.as_str()),
(Status::Ok, "hub key bde74a18996820b2")
);
}
#[test]
fn a_hub_key_this_zc_does_not_know_asks_for_an_update() {
use crate::profile::Env;
let kids = parse_action_kids(STG_PUBKEY_BODY).unwrap();
let c = action_key_check(Env::Prod, Ok(vec![]), Some(&kids));
assert_eq!(c.status, Status::Warn);
assert!(c.detail.contains("bde74a18996820b2"));
assert!(c.detail.ends_with("update Zakuro"));
let c = action_key_check(Env::Prod, Ok(vec![]), None);
assert_eq!(c.status, Status::Ok);
let c = action_key_check(Env::Prod, Err("bad key".into()), None);
assert_eq!(c.status, Status::Fail);
assert_eq!(parse_action_kids("not json"), None);
}
#[test]
fn report_ok_means_nothing_failed() {
let r = Report {
env: "prod".into(),
api_url: "https://hub.zakuro-ai.com".into(),
os: "linux".into(),
arch: "x86_64".into(),
checks: vec![cpu_check(1), signed_in_check(true, false)],
};
assert!(r.ok());
let v = serde_json::to_value(&r).unwrap();
assert_eq!(v["checks"][0]["status"], "warn");
assert_eq!(
host_of("https://hub.zakuro-ai.com/api"),
"hub.zakuro-ai.com"
);
}
#[test]
fn a_mac_too_old_for_the_vm_says_so() {
use crate::sandbox::{Probe, RuntimeKind, Selection};
let sel = Selection {
chosen: None,
tried: vec![Probe {
kind: RuntimeKind::Lima,
available: false,
detail: crate::sandbox::provision::too_old(Some((12, 7))).unwrap(),
}],
};
let c = sandbox_check(&sel, true);
assert!(
c.detail.starts_with("this version of macOS can't run"),
"{}",
c.detail
);
assert!(c.diagnostics.unwrap().contains("macOS 12.7 is too old"));
}
#[test]
fn the_sandbox_check_reports_the_engine_jobs_use() {
use crate::sandbox::{Probe, RuntimeKind, Selection};
let probe = |kind, available, detail: &str| Probe {
kind,
available,
detail: detail.into(),
};
let sel = Selection {
chosen: Some(RuntimeKind::Colima),
tried: vec![
probe(RuntimeKind::Lima, false, "limactl 1.0.6 is too old"),
probe(RuntimeKind::Colima, true, "colima 0.8.1"),
probe(RuntimeKind::Docker, false, "no docker socket"),
],
};
let c = sandbox_check(&sel, true);
assert_eq!(c.status, Status::Ok);
assert_eq!(c.label, "Sandbox: Colima VM");
assert_eq!(c.detail, "jobs run isolated");
let diag = c.diagnostics.unwrap();
assert!(diag.contains("lima: limactl 1.0.6 is too old"), "{diag}");
assert!(!diag.contains("docker"), "only the stronger ones: {diag}");
let lima = Selection {
chosen: Some(RuntimeKind::Lima),
tried: vec![probe(RuntimeKind::Lima, true, "ok")],
};
assert_eq!(sandbox_check(&lima, true).detail, "jobs run isolated");
let none = Selection {
chosen: None,
tried: vec![probe(RuntimeKind::Lima, false, "limactl not found")],
};
let c = sandbox_check(&none, true);
assert_eq!(c.status, Status::Warn);
assert!(c.detail.ends_with("can't run on this Mac"), "{}", c.detail);
let diag = c.diagnostics.unwrap();
assert!(diag.contains("brew install lima"), "{diag}");
assert!(
!diag.contains("apt") && !diag.contains("systemctl"),
"{diag}"
);
let linux = sandbox_check(&none, false);
assert!(linux.detail.starts_with("Incus is not set up"));
assert!(!linux.diagnostics.unwrap().contains("brew"));
}
#[test]
fn default_text_names_no_command_or_variable() {
use crate::profile::Env;
use crate::sandbox::{Probe, RuntimeKind, Selection};
let none = Selection {
chosen: None,
tried: vec![
Probe {
kind: RuntimeKind::Lima,
available: false,
detail: "limactl is not installed (brew install lima, or set ZAKURO_LIMACTL)"
.into(),
},
Probe {
kind: RuntimeKind::Docker,
available: false,
detail: "docker socket /var/run/docker.sock does not exist (start Docker Desktop, Colima or OrbStack, or set DOCKER_HOST)".into(),
},
],
};
let env_only = CustomHub {
origin: "ZAKURO_API_URL",
fixable: false,
};
let checks = [
sandbox_check(&none, true),
sandbox_check(&none, false),
signed_in_check(false, false),
signed_in_check(false, true),
profile_check(Env::Prod, "http://x:1", Some("dev"), None),
profile_check(Env::Prod, "http://x:1", None, Some(&env_only)),
hub_check_with(
"http://x:1",
Env::Prod,
Some(&env_only),
|| Err("refused".into()),
|_| {},
),
hub_check_with(
"https://hub.zakuro-ai.com",
Env::Prod,
None,
|| Err("dns".into()),
|_| {},
),
action_key_check(
Env::Prod,
Err("ZAKURO_ACTION_PUBKEYS: 'x' is not base64".into()),
None,
),
];
for c in &checks {
for bad in [
"ZAKURO_",
"DOCKER_HOST",
"brew ",
"apt ",
"systemctl",
"zc login",
"limactl",
] {
assert!(
!c.detail.contains(bad),
"{}: {:?} in {:?}",
c.id,
bad,
c.detail
);
}
}
assert!(checks[0]
.diagnostics
.as_deref()
.unwrap()
.contains("ZAKURO_LIMACTL"));
assert!(format_check(&checks[2]).ends_with("sign in to continue (zc login)"));
}
#[test]
fn docker_hints_follow_the_os() {
use crate::broker::deploy::runtime::RuntimeError;
let mac = crate::broker::deploy::runtime::next_step_for(RuntimeError::SocketMissing, true);
assert!(!mac.as_deref().unwrap().contains("systemctl"));
let linux =
crate::broker::deploy::runtime::next_step_for(RuntimeError::SocketMissing, false);
assert!(linux.as_deref().unwrap().contains("systemctl"));
}
#[test]
fn the_real_machine_reads_without_network() {
let r = machine_report(false);
let ids: Vec<&str> = r.checks.iter().map(|c| c.id.as_str()).collect();
for id in [
"profile",
"signed_in",
"cpu",
"memory",
"disk",
"runtime",
"node_key",
"action_key",
] {
assert!(ids.contains(&id), "{id} in {ids:?}");
}
assert!(!ids.contains(&"hub"));
#[cfg(target_os = "linux")]
assert!(total_memory().unwrap() > 0);
#[cfg(unix)]
assert!(free_disk(Path::new("/")).is_some());
}
#[test]
fn a_container_memory_limit_lowers_the_total() {
let meminfo = "MemTotal: 65728352 kB\nMemFree: 1 kB\n";
let host = meminfo_total(meminfo).unwrap();
assert_eq!(host, 65728352 * 1024);
let files = |p: &str| match p {
"/sys/fs/cgroup/kubepods/pod1/c1/memory.max" => Some("17179869184\n".to_string()),
_ => None,
};
let lim = cgroup_memory_limit("0::/kubepods/pod1/c1\n", &files);
assert_eq!(lim, Some(17179869184));
assert_eq!(effective_memory(host, lim), 17179869184);
let lxc = |p: &str| match p {
"/sys/fs/cgroup/user.slice/session-1.scope/memory.max" => Some("max\n".to_string()),
"/sys/fs/cgroup/user.slice/memory.max" => Some("max\n".to_string()),
"/sys/fs/cgroup/memory.max" => Some("8589934592\n".to_string()),
_ => None,
};
assert_eq!(
cgroup_memory_limit("0::/user.slice/session-1.scope\n", &lxc),
Some(8589934592)
);
let nested = |p: &str| match p {
"/sys/fs/cgroup/a/b/memory.max" => Some("4294967296".to_string()),
"/sys/fs/cgroup/memory.max" => Some("8589934592".to_string()),
_ => None,
};
assert_eq!(cgroup_memory_limit("0::/a/b", &nested), Some(4294967296));
let unlimited = |p: &str| (p == "/sys/fs/cgroup/memory.max").then(|| "max\n".to_string());
assert_eq!(cgroup_memory_limit("0::/\n", &unlimited), None);
let v1 = |p: &str| {
(p == "/sys/fs/cgroup/memory/memory.limit_in_bytes").then(|| "8589934592".to_string())
};
assert_eq!(
effective_memory(host, cgroup_memory_limit("", &v1)),
8589934592
);
let v1_none = |p: &str| {
(p == "/sys/fs/cgroup/memory/memory.limit_in_bytes")
.then(|| "9223372036854771712".to_string())
};
assert_eq!(
effective_memory(host, cgroup_memory_limit("", &v1_none)),
host
);
assert_eq!(
effective_memory(host, cgroup_memory_limit("", &|_| None)),
host
);
}
}