summaryrefslogtreecommitdiff
path: root/tests/archive.rs
blob: 395684960756d42a6eac68718e8845ef85eecd06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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"));
}