Skip to content

Commit 6134dd9

Browse files
committed
test: Add tests for GC ignoring lock files
Add storage tests to ensure garbage collection ignores lock files for GC count and deletes them eventually. Signed-off-by: Sunny <[email protected]>
1 parent 5ccf2fd commit 6134dd9

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

controllers/storage_test.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,28 @@ func TestStorage_getGarbageFiles(t *testing.T) {
478478
path.Join(artifactFolder, "artifact3.tar.gz"),
479479
},
480480
},
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+
},
481503
{
482504
name: "delete files based on ttl",
483505
artifactPaths: []string{
@@ -496,6 +518,26 @@ func TestStorage_getGarbageFiles(t *testing.T) {
496518
path.Join(artifactFolder, "artifact2.tar.gz"),
497519
},
498520
},
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+
},
499541
{
500542
name: "delete files based on ttl and maxItemsToBeRetained",
501543
artifactPaths: []string{
@@ -580,6 +622,7 @@ func TestStorage_GarbageCollect(t *testing.T) {
580622
tests := []struct {
581623
name string
582624
artifactPaths []string
625+
wantCollected []string
583626
wantDeleted []string
584627
wantErr string
585628
ctxTimeout time.Duration
@@ -588,13 +631,21 @@ func TestStorage_GarbageCollect(t *testing.T) {
588631
name: "garbage collects",
589632
artifactPaths: []string{
590633
path.Join(artifactFolder, "artifact1.tar.gz"),
634+
path.Join(artifactFolder, "artifact1.tar.gz.lock"),
591635
path.Join(artifactFolder, "artifact2.tar.gz"),
636+
path.Join(artifactFolder, "artifact2.tar.gz.lock"),
592637
path.Join(artifactFolder, "artifact3.tar.gz"),
593638
path.Join(artifactFolder, "artifact4.tar.gz"),
594639
},
640+
wantCollected: []string{
641+
path.Join(artifactFolder, "artifact1.tar.gz"),
642+
path.Join(artifactFolder, "artifact2.tar.gz"),
643+
},
595644
wantDeleted: []string{
596645
path.Join(artifactFolder, "artifact1.tar.gz"),
646+
path.Join(artifactFolder, "artifact1.tar.gz.lock"),
597647
path.Join(artifactFolder, "artifact2.tar.gz"),
648+
path.Join(artifactFolder, "artifact2.tar.gz.lock"),
598649
},
599650
ctxTimeout: time.Second * 1,
600651
},
@@ -632,29 +683,32 @@ func TestStorage_GarbageCollect(t *testing.T) {
632683
}
633684
}
634685

635-
deletedPaths, err := s.GarbageCollect(context.TODO(), artifact, tt.ctxTimeout)
686+
collectedPaths, err := s.GarbageCollect(context.TODO(), artifact, tt.ctxTimeout)
636687
if tt.wantErr == "" {
637688
g.Expect(err).ToNot(HaveOccurred(), "failed to collect garbage files")
638689
} else {
639690
g.Expect(err).To(HaveOccurred())
640691
g.Expect(err.Error()).To(ContainSubstring(tt.wantErr))
641692
}
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 {
645696
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())
649700
present = true
650701
break
651702
}
652703
}
653704
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))
655706
}
656707
}
657708
}
709+
for _, delFile := range tt.wantDeleted {
710+
g.Expect(filepath.Join(dir, delFile)).ToNot(BeAnExistingFile())
711+
}
658712
})
659713
}
660714
}

0 commit comments

Comments
 (0)