@@ -478,6 +478,28 @@ func TestStorage_getGarbageFiles(t *testing.T) {
478
478
path .Join (artifactFolder , "artifact3.tar.gz" ),
479
479
},
480
480
},
481
+ {
482
+ name : "delete files based on maxItemsToBeRetained, ignore lock files" ,
483
+ artifactPaths : []string {
484
+ path .Join (artifactFolder , "artifact1.tar.gz" ),
485
+ path .Join (artifactFolder , "artifact1.tar.gz.lock" ),
486
+ path .Join (artifactFolder , "artifact2.tar.gz" ),
487
+ path .Join (artifactFolder , "artifact2.tar.gz.lock" ),
488
+ path .Join (artifactFolder , "artifact3.tar.gz" ),
489
+ path .Join (artifactFolder , "artifact3.tar.gz.lock" ),
490
+ path .Join (artifactFolder , "artifact4.tar.gz" ),
491
+ path .Join (artifactFolder , "artifact5.tar.gz" ),
492
+ },
493
+ createPause : time .Millisecond * 10 ,
494
+ ttl : time .Minute * 2 ,
495
+ totalCountLimit : 10 ,
496
+ maxItemsToBeRetained : 2 ,
497
+ wantDeleted : []string {
498
+ path .Join (artifactFolder , "artifact1.tar.gz" ),
499
+ path .Join (artifactFolder , "artifact2.tar.gz" ),
500
+ path .Join (artifactFolder , "artifact3.tar.gz" ),
501
+ },
502
+ },
481
503
{
482
504
name : "delete files based on ttl" ,
483
505
artifactPaths : []string {
@@ -496,6 +518,26 @@ func TestStorage_getGarbageFiles(t *testing.T) {
496
518
path .Join (artifactFolder , "artifact2.tar.gz" ),
497
519
},
498
520
},
521
+ {
522
+ name : "delete files based on ttl, ignore lock files" ,
523
+ artifactPaths : []string {
524
+ path .Join (artifactFolder , "artifact1.tar.gz" ),
525
+ path .Join (artifactFolder , "artifact1.tar.gz.lock" ),
526
+ path .Join (artifactFolder , "artifact2.tar.gz" ),
527
+ path .Join (artifactFolder , "artifact2.tar.gz.lock" ),
528
+ path .Join (artifactFolder , "artifact3.tar.gz" ),
529
+ path .Join (artifactFolder , "artifact4.tar.gz" ),
530
+ path .Join (artifactFolder , "artifact5.tar.gz" ),
531
+ },
532
+ createPause : time .Second * 1 ,
533
+ ttl : time .Second * 3 + time .Millisecond * 500 ,
534
+ totalCountLimit : 10 ,
535
+ maxItemsToBeRetained : 4 ,
536
+ wantDeleted : []string {
537
+ path .Join (artifactFolder , "artifact1.tar.gz" ),
538
+ path .Join (artifactFolder , "artifact2.tar.gz" ),
539
+ },
540
+ },
499
541
{
500
542
name : "delete files based on ttl and maxItemsToBeRetained" ,
501
543
artifactPaths : []string {
@@ -580,6 +622,7 @@ func TestStorage_GarbageCollect(t *testing.T) {
580
622
tests := []struct {
581
623
name string
582
624
artifactPaths []string
625
+ wantCollected []string
583
626
wantDeleted []string
584
627
wantErr string
585
628
ctxTimeout time.Duration
@@ -588,13 +631,21 @@ func TestStorage_GarbageCollect(t *testing.T) {
588
631
name : "garbage collects" ,
589
632
artifactPaths : []string {
590
633
path .Join (artifactFolder , "artifact1.tar.gz" ),
634
+ path .Join (artifactFolder , "artifact1.tar.gz.lock" ),
591
635
path .Join (artifactFolder , "artifact2.tar.gz" ),
636
+ path .Join (artifactFolder , "artifact2.tar.gz.lock" ),
592
637
path .Join (artifactFolder , "artifact3.tar.gz" ),
593
638
path .Join (artifactFolder , "artifact4.tar.gz" ),
594
639
},
640
+ wantCollected : []string {
641
+ path .Join (artifactFolder , "artifact1.tar.gz" ),
642
+ path .Join (artifactFolder , "artifact2.tar.gz" ),
643
+ },
595
644
wantDeleted : []string {
596
645
path .Join (artifactFolder , "artifact1.tar.gz" ),
646
+ path .Join (artifactFolder , "artifact1.tar.gz.lock" ),
597
647
path .Join (artifactFolder , "artifact2.tar.gz" ),
648
+ path .Join (artifactFolder , "artifact2.tar.gz.lock" ),
598
649
},
599
650
ctxTimeout : time .Second * 1 ,
600
651
},
@@ -632,29 +683,32 @@ func TestStorage_GarbageCollect(t *testing.T) {
632
683
}
633
684
}
634
685
635
- deletedPaths , err := s .GarbageCollect (context .TODO (), artifact , tt .ctxTimeout )
686
+ collectedPaths , err := s .GarbageCollect (context .TODO (), artifact , tt .ctxTimeout )
636
687
if tt .wantErr == "" {
637
688
g .Expect (err ).ToNot (HaveOccurred (), "failed to collect garbage files" )
638
689
} else {
639
690
g .Expect (err ).To (HaveOccurred ())
640
691
g .Expect (err .Error ()).To (ContainSubstring (tt .wantErr ))
641
692
}
642
- if len (tt .wantDeleted ) > 0 {
643
- g .Expect (len (tt .wantDeleted )).To (Equal (len (deletedPaths )))
644
- for _ , wantDeletedPath := range tt .wantDeleted {
693
+ if len (tt .wantCollected ) > 0 {
694
+ g .Expect (len (tt .wantCollected )).To (Equal (len (collectedPaths )))
695
+ for _ , wantCollectedPath := range tt .wantCollected {
645
696
present := false
646
- for _ , deletedPath := range deletedPaths {
647
- if strings .Contains (deletedPath , wantDeletedPath ) {
648
- g .Expect (deletedPath ).ToNot (BeAnExistingFile ())
697
+ for _ , collectedPath := range collectedPaths {
698
+ if strings .Contains (collectedPath , wantCollectedPath ) {
699
+ g .Expect (collectedPath ).ToNot (BeAnExistingFile ())
649
700
present = true
650
701
break
651
702
}
652
703
}
653
704
if present == false {
654
- g .Fail (fmt .Sprintf ("expected file to be deleted , still exists: %s" , wantDeletedPath ))
705
+ g .Fail (fmt .Sprintf ("expected file to be garbage collected , still exists: %s" , wantCollectedPath ))
655
706
}
656
707
}
657
708
}
709
+ for _ , delFile := range tt .wantDeleted {
710
+ g .Expect (filepath .Join (dir , delFile )).ToNot (BeAnExistingFile ())
711
+ }
658
712
})
659
713
}
660
714
}
0 commit comments