mithril_aggregator/services/snapshotter/
appender.rs

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use anyhow::{anyhow, Context};
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use mithril_common::StdResult;

use crate::services::SnapshotError;

/// Define multiple ways to append content to a tar archive.
pub(super) trait TarAppender {
    fn append<T: Write>(&self, tar: &mut tar::Builder<T>) -> StdResult<()>;
}

pub(super) struct AppenderDirAll {
    pub db_directory: PathBuf,
}

impl TarAppender for AppenderDirAll {
    fn append<T: Write>(&self, tar: &mut tar::Builder<T>) -> StdResult<()> {
        tar.append_dir_all(".", &self.db_directory)
            .map_err(SnapshotError::CreateArchiveError)
            .with_context(|| {
                format!(
                    "Can not add directory: '{}' to the archive",
                    self.db_directory.display()
                )
            })?;
        Ok(())
    }
}

pub(super) struct AppenderEntries {
    pub(super) entries: Vec<PathBuf>,
    pub(super) db_directory: PathBuf,
}

impl TarAppender for AppenderEntries {
    fn append<T: Write>(&self, tar: &mut tar::Builder<T>) -> StdResult<()> {
        for entry in &self.entries {
            let entry_path = self.db_directory.join(entry);
            if entry_path.is_dir() {
                tar.append_dir_all(entry, entry_path.clone())
                    .with_context(|| {
                        format!(
                            "Can not add directory: '{}' to the archive",
                            entry_path.display()
                        )
                    })?;
            } else if entry_path.is_file() {
                let mut file = File::open(entry_path.clone())?;
                tar.append_file(entry, &mut file).with_context(|| {
                    format!(
                        "Can not add file: '{}' to the archive",
                        entry_path.display()
                    )
                })?;
            } else {
                return Err(anyhow!(
                    "The entry: '{}' is not valid",
                    entry_path.display()
                ));
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use uuid::Uuid;

    use crate::services::snapshotter::test_tools::*;
    use crate::services::{
        CompressedArchiveSnapshotter, Snapshotter, SnapshotterCompressionAlgorithm,
    };
    use crate::test_tools::TestLogger;

    use super::*;

    #[test]
    fn snapshot_subset_should_create_archive_only_for_specified_directories_and_files() {
        let test_dir = get_test_directory("only_for_specified_directories_and_files");
        let destination = test_dir.join(create_dir(&test_dir, "destination"));
        let source = test_dir.join(create_dir(&test_dir, "source"));

        let directory_to_archive_path = create_dir(&source, "directory_to_archive");
        let file_to_archive_path = create_file(&source, "file_to_archive.txt");
        let directory_not_to_archive_path = create_dir(&source, "directory_not_to_archive");
        let file_not_to_archive_path = create_file(&source, "file_not_to_archive.txt");

        let mut snapshotter = CompressedArchiveSnapshotter::new(
            source,
            destination,
            SnapshotterCompressionAlgorithm::Gzip,
            TestLogger::stdout(),
        )
        .unwrap();
        snapshotter.set_sub_temp_dir(Uuid::new_v4().to_string());

        let snapshot = snapshotter
            .snapshot_subset(
                Path::new(&random_archive_name()),
                vec![
                    directory_to_archive_path.clone(),
                    file_to_archive_path.clone(),
                ],
            )
            .unwrap();

        let unpack_path = unpack_gz_decoder(test_dir, snapshot);

        assert!(unpack_path.join(directory_to_archive_path).is_dir());
        assert!(unpack_path.join(file_to_archive_path).is_file());
        assert!(!unpack_path.join(directory_not_to_archive_path).exists());
        assert!(!unpack_path.join(file_not_to_archive_path).exists());
    }

    #[test]
    fn snapshot_subset_return_error_when_file_or_directory_not_exist() {
        let test_dir = get_test_directory("file_or_directory_not_exist");
        let destination = test_dir.join(create_dir(&test_dir, "destination"));
        let source = test_dir.join(create_dir(&test_dir, "source"));

        let snapshotter = CompressedArchiveSnapshotter::new(
            source,
            destination,
            SnapshotterCompressionAlgorithm::Gzip,
            TestLogger::stdout(),
        )
        .unwrap();

        snapshotter
            .snapshot_subset(
                Path::new(&random_archive_name()),
                vec![PathBuf::from("not_exist")],
            )
            .expect_err("snapshot_subset should return error when file or directory not exist");
    }

    #[test]
    fn snapshot_subset_return_error_when_empty_entries() {
        let test_dir = get_test_directory("empty_entries");
        let destination = test_dir.join(create_dir(&test_dir, "destination"));
        let source = test_dir.join(create_dir(&test_dir, "source"));

        let snapshotter = CompressedArchiveSnapshotter::new(
            source,
            destination,
            SnapshotterCompressionAlgorithm::Gzip,
            TestLogger::stdout(),
        )
        .unwrap();

        snapshotter
            .snapshot_subset(Path::new(&random_archive_name()), vec![])
            .expect_err("snapshot_subset should return error when entries is empty");
    }

    #[test]
    fn snapshot_subset_with_duplicate_files_and_directories() {
        let test_dir = get_test_directory("with_duplicate_files_and_directories");
        let destination = test_dir.join(create_dir(&test_dir, "destination"));
        let source = test_dir.join(create_dir(&test_dir, "source"));

        let directory_to_archive_path = create_dir(&source, "directory_to_archive");
        let file_to_archive_path = create_file(&source, "directory_to_archive/file_to_archive.txt");

        let mut snapshotter = CompressedArchiveSnapshotter::new(
            source,
            destination,
            SnapshotterCompressionAlgorithm::Gzip,
            TestLogger::stdout(),
        )
        .unwrap();
        snapshotter.set_sub_temp_dir(Uuid::new_v4().to_string());

        let snapshot = snapshotter
            .snapshot_subset(
                Path::new(&random_archive_name()),
                vec![
                    directory_to_archive_path.clone(),
                    directory_to_archive_path.clone(),
                    file_to_archive_path.clone(),
                    file_to_archive_path.clone(),
                ],
            )
            .unwrap();

        let unpack_path = unpack_gz_decoder(test_dir, snapshot);

        assert!(unpack_path.join(directory_to_archive_path).is_dir());
        assert!(unpack_path.join(file_to_archive_path).is_file());
    }
}