@@ -293,6 +293,7 @@ mod tests {
293
293
} ;
294
294
295
295
use crate :: {
296
+ file_uploaders:: FileUploadRetryPolicy ,
296
297
immutable_file_digest_mapper:: MockImmutableFileDigestMapper ,
297
298
services:: {
298
299
CompressedArchiveSnapshotter , DumbSnapshotter , SnapshotterCompressionAlgorithm ,
@@ -307,7 +308,6 @@ mod tests {
307
308
messages:: { CardanoDatabaseDigestListItemMessage , CardanoDatabaseDigestListMessage } ,
308
309
test_utils:: { assert_equivalent, TempDir } ,
309
310
} ;
310
- use mockall:: predicate:: eq;
311
311
use tar:: Archive ;
312
312
use uuid:: Uuid ;
313
313
@@ -338,6 +338,42 @@ mod tests {
338
338
uploader
339
339
}
340
340
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
+
341
377
#[ tokio:: test]
342
378
async fn digest_artifact_builder_return_digests_route_on_aggregator ( ) {
343
379
let temp_dir = TempDir :: create ( "digest" , current_function ! ( ) ) ;
@@ -597,56 +633,19 @@ mod tests {
597
633
}
598
634
599
635
#[ 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 ( ) {
601
637
let tmp_dir = TempDir :: create ( "digest" , current_function ! ( ) ) ;
602
638
let digests_dir = tmp_dir. join ( "digest" ) ;
603
639
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" ) ;
610
641
611
642
let compression_algorithm = CompressionAlgorithm :: Gzip ;
612
-
613
643
let beacon = CardanoDbBeacon :: new ( 3 , 456 ) ;
614
644
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
- } ) ;
646
645
647
646
let mut snapshotter = CompressedArchiveSnapshotter :: new (
648
647
digests_dir. clone ( ) ,
649
- digests_archive_dir,
648
+ digests_archive_dir. clone ( ) ,
650
649
SnapshotterCompressionAlgorithm :: Gzip ,
651
650
TestLogger :: stdout ( ) ,
652
651
)
@@ -655,39 +654,59 @@ mod tests {
655
654
656
655
let builder = DigestArtifactBuilder :: new (
657
656
SanitizedUrlWithTrailingSlash :: parse ( "https://aggregator/" ) . unwrap ( ) ,
658
- vec ! [ Arc :: new( digest_file_uploader ) ] ,
657
+ vec ! [ Arc :: new( build_local_uploader ( & uploader_path ) ) ] ,
659
658
DigestSnapshotter {
660
659
snapshotter : Arc :: new ( snapshotter) ,
661
- compression_algorithm : CompressionAlgorithm :: Gzip ,
660
+ compression_algorithm,
662
661
} ,
663
662
network,
664
663
digests_dir. clone ( ) ,
665
- Arc :: new ( immutable_file_digest_mapper ) ,
664
+ Arc :: new ( build_dummy_immutable_file_digest_mapper ( ) ) ,
666
665
TestLogger :: stdout ( ) ,
667
666
)
668
667
. unwrap ( ) ;
669
668
670
669
let _locations = builder. upload ( & beacon) . await . unwrap ( ) ;
671
670
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
+ }
689
693
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
+ }
691
710
}
692
711
693
712
#[ tokio:: test]
0 commit comments