Files
leaf/leaf-e2e/src/process.rs
T
eric 9ef8d0d343 test(e2e): add the end-to-end suite for the plugin host
`leaf-e2e` starts real leaf instances and drives them from the outside over
SOCKS5, so a case covers the path a deployment takes -- config parsing, plugin
loading, routing, the dispatcher, the C ABI data plane -- and nothing reaches
into leaf to build a handler by hand. Cases are values rather than `#[test]`
functions, so the suite is a matrix of implementation, protocol, transport and
behaviour instead of a wall of copies, and each runs in its own process, free
to crash, hang, or set load-time environment.

The lanes:

- `differential` runs a behaviour natively and again with a plugin substituted
  for one stage, and requires the two to agree. The server is stock leaf, so
  these are interoperability tests. `protocols`, `halfclose` and `teardown`
  extend that across every stack, relay and selector, the end of a request on
  each transport, and shutdown.
- `abi`, `loader` and `coexist` check what the host accepts, refuses, and can
  load at once, through a real `dlopen`.
- `hostile` and `datagram` drive conformance plugins -- one in Rust, one in Go
  -- that misbehave on request, exercising the host's bounds checks, clamps,
  buffer growth and `decode_packet` handling.
- `stress` and the meters measure both sides back to back and assert the ratio,
  since absolute numbers from a shared runner say little; throughput, latency,
  setup cost and cpu cost land in `meters.json` beside the case logs. What the
  process is holding is asserted directly instead, against the case's own
  allocator, because what matters there is that it stopped growing.
- `blackbox` runs the shipped binary: on a config file, and on a plugin
  library through `--verify-plugin`, whose report has to agree with what the
  loader itself would do.

Plugins are built by the harness, and a case whose toolchain is missing is
reported as ignored with the reason; `--strict`, which CI passes, makes that a
failure, so the suite cannot go green by running nothing. The suite runs on
Windows too, where a pause is never shorter than 16ms and cases that dribble
bytes bound their pauses instead.
2026-12-03 19:31:53 +08:00

164 lines
5.4 KiB
Rust

//! Driving a real `leaf` process.
//!
//! Almost every case runs leaf in-process, which is faster and makes its log
//! easy to read. A few things only exist outside it: whether the shipped binary
//! starts from a config file on disk, and whether the process actually exits
//! once a plugin has pulled a language runtime of its own into it. The ABI
//! warns about the second -- a Go `c-shared` build leaves a thread that has
//! called into it unable to exit cleanly -- and no in-process test can observe
//! it.
use std::net::SocketAddr;
use std::path::PathBuf;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
use anyhow::{anyhow, bail, Context, Result};
use crate::fixtures::Fixture;
use crate::logs;
use crate::net;
use crate::node::Node;
/// How long the binary may take to start listening.
const START_TIMEOUT: Duration = Duration::from_secs(20);
pub struct CliNode {
name: String,
child: Child,
socks: SocketAddr,
stopped: bool,
}
impl CliNode {
/// Writes the node's config next to the case's other artifacts and starts
/// the shipped binary on it.
pub async fn start(name: &str, node: &Node) -> Result<Self> {
let socks = node.socks_addr()?;
let directory = logs::artifact_dir().unwrap_or_else(std::env::temp_dir);
std::fs::create_dir_all(&directory)?;
let config_path: PathBuf = directory.join(format!("{}.json", name));
std::fs::write(&config_path, node.config_string())
.with_context(|| format!("writing {}", config_path.display()))?;
let child = Command::new(Fixture::LeafCli.path()?)
.arg("-c")
.arg(&config_path)
// Inherited, so the process's log lands in the case's captured
// output like everything else.
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.stdin(Stdio::null())
.spawn()
.context("starting the leaf binary")?;
let mut node = Self {
name: name.to_string(),
child,
socks,
stopped: false,
};
if let Err(err) = net::wait_tcp_ready(socks, START_TIMEOUT).await {
let _ = node.child.kill();
return Err(anyhow!("[{}] never started listening: {}", node.name, err));
}
Ok(node)
}
pub fn socks_addr(&self) -> SocketAddr {
self.socks
}
/// Asks the process to shut down the way an operator would, and requires it
/// to actually go.
pub async fn interrupt_and_wait(mut self, within: Duration) -> Result<()> {
self.stopped = true;
interrupt(self.child.id())?;
let deadline = Instant::now() + within;
loop {
match self.child.try_wait()? {
Some(status) if status.success() => return Ok(()),
Some(status) => bail!("[{}] exited with {}", self.name, status),
None if Instant::now() >= deadline => {
let _ = self.child.kill();
let _ = self.child.wait();
bail!(
"[{}] did not exit within {:?} of being interrupted",
self.name,
within
);
}
None => tokio::time::sleep(Duration::from_millis(20)).await,
}
}
}
}
impl Drop for CliNode {
fn drop(&mut self) {
if !self.stopped {
let _ = self.child.kill();
let _ = self.child.wait();
}
}
}
/// Sends the interrupt leaf listens for.
///
/// `Child::kill` would be a `SIGKILL`, which proves nothing about whether the
/// process can shut itself down.
#[cfg(unix)]
fn interrupt(pid: u32) -> Result<()> {
let status = Command::new("kill")
.arg("-INT")
.arg(pid.to_string())
.status()
.context("interrupting the leaf process")?;
if !status.success() {
bail!("kill -INT {} failed with {}", pid, status);
}
Ok(())
}
#[cfg(not(unix))]
fn interrupt(_pid: u32) -> Result<()> {
bail!("interrupting a process is only implemented for unix")
}
/// Runs `leaf -T` over a config file, which parses it and exits.
/// Runs `--verify-plugin` on a library and hands back whether it was accepted
/// and everything the binary said about it.
///
/// Both halves matter: a rejection is as much of a result as a report, and the
/// text is the whole point of the option.
pub fn verify_plugin(path: &std::path::Path, sha256: Option<&str>) -> Result<(bool, String)> {
let mut command = Command::new(Fixture::LeafCli.path()?);
command.arg("--verify-plugin").arg(path);
if let Some(sha256) = sha256 {
command.arg("--verify-plugin-sha256").arg(sha256);
}
let output = command.output().context("running the leaf binary")?;
let mut text = String::from_utf8_lossy(&output.stdout).into_owned();
text.push_str(&String::from_utf8_lossy(&output.stderr));
Ok((output.status.success(), text))
}
pub fn test_config(path: &std::path::Path) -> Result<()> {
let output = Command::new(Fixture::LeafCli.path()?)
.arg("-T")
.arg("-c")
.arg(path)
.output()
.context("running the leaf binary")?;
if !output.status.success() {
bail!(
"{} was rejected: {}{}",
path.display(),
String::from_utf8_lossy(&output.stdout).trim(),
String::from_utf8_lossy(&output.stderr).trim()
);
}
Ok(())
}