Skip to content

Commit e65c85c

Browse files
Alenardlachaume
andcommitted
test(aggregator): streamline tooling for snapshotter & file archiver
Co-authored-by: Damien Lachaume <[email protected]>
1 parent f8dc2f2 commit e65c85c

File tree

6 files changed

+33
-52
lines changed

6 files changed

+33
-52
lines changed

mithril-aggregator/src/services/snapshotter/compressed_archive_snapshotter.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,10 @@ mod tests {
273273
let test_dir =
274274
get_test_directory("should_create_snapshots_in_its_ongoing_snapshot_directory");
275275
let pending_snapshot_directory = test_dir.join("pending_snapshot");
276-
let db_directory = test_dir.join(create_dir(&test_dir, "db"));
276+
let db_directory = test_dir.join("db");
277277

278-
create_file(&db_directory, "file_to_archive.txt");
278+
fs::create_dir(&db_directory).unwrap();
279+
File::create(&db_directory.join("file_to_archive.txt")).unwrap();
279280

280281
let snapshotter = CompressedArchiveSnapshotter::new(
281282
db_directory,
@@ -313,7 +314,7 @@ mod tests {
313314
.snapshot_immutable_trio(2, "immutable-2")
314315
.unwrap();
315316

316-
let unpack_dir = unpack_gz_decoder(test_dir, snapshot);
317+
let unpack_dir = snapshot.unpack_gzip(&test_dir);
317318
let unpacked_files = list_files(&unpack_dir);
318319
let unpacked_immutable_files = list_files(&unpack_dir.join(IMMUTABLE_DIR));
319320

@@ -354,7 +355,7 @@ mod tests {
354355

355356
let snapshot = snapshotter.snapshot_ancillary(2, "ancillary").unwrap();
356357

357-
let unpack_dir = unpack_gz_decoder(test_dir, snapshot);
358+
let unpack_dir = snapshot.unpack_gzip(&test_dir);
358359

359360
let expected_immutable_path = unpack_dir.join(IMMUTABLE_DIR);
360361
assert!(expected_immutable_path.join("00003.chunk").exists());

mithril-aggregator/src/services/snapshotter/mod.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,11 @@ pub use test_doubles::*;
88

99
#[cfg(test)]
1010
pub(crate) mod test_tools {
11-
use flate2::read::GzDecoder;
12-
use std::fs::File;
13-
use std::path::{Path, PathBuf};
14-
use tar::Archive;
11+
use std::path::PathBuf;
1512

1613
use mithril_common::test_utils::TempDir;
1714

18-
use crate::tools::file_archiver::FileArchive;
19-
2015
pub fn get_test_directory(dir_name: &str) -> PathBuf {
2116
TempDir::create("snapshotter", dir_name)
2217
}
23-
24-
pub fn create_file(root: &Path, filename: &str) -> PathBuf {
25-
let file_path = PathBuf::from(filename);
26-
File::create(root.join(file_path.clone())).unwrap();
27-
file_path
28-
}
29-
30-
pub fn create_dir(root: &Path, dirname: &str) -> PathBuf {
31-
let dir_path = PathBuf::from(dirname);
32-
std::fs::create_dir(root.join(dir_path.clone())).unwrap();
33-
dir_path
34-
}
35-
36-
pub fn unpack_gz_decoder(test_dir: PathBuf, snapshot: FileArchive) -> PathBuf {
37-
let file_tar_gz = File::open(snapshot.get_file_path()).unwrap();
38-
let file_tar_gz_decoder = GzDecoder::new(file_tar_gz);
39-
let mut archive = Archive::new(file_tar_gz_decoder);
40-
let unpack_path = test_dir.join(create_dir(&test_dir, "unpack"));
41-
archive.unpack(&unpack_path).unwrap();
42-
43-
unpack_path
44-
}
4518
}

mithril-aggregator/src/tools/file_archiver/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ mod tests {
454454

455455
assert_ne!(first_snapshot_size, second_snapshot_size);
456456

457-
let unpack_path = unpack_gz_decoder(test_dir, second_snapshot);
457+
let unpack_path = second_snapshot.unpack_gzip(&test_dir);
458458
assert!(unpack_path.join("another_file_to_archive.txt").exists());
459459
}
460460

mithril-aggregator/src/tools/file_archiver/appender.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ mod tests {
197197
)
198198
.unwrap();
199199

200-
let unpack_path = unpack_gz_decoder(&test_dir, snapshot);
200+
let unpack_path = snapshot.unpack_gzip(&test_dir);
201201

202202
assert!(unpack_path.join(directory_to_archive_path).is_dir());
203203
assert!(unpack_path.join(file_to_archive_path).is_file());
@@ -262,7 +262,7 @@ mod tests {
262262
)
263263
.unwrap();
264264

265-
let unpack_path = unpack_gz_decoder(&test_dir, snapshot);
265+
let unpack_path = snapshot.unpack_gzip(&test_dir);
266266

267267
assert!(unpack_path.join(directory_to_archive_path).is_dir());
268268
assert!(unpack_path.join(file_to_archive_path).is_file());
@@ -285,7 +285,7 @@ mod tests {
285285
)
286286
.unwrap();
287287

288-
let unpack_path = unpack_gz_decoder(&test_dir, archive);
288+
let unpack_path = archive.unpack_gzip(&test_dir);
289289

290290
assert!(unpack_path.join(file_to_archive).exists());
291291
}

mithril-aggregator/src/tools/file_archiver/entities.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ impl FileArchive {
7979
pub fn get_compression_algorithm(&self) -> CompressionAlgorithm {
8080
self.compression_algorithm
8181
}
82+
83+
/// Unpack the archive to a directory.
84+
///
85+
/// An 'unpack' directory will be created in the given parent directory.
86+
#[cfg(test)]
87+
pub fn unpack_gzip<P: AsRef<Path>>(&self, parent_dir: P) -> PathBuf {
88+
use super::test_tools::create_dir;
89+
use flate2::read::GzDecoder;
90+
use std::fs::File;
91+
use tar::Archive;
92+
if self.compression_algorithm != CompressionAlgorithm::Gzip {
93+
panic!("Only Gzip compression is supported");
94+
}
95+
96+
let parent_dir = parent_dir.as_ref();
97+
let file_tar_gz = File::open(self.get_file_path()).unwrap();
98+
let file_tar_gz_decoder = GzDecoder::new(file_tar_gz);
99+
let mut archive = Archive::new(file_tar_gz_decoder);
100+
let unpack_path = parent_dir.join(create_dir(parent_dir, "unpack"));
101+
archive.unpack(&unpack_path).unwrap();
102+
103+
unpack_path
104+
}
82105
}
83106

84107
#[cfg(test)]

mithril-aggregator/src/tools/file_archiver/mod.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ mod entities;
55
pub use api::*;
66
pub use entities::*;
77

8-
// Todo: unduplicate this code
98
#[cfg(test)]
109
pub(crate) mod test_tools {
11-
use flate2::read::GzDecoder;
1210
use std::fs::File;
1311
use std::path::{Path, PathBuf};
14-
use tar::Archive;
1512

1613
use mithril_common::test_utils::TempDir;
1714

18-
use super::*;
19-
2015
pub fn get_test_directory(dir_name: &str) -> PathBuf {
2116
TempDir::create("file_archiver", dir_name)
2217
}
@@ -38,15 +33,4 @@ pub(crate) mod test_tools {
3833
std::fs::create_dir(root.join(dir_path.clone())).unwrap();
3934
dir_path
4035
}
41-
42-
pub fn unpack_gz_decoder<P: AsRef<Path>>(test_dir: P, file_archive: FileArchive) -> PathBuf {
43-
let test_dir = test_dir.as_ref();
44-
let file_tar_gz = File::open(file_archive.get_file_path()).unwrap();
45-
let file_tar_gz_decoder = GzDecoder::new(file_tar_gz);
46-
let mut archive = Archive::new(file_tar_gz_decoder);
47-
let unpack_path = test_dir.join(create_dir(test_dir, "unpack"));
48-
archive.unpack(&unpack_path).unwrap();
49-
50-
unpack_path
51-
}
5236
}

0 commit comments

Comments
 (0)