8000 test(lock): find pid_ns_helper under Cargo's 1.100 build-dir layout by adityamukho · Pull Request #917 · project-minigraf/minigraf · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions tests/docker_lock_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,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
Expand All @@ -32,32 +35,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.
Expand Down
28 changes: 2 additions & 26 deletions tests/nfs_lock_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,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-<hash>`, 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)
Expand Down
45 changes: 3 additions & 42 deletions tests/pid_namespace_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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.
///
Expand Down Expand Up @@ -90,48 +93,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-<hash>.
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 {
Expand Down
40 changes: 40 additions & 0 deletions tests/pid_ns_helper/mod.rs
Original file line number Diff line number Diff line change
@@ -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/<test>-<hash>` up to Rust 1.99 and at
/// `target/debug/build/minigraf/<hash>/out/<test>-<hash>` 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()
);
}
Loading
0