From bec6f739500d4c9aa45225609656f58550a43b97 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Thu, 26 Nov 2026 11:00:30 +0530 Subject: [PATCH] test(lock): find pid_ns_helper under Cargo's 1.100 build-dir layout Rust 1.100 runs integration tests from target/debug/build///out/ instead of target/debug/deps/, so the fixed two-level pop in docker_lock_test, nfs_lock_test and pid_namespace_test no longer reaches target/debug/examples/ and the Docker lock tests fail on every Linux runner. The three copies become one tests/pid_ns_helper module that takes the nearest ancestor of the test binary holding examples/pid_ns_helper, which covers both layouts. MINIGRAF_PID_NS_HELPER still overrides it. --- tests/docker_lock_test.rs | 31 ++++--------------------- tests/nfs_lock_test.rs | 28 ++--------------------- tests/pid_namespace_test.rs | 45 +++---------------------------------- tests/pid_ns_helper/mod.rs | 40 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 95 deletions(-) create mode 100644 tests/pid_ns_helper/mod.rs diff --git a/tests/docker_lock_test.rs b/tests/docker_lock_test.rs index 7c15b55378..f70b32c26c 100644 --- a/tests/docker_lock_test.rs +++ b/tests/docker_lock_test.rs @@ -12,10 +12,13 @@ //! when `docker` is unavailable, so plain `cargo test` never touches it. #![cfg(target_os = "linux")] -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::{Command, Output, Stdio}; use std::time::{SystemTime, UNIX_EPOCH}; +mod pid_ns_helper; +use pid_ns_helper::{helper_binary, require_helper_built}; + const IMAGE: &str = "ubuntu:24.04"; /// Whether a Docker daemon is actually reachable, not just whether the CLI @@ -31,32 +34,6 @@ fn docker_available() -> bool { .unwrap_or(false) } -/// Path to `examples/pid_ns_helper`. -/// -/// `MINIGRAF_PID_NS_HELPER`, if set, is used as-is. Needed under `cargo -/// llvm-cov`, whose instrumented build puts test binaries where the -/// `examples/`-relative lookup below fails, so the helper has to live in -/// a target dir it never touches; `.github/workflows/coverage-gates.yml` -/// builds it there and sets this variable. Otherwise resolved relative to the -/// test binary, the same shape as `tests/pid_namespace_test.rs::helper_binary`. -fn helper_binary() -> PathBuf { - if let Ok(path) = std::env::var("MINIGRAF_PID_NS_HELPER") { - return PathBuf::from(path); - } - let mut dir = std::env::current_exe().expect("test binary path"); - dir.pop(); // deps/ - dir.pop(); // debug/ (or release/) - dir.join("examples").join("pid_ns_helper") -} - -fn require_helper_built(helper: &Path) { - assert!( - helper.exists(), - "helper binary not found at {}; run `cargo build --examples` first", - helper.display() - ); -} - /// A name unlikely to collide with a concurrently running test or a prior /// run's leftovers. Rust runs `#[test]` functions in parallel by default, and /// this file has two tests that each create a container and a volume. diff --git a/tests/nfs_lock_test.rs b/tests/nfs_lock_test.rs index ebc0ae3a09..58b8a8043f 100644 --- a/tests/nfs_lock_test.rs +++ b/tests/nfs_lock_test.rs @@ -20,32 +20,8 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Output}; -/// Path to `examples/pid_ns_helper`. -/// -/// `MINIGRAF_PID_NS_HELPER`, if set, is used as-is. Needed under `cargo -/// llvm-cov`, whose instrumented build puts test binaries where the -/// `examples/`-relative lookup below fails, so the helper has to live in -/// a target dir it never touches; `.github/workflows/coverage-gates.yml` -/// builds it there and sets this variable. Otherwise resolved relative to the -/// test binary at `target/debug/deps/nfs_lock_test-`, the same shape as -/// `tests/pid_namespace_test.rs::helper_binary`. -fn helper_binary() -> PathBuf { - if let Ok(path) = std::env::var("MINIGRAF_PID_NS_HELPER") { - return PathBuf::from(path); - } - let mut dir = std::env::current_exe().expect("test binary path"); - dir.pop(); // deps/ - dir.pop(); // debug/ - dir.join("examples").join("pid_ns_helper") -} - -fn require_helper_built(helper: &Path) { - assert!( - helper.exists(), - "helper binary not found at {}; run `cargo build --examples` first", - helper.display() - ); -} +mod pid_ns_helper; +use pid_ns_helper::{helper_binary, require_helper_built}; fn run_helper(helper: &Path, db: &Path, mode: &str) -> Output { Command::new(helper) diff --git a/tests/pid_namespace_test.rs b/tests/pid_namespace_test.rs index 192b9dbb87..f979a0b8c6 100644 --- a/tests/pid_namespace_test.rs +++ b/tests/pid_namespace_test.rs @@ -10,6 +10,9 @@ use std::path::Path; use std::process::{Command, Stdio}; +mod pid_ns_helper; +use pid_ns_helper::{helper_binary, require_helper_built}; + /// The `unshare` invocation that puts a command at PID 1 in a fresh namespace, /// or `None` if this environment cannot create one. /// @@ -89,48 +92,6 @@ fn spawn_in_namespace( cmd.spawn().expect("spawn in namespace") } -/// Path to the compiled helper binary (see `examples/pid_ns_helper.rs`). -/// -/// `MINIGRAF_PID_NS_HELPER`, if set, is used as-is -- needed under -/// `cargo-llvm-cov`, whose instrumented build places test binaries under a -/// build-script-shaped path instead of `target/debug/deps/`, breaking the -/// relative lookup below. See `tests/docker_lock_test.rs::helper_binary` for -/// the same pattern. -fn helper_binary() -> std::path::PathBuf { - if let Ok(path) = std::env::var("MINIGRAF_PID_NS_HELPER") { - return std::path::PathBuf::from(path); - } - // target/debug/examples/pid_ns_helper, resolved relative to the test binary - // at target/debug/deps/pid_namespace_test-. - let mut dir = std::env::current_exe().expect("test binary path"); - dir.pop(); // deps/ - dir.pop(); // debug/ - dir.join("examples").join("pid_ns_helper") -} - -/// Fails loudly if the helper binary this environment could have run is -/// missing, instead of silently skipping. -/// -/// Call this only after `unshare_prefix()` has already returned `Some`: at -/// that point this environment has proven it can create PID namespaces, so -/// the one remaining reason `helper_binary()` could be absent is that -/// `cargo build --examples` was not run -- a build problem the developer -/// should see immediately, not a reason to assert nothing and report green. -/// A skip is reserved for `unshare_prefix()` returning `None`, i.e. this -/// environment genuinely cannot create PID namespaces at all. -/// -/// This also guards against the assumption (verified by hand, not encoded -/// anywhere else) that plain `cargo test` builds `examples/` before running -/// integration tests: if a future cargo stops doing that, this test turns -/// red and visible in CI instead of green and vacuous. -fn require_helper_built(helper: &Path) { - assert!( - helper.exists(), - "helper binary not found at {}; run `cargo build --examples` first", - helper.display() - ); -} - #[test] fn test_lock_survives_holder_death_in_another_pid_namespace() { let Some(prefix) = unshare_prefix() else { diff --git a/tests/pid_ns_helper/mod.rs b/tests/pid_ns_helper/mod.rs new file mode 100644 index 0000000000..a2d67f96bb --- /dev/null +++ b/tests/pid_ns_helper/mod.rs @@ -0,0 +1,40 @@ +//! Locates the compiled `examples/pid_ns_helper` for the lock tests that run +//! it in another process (`docker_lock_test`, `nfs_lock_test`, +//! `pid_namespace_test`). A directory module, so Cargo does not build it as a +//! test target of its own. + +use std::path::{Path, PathBuf}; + +/// `MINIGRAF_PID_NS_HELPER`, if set, is used as-is: `cargo llvm-cov` builds +/// into a target dir the lookup below cannot reach, so +/// `.github/workflows/coverage-gates.yml` builds the helper elsewhere and sets +/// it. +/// +/// Otherwise the nearest ancestor of the test binary with an +/// `examples/pid_ns_helper`. The test binary is at +/// `target/debug/deps/-` up to Rust 1.99 and at +/// `target/debug/build/minigraf//out/-` from 1.100, while +/// Cargo still copies the example to `target/debug/examples/`. +pub fn helper_binary() -> PathBuf { + if let Ok(path) = std::env::var("MINIGRAF_PID_NS_HELPER") { + return PathBuf::from(path); + } + let exe = std::env::current_exe().expect("test binary path"); + let relative = Path::new("examples").join("pid_ns_helper"); + exe.ancestors() + .skip(1) + .map(|dir| dir.join(&relative)) + .find(|candidate| candidate.is_file()) + .unwrap_or_else(|| exe.with_file_name(&relative)) +} + +/// Fails loudly rather than skipping: a missing helper is a build problem +/// (`cargo build --examples` not run, or a Cargo layout change), and a skip +/// here would report green while asserting nothing. +pub fn require_helper_built(helper: &Path) { + assert!( + helper.exists(), + "helper binary not found at {}; run `cargo build --examples` first", + helper.display() + ); +}