Files
ianitor/crates/control/src/crash.rs
T

66 lines
2.3 KiB
Rust

// SPDX-License-Identifier: CPAL-1.0
// Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution
// License 1.0: see LICENSE (its Exhibit A is this file's notice).
//! Crash points for the crash-safety tests: with `IANITOR_TEST_CRASH_AT=<stage>` in its
//! environment, control exits at once with status 134 (no cleanup in Ianitor's code, like a
//! kill -9) when an apply reaches that stage. It does not raise SIGABRT: a signal would make
//! the kernel log a register dump per crash test and could hand the process to the host's
//! core-dump handler, and the tests must not reach into the host's systemd.
//!
//! `IANITOR_TEST_FAIL_WRITE=<file name>` 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] = [
"prepared",
"committed",
"trial_written",
"trial_loaded",
"watching",
"watched",
"confirmed",
"promoted",
"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}: exiting on purpose");
std::process::exit(134);
}
}
#[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"),
path.file_name(),
) {
(Some(want), Some(name)) if want == name => Err(format!(
"cannot write {}: IANITOR_TEST_FAIL_WRITE (injected)",
path.display()
)),
_ => Ok(()),
}
}
#[cfg(not(feature = "test-hooks"))]
#[inline(always)]
pub fn fail_write(_path: &std::path::Path) -> Result<(), String> {
Ok(())
}