lanzatool: intgeration test infrastrucutre + gc tests

This commit is contained in:
nikstur 2022-12-30 16:22:06 +01:00
parent 676786f811
commit d3a96b1c3c
5 changed files with 287 additions and 4 deletions

View File

@ -50,14 +50,14 @@
{ src { src
, target ? null , target ? null
, doCheck ? true , doCheck ? true
, extraArgs ? { }
}: }:
let let
cleanedSrc = craneLib.cleanCargoSource src;
commonArgs = { commonArgs = {
src = cleanedSrc; inherit src;
CARGO_BUILD_TARGET = target; CARGO_BUILD_TARGET = target;
inherit doCheck; inherit doCheck;
}; } // extraArgs;
cargoArtifacts = craneLib.buildDepsOnly commonArgs; cargoArtifacts = craneLib.buildDepsOnly commonArgs;
in in
@ -73,7 +73,7 @@
}; };
lanzabooteCrane = buildRustApp { lanzabooteCrane = buildRustApp {
src = ./rust/lanzaboote; src = craneLib.cleanCargoSource ./rust/lanzaboote;
target = "x86_64-unknown-uefi"; target = "x86_64-unknown-uefi";
doCheck = false; doCheck = false;
}; };
@ -82,6 +82,13 @@
lanzatoolCrane = buildRustApp { lanzatoolCrane = buildRustApp {
src = ./rust/lanzatool; src = ./rust/lanzatool;
extraArgs = {
TEST_SYSTEMD = pkgs.systemd;
checkInputs = with pkgs; [
binutils-unwrapped
sbsigntool
];
};
}; };
lanzatool-unwrapped = lanzatoolCrane.package; lanzatool-unwrapped = lanzatoolCrane.package;
@ -134,6 +141,8 @@
lanzaboote lanzaboote
lanzatool lanzatool
]; ];
TEST_SYSTEMD = pkgs.systemd;
}; };
checks.x86_64-linux = { checks.x86_64-linux = {

View File

@ -0,0 +1,155 @@
use std::ffi::OsStr;
use std::fs;
use std::io::Write;
use std::os::unix::fs::symlink;
use std::path::{Path, PathBuf};
use std::process::Output;
use anyhow::{Context, Result};
use assert_cmd::Command;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use serde_json::json;
/// Create a mock generation link.
///
/// Creates the generation link using the specified version inside a mock profiles directory
/// (mimicking /nix/var/nix/profiles). Returns the path to the generation link.
pub fn setup_generation_link(
tmpdir: &Path,
profiles_directory: &Path,
version: u64,
) -> Result<PathBuf> {
let toplevel = setup_toplevel(tmpdir).context("Failed to setup toplevel")?;
let bootspec = json!({
"v1": {
"init": format!("init-v{}", version),
"initrd": toplevel.join("initrd"),
"kernel": toplevel.join("kernel"),
"kernelParams": [
"amd_iommu=on",
"amd_iommu=pt",
"iommu=pt",
"kvm.ignore_msrs=1",
"kvm.report_ignored_msrs=0",
"udev.log_priority=3",
"systemd.unified_cgroup_hierarchy=1",
"loglevel=4"
],
"label": "LanzaOS",
"toplevel": toplevel,
"system": "x86_64-linux",
"specialisation": {},
"extensions": {
"lanzaboote": { "osRelease": toplevel.join("os-release") }
}
}
});
let generation_link_path = profiles_directory.join(format!("system-{}-link", version));
fs::create_dir(&generation_link_path)?;
let bootspec_path = generation_link_path.join("boot.json");
let mut file = fs::File::create(bootspec_path)?;
file.write_all(&serde_json::to_vec(&bootspec)?)?;
Ok(generation_link_path)
}
/// Setup a mock toplevel inside a temporary directory.
///
/// Accepts the temporary directory as a parameter so that the invoking function retains control of
/// it (and when it goes out of scope).
fn setup_toplevel(tmpdir: &Path) -> Result<PathBuf> {
// Generate a random toplevel name so that multiple toplevel paths can live alongside each
// other in the same directory.
let toplevel = tmpdir.join(format!("toplevel-{}", random_string(8)));
fs::create_dir(&toplevel)?;
let test_systemd = systemd_location_from_env()?;
let test_systemd_stub = format!("{test_systemd}/lib/systemd/boot/efi/linuxx64.efi.stub");
let initrd_path = toplevel.join("initrd");
let kernel_path = toplevel.join("kernel");
let systemd_path = toplevel.join("systemd");
let os_release_path = toplevel.join("os-release");
// To simplify the test setup, we use the systemd stub for all PE binaries used by lanzatool.
// Lanzatool doesn't care whether its actually a kernel or initrd but only whether it can
// manipulate the PE binary with objcopy and/or sign it with sbsigntool. For testing lanzatool
// in isolation this should suffice.
fs::copy(&test_systemd_stub, initrd_path)?;
fs::copy(&test_systemd_stub, kernel_path)?;
symlink(&test_systemd, systemd_path)?;
setup_os_release(&os_release_path).context("Failed to setup os-release")?;
Ok(toplevel)
}
fn random_string(length: usize) -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(length)
.map(char::from)
.collect()
}
fn setup_os_release(path: &Path) -> Result<()> {
let content = r#"
ID=lanzaos
NAME=LanzaOS
PRETTY_NAME="LanzaOS 23.05 (Goat)"
VERSION="23.05 (Goat)"
"#;
let mut file = fs::File::create(path)?;
file.write_all(content.as_bytes())?;
Ok(())
}
/// Call the `lanzaboote install` command.
pub fn lanzaboote_install(
config_limit: u64,
esp_mountpoint: &Path,
generation_links: impl IntoIterator<Item = impl AsRef<OsStr>>,
) -> Result<Output> {
// To simplify the test setup, we use the systemd stub here instead of the lanzaboote stub. See
// the comment in setup_toplevel for details.
let test_systemd = systemd_location_from_env()?;
let test_systemd_stub = format!("{test_systemd}/lib/systemd/boot/efi/linuxx64.efi.stub");
let mut cmd = Command::cargo_bin("lanzatool")?;
let output = cmd
.env("LANZABOOTE_STUB", test_systemd_stub)
.arg("install")
.arg("--public-key")
.arg("tests/fixtures/uefi-keys/db.pem")
.arg("--private-key")
.arg("tests/fixtures/uefi-keys/db.key")
.arg("--configuration-limit")
.arg(config_limit.to_string())
.arg(esp_mountpoint)
.args(generation_links)
.output()?;
// Print debugging output.
// This is a weird hack to make cargo test capture the output.
// See https://github.com/rust-lang/rust/issues/12309
print!("{}", String::from_utf8(output.stdout.clone())?);
print!("{}", String::from_utf8(output.stderr.clone())?);
// Also walk the entire ESP mountpoint and print each path for debugging
for entry in walkdir::WalkDir::new(esp_mountpoint) {
println!("{}", entry?.path().display());
}
Ok(output)
}
/// Read location of systemd installation from an environment variable.
fn systemd_location_from_env() -> Result<String> {
let error_msg = "TEST_SYSTEMD environment variable is not set. TEST_SYSTEMD has to point to a systemd installation.
On a system with Nix installed, you can set it with: export TEST_SYSTEMD=$(nix-build '<nixpkgs>' -A systemd)";
std::env::var("TEST_SYSTEMD").context(error_msg)
}

View File

@ -0,0 +1,52 @@
-----BEGIN PRIVATE KEY-----
MIIJ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
GBG/orDJrOClvt8qABL60ojnilLh4J5BwGDwBcXE/wETvXb+o3GsjTqqHdXYCQx0
e3SpR/xQCfKzuaE4ysaszKFcNbhaezLmo/pzD2v0UA6Jr56re/jkYMtLQL4+LAUP
kCH66vc6qETQwtAzV3CYQ5TirwLLD6z3luPhm2FzyXTU9eydbsRp7FuSKq8mVXKP
cskrVJSjWD7fOsy/sZq3S+xmA3w1C8XRAgz+xlfIQkuu0mAa3jJ3JGjDyu1lAZ6N
oUc897npQ2tpYyGRQ/zOmKSE9oUlcEqXLPk2nDgenhG0oAdM/jJ54rnuR2x3zhBx
gyPEvWVzjzX8E//9Kc6VocisnI/tckibUPHlMJFHFBXiU8m6vO28cCRl4tRsH/K9
jOnm+ztraiXI7mqBfTchR7Ga0EmnWFf38y3/YkrbVYbjK6o/+/T72Sje0MfCAvTl
KLOtvaQWQm36KP/2PH3DZ/k/v8+U76oeUBoI/DDH8E4rR1cUORvUYH8MZyPIKM4e
GN3+/zjr5gw70iLC/p8C+8rCzxwoernIMcFlL+SaeybnzXvH3VqO/XAFA/lxMu4V
K7Gzrtr5n+FiSTXytdIu18o6Av65XN5hzwQvVeWIx/E5Jry/JlSQgho8CnqLK8g5
USdRgvRJN7xJM3szP93f7aNgIYSys2cCxtZxunU=
-----END CERTIFICATE-----

View File

@ -0,0 +1,38 @@
use std::fs;
use std::path::{Path, PathBuf};
use anyhow::Result;
use tempfile::tempdir;
mod common;
#[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();
// Install all 3 generations.
let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), generation_links.clone())?;
assert!(output0.status.success());
assert_eq!(stub_count(), 3);
// Call `lanzatool install` again with a config limit of 2 and assert that one is deleted.
let output1 = common::lanzaboote_install(2, esp_mountpoint.path(), generation_links)?;
assert!(output1.status.success());
assert_eq!(stub_count(), 2);
Ok(())
}
fn count_files(path: &Path) -> Result<usize> {
Ok(fs::read_dir(path)?.count())
}