Skip to content

Commit

Permalink
filetree: add failpoint when doing exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
HuijingHei committed Jul 12, 2024
1 parent 246a5c9 commit 7a13fa5
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 3 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ camino = "1.1.7"
chrono = { version = "0.4.38", features = ["serde"] }
clap = { version = "3.2", default-features = false, features = ["cargo", "derive", "std", "suggestions"] }
env_logger = "0.10"
fail = { version = "0.5", features = ["failpoints"] }
fn-error-context = "0.2.1"
fs2 = "0.4.3"
hex = "0.4.3"
Expand Down
15 changes: 15 additions & 0 deletions src/bootupd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ pub(crate) fn print_status(status: &Status) -> Result<()> {
}

pub(crate) fn client_run_update() -> Result<()> {
crate::try_fail_point!("update");
let status: Status = status()?;
if status.components.is_empty() && status.adoptable.is_empty() {
println!("No components installed.");
Expand Down Expand Up @@ -489,3 +490,17 @@ pub(crate) fn client_run_validate() -> Result<()> {
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_failpoint_update() {
let guard = fail::FailScenario::setup();
fail::cfg("update", "return").unwrap();
let r = client_run_update();
assert_eq!(r.is_err(), true);
guard.teardown();
}
}
21 changes: 21 additions & 0 deletions src/failpoints.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! Wrappers and utilities on top of the `fail` crate.
// SPDX-License-Identifier: Apache-2.0 OR MIT

/// TODO: Use https://github.com/tikv/fail-rs/pull/68 once it merges
/// copy from https://github.com/coreos/rpm-ostree/commit/aa8d7fb0ceaabfaf10252180e2ddee049d07aae3#diff-adcc419e139605fae34d17b31418dbaf515af2fe9fb766fcbdb2eaad862b3daa
#[macro_export]
macro_rules! try_fail_point {
($name:expr) => {{
if let Some(e) = fail::eval($name, |msg| {
let msg = msg.unwrap_or_else(|| "synthetic failpoint".to_string());
anyhow::Error::msg(msg)
}) {
return Err(From::from(e));
}
}};
($name:expr, $cond:expr) => {{
if $cond {
$crate::try_fail_point!($name);
}
}};
}
2 changes: 1 addition & 1 deletion src/filetree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ pub(crate) fn apply_diff(
.local_rename(tmp, dst)
.with_context(|| format!("rename for {} and {:?}", tmp, dst))?;
}
crate::try_fail_point!("exchange");
}
// Ensure all of the updates & changes are written persistently to disk
if !opts.skip_sync {
Expand Down Expand Up @@ -705,7 +706,6 @@ mod tests {
let b_btime_foo_new = fs::metadata(pb.join(foo))?.created()?;
assert_eq!(b_btime_foo_new, b_btime_foo);
}

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod component;
mod coreos;
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
mod efi;
mod failpoints;
mod filesystem;
mod filetree;
#[cfg(any(
Expand All @@ -43,6 +44,7 @@ use clap::crate_name;

/// Binary entrypoint, for both daemon and client logic.
fn main() {
let _scenario = fail::FailScenario::setup();
let exit_code = run_cli();
std::process::exit(exit_code);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e-update/e2e-update-in-vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ tmpefimount=$(mount_tmp_efi)

assert_not_has_file ${tmpefimount}/EFI/fedora/test-bootupd.efi

if env FAILPOINTS='exchange=return' bootupctl update -vvv 2>err.txt; then
fatal "should have errored"
fi
assert_file_has_content_literal err.txt "error: synthetic failpoint"

bootupctl update | tee out.txt
assert_file_has_content out.txt "Previous EFI: .*"
assert_file_has_content out.txt "Updated EFI: ${TARGET_GRUB_PKG}.*,test-bootupd-payload-1.0"
Expand Down
8 changes: 6 additions & 2 deletions tests/e2e-update/e2e-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ export test_tmpdir=${testtmp}

# This is new content for our update
test_bootupd_payload_file=/boot/efi/EFI/fedora/test-bootupd.efi
test_bootupd_payload_file1=/boot/efi/EFI/BOOT/test-bootupd1.efi
build_rpm test-bootupd-payload \
files ${test_bootupd_payload_file} \
files "${test_bootupd_payload_file}
${test_bootupd_payload_file1}" \
install "mkdir -p %{buildroot}/$(dirname ${test_bootupd_payload_file})
echo test-payload > %{buildroot}/${test_bootupd_payload_file}"
echo test-payload > %{buildroot}/${test_bootupd_payload_file}
mkdir -p %{buildroot}/$(dirname ${test_bootupd_payload_file1})
echo test-payload1 > %{buildroot}/${test_bootupd_payload_file1}"

# Start in cosa dir
cd ${COSA_DIR}
Expand Down

0 comments on commit 7a13fa5

Please sign in to comment.