|
| 1 | +//! Integration test utilities for composefs-rs |
| 2 | +//! |
| 3 | +//! This library provides utilities for running integration tests. |
| 4 | +//! The main test runner is in main.rs. |
| 5 | +
|
| 6 | +use std::process::{Command, ExitStatus, Stdio}; |
| 7 | +use std::sync::Arc; |
| 8 | + |
| 9 | +use anyhow::{Context, Result}; |
| 10 | +use composefs::fsverity::Sha256HashValue; |
| 11 | +use composefs::repository::Repository; |
| 12 | +use tempfile::TempDir; |
| 13 | + |
| 14 | +/// Test label for cleanup |
| 15 | +pub const INTEGRATION_TEST_LABEL: &str = "composefs-rs.integration-test=1"; |
| 16 | + |
| 17 | +/// Get the path to cfsctl binary |
| 18 | +pub fn get_cfsctl_path() -> Result<String> { |
| 19 | + // Check environment first |
| 20 | + if let Ok(path) = std::env::var("CFSCTL_PATH") { |
| 21 | + return Ok(path); |
| 22 | + } |
| 23 | + // Look in common locations |
| 24 | + for path in [ |
| 25 | + "./target/release/cfsctl", |
| 26 | + "./target/debug/cfsctl", |
| 27 | + "/usr/bin/cfsctl", |
| 28 | + ] { |
| 29 | + if std::path::Path::new(path).exists() { |
| 30 | + return Ok(path.to_string()); |
| 31 | + } |
| 32 | + } |
| 33 | + anyhow::bail!("cfsctl not found; set CFSCTL_PATH or build with `cargo build --release`") |
| 34 | +} |
| 35 | + |
| 36 | +/// Get the primary test image |
| 37 | +pub fn get_primary_image() -> String { |
| 38 | + std::env::var("COMPOSEFS_RS_PRIMARY_IMAGE") |
| 39 | + .unwrap_or_else(|_| "quay.io/centos-bootc/centos-bootc:stream10".to_string()) |
| 40 | +} |
| 41 | + |
| 42 | +/// Get all test images |
| 43 | +pub fn get_all_images() -> Vec<String> { |
| 44 | + std::env::var("COMPOSEFS_RS_ALL_IMAGES") |
| 45 | + .unwrap_or_else(|_| get_primary_image()) |
| 46 | + .split_whitespace() |
| 47 | + .map(String::from) |
| 48 | + .collect() |
| 49 | +} |
| 50 | + |
| 51 | +/// Captured command output |
| 52 | +#[derive(Debug)] |
| 53 | +pub struct CapturedOutput { |
| 54 | + /// Exit status |
| 55 | + pub status: ExitStatus, |
| 56 | + /// Captured stdout |
| 57 | + pub stdout: String, |
| 58 | + /// Captured stderr |
| 59 | + pub stderr: String, |
| 60 | +} |
| 61 | + |
| 62 | +impl CapturedOutput { |
| 63 | + /// Assert the command succeeded |
| 64 | + pub fn assert_success(&self) -> Result<()> { |
| 65 | + if !self.status.success() { |
| 66 | + anyhow::bail!( |
| 67 | + "Command failed with status {}\nstdout: {}\nstderr: {}", |
| 68 | + self.status, |
| 69 | + self.stdout, |
| 70 | + self.stderr |
| 71 | + ); |
| 72 | + } |
| 73 | + Ok(()) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Run a command and capture output |
| 78 | +pub fn run_command(cmd: &str, args: &[&str]) -> Result<CapturedOutput> { |
| 79 | + let output = Command::new(cmd) |
| 80 | + .args(args) |
| 81 | + .stdout(Stdio::piped()) |
| 82 | + .stderr(Stdio::piped()) |
| 83 | + .output() |
| 84 | + .with_context(|| format!("Failed to execute: {} {:?}", cmd, args))?; |
| 85 | + |
| 86 | + Ok(CapturedOutput { |
| 87 | + status: output.status, |
| 88 | + stdout: String::from_utf8_lossy(&output.stdout).to_string(), |
| 89 | + stderr: String::from_utf8_lossy(&output.stderr).to_string(), |
| 90 | + }) |
| 91 | +} |
| 92 | + |
| 93 | +/// Run cfsctl with arguments |
| 94 | +pub fn run_cfsctl(args: &[&str]) -> Result<CapturedOutput> { |
| 95 | + let cfsctl = get_cfsctl_path()?; |
| 96 | + run_command(&cfsctl, args) |
| 97 | +} |
| 98 | + |
| 99 | +/// Create a test repository in a temporary directory. |
| 100 | +/// |
| 101 | +/// The TempDir is returned alongside the repo to keep it alive. |
| 102 | +pub fn create_test_repository(tempdir: &TempDir) -> Result<Arc<Repository<Sha256HashValue>>> { |
| 103 | + let fd = rustix::fs::open( |
| 104 | + tempdir.path(), |
| 105 | + rustix::fs::OFlags::CLOEXEC | rustix::fs::OFlags::PATH, |
| 106 | + 0.into(), |
| 107 | + )?; |
| 108 | + |
| 109 | + let mut repo = Repository::open_path(&fd, ".")?; |
| 110 | + repo.set_insecure(true); |
| 111 | + Ok(Arc::new(repo)) |
| 112 | +} |
| 113 | + |
| 114 | +/// Build a minimal test image using podman and return its ID |
| 115 | +pub fn build_test_image() -> Result<String> { |
| 116 | + let temp_dir = TempDir::new()?; |
| 117 | + let containerfile = temp_dir.path().join("Containerfile"); |
| 118 | + |
| 119 | + // Create a simple Containerfile with various file sizes to test |
| 120 | + // both inline and external storage paths |
| 121 | + std::fs::write( |
| 122 | + &containerfile, |
| 123 | + r#"FROM busybox:latest |
| 124 | +# Small file (should be inlined) |
| 125 | +RUN echo "small content" > /small.txt |
| 126 | +# Larger file (should be external) |
| 127 | +RUN dd if=/dev/zero of=/large.bin bs=1024 count=100 2>/dev/null |
| 128 | +# Directory with files |
| 129 | +RUN mkdir -p /testdir && echo "file1" > /testdir/a.txt && echo "file2" > /testdir/b.txt |
| 130 | +# Symlink |
| 131 | +RUN ln -s /small.txt /link.txt |
| 132 | +"#, |
| 133 | + )?; |
| 134 | + |
| 135 | + let iid_file = temp_dir.path().join("image.iid"); |
| 136 | + |
| 137 | + let output = Command::new("podman") |
| 138 | + .args([ |
| 139 | + "build", |
| 140 | + "--pull=newer", |
| 141 | + &format!("--iidfile={}", iid_file.display()), |
| 142 | + "-f", |
| 143 | + &containerfile.to_string_lossy(), |
| 144 | + &temp_dir.path().to_string_lossy(), |
| 145 | + ]) |
| 146 | + .output()?; |
| 147 | + |
| 148 | + if !output.status.success() { |
| 149 | + anyhow::bail!( |
| 150 | + "podman build failed: {}", |
| 151 | + String::from_utf8_lossy(&output.stderr) |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + let image_id = std::fs::read_to_string(&iid_file)?.trim().to_string(); |
| 156 | + Ok(image_id) |
| 157 | +} |
| 158 | + |
| 159 | +/// Remove a test image |
| 160 | +pub fn cleanup_test_image(image_id: &str) { |
| 161 | + let _ = Command::new("podman") |
| 162 | + .args(["rmi", "-f", image_id]) |
| 163 | + .output(); |
| 164 | +} |
0 commit comments