Test that compute-composefs-digest works on a directory
()
| 277 | |
| 278 | /// Test that compute-composefs-digest works on a directory |
| 279 | pub(crate) fn test_compute_composefs_digest() -> Result<()> { |
| 280 | use std::os::unix::fs::PermissionsExt; |
| 281 | |
| 282 | // Create temp directory with test filesystem structure |
| 283 | let td = tempfile::tempdir()?; |
| 284 | let root = td.path(); |
| 285 | |
| 286 | // Create directories required by transform_for_boot |
| 287 | fs::create_dir_all(root.join("boot"))?; |
| 288 | fs::create_dir_all(root.join("sysroot"))?; |
| 289 | |
| 290 | // Create usr/bin/hello (executable) |
| 291 | let usr_bin = root.join("usr/bin"); |
| 292 | fs::create_dir_all(&usr_bin)?; |
| 293 | let hello_path = usr_bin.join("hello"); |
| 294 | fs::write(&hello_path, "test\n")?; |
| 295 | fs::set_permissions(&hello_path, fs::Permissions::from_mode(0o755))?; |
| 296 | |
| 297 | // Create etc/config (regular file) |
| 298 | let etc = root.join("etc"); |
| 299 | fs::create_dir_all(&etc)?; |
| 300 | let config_path = etc.join("config"); |
| 301 | fs::write(&config_path, "test\n")?; |
| 302 | fs::set_permissions(&config_path, fs::Permissions::from_mode(0o644))?; |
| 303 | |
| 304 | // Run bootc container compute-composefs-digest |
| 305 | let sh = Shell::new()?; |
| 306 | let path_str = root.to_str().unwrap(); |
| 307 | let digest = cmd!(sh, "bootc container compute-composefs-digest {path_str}").read()?; |
| 308 | let digest = digest.trim(); |
| 309 | |
| 310 | // Verify it's a valid hex string of expected length (SHA-512 = 128 hex chars) |
| 311 | assert_eq!( |
| 312 | digest.as_bytes().len(), |
| 313 | 128, |
| 314 | "Expected 512-bit hex digest, got length {}", |
| 315 | digest.as_bytes().len() |
| 316 | ); |
| 317 | assert!( |
| 318 | digest.chars().all(|c| c.is_ascii_hexdigit()), |
| 319 | "Digest contains non-hex characters: {digest}" |
| 320 | ); |
| 321 | |
| 322 | // Verify consistency - running the command twice produces the same result |
| 323 | let digest2 = cmd!(sh, "bootc container compute-composefs-digest {path_str}").read()?; |
| 324 | assert_eq!( |
| 325 | digest, |
| 326 | digest2.trim(), |
| 327 | "Digest should be consistent across multiple invocations" |
| 328 | ); |
| 329 | |
| 330 | Ok(()) |
| 331 | } |
| 332 | |
| 333 | /// Tests that should be run in a default container image. |
| 334 | #[context("Container tests")] |