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::>(); 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")); }