use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;
use anyhow::{Context, Result, bail};
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use crate::serve::api::{ContainerLogs, InstancesList, MAX_LINES, ProfileSet, Renamed};
use crate::serve::proto::Response;
use crate::snapshot::{InstanceRow, SandboxRow};
use crate::{Command, ServiceCommand};
pub(crate) type Call<'a> = dyn FnMut(&str, Value) -> Result<Response> + 'a;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Use {
Logs { lines: usize },
BuildLog { follow: bool },
Inspect { json: bool },
Done(bool),
Rename(String),
Profile(Option<String>),
Ls { json: bool },
ServiceLs { json: bool },
Ps { all: bool, json: bool },
Link,
Refused(Refused),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Refused {
Vscode,
}
pub(crate) fn use_of(command: &Command) -> Use {
match command {
Command::Logs { build: true, follow, .. } => Use::BuildLog { follow: *follow },
Command::Logs { lines, .. } => Use::Logs { lines: *lines },
Command::Inspect { json, .. } => Use::Inspect { json: *json },
Command::Done { .. } => Use::Done(true),
Command::Undone { .. } => Use::Done(false),
Command::Rename { new_name, .. } => Use::Rename(new_name.clone()),
Command::Profile { profile, .. } => Use::Profile(profile.clone()),
Command::Ls { json } => Use::Ls { json: *json },
Command::Ps { all, json } => Use::Ps { all: *all, json: *json },
Command::Service { cmd: ServiceCommand::Ls { json } } => Use::ServiceLs { json: *json },
Command::Vscode { .. } => Use::Refused(Refused::Vscode),
_ => Use::Link,
}
}
pub(crate) fn refusal(m: &str, what: Refused) -> anyhow::Error {
let what = match what {
Refused::Vscode => "`devsandbox vscode`",
};
anyhow::anyhow!("machine `{m}`: {what} isn't available over its link yet")
}
fn answer_error(m: &str, e: crate::serve::proto::ErrorBody) -> anyhow::Error {
match e.code.as_str() {
"denied" => anyhow::anyhow!("machine `{m}`: {}", e.message),
_ => anyhow::anyhow!("{}", e.message),
}
}
fn call_on<T: DeserializeOwned>(call: &mut Call, m: &str, method: &str, mut params: Value) -> Result<T> {
params["machine"] = json!(m);
let r = call(method, params)?;
if let Some(e) = r.error {
return Err(answer_error(m, e));
}
serde_json::from_value(r.result.unwrap_or(Value::Null)).with_context(|| format!("bad {method} result from devsandbox serve"))
}
fn list(call: &mut Call, m: &str, dir: &Path, depth: Option<&str>) -> Result<InstancesList> {
let mut params = json!({ "dir": dir });
if let Some(depth) = depth {
params["depth"] = json!(depth);
}
call_on(call, m, "instances.list", params)
}
fn row_of(call: &mut Call, m: &str, root: &Path, name: &str) -> Result<Option<InstanceRow>> {
Ok(list(call, m, root, Some("disk"))?.snapshot.instances.into_iter().find(|r| r.name == name))
}
fn allowed_of(row: &InstanceRow, sandboxes: &[SandboxRow]) -> Result<Vec<String>, String> {
let named: Vec<&SandboxRow> = sandboxes.iter().filter(|s| s.name == row.sandbox).collect();
let own = named.iter().find(|s| row.root.is_some() && s.root == row.root).or(named.first());
match own {
Some(s) if !s.config_hash.is_empty() => Ok(s.profiles.clone()),
_ => Err(format!("sandbox `{}` isn't in its config any more", row.sandbox)),
}
}
fn container_of(row: Option<&InstanceRow>, name: &str) -> String {
match row {
Some(row) => row.container.clone(),
None if name.starts_with(crate::runtime::NAME_PREFIX) => name.to_string(),
None => format!("{}{name}", crate::runtime::NAME_PREFIX),
}
}
pub(crate) fn named(call: &mut Call, u: &Use, m: &str, name: &str, root: &Path, tty: bool, out: &mut dyn Write, err: &mut dyn Write) -> Result<i32> {
match u {
Use::Logs { lines } => {
let container = container_of(row_of(call, m, root, name)?.as_ref(), name);
let logs: ContainerLogs = call_on(call, m, "containers.logs", json!({ "container": container, "lines": lines }))?;
writeln!(out, "{}", crate::commands::logs::render_logs(&logs.text))?;
}
Use::BuildLog { follow: true } => {
let id = row_of(call, m, root, name)?.map_or_else(|| name.to_string(), |r| r.instance_id);
follow_build_log(call, m, name, &id, tty, out, &mut std::thread::sleep)?;
}
Use::BuildLog { follow: false } => {
let tail: Option<crate::build_log::Tail> =
call_on(call, m, "instances.buildLog", json!({ "instance": name, "lines": MAX_LINES, "ansi": tty }))?;
let Some(tail) = tail else { bail!("`{m}/{name}` has no build log") };
if tail.cut {
writeln!(err, "(the last {MAX_LINES} lines of `{m}/{name}`'s build log)")?;
}
out.write_all(tail.text.as_bytes())?;
if !tail.text.is_empty() && !tail.text.ends_with('\n') {
writeln!(out)?;
}
}
Use::Inspect { json } => {
let container = container_of(row_of(call, m, root, name)?.as_ref(), name);
let doc: Value = call_on(call, m, "containers.inspect", json!({ "container": container }))?;
writeln!(out, "{}", crate::commands::inspect::render(&doc.to_string(), *json))?;
}
Use::Done(done) => {
let was = row_of(call, m, root, name)?.map(|r| r.done).unwrap_or(!*done);
let set: crate::serve::api::DoneSet = call_on(call, m, "instances.done", json!({ "instance": name, "done": done }))?;
if let Some(warning) = &set.warning {
writeln!(err, "warning: {warning}")?;
}
writeln!(out, "{}", crate::commands::done::status_line(name, *done, was))?;
}
Use::Rename(new_name) => {
let r: Renamed = call_on(call, m, "instances.rename", json!({ "instance": name, "newName": new_name }))?;
writeln!(out, "{}", crate::commands::rename::renamed_line(name, &r.instance))?;
}
Use::Profile(Some(profile)) => {
let r: ProfileSet = call_on(call, m, "instances.setProfile", json!({ "instance": name, "profile": profile }))?;
writeln!(out, "{}", crate::commands::profile::profile_line(name, Some(&r.profile), false))?;
}
Use::Profile(None) => {
let snapshot = list(call, m, root, Some("disk"))?.snapshot;
let Some(row) = snapshot.instances.iter().find(|r| r.name == *name) else {
bail!("no instance named `{name}` on machine `{m}` (see `devsandbox ps -a`)");
};
let allowed = allowed_of(row, &snapshot.sandboxes);
for line in crate::commands::profile::show_lines(name, row.profile.as_deref(), row.profile_disallowed, Some(&allowed)) {
writeln!(out, "{line}")?;
}
}
Use::Ls { .. } | Use::ServiceLs { .. } | Use::Ps { .. } | Use::Link | Use::Refused(_) => {
bail!("not a named re-run")
}
}
Ok(0)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Follow {
Fetch,
Wait,
Done,
}
pub(crate) fn follow_next(page: &crate::build_log::Page, offset: u64) -> Follow {
if page.complete {
Follow::Done
} else if page.next == offset {
Follow::Wait
} else {
Follow::Fetch
}
}
pub(crate) fn follow_build_log(
call: &mut Call,
m: &str,
name: &str,
id: &str,
tty: bool,
out: &mut dyn Write,
sleep: &mut dyn FnMut(Duration),
) -> Result<()> {
let mut offset = 0;
let mut strip: Option<Option<crate::build_log::Strip>> = None;
loop {
let r = call("instances.buildLog", json!({ "instance": id, "offset": offset, "machine": m }))?;
let page: Option<crate::build_log::Page> = match r.error {
Some(e) if strip.is_some() && e.code == "not-found" => return Ok(()),
Some(e) => return Err(answer_error(m, e)),
None => serde_json::from_value(r.result.unwrap_or(Value::Null))
.context("bad instances.buildLog result from devsandbox serve")?,
};
let Some(page) = page else {
if strip.is_none() {
bail!("`{m}/{name}` has no build log");
}
return Ok(());
};
let strip = strip.get_or_insert_with(|| (!tty && page.tty).then(crate::build_log::Strip::default));
match strip {
Some(strip) => out.write_all(&strip.feed(page.text.as_bytes()))?,
None => out.write_all(page.text.as_bytes())?,
}
out.flush()?;
let next = follow_next(&page, offset);
offset = page.next;
match next {
Follow::Done => return Ok(()),
Follow::Wait => sleep(crate::commands::logs::FOLLOW_POLL),
Follow::Fetch => {}
}
}
}
pub(crate) fn listing(call: &mut Call, u: &Use, m: &str, root: &Path, out: &mut dyn Write) -> Result<i32> {
let Use::Ps { all, json } = u else { bail!("not a listing re-run") };
let snapshot = list(call, m, root, None)?.snapshot;
write!(out, "{}", crate::commands::ps::render(snapshot, &[], *all, *json)?)?;
Ok(0)
}
pub(crate) fn config_roots(at: &Path, list: &InstancesList) -> Vec<PathBuf> {
let mut roots: Vec<PathBuf> = list
.snapshot
.instances
.iter()
.filter_map(|r| r.root.as_ref().map(PathBuf::from))
.filter(|r| r.starts_with(at))
.collect();
let configless = list.snapshot.error.as_deref().is_some_and(|e| e.contains("config: "));
if roots.is_empty() || !configless {
roots.push(at.to_path_buf());
}
roots.sort();
roots.dedup();
roots
}
pub(crate) fn per_root(call: &mut Call, u: &Use, m: &str, roots: &[PathBuf], out: &mut dyn Write, err: &mut dyn Write) -> Result<i32> {
let json = match u {
Use::Ls { json } | Use::ServiceLs { json } => *json,
_ => bail!("not a per-root re-run"),
};
let mut config = Vec::new();
for at in roots {
config.extend(config_roots(at, &list(call, m, at, Some("disk"))?));
}
config.sort();
config.dedup();
let banners = super::root_banners(m, &config, json)?;
for (root, banner) in config.iter().zip(banners) {
writeln!(err, "{banner}")?;
match u {
Use::ServiceLs { json } => {
let snapshot = list(call, m, root, Some("listing"))?.snapshot;
if let Some(e) = snapshot.error {
if snapshot.services.is_empty() && e.contains("config: ") {
bail!("{m}:{}: {e}", root.display());
}
writeln!(err, "{e}")?;
}
let config = root.join(crate::config::CONFIG_FILE);
write!(out, "{}", crate::commands::services::ls_render(&snapshot.services, *json, &config)?)?;
}
_ => {
let snapshot = list(call, m, root, Some("disk"))?.snapshot;
if json {
writeln!(out, "{}", crate::commands::ls::render_json(&snapshot.sandboxes)?)?;
} else if !snapshot.sandboxes.is_empty() {
let lines: Vec<_> = snapshot.sandboxes.iter().map(crate::commands::ls::Line::of_row).collect();
write!(out, "{}", crate::commands::ls::table(&lines))?;
} else if let Some(e) = snapshot.error.filter(|e| e.contains("config: ")) {
bail!("{m}:{}: {e}", root.display());
} else {
writeln!(out, "{}", crate::commands::ls::none_defined(root))?;
}
}
}
}
Ok(0)
}
#[cfg(test)]
mod tests {
use clap::Parser;
use super::*;
use crate::Cli;
use crate::serve::proto::ErrorBody;
fn command(args: &[&str]) -> Command {
Cli::try_parse_from(std::iter::once("devsandbox").chain(args.iter().copied())).unwrap_or_else(|e| panic!("{e}")).command.unwrap()
}
#[test]
fn each_command_maps_to_a_call_or_a_refusal() {
let u = |args: &[&str]| use_of(&command(args));
assert_eq!(u(&["logs", "lap/web"]), Use::Logs { lines: 50 });
assert_eq!(u(&["logs", "-n", "7", "lap/web"]), Use::Logs { lines: 7 });
assert_eq!(u(&["logs", "--build", "lap/web"]), Use::BuildLog { follow: false });
assert_eq!(u(&["logs", "--build", "-f", "lap/web"]), Use::BuildLog { follow: true });
assert_eq!(u(&["inspect", "lap/web"]), Use::Inspect { json: false });
assert_eq!(u(&["inspect", "--json", "lap/web"]), Use::Inspect { json: true });
assert_eq!(u(&["done", "lap/web"]), Use::Done(true));
assert_eq!(u(&["undone", "lap/web"]), Use::Done(false));
assert_eq!(u(&["rename", "lap/web", "api"]), Use::Rename("api".into()));
assert_eq!(u(&["profile", "lap/web", "work"]), Use::Profile(Some("work".into())));
assert_eq!(u(&["profile", "lap/web"]), Use::Profile(None));
assert_eq!(u(&["ls"]), Use::Ls { json: false });
assert_eq!(u(&["ls", "--json"]), Use::Ls { json: true });
assert_eq!(u(&["ps", "-a"]), Use::Ps { all: true, json: false });
assert_eq!(u(&["ps", "--json"]), Use::Ps { all: false, json: true });
assert_eq!(u(&["gc", "--force"]), Use::Link);
assert_eq!(u(&["service", "ls"]), Use::ServiceLs { json: false });
assert_eq!(u(&["service", "ls", "--json"]), Use::ServiceLs { json: true });
assert_eq!(u(&["vscode", "lap/web"]), Use::Refused(Refused::Vscode));
for args in [&["exec", "lap/web"][..], &["exec", "lap/web", "--", "ls"], &["port", "lap/web", "3000"], &["stop", "lap/web"]] {
assert_eq!(u(args), Use::Link, "{args:?}");
}
}
#[test]
fn refusals_name_the_command_and_why() {
let r = |what| refusal("lap", what).to_string();
assert_eq!(r(Refused::Vscode), "machine `lap`: `devsandbox vscode` isn't available over its link yet");
}
fn row(name: &str, container: &str, root: &str, done: bool) -> Value {
json!({"name": name, "sandbox": "web", "container": container, "status": {"state": "running", "text": "Up 3 minutes"}, "uptime_secs": 0,
"cpu": null, "mem": null, "folder": "", "worktree": false, "services": [], "workspace": "",
"remote_user": null, "remote_env_len": 0, "base_folder": "", "drift": false, "instance_id": "id",
"done": done, "root": root})
}
fn sandbox(name: &str, source: &str, folder: Option<&str>, services: &[&str], extends: &[&str]) -> Value {
json!({"name": name, "source": source, "folder": folder, "services": services, "extends": extends,
"config_hash": "h", "build_hash": "", "issues": [], "dispatcher": false, "worktree_branch": null, "root": "/f/cfg"})
}
fn listing(rows: Vec<Value>, sandboxes: Vec<Value>, error: Option<&str>) -> Value {
json!({"instances": rows, "sandboxes": sandboxes, "services": [], "sandbox_count": 0, "runtime_name": "docker",
"runtime_version": null, "error": error})
}
struct Fake<F> {
calls: Vec<(String, Value)>,
answer: F,
}
impl<F: FnMut(&str, &Value) -> Result<Value, (&'static str, String)>> Fake<F> {
fn call(&mut self, method: &str, params: Value) -> Result<Response> {
let r = (self.answer)(method, ¶ms);
self.calls.push((method.to_string(), params));
Ok(match r {
Ok(v) => Response { id: None, result: Some(v), error: None },
Err((code, message)) => {
Response { id: None, result: None, error: Some(ErrorBody { code: code.into(), message, data: Default::default() }) }
}
})
}
}
fn fake<F: FnMut(&str, &Value) -> Result<Value, (&'static str, String)>>(answer: F) -> Fake<F> {
Fake { calls: Vec::new(), answer }
}
fn run_named(u: Use, name: &str, answer: impl FnMut(&str, &Value) -> Result<Value, (&'static str, String)>) -> (Result<i32>, String, String, Vec<(String, Value)>) {
let mut f = fake(answer);
let (mut out, mut err) = (Vec::new(), Vec::new());
let r = named(&mut |m, p| f.call(m, p), &u, "lap", name, Path::new("/f/cfg"), false, &mut out, &mut err);
(r, String::from_utf8(out).unwrap(), String::from_utf8(err).unwrap(), f.calls)
}
#[test]
fn logs_and_inspect_print_as_the_local_commands() {
let rows = || listing(vec![row("web", "devsandbox-old", "/f/cfg", false)], vec![], None);
let (r, out, _, calls) = run_named(Use::Logs { lines: 7 }, "web", |m, _| {
Ok(if m == "instances.list" { rows() } else { json!({"text": "a\nb"}) })
});
assert_eq!(r.unwrap(), 0);
assert_eq!(out, format!("{}\n", crate::commands::logs::render_logs("a\nb")));
assert_eq!(calls[0], ("instances.list".into(), json!({"dir": "/f/cfg", "depth": "disk", "machine": "lap"})));
assert_eq!(calls[1], ("containers.logs".into(), json!({"container": "devsandbox-old", "lines": 7, "machine": "lap"})));
let (_, out, ..) = run_named(Use::Logs { lines: 50 }, "web", |m, _| Ok(if m == "instances.list" { rows() } else { json!({"text": " \n"}) }));
assert_eq!(out, "(no log output)\n");
let doc = json!([{"Id": "abc", "State": {"Running": true}}]);
for json in [false, true] {
let (_, out, _, calls) =
run_named(Use::Inspect { json }, "web", |m, _| Ok(if m == "instances.list" { rows() } else { doc.clone() }));
assert_eq!(out, format!("{}\n", crate::commands::inspect::render(&doc.to_string(), json)));
assert_eq!(calls[1].1, json!({"container": "devsandbox-old", "machine": "lap"}));
}
let (_, _, _, calls) = run_named(Use::Inspect { json: true }, "x", |m, _| Ok(if m == "instances.list" { rows() } else { json!({}) }));
assert_eq!(calls[1].1["container"], "devsandbox-x");
}
#[test]
fn build_logs_print_the_tail_or_say_there_is_none() {
let (r, out, err, calls) = run_named(Use::BuildLog { follow: false }, "web", |_, _| Ok(json!({"text": "$ run\nok", "cut": true})));
assert_eq!((r.unwrap(), out.as_str()), (0, "$ run\nok\n"));
assert_eq!(err, format!("(the last {MAX_LINES} lines of `lap/web`'s build log)\n"));
assert_eq!(calls, [("instances.buildLog".into(), json!({"instance": "web", "lines": MAX_LINES, "ansi": false, "machine": "lap"}))]);
let (r, ..) = run_named(Use::BuildLog { follow: false }, "web", |_, _| Ok(Value::Null));
assert_eq!(r.unwrap_err().to_string(), "`lap/web` has no build log");
let (r, ..) = run_named(Use::BuildLog { follow: false }, "web", |_, _| Err(("not-found", "no instance `web`".into())));
assert_eq!(r.unwrap_err().to_string(), "no instance `web`");
}
fn page(text: &str, next: u64, tty: bool, complete: bool) -> crate::build_log::Page {
crate::build_log::Page { text: text.into(), next, tty, complete }
}
#[test]
fn a_follow_fetches_while_pages_bring_something_and_waits_when_none_do() {
assert_eq!(follow_next(&page("abc", 3, false, false), 0), Follow::Fetch);
assert_eq!(follow_next(&page("", 3, false, false), 3), Follow::Wait);
assert_eq!(follow_next(&page("abc", 6, false, true), 3), Follow::Done);
assert_eq!(follow_next(&page("", 6, false, true), 6), Follow::Done);
}
fn run_follow(tty: bool, answers: Vec<Result<Value, (&'static str, String)>>) -> (Result<()>, String, Vec<Value>, usize) {
let mut answers = answers.into_iter();
let mut last = None;
let mut f = fake(|m: &str, _: &Value| {
assert_eq!(m, "instances.buildLog");
match answers.next() {
Some(a) => {
last = Some(a.clone());
a
}
None => last.clone().unwrap(),
}
});
let (mut out, mut sleeps) = (Vec::new(), 0);
let r = follow_build_log(&mut |m, p| f.call(m, p), "lap", "web", "id-web", tty, &mut out, &mut |d| {
assert_eq!(d, crate::commands::logs::FOLLOW_POLL);
sleeps += 1;
});
(r, String::from_utf8(out).unwrap(), f.calls.into_iter().map(|(_, p)| p).collect(), sleeps)
}
#[test]
fn a_followed_build_log_prints_its_pages_until_one_is_complete() {
let v = |p: crate::build_log::Page| Ok(serde_json::to_value(p).unwrap());
let pages = || {
vec![
v(page("$ run\n", 6, false, false)),
v(page("", 6, false, false)),
v(page("\x1b[32mok\x1b[0m\r\n", 19, false, false)),
v(page("end\n", 23, false, true)),
]
};
let (r, out, params, sleeps) = run_follow(false, pages());
r.unwrap();
assert_eq!(out, "$ run\n\x1b[32mok\x1b[0m\r\nend\n", "a pipe log as it is");
assert_eq!(sleeps, 1);
let offsets: Vec<_> = params.iter().map(|p| p["offset"].as_u64().unwrap()).collect();
assert_eq!(offsets, [0, 6, 6, 19]);
assert_eq!(params[0], json!({"instance": "id-web", "offset": 0, "machine": "lap"}));
let pty = || vec![v(page("\x1b[32mok\x1b[0m\r\n", 13, true, false)), v(page("50%\r100%\r\n", 24, true, true))];
assert_eq!(run_follow(true, pty()).1, "\x1b[32mok\x1b[0m\r\n50%\r100%\r\n");
let mut strip = crate::build_log::Strip::default();
let want = [strip.feed(b"\x1b[32mok\x1b[0m\r\n"), strip.feed(b"50%\r100%\r\n")].concat();
assert_eq!(run_follow(false, pty()).1, String::from_utf8(want).unwrap());
assert_eq!(run_follow(false, vec![Ok(Value::Null)]).0.unwrap_err().to_string(), "`lap/web` has no build log");
let gone = || Err(("not-found", "no instance `id-web`".to_string()));
assert_eq!(run_follow(false, vec![gone()]).0.unwrap_err().to_string(), "no instance `id-web`");
let (r, out, ..) = run_follow(false, vec![v(page("a", 1, false, false)), gone()]);
assert_eq!((r.is_ok(), out.as_str()), (true, "a"));
let (r, ..) = run_follow(false, vec![v(page("a", 1, false, false)), Ok(Value::Null)]);
assert!(r.is_ok());
}
#[test]
fn a_named_follow_asks_by_instance_id() {
let (r, out, _, calls) = run_named(Use::BuildLog { follow: true }, "web", |m, _| {
Ok(if m == "instances.list" {
listing(vec![row("web", "c", "/f/cfg", false)], vec![], None)
} else {
serde_json::to_value(page("done\n", 5, false, true)).unwrap()
})
});
assert_eq!((r.unwrap(), out.as_str()), (0, "done\n"));
assert_eq!(calls[1], ("instances.buildLog".into(), json!({"instance": "id", "offset": 0, "machine": "lap"})));
}
#[test]
fn done_and_rename_print_as_the_local_commands() {
for (done, was) in [(true, false), (true, true), (false, true), (false, false)] {
let (r, out, _, calls) = run_named(Use::Done(done), "web", |m, _| {
Ok(if m == "instances.list" { listing(vec![row("web", "c", "/f/cfg", was)], vec![], None) } else { json!({"instance": "web", "done": done}) })
});
assert_eq!(r.unwrap(), 0);
assert_eq!(out, format!("{}\n", crate::commands::done::status_line("web", done, was)));
assert_eq!(calls[1], ("instances.done".into(), json!({"instance": "web", "done": done, "machine": "lap"})));
}
let (r, out, err, _) = run_named(Use::Done(true), "web", |m, _| {
Ok(if m == "instances.list" {
listing(vec![row("web", "c", "/f/cfg", false)], vec![], None)
} else {
json!({"instance": "web", "done": true, "warning": "cannot fetch lap/web back: no link"})
})
});
assert_eq!(r.unwrap(), 0);
assert_eq!((out, err.as_str()), (format!("{}\n", crate::commands::done::status_line("web", true, false)), "warning: cannot fetch lap/web back: no link\n"));
let (_, out, _, calls) = run_named(Use::Rename(" api ".into()), "web", |_, _| Ok(json!({"instance": "api"})));
assert_eq!(out, "renamed web -> api\n");
assert_eq!(calls, [("instances.rename".into(), json!({"instance": "web", "newName": " api ", "machine": "lap"}))]);
let (r, ..) = run_named(Use::Rename("x".into()), "web", |_, _| Err(("invalid", "`x` is taken".into())));
assert_eq!(r.unwrap_err().to_string(), "`x` is taken");
}
#[test]
fn profile_prints_as_the_local_command() {
let (r, out, _, calls) = run_named(Use::Profile(Some("work".into())), "web", |_, _| Ok(json!({"instance": "web", "profile": "work"})));
assert_eq!((r.unwrap(), out.as_str()), (0, "profile web: work\n"));
assert_eq!(calls, [("instances.setProfile".into(), json!({"instance": "web", "profile": "work", "machine": "lap"}))]);
let (r, ..) = run_named(Use::Profile(Some("x".into())), "web", |_, _| Err(("invalid", "sandbox `web` doesn't allow profile `x`; allowed: work".into())));
assert_eq!(r.unwrap_err().to_string(), "sandbox `web` doesn't allow profile `x`; allowed: work");
let mut flagged = row("web", "c", "/f/cfg", false);
flagged["profile"] = json!("gone");
flagged["profile_disallowed"] = json!(true);
let mut web = sandbox("web", "image x", None, &[], &[]);
web["profiles"] = json!(["work", "home"]);
let mut elsewhere = web.clone();
elsewhere["root"] = json!("/f/other");
elsewhere["profiles"] = json!(["x"]);
let (r, out, _, calls) =
run_named(Use::Profile(None), "web", |_, _| Ok(listing(vec![flagged.clone()], vec![elsewhere.clone(), web.clone()], None)));
assert_eq!((r.unwrap(), out.as_str()), (0, "profile web: gone (its sandbox doesn't allow it any more)\nallowed: work, home\n"));
assert_eq!(calls.len(), 1, "read only");
let (_, out, ..) = run_named(Use::Profile(None), "web", |_, _| Ok(listing(vec![flagged.clone()], vec![], None)));
assert_eq!(out, "profile web: gone (its sandbox doesn't allow it any more)\nallowed: unknown (sandbox `web` isn't in its config any more)\n");
web["profiles"] = json!([]);
let (_, out, ..) = run_named(Use::Profile(None), "web", |_, _| Ok(listing(vec![flagged.clone()], vec![web.clone()], None)));
assert!(out.ends_with("allowed: (none: its sandbox has no profiles)\n"), "{out}");
let (r, ..) = run_named(Use::Profile(None), "api", |_, _| Ok(listing(vec![flagged.clone()], vec![], None)));
assert_eq!(r.unwrap_err().to_string(), "no instance named `api` on machine `lap` (see `devsandbox ps -a`)");
}
fn list_of(v: Value) -> InstancesList {
serde_json::from_value(v).unwrap()
}
#[test]
fn a_root_stands_for_itself_or_the_config_roots_under_it() {
let p = |s: &[&str]| s.iter().map(PathBuf::from).collect::<Vec<_>>();
let rows = || vec![row("a", "c", "/f/b", false), row("b", "c", "/f/a", false), row("c", "c", "/f/a", false), row("d", "c", "/r", false)];
let configless = Some("config: no devsandboxes.toml");
assert_eq!(config_roots(Path::new("/f"), &list_of(listing(rows(), vec![], configless))), p(&["/f/a", "/f/b"]));
assert_eq!(config_roots(Path::new("/f"), &list_of(listing(vec![], vec![], configless))), p(&["/f"]));
assert_eq!(config_roots(Path::new("/r"), &list_of(listing(rows(), vec![], None))), p(&["/r"]));
assert_eq!(config_roots(Path::new("/s"), &list_of(listing(rows(), vec![], None))), p(&["/s"]), "no instances there yet");
assert_eq!(config_roots(Path::new("/f"), &list_of(listing(rows(), vec![], None))), p(&["/f", "/f/a", "/f/b"]));
}
fn run_per_root(u: Use, answer: impl FnMut(&str, &Value) -> Result<Value, (&'static str, String)>) -> (Result<i32>, String, String) {
let mut f = fake(answer);
let (mut out, mut err) = (Vec::new(), Vec::new());
let r = per_root(&mut |m, p| f.call(m, p), &u, "lap", &[PathBuf::from("/f")], &mut out, &mut err);
(r, String::from_utf8(out).unwrap(), String::from_utf8(err).unwrap())
}
#[test]
fn ls_prints_as_the_local_command_per_config_root() {
let sandboxes = || vec![sandbox("web", "image node", Some("app"), &["db", "redis"], &["base"]), sandbox("cli", "dockerfile D", None, &[], &[])];
let answer = |m: &str, p: &Value| {
assert_eq!(m, "instances.list");
Ok(match p["dir"].as_str().unwrap() {
"/f" => listing(vec![row("w", "c", "/f/cfg", false)], vec![], Some("config: no devsandboxes.toml")),
_ => listing(vec![row("w", "c", "/f/cfg", false)], sandboxes(), None),
})
};
let (r, out, err) = run_per_root(Use::Ls { json: false }, answer);
assert_eq!(r.unwrap(), 0);
assert_eq!(err, "==> lap:/f/cfg\n");
let rows: Vec<crate::snapshot::SandboxRow> = serde_json::from_value(json!(sandboxes())).unwrap();
let lines: Vec<_> = rows.iter().map(crate::commands::ls::Line::of_row).collect();
assert_eq!(out, crate::commands::ls::table(&lines));
assert_eq!(
lines[0],
crate::commands::ls::Line {
name: "web".into(),
source: "image node".into(),
folder: Some("app".into()),
services: Some("db, redis".into()),
extends: Some("base".into())
}
);
assert_eq!((lines[1].folder.as_deref(), lines[1].services.as_deref(), lines[1].extends.as_deref()), (None, None, None));
let (_, out, _) = run_per_root(Use::Ls { json: true }, answer);
assert_eq!(out, format!("{}\n", crate::commands::ls::render_json(&rows).unwrap()));
}
#[test]
fn ps_prints_as_the_local_command() {
let mut done = row("a", "c", "/f/cfg", true);
done["profile"] = json!("work");
done["profile_disallowed"] = json!(true);
let mut stopped = row("b", "c", "/f/cfg", false);
stopped["status"] = json!({"state": "exited", "text": "Exited (0)"});
let answer = listing(vec![done, stopped], vec![], None);
let run = |all: bool, json: bool| {
let mut f = fake(|_: &str, _: &Value| Ok(answer.clone()));
let mut out = Vec::new();
let r = super::listing(&mut |m, p| f.call(m, p), &Use::Ps { all, json }, "lap", Path::new("/f/cfg"), &mut out);
(r.unwrap(), String::from_utf8(out).unwrap(), f.calls)
};
for (all, json) in [(false, false), (true, false), (false, true), (true, true)] {
let (code, out, calls) = run(all, json);
let want = crate::commands::ps::render(list_of(answer.clone()).snapshot, &[], all, json).unwrap();
assert_eq!((code, out), (0, want));
assert_eq!(calls, [("instances.list".into(), json!({"dir": "/f/cfg", "machine": "lap"}))]);
}
let (_, out, _) = run(false, false);
assert!(out.contains("running (done)") && out.contains("work (not allowed)") && !out.contains("exited"), "{out}");
let denied = "`/x` isn't one of this member's roots (`devsandbox machine roots add <path>` there adds one)";
let mut f = fake(|_: &str, _: &Value| Err(("denied", denied.into())));
let r = super::listing(&mut |m, p| f.call(m, p), &Use::Ps { all: false, json: false }, "lap", Path::new("/x"), &mut Vec::new());
assert_eq!(r.unwrap_err().to_string(), format!("machine `lap`: {denied}"));
}
#[test]
fn service_ls_prints_as_the_local_command_per_config_root() {
let services = json!([
{"name": "db", "scope": "isolated", "source": "image postgres", "ports": ["5432"],
"containers": [["devsandbox-svc-p-db-id", {"state": "running", "text": "Up 1 hour"}]], "used_by": ["w"],
"env_len": 0, "command": null, "config_hash": "h", "drift": false, "root": "/f/cfg"},
{"name": "cache", "scope": "global", "source": "?", "ports": [], "containers": [["devsandbox-svc-p-cache", {"state": "missing"}]],
"used_by": [], "env_len": 0, "command": null, "config_hash": "", "drift": false, "root": "/f/cfg"},
]);
let depths = std::cell::RefCell::new(Vec::new());
let answer = |error: Option<&'static str>, services: Value| {
let depths = &depths;
move |_: &str, p: &Value| {
depths.borrow_mut().push((p["dir"].clone(), p["depth"].clone()));
let error = if p["dir"] == "/f" { Some("config: no devsandboxes.toml") } else { error };
let mut l = listing(vec![row("w", "c", "/f/cfg", false)], vec![], error);
l["services"] = services.clone();
Ok(l)
}
};
let rows: Vec<crate::snapshot::ServiceRow> = serde_json::from_value(services.clone()).unwrap();
let config = Path::new("/f/cfg/devsandboxes.toml");
for json in [false, true] {
let (r, out, err) = run_per_root(Use::ServiceLs { json }, answer(Some("service `cache`: no image"), services.clone()));
assert_eq!(r.unwrap(), 0);
assert_eq!(out, crate::commands::services::ls_render(&rows, json, config).unwrap());
assert_eq!(err, "==> lap:/f/cfg\nservice `cache`: no image\n");
}
assert_eq!(depths.borrow().last().unwrap(), &(json!("/f/cfg"), json!("listing")));
assert!(crate::commands::services::ls_render(&rows, false, config).unwrap().contains("running"));
let (_, out, err) = run_per_root(Use::ServiceLs { json: false }, answer(None, json!([])));
assert_eq!((out.as_str(), err.as_str()), ("no services defined in /f/cfg/devsandboxes.toml\n", "==> lap:/f/cfg\n"));
let (r, ..) = run_per_root(Use::ServiceLs { json: true }, answer(Some("config: no devsandboxes.toml"), json!([])));
assert_eq!(r.unwrap_err().to_string(), "lap:/f/cfg: config: no devsandboxes.toml");
}
#[test]
fn ls_without_sandboxes_or_with_several_roots() {
let (r, ..) = run_per_root(Use::Ls { json: false }, |_, _| Ok(listing(vec![], vec![], Some("config: no devsandboxes.toml"))));
assert_eq!(r.unwrap_err().to_string(), "lap:/f: config: no devsandboxes.toml");
let (_, out, _) = run_per_root(Use::Ls { json: false }, |_, _| Ok(listing(vec![], vec![], None)));
assert_eq!(out, "no sandboxes defined in /f\n");
let two = |_: &str, p: &Value| {
let error = (p["dir"] == "/f").then_some("config: no devsandboxes.toml");
Ok(listing(vec![row("a", "c", "/f/a", false), row("b", "c", "/f/b", false)], vec![], error))
};
let (r, ..) = run_per_root(Use::Ls { json: true }, two);
assert!(r.unwrap_err().to_string().contains("/f/a, /f/b"));
let (r, _, err) = run_per_root(Use::Ls { json: false }, two);
assert_eq!((r.unwrap(), err.as_str()), (0, "==> lap:/f/a\n==> lap:/f/b\n"));
}
}