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 <darkowlzz@protonmail.com>
This commit is contained in:
parent
5ccf2fd6d3
commit
6134dd97d5
|
|
@ -478,6 +478,28 @@ func TestStorage_getGarbageFiles(t *testing.T) {
|
||||||
path.Join(artifactFolder, "artifact3.tar.gz"),
|
path.Join(artifactFolder, "artifact3.tar.gz"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "delete files based on maxItemsToBeRetained, ignore lock files",
|
||||||
|
artifactPaths: []string{
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz.lock"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz.lock"),
|
||||||
|
path.Join(artifactFolder, "artifact3.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact3.tar.gz.lock"),
|
||||||
|
path.Join(artifactFolder, "artifact4.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact5.tar.gz"),
|
||||||
|
},
|
||||||
|
createPause: time.Millisecond * 10,
|
||||||
|
ttl: time.Minute * 2,
|
||||||
|
totalCountLimit: 10,
|
||||||
|
maxItemsToBeRetained: 2,
|
||||||
|
wantDeleted: []string{
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact3.tar.gz"),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "delete files based on ttl",
|
name: "delete files based on ttl",
|
||||||
artifactPaths: []string{
|
artifactPaths: []string{
|
||||||
|
|
@ -496,6 +518,26 @@ func TestStorage_getGarbageFiles(t *testing.T) {
|
||||||
path.Join(artifactFolder, "artifact2.tar.gz"),
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "delete files based on ttl, ignore lock files",
|
||||||
|
artifactPaths: []string{
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz.lock"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz.lock"),
|
||||||
|
path.Join(artifactFolder, "artifact3.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact4.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact5.tar.gz"),
|
||||||
|
},
|
||||||
|
createPause: time.Second * 1,
|
||||||
|
ttl: time.Second*3 + time.Millisecond*500,
|
||||||
|
totalCountLimit: 10,
|
||||||
|
maxItemsToBeRetained: 4,
|
||||||
|
wantDeleted: []string{
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "delete files based on ttl and maxItemsToBeRetained",
|
name: "delete files based on ttl and maxItemsToBeRetained",
|
||||||
artifactPaths: []string{
|
artifactPaths: []string{
|
||||||
|
|
@ -580,6 +622,7 @@ func TestStorage_GarbageCollect(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
artifactPaths []string
|
artifactPaths []string
|
||||||
|
wantCollected []string
|
||||||
wantDeleted []string
|
wantDeleted []string
|
||||||
wantErr string
|
wantErr string
|
||||||
ctxTimeout time.Duration
|
ctxTimeout time.Duration
|
||||||
|
|
@ -588,14 +631,22 @@ func TestStorage_GarbageCollect(t *testing.T) {
|
||||||
name: "garbage collects",
|
name: "garbage collects",
|
||||||
artifactPaths: []string{
|
artifactPaths: []string{
|
||||||
path.Join(artifactFolder, "artifact1.tar.gz"),
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz.lock"),
|
||||||
path.Join(artifactFolder, "artifact2.tar.gz"),
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz.lock"),
|
||||||
path.Join(artifactFolder, "artifact3.tar.gz"),
|
path.Join(artifactFolder, "artifact3.tar.gz"),
|
||||||
path.Join(artifactFolder, "artifact4.tar.gz"),
|
path.Join(artifactFolder, "artifact4.tar.gz"),
|
||||||
},
|
},
|
||||||
wantDeleted: []string{
|
wantCollected: []string{
|
||||||
path.Join(artifactFolder, "artifact1.tar.gz"),
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
path.Join(artifactFolder, "artifact2.tar.gz"),
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
},
|
},
|
||||||
|
wantDeleted: []string{
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact1.tar.gz.lock"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz"),
|
||||||
|
path.Join(artifactFolder, "artifact2.tar.gz.lock"),
|
||||||
|
},
|
||||||
ctxTimeout: time.Second * 1,
|
ctxTimeout: time.Second * 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -632,29 +683,32 @@ func TestStorage_GarbageCollect(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
deletedPaths, err := s.GarbageCollect(context.TODO(), artifact, tt.ctxTimeout)
|
collectedPaths, err := s.GarbageCollect(context.TODO(), artifact, tt.ctxTimeout)
|
||||||
if tt.wantErr == "" {
|
if tt.wantErr == "" {
|
||||||
g.Expect(err).ToNot(HaveOccurred(), "failed to collect garbage files")
|
g.Expect(err).ToNot(HaveOccurred(), "failed to collect garbage files")
|
||||||
} else {
|
} else {
|
||||||
g.Expect(err).To(HaveOccurred())
|
g.Expect(err).To(HaveOccurred())
|
||||||
g.Expect(err.Error()).To(ContainSubstring(tt.wantErr))
|
g.Expect(err.Error()).To(ContainSubstring(tt.wantErr))
|
||||||
}
|
}
|
||||||
if len(tt.wantDeleted) > 0 {
|
if len(tt.wantCollected) > 0 {
|
||||||
g.Expect(len(tt.wantDeleted)).To(Equal(len(deletedPaths)))
|
g.Expect(len(tt.wantCollected)).To(Equal(len(collectedPaths)))
|
||||||
for _, wantDeletedPath := range tt.wantDeleted {
|
for _, wantCollectedPath := range tt.wantCollected {
|
||||||
present := false
|
present := false
|
||||||
for _, deletedPath := range deletedPaths {
|
for _, collectedPath := range collectedPaths {
|
||||||
if strings.Contains(deletedPath, wantDeletedPath) {
|
if strings.Contains(collectedPath, wantCollectedPath) {
|
||||||
g.Expect(deletedPath).ToNot(BeAnExistingFile())
|
g.Expect(collectedPath).ToNot(BeAnExistingFile())
|
||||||
present = true
|
present = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if present == false {
|
if present == false {
|
||||||
g.Fail(fmt.Sprintf("expected file to be deleted, still exists: %s", wantDeletedPath))
|
g.Fail(fmt.Sprintf("expected file to be garbage collected, still exists: %s", wantCollectedPath))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, delFile := range tt.wantDeleted {
|
||||||
|
g.Expect(filepath.Join(dir, delFile)).ToNot(BeAnExistingFile())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue