mithril_aggregator/services/snapshotter/
test_doubles.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
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::RwLock;

use mithril_common::StdResult;

use crate::services::{OngoingSnapshot, SnapshotError, Snapshotter};

/// Snapshotter that does nothing. It is mainly used for test purposes.
pub struct DumbSnapshotter {
    last_snapshot: RwLock<Option<OngoingSnapshot>>,
}

impl DumbSnapshotter {
    /// Create a new instance of DumbSnapshotter.
    pub fn new() -> Self {
        Self {
            last_snapshot: RwLock::new(None),
        }
    }

    /// Return the last fake snapshot produced.
    pub fn get_last_snapshot(&self) -> StdResult<Option<OngoingSnapshot>> {
        let value = self
            .last_snapshot
            .read()
            .map_err(|e| SnapshotError::UploadFileError(e.to_string()))?
            .as_ref()
            .cloned();

        Ok(value)
    }
}

impl Default for DumbSnapshotter {
    fn default() -> Self {
        Self::new()
    }
}

impl Snapshotter for DumbSnapshotter {
    fn snapshot_all(&self, archive_name: &Path) -> StdResult<OngoingSnapshot> {
        let mut value = self
            .last_snapshot
            .write()
            .map_err(|e| SnapshotError::UploadFileError(e.to_string()))?;
        let snapshot = OngoingSnapshot {
            filepath: archive_name.to_path_buf(),
            filesize: 0,
        };
        *value = Some(snapshot.clone());

        Ok(snapshot)
    }

    fn snapshot_subset(
        &self,
        archive_name: &Path,
        _files: Vec<PathBuf>,
    ) -> StdResult<OngoingSnapshot> {
        self.snapshot_all(archive_name)
    }
}

/// Snapshotter that writes empty files to the filesystem. Used for testing purposes.
pub struct FakeSnapshotter {
    work_dir: PathBuf,
}

impl FakeSnapshotter {
    /// `FakeSnapshotter` factory
    pub fn new<T: AsRef<Path>>(work_dir: T) -> Self {
        Self {
            work_dir: work_dir.as_ref().to_path_buf(),
        }
    }
}

impl Snapshotter for FakeSnapshotter {
    fn snapshot_all(&self, filepath: &Path) -> StdResult<OngoingSnapshot> {
        let fake_archive_path = self.work_dir.join(filepath);
        if let Some(archive_dir) = fake_archive_path.parent() {
            fs::create_dir_all(archive_dir).unwrap();
        }
        File::create(&fake_archive_path).unwrap();

        Ok(OngoingSnapshot {
            filepath: fake_archive_path,
            filesize: 0,
        })
    }

    fn snapshot_subset(&self, filepath: &Path, _files: Vec<PathBuf>) -> StdResult<OngoingSnapshot> {
        self.snapshot_all(filepath)
    }
}

#[cfg(test)]
mod tests {
    use crate::services::snapshotter::test_tools::*;

    use super::*;

    mod dumb_snapshotter {
        use super::*;

        #[test]
        fn test_dumb_snapshotter_snapshot_return_archive_name_with_size_0() {
            let snapshotter = DumbSnapshotter::new();
            let snapshot = snapshotter
                .snapshot_all(Path::new("archive.tar.gz"))
                .unwrap();

            assert_eq!(PathBuf::from("archive.tar.gz"), *snapshot.get_file_path());
            assert_eq!(0, *snapshot.get_file_size());

            let snapshot = snapshotter
                .snapshot_subset(Path::new("archive.tar.gz"), vec![PathBuf::from("whatever")])
                .unwrap();
            assert_eq!(PathBuf::from("archive.tar.gz"), *snapshot.get_file_path());
            assert_eq!(0, *snapshot.get_file_size());
        }

        #[test]
        fn test_dumb_snapshotter() {
            let snapshotter = DumbSnapshotter::new();
            assert!(snapshotter
                .get_last_snapshot()
                .expect(
                    "Dumb snapshotter::get_last_snapshot should not fail when no last snapshot."
                )
                .is_none());

            let snapshot = snapshotter
                .snapshot_all(Path::new("whatever"))
                .expect("Dumb snapshotter::snapshot should not fail.");
            assert_eq!(
                Some(snapshot),
                snapshotter.get_last_snapshot().expect(
                    "Dumb snapshotter::get_last_snapshot should not fail when some last snapshot."
                )
            );

            let snapshot = snapshotter
                .snapshot_subset(Path::new("another_whatever"), vec![PathBuf::from("subdir")])
                .expect("Dumb snapshotter::snapshot should not fail.");
            assert_eq!(
                Some(snapshot),
                snapshotter.get_last_snapshot().expect(
                    "Dumb snapshotter::get_last_snapshot should not fail when some last snapshot."
                )
            );
        }
    }

    mod fake_snapshotter {
        use super::*;
        #[test]
        fn snapshot_all_create_empty_file_located_at_work_dir_joined_filepath() {
            let test_dir = get_test_directory("fake_snapshotter_snapshot_all_create_empty_file");
            let fake_snapshotter = FakeSnapshotter::new(&test_dir);

            for path in [
                Path::new("direct_child.tar.gz"),
                Path::new("one_level_subdir/child.tar.gz"),
                Path::new("two_levels/subdir/child.tar.gz"),
            ] {
                let snapshot = fake_snapshotter.snapshot_all(path).unwrap();

                assert!(test_dir.join(path).is_file());
                assert_eq!(snapshot.get_file_path(), &test_dir.join(path));
            }
        }

        #[test]
        fn snapshot_subset_create_empty_file_located_at_work_dir_joined_filepath() {
            let test_dir = get_test_directory("fake_snapshotter_snapshot_subset_create_empty_file");
            let fake_snapshotter = FakeSnapshotter::new(&test_dir);

            for path in [
                Path::new("direct_child.tar.gz"),
                Path::new("one_level_subdir/child.tar.gz"),
                Path::new("two_levels/subdir/child.tar.gz"),
            ] {
                let snapshot = fake_snapshotter.snapshot_subset(path, vec![]).unwrap();

                assert!(test_dir.join(path).is_file());
                assert_eq!(snapshot.get_file_path(), &test_dir.join(path));
            }
        }
    }
}