use std::net::Shutdown;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc;
use std::time::{Duration, Instant};
use serde_json::{Value, json};
use super::link;
use super::service::Handle;
use crate::serve::api::{ApiError, Found, Resolved, SandboxEntry};
use crate::serve::client::Streamed;
use crate::serve::proto::{Notification, Response};
use crate::serve::snapshots::LinkState;
use crate::serve::verbs;
pub const QUICK: &[&str] = &[
"instances.list",
"instances.cancel",
"instances.procs",
"instances.signal",
"instances.rename",
"instances.setProfile",
"instances.done",
"instances.buildLog",
"instances.resolve",
"sessions.list",
"sessions.rm",
"containers.logs",
"containers.inspect",
"sandboxes.list",
"sandboxes.get",
"services.get",
"config.findings",
"config.migrate",
"config.ack",
];
pub fn is_quick(method: &str) -> bool {
QUICK.contains(&method)
}
pub const HERE: &[&str] = &["forwards.add", "placements.fetch"];
pub fn is_here(method: &str) -> bool {
HERE.contains(&method)
}
pub const FANOUT_WAIT: Duration = Duration::from_secs(5);
const HANDSHAKE: Duration = Duration::from_secs(20);
const TICK: Duration = Duration::from_millis(50);
pub fn machine_of(params: &Value) -> Result<Option<String>, ApiError> {
match params.get("machine") {
None | Some(Value::Null) => Ok(None),
Some(Value::String(m)) if !m.is_empty() => Ok(Some(m.clone())),
Some(_) => Err(ApiError::invalid("`machine` must be a machine name")),
}
}
pub fn federated(params: &Value) -> bool {
params.get("machines").and_then(Value::as_bool) == Some(true)
}
pub fn strip(mut params: Value) -> Value {
if let Some(p) = params.as_object_mut() {
p.remove("machine");
p.remove("machines");
}
params
}
fn ask_all(links: &Handle, calls: Vec<(String, &'static str, Value)>, wait: Duration) -> Vec<Result<Response, String>> {
let deadline = Instant::now() + wait;
let (tx, rx) = mpsc::channel();
let n = calls.len();
let mut answers: Vec<Option<Result<Response, String>>> = (0..n).map(|_| None).collect();
for (i, (machine, method, params)) in calls.into_iter().enumerate() {
let (tx, links) = (tx.clone(), links.clone());
let spawned = std::thread::Builder::new().name(format!("serve-fanout-{machine}")).spawn(move || {
let answer = links.forward(&machine, method, params).map_err(|e| e.message);
let _ = tx.send((i, answer));
});
if let Err(e) = spawned {
answers[i] = Some(Err(format!("cannot start a thread: {e}")));
}
}
drop(tx);
while answers.iter().any(Option::is_none) {
let left = deadline.saturating_duration_since(Instant::now());
match rx.recv_timeout(left) {
Ok((i, answer)) => answers[i] = Some(answer),
Err(_) => break,
}
}
let late = format!("no answer within {} s", wait.as_secs());
answers.into_iter().map(|a| a.unwrap_or_else(|| Err(late.clone()))).collect()
}
pub fn resolve_everywhere(params: &Value, local: Result<Value, ApiError>, links: Option<&Handle>) -> Result<Value, ApiError> {
if let Err(e) = &local
&& e.code != "not-found"
{
return local;
}
let name = params.get("name").and_then(Value::as_str).unwrap_or_default();
let remotes = match links {
Some(links) => {
let mut machines: Vec<String> = links.entries().into_iter().map(|e| e.name).collect();
machines.extend(links.routed().into_iter().map(|r| r.name));
let calls = machines.iter().map(|m| (m.clone(), "instances.resolve", json!({ "name": name }))).collect();
machines.into_iter().zip(ask_all(links, calls, FANOUT_WAIT)).collect()
}
None => Vec::new(),
};
join_resolve(name, local, remotes)
}
fn join_resolve(
name: &str,
local: Result<Value, ApiError>,
remotes: Vec<(String, Result<Response, String>)>,
) -> Result<Value, ApiError> {
let mut all: Vec<(String, Found)> = Vec::new();
let local_message = match local {
Ok(v) => {
let r = resolved(v);
all.extend(r.matches.into_iter().zip(r.found));
None
}
Err(e) => Some(e.message),
};
let mut unreachable = Vec::new();
for (machine, answer) in remotes {
let r = match answer {
Ok(Response { result: Some(v), error: None, .. }) => resolved(v),
Ok(Response { error: Some(e), .. }) if e.code == "not-found" => continue,
Ok(Response { error: Some(e), .. }) => {
unreachable.push(format!("{machine} ({})", tidy(&machine, &e.message)));
continue;
}
Ok(_) => continue,
Err(why) => {
unreachable.push(format!("{machine} ({})", tidy(&machine, &why)));
continue;
}
};
for (m, f) in r.matches.into_iter().zip(r.found) {
all.push((format!("{machine}/{m}"), Found { machine: Some(machine.clone()), ..f }));
}
}
if all.is_empty() {
let mut message = local_message.unwrap_or_else(|| crate::commands::no_instance_matches(name));
if !unreachable.is_empty() {
message.push_str(&format!("; unreachable: {}", unreachable.join(", ")));
}
return Err(ApiError::not_found(message));
}
all.sort_by(|a, b| a.0.cmp(&b.0));
let (matches, found) = all.into_iter().unzip();
serde_json::to_value(Resolved { matches, found }).map_err(|e| ApiError::internal(e.into()))
}
fn resolved(v: Value) -> Resolved {
let mut r: Resolved = serde_json::from_value(v).unwrap_or(Resolved { matches: Vec::new(), found: Vec::new() });
r.found = r
.matches
.iter()
.map(|m| {
let f = r.found.iter().find(|f| f.name == *m);
Found {
name: m.clone(),
machine: None,
root: f.and_then(|f| f.root.clone()),
sandbox: f.and_then(|f| f.sandbox.clone()),
}
})
.collect();
r
}
fn tidy(machine: &str, message: &str) -> String {
let m = message.strip_prefix(&format!("{machine}: ")).unwrap_or(message);
m.strip_prefix("unreachable: ").unwrap_or(m).to_string()
}
pub fn sandboxes_everywhere(local: Result<Value, ApiError>, links: Option<&Handle>) -> Result<Value, ApiError> {
let local = local?;
let Some(links) = links else { return Ok(local) };
let mut roots: Vec<(String, PathBuf)> =
links.entries().into_iter().flat_map(|e| e.roots.into_iter().map(move |r| (e.name.clone(), r))).collect();
roots.extend(links.routed().into_iter().flat_map(|m| m.roots.into_iter().map(move |r| (m.name.clone(), r))));
let calls = roots.iter().map(|(m, r)| (m.clone(), "sandboxes.list", json!({ "root": r }))).collect();
let answers = ask_all(links, calls, FANOUT_WAIT);
Ok(join_sandboxes(local, roots.into_iter().zip(answers).collect()))
}
fn join_sandboxes(local: Value, remotes: Vec<((String, PathBuf), Result<Response, String>)>) -> Value {
let mut out = match local {
Value::Array(rows) => rows,
other => return other,
};
for ((machine, root), answer) in remotes {
let Ok(Response { result: Some(v), error: None, .. }) = answer else { continue };
let Ok(rows) = serde_json::from_value::<Vec<SandboxEntry>>(v) else { continue };
out.extend(rows.into_iter().map(|row| {
let row = SandboxEntry { machine: Some(machine.clone()), root: Some(root.clone()), ..row };
serde_json::to_value(row).unwrap_or(Value::Null)
}));
}
Value::Array(out)
}
pub struct VerbCtx<'a> {
pub links: &'a Handle,
pub held: &'a AtomicUsize,
pub who: &'a str,
pub log: fn(&str),
}
struct Holding<'a>(&'a AtomicUsize);
impl<'a> Holding<'a> {
fn new(n: &'a AtomicUsize) -> Self {
n.fetch_add(1, Ordering::SeqCst);
Self(n)
}
}
impl Drop for Holding<'_> {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::SeqCst);
}
}
pub fn verb(id: Option<u64>, method: &str, params: Value, machine: &str, ctx: &VerbCtx, peer: &dyn verbs::Peer) -> Option<Response> {
let failed = |message: String| {
Some(Response::err_with(id, "failed", message, Some(json!({ "exit": null, "instance": null }))))
};
if ctx.links.entries().iter().all(|e| e.name != machine) && ctx.links.routed_one(machine).is_none() {
let e = ctx.links.unknown(machine);
return Some(Response::err(id, e.code, e.message));
}
match check_verb_params(¶ms) {
Ok(Some(agent)) => crate::devsbd::bridge::report_agent(agent),
Ok(None) => {}
Err(e) => return Some(Response::err(id, e.code, e.message)),
}
let _holding = Holding::new(ctx.held);
let dialed = ctx.links.dial(machine).unwrap_or_else(|| Err(anyhow::anyhow!("no longer a machine here")));
let duplex = match dialed {
Ok(duplex) => duplex,
Err(e) => {
let down = ctx.links.remotes().get(machine).filter(|m| m.state != LinkState::Connected).and_then(|m| m.error);
return failed(match down {
Some(why) => format!("{machine}: unreachable: {why}"),
None if ctx.links.remotes().get(machine).is_none() => format!("{machine}: unreachable: {e:#}"),
None => format!("{machine}: {e:#}"),
});
}
};
let cut = duplex.stream.try_clone().ok();
let mut link = match link::handshake(machine, duplex, HANDSHAKE, link::Over::Link) {
Ok(link) => link,
Err(e) => return failed(e.to_string()),
};
(ctx.log)(&format!("{}: {method} on {machine}", ctx.who));
let mut on = |n: Option<&Notification>| match n {
Some(n) if n.method == "output" || n.method == "progress" => {
let mut params = n.params.clone();
if let Some(p) = params.as_object_mut() {
p.insert("request".into(), json!(id));
}
peer.notify(&n.method, params).is_ok() && !peer.draining() && !peer.hung_up(Duration::ZERO)
}
Some(_) => true,
None => !peer.draining() && !peer.hung_up(TICK),
};
let streamed = link.conn.call_streaming(method, strip(params), TICK, &mut on);
let answer = match streamed {
Ok(Streamed::Answer(r)) => Some(Response { id, ..r }),
Ok(Streamed::HungUp) => {
(ctx.log)(&format!("{}: {method} on {machine}: the client left, closing the machine's connection", ctx.who));
None
}
Ok(Streamed::Lost) => failed(format!("{machine}: connection lost; the verb may still be running there")),
Err(e) => failed(format!("{machine}: {e:#}")),
};
if let Some(stream) = cut {
let _ = stream.shutdown(Shutdown::Both);
}
drop(link);
answer
}
fn check_verb_params(params: &Value) -> Result<Option<PathBuf>, ApiError> {
let Some(env) = params.get("env").and_then(Value::as_object) else {
return Err(ApiError::invalid("bad params: `env` must be an object"));
};
match params.get("root").and_then(Value::as_str) {
Some(root) if Path::new(root).is_absolute() => {}
Some(root) => return Err(ApiError::invalid(format!("`root` must be absolute, got `{root}`"))),
None => return Err(ApiError::invalid("bad params: `root` must be a path")),
}
let agent = env.get("SSH_AUTH_SOCK").and_then(Value::as_str).map(PathBuf::from).filter(|p| p.is_absolute());
Ok(agent)
}
#[cfg(test)]
mod tests {
use std::io::{BufRead, BufReader, Write};
use std::path::Path;
use super::*;
use crate::serve::machines::Entry;
use crate::serve::client::{self, Conn, Hello};
use crate::serve::daemon::tests::{opts, scratch, spawn_daemon};
use crate::serve::daemon::Exit;
use crate::serve::endpoint::{self, Stream};
use crate::serve::proto::{ErrorBody, Version};
#[test]
fn machine_params_and_strip() {
assert_eq!(machine_of(&json!({})).unwrap(), None);
assert_eq!(machine_of(&json!({"machine": null})).unwrap(), None);
assert_eq!(machine_of(&json!({"machine": "a"})).unwrap().as_deref(), Some("a"));
assert_eq!(machine_of(&json!({"machine": ""})).unwrap_err().code, "invalid");
assert_eq!(machine_of(&json!({"machine": 1})).unwrap_err().code, "invalid");
assert_eq!(strip(json!({"machine": "a", "machines": true, "name": "w"})), json!({"name": "w"}));
assert!(federated(&json!({"machines": true})) && !federated(&json!({"machines": false})) && !federated(&json!({})));
assert!(is_quick("instances.buildLog") && !is_quick("inbox.threads.list") && !is_quick("run"));
}
fn ok(v: Value) -> Result<Response, String> {
Ok(Response::ok(Some(1), v))
}
fn err(code: &str, message: &str) -> Result<Response, String> {
Ok(Response { id: Some(1), result: None, error: Some(ErrorBody { code: code.into(), message: message.into(), data: None }) })
}
#[test]
fn resolve_joins_qualify_and_sort() {
let local = Ok(json!({"matches": ["web-1"], "found": [{"name": "web-1", "machine": null, "root": "/here", "sandbox": "web"}]}));
let remotes = vec![
("b".to_string(), ok(json!({"matches": ["web-2"], "found": [{"name": "web-2", "machine": null, "root": "/there", "sandbox": "web"}]}))),
("a".to_string(), ok(json!({"matches": ["web-3"]}))),
("c".to_string(), err("not-found", "no sandbox instance matches `web`")),
("d".to_string(), Err("no answer within 5 s".into())),
];
let r = join_resolve("web", local, remotes).unwrap();
assert_eq!(r["matches"], json!(["a/web-3", "b/web-2", "web-1"]));
assert_eq!(
r["found"],
json!([
{"name": "web-3", "machine": "a", "root": null, "sandbox": null},
{"name": "web-2", "machine": "b", "root": "/there", "sandbox": "web"},
{"name": "web-1", "machine": null, "root": "/here", "sandbox": "web"},
])
);
let none = || Err(ApiError::not_found(crate::commands::no_instance_matches("web")));
let e = join_resolve("web", none(), vec![("c".into(), err("not-found", "x"))]).unwrap_err();
assert_eq!((e.code, e.message), ("not-found", crate::commands::no_instance_matches("web")));
let down = vec![
("d".to_string(), err("failed", "d: unreachable: ssh: Could not resolve hostname d")),
("e".to_string(), Err("no answer within 5 s".into())),
];
let e = join_resolve("web", none(), down).unwrap_err();
let want = format!(
"{}; unreachable: d (ssh: Could not resolve hostname d), e (no answer within 5 s)",
crate::commands::no_instance_matches("web")
);
assert_eq!((e.code, e.message), ("not-found", want));
}
#[test]
fn sandbox_joins_tag_the_machines_rows() {
let local = json!([{"name": "loc", "root": "/here"}]);
let remotes = vec![
(("a".to_string(), PathBuf::from("/r")), ok(json!([{"name": "web", "root": "/r"}]))),
(("a".to_string(), PathBuf::from("/gone")), err("not-found", "no directory /gone")),
(("b".to_string(), PathBuf::from("/b")), Err("no answer within 5 s".into())),
];
assert_eq!(
join_sandboxes(local, remotes),
json!([{"name": "loc", "root": "/here"}, {"name": "web", "machine": "a", "root": "/r"}])
);
}
fn instance(key: &str, sandbox: &str, root: &Path) -> String {
format!(
"[instance.{key}]\nsandbox = \"{sandbox}\"\ncontainer = \"devsandbox-{key}\"\nfolder = \"/w\"\n\
workspace = \"/w\"\ncreated_unix = 1\nconfig_dir = {:?}\n",
root.to_str().unwrap()
)
}
struct Pair {
base: PathBuf,
remote_dir: PathBuf,
verb_dir: PathBuf,
remote_root: PathBuf,
local_root: PathBuf,
local: Conn,
daemons: Vec<std::thread::JoinHandle<anyhow::Result<Exit>>>,
}
impl Pair {
fn new(tag: &str, script: &str) -> Self {
let base = scratch(tag);
let (remote_root, local_root) = (base.join("remote-root"), base.join("local-root"));
for (root, sandbox) in [(&remote_root, "web"), (&local_root, "loc")] {
std::fs::create_dir_all(root).unwrap();
std::fs::write(root.join(crate::config::CONFIG_FILE), format!("[sandbox.{sandbox}]\nimage = \"a\"\n")).unwrap();
}
let remote_root = std::fs::canonicalize(&remote_root).unwrap();
let local_root = std::fs::canonicalize(&local_root).unwrap();
let (remote_state, local_state) = (base.join("remote-data/state.toml"), base.join("local-data/state.toml"));
std::fs::create_dir_all(remote_state.parent().unwrap()).unwrap();
std::fs::create_dir_all(local_state.parent().unwrap()).unwrap();
std::fs::write(&remote_state, instance("web-r", "web", &remote_root)).unwrap();
std::fs::write(&local_state, instance("web-l", "web", &local_root)).unwrap();
let registry = base.join("machines.toml");
crate::serve::machines::update(®istry, |e| e.extend([Entry::ssh("box", "me@box"), Entry::ssh("down", "me@down")])).unwrap();
let remote_dir = base.join("remote");
let (runner, verb_dir) = verbs::tests::script_runner(tag, script);
let mut remote = opts(1, 60_000);
remote.verbs = Some(runner);
remote.state = Some(remote_state);
remote.home = Some(base.join("remote-home"));
remote.machines = Some(base.join("remote-machines.toml"));
remote.member = Some(base.join("remote-data/member.toml"));
std::fs::write(base.join("remote-data/member.toml"), "trust = \"full\"\n").unwrap();
let mut local = opts(1, 300);
local.state = Some(local_state);
local.machines = Some(registry);
local.member = Some(base.join("local-data/member.toml"));
std::fs::write(base.join("local-data/member.toml"), "name = \"lap\"\n").unwrap();
let remote_member = base.join("remote-data/member.toml");
crate::serve::fleet::membership_api::test_support::fleet_of(
"box",
&[("lap", &base.join("local-data/member.toml")), ("box", &remote_member)],
);
let (peer, _) = crate::serve::fleet::dialer::tests::peer_transport(&remote_dir, &remote_member);
local.link_transport = Some(crate::serve::fleet::transport::Transport::new(move |target, args| match target {
"me@box" => peer.dial(target, "devsandbox", args),
other => anyhow::bail!("ssh: Could not resolve hostname {}", other.trim_start_matches("me@")),
}));
let daemons = vec![spawn_daemon(&remote_dir, remote), spawn_daemon(&base.join("local"), local)];
drop(conn_at(&remote_dir));
let mut local = conn_at(&base.join("local"));
let deadline = Instant::now() + Duration::from_secs(10);
let listed = |c: &mut Conn| machine_row(c, "box")["roots"].as_array().is_some_and(|a| a.contains(&json!(remote_root)));
while machine_state(&mut local, "box") != "connected" || !listed(&mut local) || machine_state(&mut local, "down") != "unreachable" {
assert!(Instant::now() < deadline, "the links never settled");
std::thread::sleep(Duration::from_millis(20));
}
Pair { base, remote_dir, verb_dir, remote_root, local_root, local, daemons }
}
fn local_dir(&self) -> PathBuf {
self.base.join("local")
}
fn finish(mut self) {
let r = conn_at(&self.remote_dir).call("shutdown", json!({})).unwrap();
assert!(r.error.is_none(), "{r:?}");
assert!(self.local.call("shutdown", json!({})).unwrap().error.is_none());
drop(self.local);
let mut daemons = self.daemons.into_iter();
assert_eq!(daemons.next().unwrap().join().unwrap().unwrap(), Exit::Shutdown);
assert_eq!(daemons.next().unwrap().join().unwrap().unwrap(), Exit::Shutdown);
let _ = std::fs::remove_dir_all(&self.base);
let _ = std::fs::remove_dir_all(&self.verb_dir);
}
}
fn stream_at(dir: &Path) -> Stream {
let deadline = Instant::now() + Duration::from_secs(5);
loop {
if let Ok(mut stream) = endpoint::connect(dir) {
let version = Version { semver: "0.0.0".into(), build: 0 };
match client::hello(&mut stream, &version, "test").unwrap() {
Hello::Ready(_) => return stream,
other => panic!("{other:?}"),
}
}
assert!(Instant::now() < deadline, "daemon never listened");
std::thread::sleep(Duration::from_millis(20));
}
}
fn conn_at(dir: &Path) -> Conn {
let stream = stream_at(dir);
let reply = crate::serve::proto::HelloResult { version: "0.0.0".into(), build: 0, protocol: 1, handoff: false };
Conn::new(stream, reply).unwrap()
}
fn code_message(r: &Response) -> (&str, &str) {
let e = r.error.as_ref().unwrap_or_else(|| panic!("succeeded: {r:?}"));
(e.code.as_str(), e.message.as_str())
}
#[test]
fn quick_calls_and_federated_lookups_reach_the_machine() {
let mut pair = Pair::new("fwd-quick", "exit 0");
let c = &mut pair.local;
let r = c.call("instances.buildLog", json!({"instance": "nope", "machine": "box"})).unwrap();
assert_eq!(code_message(&r), ("not-found", "no instance `nope`"));
let r = c.call("instances.resolve", json!({"name": "web-r", "machine": "box"})).unwrap();
assert_eq!(r.result.unwrap(), json!({"matches": ["web-r"], "found": [{"name": "web-r", "machine": null, "root": pair.remote_root, "sandbox": "web"}]}));
let r = c.call("sandboxes.list", json!({"root": pair.remote_root, "machine": "box", "machines": true})).unwrap();
assert_eq!(r.result.unwrap(), json!([{"name": "web", "root": pair.remote_root}]), "no fan-out there");
let r = c.call("instances.buildLog", json!({"instance": "x", "machine": "nope"})).unwrap();
assert_eq!(code_message(&r), ("not-found", "no machine named `nope`"));
let r = c.call("instances.buildLog", json!({"instance": "x", "machine": ""})).unwrap();
assert_eq!(code_message(&r).0, "invalid");
let r = c.call("inbox.threads.list", json!({"machine": "box"})).unwrap();
assert_eq!(code_message(&r), ("invalid", "`inbox.threads.list` can't take a machine yet"));
let add = json!({"dir": pair.remote_root, "instance": "web-r", "spec": "3000", "machine": "box"});
let r = c.call("forwards.add", add).unwrap();
assert_eq!(code_message(&r), ("internal", "this daemon runs no forwards"));
let r = c.call("instances.list", json!({"dir": "/x", "machine": "down"})).unwrap();
assert_eq!(code_message(&r), ("failed", "down: unreachable: ssh: Could not resolve hostname down"));
let r = c.call("instances.resolve", json!({"name": "web", "machines": true})).unwrap().result.unwrap();
assert_eq!(r["matches"], json!(["box/web-r", "web-l"]));
assert_eq!(
r["found"],
json!([
{"name": "web-r", "machine": "box", "root": pair.remote_root, "sandbox": "web"},
{"name": "web-l", "machine": null, "root": pair.local_root, "sandbox": "web"},
])
);
crate::serve::dts::assert_matches("InstancesResolved", &r);
let r = c.call("instances.resolve", json!({"name": "web-r", "machines": true})).unwrap().result.unwrap();
assert_eq!(r["matches"], json!(["box/web-r"]), "only there");
let r = c.call("instances.resolve", json!({"name": "web-r"})).unwrap();
assert_eq!(code_message(&r).0, "not-found");
let r = c.call("instances.resolve", json!({"name": "zzz", "machines": true})).unwrap();
let (code, message) = code_message(&r);
let want = format!("{}; unreachable: down (", crate::commands::no_instance_matches("zzz"));
assert!(code == "not-found" && message.starts_with(&want), "{message}");
assert_eq!(code_message(&c.call("instances.resolve", json!({"machines": true})).unwrap()).0, "invalid");
let r = c.call("sandboxes.list", json!({"root": pair.local_root, "machines": true})).unwrap().result.unwrap();
assert_eq!(
r,
json!([{"name": "loc", "root": pair.local_root}, {"name": "web", "machine": "box", "root": pair.remote_root}])
);
let r = c.call("sandboxes.list", json!({"root": pair.local_root})).unwrap().result.unwrap();
assert_eq!(r, json!([{"name": "loc", "root": pair.local_root}]), "this host only");
pair.finish();
}
const SCRIPT: &str = r#"for a; do n=$a; done
case "$*" in
*slow*) trap 'touch "$DIR/int-$n"; exit 130' INT; echo ready; sleep 2; touch "$DIR/done-$n" ;;
*) echo "hi $n"; echo 'Error: nope' >&2; exit 2 ;;
esac"#;
fn send(stream: &Stream, id: u64, method: &str, params: Value) {
writeln!(&*stream, "{}", json!({"id": id, "method": method, "params": params})).unwrap();
}
fn verb_params(root: &Path, extra: Value) -> Value {
let mut p = json!({"root": root, "env": {"PATH": std::env::var("PATH").unwrap_or_default()}, "machine": "box"});
p.as_object_mut().unwrap().extend(extra.as_object().unwrap().clone());
p
}
fn answer(reader: &mut BufReader<Stream>, id: u64) -> (Value, Vec<Value>) {
let mut notes = Vec::new();
loop {
let mut line = String::new();
assert!(reader.read_line(&mut line).unwrap() > 0, "closed before the answer");
let msg: Value = serde_json::from_str(&line).unwrap();
if msg.get("id").is_some() {
assert_eq!(msg["id"], id);
return (msg, notes);
}
notes.push(msg);
}
}
fn until_ready(reader: &mut BufReader<Stream>) {
loop {
let mut line = String::new();
assert!(reader.read_line(&mut line).unwrap() > 0, "closed before ready");
assert!(!line.contains("\"id\""), "answered early: {line}");
if line.contains("ready") {
return;
}
}
}
fn wait_file(path: &Path, within: Duration) -> bool {
let deadline = Instant::now() + within;
while !path.exists() {
if Instant::now() >= deadline {
return false;
}
std::thread::sleep(Duration::from_millis(20));
}
true
}
#[test]
fn forwarded_verbs_stream_and_a_hang_up_cancels_there() {
let pair = Pair::new("fwd-verbs", SCRIPT);
let root = pair.verb_dir.clone();
let stream = stream_at(&pair.local_dir());
let mut reader = BufReader::new(stream.try_clone().unwrap());
send(&stream, 7, "stop", verb_params(&root, json!({"instance": "fast"})));
let (a, notes) = answer(&mut reader, 7);
assert_eq!(a["error"], json!({"code": "failed", "message": "nope", "data": {"exit": 2, "instance": null}}));
let out: String = notes
.iter()
.filter(|n| n["method"] == "output" && n["params"]["stream"] == "stdout")
.map(|n| n["params"]["data"].as_str().unwrap())
.collect();
assert_eq!(out, "hi fast\n");
assert!(!notes.is_empty() && notes.iter().all(|n| n["params"]["request"] == 7), "{notes:?}");
send(&stream, 8, "stop", verb_params(Path::new("rel"), json!({"instance": "fast"})));
assert_eq!(answer(&mut reader, 8).0["error"]["code"], "invalid");
send(&stream, 9, "stop", verb_params(&root, json!({"instance": "fast", "machine": "nope"})));
assert_eq!(answer(&mut reader, 9).0["error"]["code"], "not-found");
send(&stream, 10, "stop", verb_params(&root, json!({"instance": "fast", "machine": "down"})));
let e = answer(&mut reader, 10).0["error"].clone();
assert_eq!(
e,
json!({"code": "failed", "message": "down: unreachable: ssh: Could not resolve hostname down", "data": {"exit": null, "instance": null}})
);
send(&stream, 11, "stop", verb_params(&root, json!({"instance": "slow-a"})));
until_ready(&mut reader);
drop((reader, stream));
assert!(wait_file(&root.join("int-slow-a"), Duration::from_secs(3)), "no SIGINT there");
assert!(!root.join("done-slow-a").exists());
let stream = stream_at(&pair.local_dir());
let mut reader = BufReader::new(stream.try_clone().unwrap());
send(&stream, 1, "stop", verb_params(&root, json!({"instance": "slow-b", "detach": true})));
until_ready(&mut reader);
drop((reader, stream));
assert!(wait_file(&root.join("done-slow-b"), Duration::from_secs(5)), "the verb didn't finish");
assert!(!root.join("int-slow-b").exists(), "SIGINTed");
pair.finish();
}
fn machine_state(c: &mut Conn, name: &str) -> String {
machine_row(c, name)["state"].as_str().unwrap().to_string()
}
fn machine_row(c: &mut Conn, name: &str) -> Value {
let rows = c.call("machines.list", Value::Null).unwrap().result.unwrap();
rows.as_array().unwrap().iter().find(|r| r["name"] == name).unwrap().clone()
}
#[test]
fn a_forwarded_verb_reports_the_agent() {
let pair = Pair::new("fwd-agent", SCRIPT);
let root = pair.verb_dir.clone();
let agent = std::env::temp_dir().join(format!("dsv-fwd-agent-{}.sock", std::process::id()));
let stream = stream_at(&pair.local_dir());
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut params = verb_params(&root, json!({"instance": "slow-w"}));
params["env"]["SSH_AUTH_SOCK"] = json!(agent);
send(&stream, 1, "stop", params);
until_ready(&mut reader);
assert!(crate::devsbd::bridge::reported_agents().contains(&agent), "the requester's agent reported");
let (a, _) = answer(&mut reader, 1);
assert!(a["error"].is_null(), "{a}");
drop((reader, stream));
pair.finish();
}
#[test]
fn a_lookup_reports_the_callers_agent() {
let mut pair = Pair::new("fwd-get-agent", "exit 0");
let r = pair.local.call("machines.get", json!({"name": "box", "agent": "rel.sock"})).unwrap();
assert_eq!(code_message(&r), ("invalid", "`agent` must be absolute, got `rel.sock`"));
let agent = std::env::temp_dir().join(format!("dsv-get-agent-{}.sock", std::process::id()));
let r = pair.local.call("machines.get", json!({"name": "box", "agent": agent})).unwrap();
assert_eq!(r.result.unwrap()["name"], "box");
assert!(crate::devsbd::bridge::reported_agents().contains(&agent), "the caller's agent reported");
pair.finish();
}
}