2022-12-30 09:22:06 -06:00
|
|
|
use std::fs;
|
2023-02-15 16:09:24 -06:00
|
|
|
use std::path::PathBuf;
|
2022-12-30 09:22:06 -06:00
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use tempfile::tempdir;
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
2023-02-15 16:09:24 -06:00
|
|
|
use common::count_files;
|
|
|
|
|
2022-12-30 09:22:06 -06:00
|
|
|
#[test]
|
|
|
|
fn keep_only_configured_number_of_generations() -> Result<()> {
|
|
|
|
let esp_mountpoint = tempdir()?;
|
|
|
|
let tmpdir = tempdir()?;
|
|
|
|
let profiles = tempdir()?;
|
|
|
|
let generation_links: Vec<PathBuf> = [1, 2, 3]
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
common::setup_generation_link(tmpdir.path(), profiles.path(), v)
|
|
|
|
.expect("Failed to setup generation link")
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let stub_count = || count_files(&esp_mountpoint.path().join("EFI/Linux")).unwrap();
|
2023-01-03 17:41:47 -06:00
|
|
|
let kernel_and_initrd_count = || count_files(&esp_mountpoint.path().join("EFI/nixos")).unwrap();
|
2022-12-30 09:22:06 -06:00
|
|
|
|
|
|
|
// Install all 3 generations.
|
|
|
|
let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), generation_links.clone())?;
|
|
|
|
assert!(output0.status.success());
|
2023-01-03 17:41:47 -06:00
|
|
|
assert_eq!(stub_count(), 3, "Wrong number of stubs after installation");
|
|
|
|
assert_eq!(
|
|
|
|
kernel_and_initrd_count(),
|
2023-08-12 08:23:21 -05:00
|
|
|
2,
|
2023-01-03 17:41:47 -06:00
|
|
|
"Wrong number of kernels & initrds after installation"
|
|
|
|
);
|
2022-12-30 09:22:06 -06:00
|
|
|
|
|
|
|
// Call `lanzatool install` again with a config limit of 2 and assert that one is deleted.
|
2023-08-12 08:23:21 -05:00
|
|
|
// In addition, the garbage kernel should be deleted as well.
|
2022-12-30 09:22:06 -06:00
|
|
|
let output1 = common::lanzaboote_install(2, esp_mountpoint.path(), generation_links)?;
|
|
|
|
assert!(output1.status.success());
|
2023-01-03 17:41:47 -06:00
|
|
|
assert_eq!(stub_count(), 2, "Wrong number of stubs after gc.");
|
|
|
|
assert_eq!(
|
|
|
|
kernel_and_initrd_count(),
|
2023-08-12 08:23:21 -05:00
|
|
|
2,
|
|
|
|
"Wrong number of kernels & initrds after gc."
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn delete_garbage_kernel() -> Result<()> {
|
|
|
|
let esp_mountpoint = tempdir()?;
|
|
|
|
let tmpdir = tempdir()?;
|
|
|
|
let profiles = tempdir()?;
|
|
|
|
let generation_links: Vec<PathBuf> = [1, 2, 3]
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
common::setup_generation_link(tmpdir.path(), profiles.path(), v)
|
|
|
|
.expect("Failed to setup generation link")
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let stub_count = || count_files(&esp_mountpoint.path().join("EFI/Linux")).unwrap();
|
|
|
|
let kernel_and_initrd_count = || count_files(&esp_mountpoint.path().join("EFI/nixos")).unwrap();
|
|
|
|
|
|
|
|
// Install all 3 generations.
|
|
|
|
let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), generation_links.clone())?;
|
|
|
|
assert!(output0.status.success());
|
|
|
|
|
|
|
|
// Create a garbage kernel, which should be deleted.
|
|
|
|
fs::write(
|
|
|
|
esp_mountpoint.path().join("EFI/nixos/kernel-garbage.efi"),
|
|
|
|
"garbage",
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// Call `lanzatool install` again with a config limit of 2.
|
|
|
|
// In addition, the garbage kernel should be deleted as well.
|
|
|
|
let output1 = common::lanzaboote_install(2, esp_mountpoint.path(), generation_links)?;
|
|
|
|
assert!(output1.status.success());
|
|
|
|
|
|
|
|
assert_eq!(stub_count(), 2, "Wrong number of stubs after gc.");
|
|
|
|
assert_eq!(
|
|
|
|
kernel_and_initrd_count(),
|
|
|
|
2,
|
2023-01-03 17:41:47 -06:00
|
|
|
"Wrong number of kernels & initrds after gc."
|
|
|
|
);
|
2022-12-30 09:22:06 -06:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-01-03 18:20:50 -06:00
|
|
|
#[test]
|
|
|
|
fn keep_unrelated_files_on_esp() -> Result<()> {
|
|
|
|
let esp_mountpoint = tempdir()?;
|
|
|
|
let tmpdir = tempdir()?;
|
|
|
|
let profiles = tempdir()?;
|
|
|
|
let generation_links: Vec<PathBuf> = [1, 2, 3]
|
|
|
|
.into_iter()
|
|
|
|
.map(|v| {
|
|
|
|
common::setup_generation_link(tmpdir.path(), profiles.path(), v)
|
|
|
|
.expect("Failed to setup generation link")
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Install all 3 generations.
|
|
|
|
let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), generation_links.clone())?;
|
|
|
|
assert!(output0.status.success());
|
|
|
|
|
2023-01-26 17:37:05 -06:00
|
|
|
let unrelated_loader_config = esp_mountpoint.path().join("loader/loader.conf");
|
2023-01-03 18:20:50 -06:00
|
|
|
let unrelated_uki = esp_mountpoint.path().join("EFI/Linux/ubuntu.efi");
|
|
|
|
let unrelated_os = esp_mountpoint.path().join("EFI/windows");
|
|
|
|
let unrelated_firmware = esp_mountpoint.path().join("dell");
|
2023-01-26 17:37:05 -06:00
|
|
|
fs::File::create(&unrelated_loader_config)?;
|
2023-01-03 18:20:50 -06:00
|
|
|
fs::File::create(&unrelated_uki)?;
|
|
|
|
fs::create_dir(&unrelated_os)?;
|
|
|
|
fs::create_dir(&unrelated_firmware)?;
|
|
|
|
|
|
|
|
// Call `lanzatool install` again with a config limit of 2.
|
|
|
|
let output1 = common::lanzaboote_install(2, esp_mountpoint.path(), generation_links)?;
|
|
|
|
assert!(output1.status.success());
|
|
|
|
|
2023-01-26 17:37:05 -06:00
|
|
|
assert!(unrelated_loader_config.exists());
|
2023-01-03 18:20:50 -06:00
|
|
|
assert!(unrelated_uki.exists());
|
|
|
|
assert!(unrelated_os.exists());
|
|
|
|
assert!(unrelated_firmware.exists());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|