From 6237e721d62533f8bb182223dd5cb5b42f3d14c9 Mon Sep 17 00:00:00 2001 From: mnovak Date: Sat, 7 Nov 2026 23:23:51 +0100 Subject: [PATCH] [control] Test hooks: IANITOR_TEST_CRASH_AT and IANITOR_TEST_FAIL_WRITE only with the default test-hooks feature; a --no-default-features build has neither --- crates/control/Cargo.toml | 6 ++++++ crates/control/src/crash.rs | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/control/Cargo.toml b/crates/control/Cargo.toml index d2a3801..74615ba 100644 --- a/crates/control/Cargo.toml +++ b/crates/control/Cargo.toml @@ -15,6 +15,12 @@ path = "src/lib.rs" name = "ianitor-control" path = "src/main.rs" +[features] +# The crash and write-failure hooks of the crash-safety tests (src/crash.rs). The Nix release +# build turns them off with --no-default-features. +default = ["test-hooks"] +test-hooks = [] + [dependencies] ianitor-core.workspace = true axum = { version = "0.8.9", default-features = false, features = ["http1", "json", "query", "tokio", "matched-path"] } diff --git a/crates/control/src/crash.rs b/crates/control/src/crash.rs index b85bbe4..34354da 100644 --- a/crates/control/src/crash.rs +++ b/crates/control/src/crash.rs @@ -4,10 +4,13 @@ //! Crash points for the crash-safety tests: with `IANITOR_TEST_CRASH_AT=` in its //! environment, control aborts (no cleanup, like a kill -9) when an apply reaches that stage. -//! Without the variable this does nothing; the NixOS unit never sets it. //! //! `IANITOR_TEST_FAIL_WRITE=` makes every write of a state file with that name //! fail, as on a full disk (fault injection for the same tests). +//! +//! Both exist only with the `test-hooks` cargo feature (on by default, so `cargo test` has +//! them). The release build (`nix build .#ianitor`) is built with `--no-default-features`: +//! there these functions do nothing and read no environment variable. /// The stages, in order. pub const STAGES: [&str; 9] = [ @@ -22,6 +25,10 @@ pub const STAGES: [&str; 9] = [ "reverting", ]; +/// Whether this binary has the test hooks. +pub const ENABLED: bool = cfg!(feature = "test-hooks"); + +#[cfg(feature = "test-hooks")] pub fn point(stage: &str) { if std::env::var("IANITOR_TEST_CRASH_AT").as_deref() == Ok(stage) { log::error!(target: "ianitor::crash", "IANITOR_TEST_CRASH_AT={stage}: aborting on purpose"); @@ -29,7 +36,12 @@ pub fn point(stage: &str) { } } +#[cfg(not(feature = "test-hooks"))] +#[inline(always)] +pub fn point(_stage: &str) {} + /// Fail a state-file write when `IANITOR_TEST_FAIL_WRITE` names this file. +#[cfg(feature = "test-hooks")] pub fn fail_write(path: &std::path::Path) -> Result<(), String> { match ( std::env::var_os("IANITOR_TEST_FAIL_WRITE"), @@ -42,3 +54,9 @@ pub fn fail_write(path: &std::path::Path) -> Result<(), String> { _ => Ok(()), } } + +#[cfg(not(feature = "test-hooks"))] +#[inline(always)] +pub fn fail_write(_path: &std::path::Path) -> Result<(), String> { + Ok(()) +}