use std::process::{Command, Output};
use serde_json::Value;
use tempfile::{TempDir, tempdir};
struct Fixture {
binary: String,
home: TempDir,
runtime: TempDir,
}
impl Fixture {
fn new() -> Self {
Self {
binary: env!("CARGO_BIN_EXE_apoc").to_string(),
home: tempdir().unwrap(),
runtime: tempdir().unwrap(),
}
}
fn run(&self, args: &[&str]) -> Output {
Command::new(&self.binary)
.args(args)
.env("APOC_HOME", self.home.path())
.env("APOC_RUNTIME_DIR", self.runtime.path())
.output()
.unwrap()
}
fn code_run(&self, code: &str, key: &str) -> Value {
let output = self.run(&[
"code",
"run",
code,
"--purpose",
"Exercising nested Code Mode invocation.",
"--idempotency-key",
key,
"--format",
"json",
]);
let stdout = String::from_utf8_lossy(&output.stdout);
serde_json::from_str(&stdout).unwrap_or_else(|error| {
panic!(
"expected one JSON execution state, got {error}\nstdout: {stdout}\nstderr: {}",
String::from_utf8_lossy(&output.stderr)
)
})
}
}
impl Drop for Fixture {
fn drop(&mut self) {
let _ = self.run(&[
"daemon",
"stop",
"--purpose",
"Stopping the nested Code Mode fixture daemon.",
"--idempotency-key",
"recursion-teardown",
]);
}
}
#[test]
fn code_mode_programs_nest_apoc_invocations_recursively() {
let fixture = Fixture::new();
let binary = serde_json::to_string(&fixture.binary).unwrap();
let depth_three = serde_json::to_string("return \"INNERMOST-OK\"").unwrap();
let depth_two = serde_json::to_string(&format!(
r#"
const run = await apoc.execution_command({{
executable: {binary},
arg: ["code", "run", {depth_three},
"--purpose", "Innermost nested program.",
"--idempotency-key", "depth-three",
"--format", "json"],
purpose: "Running the innermost nested program.",
idempotency_key: "depth-two-spawn",
expect_exit_code: [0]
}});
const logs = await apoc.execution_logs({{
id: run.id, tail_bytes: 65536,
purpose: "Reading the innermost program's output."
}});
return {{ depth: 2, outcome: run.outcome, inner: JSON.parse(logs.stdout).result }};
"#
))
.unwrap();
let outer = format!(
r#"
const run = await apoc.execution_command({{
executable: {binary},
arg: ["code", "run", {depth_two},
"--purpose", "Middle nested program.",
"--idempotency-key", "depth-two",
"--format", "json"],
purpose: "Running the middle nested program.",
idempotency_key: "outer-spawn",
expect_exit_code: [0]
}});
const logs = await apoc.execution_logs({{
id: run.id, tail_bytes: 65536,
purpose: "Reading the middle program's output."
}});
return {{ depth: 1, outcome: run.outcome, middle: JSON.parse(logs.stdout).result }};
"#
);
let state = fixture.code_run(&outer, "outer");
assert_eq!(
state.get("status").and_then(Value::as_str),
Some("completed"),
"three levels of nested apoc must complete: {state:#}"
);
let result = state.get("result").expect("a result");
assert_eq!(result.get("depth").and_then(Value::as_i64), Some(1));
let middle = result.get("middle").expect("the middle program's result");
assert_eq!(
middle.get("depth").and_then(Value::as_i64),
Some(2),
"the middle level must report itself: {result:#}"
);
assert_eq!(
middle.get("inner").and_then(Value::as_str),
Some("INNERMOST-OK"),
"the innermost value must survive two levels of nesting: {result:#}"
);
}
#[test]
fn a_program_reaches_apoc_without_a_socket_back_to_its_own_daemon() {
let fixture = Fixture::new();
let socket = fixture.runtime.path().join("apoc.sock");
let aside = fixture.runtime.path().join("apoc.sock.aside");
let code = format!(
r#"
const purpose = "Proving a program's apoc calls do not use the socket.";
const moved = await apoc.execution_command({{
executable: "/bin/mv", arg: [{socket:?}, {aside:?}],
idempotency_key: "socket-aside", purpose }});
let ran;
try {{
const result = await apoc.execution_command({{
executable: "/usr/bin/true", idempotency_key: "without-socket", purpose }});
ran = result.outcome;
}} catch (error) {{
ran = "threw: " + String(error);
}}
const restored = await apoc.execution_command({{
executable: "/bin/mv", arg: [{aside:?}, {socket:?}],
idempotency_key: "socket-back", purpose }});
return {{ moved: moved.outcome, ran, restored: restored.outcome }};
"#,
socket = socket.to_str().unwrap(),
aside = aside.to_str().unwrap(),
);
let state = fixture.code_run(&code, "no-loopback");
if aside.exists() {
std::fs::rename(&aside, &socket).unwrap();
}
let result = &state["result"];
assert_eq!(result["moved"], "passed", "state: {state}");
assert_eq!(
result["ran"], "passed",
"a call made with the socket gone must still reach apoc; state: {state}"
);
assert_eq!(result["restored"], "passed", "state: {state}");
}
#[test]
fn a_program_owned_command_is_canceled_when_the_program_ends() {
let fixture = Fixture::new();
let state = fixture.code_run(
r#"
const started = await apoc.execution_start({
executable: "/bin/sleep", arg: ["60"], owner: "execution",
idempotency_key: "owned-sleep",
purpose: "Starting a command that lives only as long as this program." });
return { id: started.id };
"#,
"owned-child",
);
let id = state["result"]["id"]
.as_str()
.unwrap_or_else(|| panic!("program should return the child id; state: {state}"))
.to_string();
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
let status = loop {
let output = fixture.run(&[
"execution",
"get",
&id,
"--purpose",
"Checking whether the program-owned command was canceled.",
"--verbosity",
"trace",
"--format",
"json",
]);
let record: Value = serde_json::from_slice(&output.stdout).unwrap_or_default();
let status = record["status"].as_str().unwrap_or_default().to_string();
if status != "running" && status != "starting" && !status.is_empty() {
break status;
}
if std::time::Instant::now() > deadline {
break status;
}
std::thread::sleep(std::time::Duration::from_millis(200));
};
assert_eq!(
status, "canceled",
"a command owned by a finished program must not keep running"
);
}