Skip to content

Commit 20c20bf

Browse files
committed
refacto: test simplification
1 parent ba98002 commit 20c20bf

File tree

1 file changed

+81
-62
lines changed
  • mithril-aggregator/src/artifact_builder/cardano_database_artifacts

1 file changed

+81
-62
lines changed

mithril-aggregator/src/artifact_builder/cardano_database_artifacts/digest.rs

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ mod tests {
293293
};
294294

295295
use crate::{
296+
file_uploaders::FileUploadRetryPolicy,
296297
immutable_file_digest_mapper::MockImmutableFileDigestMapper,
297298
services::{
298299
CompressedArchiveSnapshotter, DumbSnapshotter, SnapshotterCompressionAlgorithm,
@@ -307,7 +308,6 @@ mod tests {
307308
messages::{CardanoDatabaseDigestListItemMessage, CardanoDatabaseDigestListMessage},
308309
test_utils::{assert_equivalent, TempDir},
309310
};
310-
use mockall::predicate::eq;
311311
use tar::Archive;
312312
use uuid::Uuid;
313313

@@ -338,6 +338,42 @@ mod tests {
338338
uploader
339339
}
340340

341+
fn path_content(path: &Path) -> Vec<PathBuf> {
342+
std::fs::read_dir(path)
343+
.unwrap()
344+
.map(|res| res.unwrap().path())
345+
.collect()
346+
}
347+
348+
fn build_local_uploader(path: &Path) -> LocalUploader {
349+
std::fs::create_dir_all(path).unwrap();
350+
LocalUploader::new(
351+
SanitizedUrlWithTrailingSlash::parse("http://server/").unwrap(),
352+
path,
353+
FileUploadRetryPolicy::never(),
354+
TestLogger::stdout(),
355+
)
356+
}
357+
358+
fn build_dummy_immutable_file_digest_mapper() -> MockImmutableFileDigestMapper {
359+
let mut immutable_file_digest_mapper = MockImmutableFileDigestMapper::new();
360+
immutable_file_digest_mapper
361+
.expect_get_immutable_file_digest_map()
362+
.returning(|| Ok(BTreeMap::new()));
363+
immutable_file_digest_mapper
364+
}
365+
366+
fn unpack_archive(archive_path: &Path, unpack_dir: &Path) -> StdResult<()> {
367+
let mut archive = {
368+
let file_tar_gz = File::open(archive_path)?;
369+
let file_tar_gz_decoder = GzDecoder::new(file_tar_gz);
370+
Archive::new(file_tar_gz_decoder)
371+
};
372+
373+
archive.unpack(unpack_dir)?;
374+
Ok(())
375+
}
376+
341377
#[tokio::test]
342378
async fn digest_artifact_builder_return_digests_route_on_aggregator() {
343379
let temp_dir = TempDir::create("digest", current_function!());
@@ -597,56 +633,19 @@ mod tests {
597633
}
598634

599635
#[tokio::test]
600-
async fn upload_should_call_upload_with_created_digest_archive_file_and_delete_the_file() {
636+
async fn upload_should_upload_a_digest_archive_file_and_delete_created_files() {
601637
let tmp_dir = TempDir::create("digest", current_function!());
602638
let digests_dir = tmp_dir.join("digest");
603639
let digests_archive_dir = tmp_dir.join("archive");
604-
let mut immutable_file_digest_mapper = MockImmutableFileDigestMapper::new();
605-
immutable_file_digest_mapper
606-
.expect_get_immutable_file_digest_map()
607-
.returning(|| Ok(BTreeMap::new()));
608-
609-
let mut digest_file_uploader = MockDigestFileUploader::new();
640+
let uploader_path = tmp_dir.join("uploaded_digests");
610641

611642
let compression_algorithm = CompressionAlgorithm::Gzip;
612-
613643
let beacon = CardanoDbBeacon::new(3, 456);
614644
let network = CardanoNetwork::DevNet(24);
615-
let digest_file_name =
616-
DigestArtifactBuilder::get_digests_file_name(&network, &beacon, "json");
617-
let archive_path = DigestArtifactBuilder::get_digests_file_path(
618-
&digests_archive_dir,
619-
&network,
620-
&beacon,
621-
&compression_algorithm.tar_file_extension(),
622-
);
623-
624-
digest_file_uploader
625-
.expect_upload()
626-
.with(eq(archive_path.clone()), eq(Some(compression_algorithm)))
627-
.times(1)
628-
.return_once(move |_, _| {
629-
assert!(
630-
archive_path.exists(),
631-
"Path to upload should exist: {}",
632-
archive_path.display()
633-
);
634-
635-
let unpack_dir = tmp_dir.join("unpack");
636-
unpack_archive(&archive_path, &unpack_dir);
637-
638-
let unpack_digest_file = unpack_dir.join(digest_file_name);
639-
assert!(unpack_digest_file.is_file());
640-
641-
Ok(DigestLocation::CloudStorage {
642-
uri: "an_uri".to_string(),
643-
compression_algorithm: None,
644-
})
645-
});
646645

647646
let mut snapshotter = CompressedArchiveSnapshotter::new(
648647
digests_dir.clone(),
649-
digests_archive_dir,
648+
digests_archive_dir.clone(),
650649
SnapshotterCompressionAlgorithm::Gzip,
651650
TestLogger::stdout(),
652651
)
@@ -655,39 +654,59 @@ mod tests {
655654

656655
let builder = DigestArtifactBuilder::new(
657656
SanitizedUrlWithTrailingSlash::parse("https://aggregator/").unwrap(),
658-
vec![Arc::new(digest_file_uploader)],
657+
vec![Arc::new(build_local_uploader(&uploader_path))],
659658
DigestSnapshotter {
660659
snapshotter: Arc::new(snapshotter),
661-
compression_algorithm: CompressionAlgorithm::Gzip,
660+
compression_algorithm,
662661
},
663662
network,
664663
digests_dir.clone(),
665-
Arc::new(immutable_file_digest_mapper),
664+
Arc::new(build_dummy_immutable_file_digest_mapper()),
666665
TestLogger::stdout(),
667666
)
668667
.unwrap();
669668

670669
let _locations = builder.upload(&beacon).await.unwrap();
671670

672-
let remaining_files: Vec<_> = std::fs::read_dir(&digests_dir)
673-
.unwrap()
674-
.map(|res| res.unwrap().path())
675-
.collect();
676-
assert!(
677-
remaining_files.is_empty(),
678-
"There should be no remaining files, but found: {:?}",
679-
remaining_files
680-
);
681-
}
682-
683-
fn unpack_archive(archive_path: &Path, unpack_dir: &Path) {
684-
let mut archive = {
685-
let file_tar_gz = File::open(archive_path).unwrap();
686-
let file_tar_gz_decoder = GzDecoder::new(file_tar_gz);
687-
Archive::new(file_tar_gz_decoder)
688-
};
671+
// Check uploader archive contains digests.json
672+
{
673+
let digest_archive_path =
674+
uploader_path.join(DigestArtifactBuilder::get_digests_file_name(
675+
&network,
676+
&beacon,
677+
&compression_algorithm.tar_file_extension(),
678+
));
679+
assert!(
680+
digest_archive_path.exists(),
681+
"Archive should have been uploaded to {}",
682+
digest_archive_path.display()
683+
);
684+
685+
let unpack_dir = tmp_dir.join("unpack");
686+
unpack_archive(&digest_archive_path, &unpack_dir).unwrap();
687+
688+
let digest_file_path = unpack_dir.join(DigestArtifactBuilder::get_digests_file_name(
689+
&network, &beacon, "json",
690+
));
691+
assert!(digest_file_path.is_file());
692+
}
689693

690-
archive.unpack(unpack_dir).unwrap();
694+
// Check that all files have been deleted
695+
{
696+
let remaining_files = path_content(&digests_dir);
697+
assert!(
698+
remaining_files.is_empty(),
699+
"There should be no remaining files in digests folder, but found: {:?}",
700+
remaining_files
701+
);
702+
703+
let remaining_files = path_content(&digests_archive_dir);
704+
assert!(
705+
remaining_files.is_empty(),
706+
"There should be no remaining files in archive folder, but found: {:?}",
707+
remaining_files
708+
);
709+
}
691710
}
692711

693712
#[tokio::test]

0 commit comments

Comments
 (0)