diff options
| author | Hermes Agent <hermes@localhost> | 2026-08-12 01:25:38 +0000 |
|---|---|---|
| committer | Hermes Agent <hermes@localhost> | 2026-08-12 01:25:38 +0000 |
| commit | 020fbf6a034b3c90de1bdd3ee5e3e5e15c783059 (patch) | |
| tree | e43a833f092357dc76ea664ee2d840ed6f706c55 /tests/archive.rs | |
| parent | 9a07a0e20b88838d22bb04fae80b2353d41b2cd0 (diff) | |
Add native deterministic tar writer
Diffstat (limited to 'tests/archive.rs')
| -rw-r--r-- | tests/archive.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/archive.rs b/tests/archive.rs new file mode 100644 index 0000000..3956849 --- /dev/null +++ b/tests/archive.rs @@ -0,0 +1,57 @@ +use std::fs; +use std::os::unix::fs::symlink; + +use alt_controller_image::archive::NativeTarWriter; +use tar::Archive; +use tempfile::tempdir; + +#[test] +fn writes_deterministic_tar_with_files_directories_and_symlinks() { + let fixture = tempdir().expect("fixture directory"); + let rootfs = fixture.path().join("rootfs"); + fs::create_dir_all(rootfs.join("etc/nested")).expect("create rootfs tree"); + fs::write(rootfs.join("etc/nested/config"), "controller=true\n").expect("write config"); + symlink("nested/config", rootfs.join("etc/config")).expect("create symlink"); + fs::create_dir_all(rootfs.join(".host/private")).expect("create host metadata"); + fs::write(rootfs.join(".host/private/key"), "excluded").expect("write host metadata"); + fs::create_dir_all(rootfs.join("var/.fakedata")).expect("create fake metadata"); + fs::write(rootfs.join("var/.fakedata/db"), "excluded").expect("write fake metadata"); + + let first = fixture.path().join("first.tar"); + let second = fixture.path().join("second.tar"); + NativeTarWriter::new().write(&rootfs, &first).expect("write first archive"); + NativeTarWriter::new().write(&rootfs, &second).expect("write second archive"); + + assert_eq!(fs::read(&first).expect("read first"), fs::read(&second).expect("read second")); + + let mut archive = Archive::new(fs::File::open(first).expect("open archive")); + let entries = archive + .entries() + .expect("read archive entries") + .map(|entry| { + let entry = entry.expect("read entry"); + let path = entry.path().expect("entry path").into_owned(); + (path, entry.header().entry_type(), entry.header().mtime().expect("mtime"), entry.header().uid().expect("uid"), entry.header().gid().expect("gid")) + }) + .collect::<Vec<_>>(); + + assert_eq!( + entries, + vec![ + ("etc".into(), tar::EntryType::Directory, 0, 0, 0), + ("etc/config".into(), tar::EntryType::Symlink, 0, 0, 0), + ("etc/nested".into(), tar::EntryType::Directory, 0, 0, 0), + ("etc/nested/config".into(), tar::EntryType::Regular, 0, 0, 0), + ("var".into(), tar::EntryType::Directory, 0, 0, 0), + ] + ); +} + +#[test] +fn rejects_a_missing_rootfs() { + let fixture = tempdir().expect("fixture directory"); + let error = NativeTarWriter::new() + .write(fixture.path().join("missing"), fixture.path().join("image.tar")) + .expect_err("missing rootfs must fail"); + assert!(error.to_string().contains("rootfs is not a directory")); +} |