Skip to content
Closed
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
70 changes: 69 additions & 1 deletion library/src/cache/patch_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,15 @@ impl ManagePatches for PatchManager {
}

fn remove_patch(&mut self, patch_number: usize) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is only used for the rollback case, we should comment that.

self.try_fall_back_from_patch(patch_number)
self.delete_patch_artifacts(patch_number)?;
// If the patch to remove is the next boot patch, clear it.
if let Some(next_boot_patch) = self.patches_state.next_boot_patch.clone() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the change here is to clear the next_boot_patch if it happens to be the same as the patch being removed. Do we need to clear it from other fields as well?

if next_boot_patch.number == patch_number {
self.patches_state.next_boot_patch = None;
}
}

self.save_patches_state()
}

fn reset(&mut self) -> Result<()> {
Expand Down Expand Up @@ -1212,3 +1220,63 @@ mod reset_tests {
Ok(())
}
}

#[cfg(test)]
mod remove_patch_tests {
use super::*;
use anyhow::{Ok, Result};
use tempdir::TempDir;

#[test]
fn deletes_patch_artifacts() -> Result<()> {
let temp_dir = TempDir::new("patch_manager")?;
let mut manager = PatchManager::manager_for_test(&temp_dir);
manager.add_patch_for_test(&temp_dir, 1)?;
assert!(manager.remove_patch(1).is_ok());
assert!(!manager.patch_dir(1).exists());

Ok(())
}

#[test]
fn clears_next_boot_patch_if_it_is_the_removed_patch() -> Result<()> {
let temp_dir = TempDir::new("patch_manager")?;
let mut manager = PatchManager::manager_for_test(&temp_dir);
manager.add_patch_for_test(&temp_dir, 1)?;
manager.record_boot_start_for_patch(1)?;
manager.record_boot_success()?;

manager.add_patch_for_test(&temp_dir, 2)?;
manager.record_boot_start_for_patch(2)?;
manager.record_boot_success()?;

assert!(manager.remove_patch(1).is_ok());

assert_eq!(manager.patches_state.last_booted_patch.unwrap().number, 2);
assert_eq!(manager.patches_state.next_boot_patch.unwrap().number, 2);

Ok(())
}

#[test]
fn does_not_clear_next_boot_patch_if_it_is_not_the_removed_patch() -> Result<()> {
let temp_dir = TempDir::new("patch_manager")?;
let mut manager = PatchManager::manager_for_test(&temp_dir);
manager.add_patch_for_test(&temp_dir, 1)?;
manager.record_boot_start_for_patch(1)?;
manager.record_boot_success()?;

manager.add_patch_for_test(&temp_dir, 2)?;
manager.record_boot_start_for_patch(2)?;
manager.record_boot_success()?;

assert!(manager.remove_patch(2).is_ok());

assert_eq!(manager.patches_state.last_booted_patch.unwrap().number, 2);

// Because patch 2 booted successfully, we cleared patch 1, so now have no next boot patch.
assert!(manager.patches_state.next_boot_patch.is_none());

Ok(())
}
}
8 changes: 8 additions & 0 deletions library/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,14 @@ mod rollback_tests {
Ok(())
})?;

with_mut_state(|state| {
assert_eq!(
state.last_successfully_booted_patch().map(|p| p.number),
Some(1)
);
Ok(())
})?;

Ok(())
}

Expand Down