// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. //! Crash points for the crash-safety tests: with `IANITOR_TEST_CRASH_AT=` 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=` 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(()) }