From 3ea49d71012770d0d427291942df233e63e79d79 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Thu, 21 Nov 2024 18:13:58 +0200 Subject: [PATCH] fix prune when based on counting Signed-off-by: Avi Deitcher --- pkg/core/prune.go | 2 +- pkg/core/prune_test.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/core/prune.go b/pkg/core/prune.go index a67f1e8..f066a8b 100644 --- a/pkg/core/prune.go +++ b/pkg/core/prune.go @@ -122,7 +122,7 @@ func pruneTarget(ctx context.Context, logger *logrus.Entry, target storage.Stora return 0 }) slices.Reverse(filesWithTimes) - if retainCount >= len(filesWithTimes) { + if retainCount < len(filesWithTimes) { for i := 0 + retainCount; i < len(filesWithTimes); i++ { logger.Debugf("Adding candidate file %s:", filesWithTimes[i].filename) candidates = append(candidates, filesWithTimes[i].filename) diff --git a/pkg/core/prune_test.go b/pkg/core/prune_test.go index cf5efe5..dc6a633 100644 --- a/pkg/core/prune_test.go +++ b/pkg/core/prune_test.go @@ -87,6 +87,9 @@ func TestPrune(t *testing.T) { {"2 days", PruneOptions{Retention: "2d", Now: now}, filenames, filenames[0:6], nil}, // 3 weeks - file[13] is 504h+30m = 504.5h, so it should be pruned {"3 weeks", PruneOptions{Retention: "3w", Now: now}, filenames, filenames[0:13], nil}, + // 2 most recent files + {"2 most recent", PruneOptions{Retention: "2c", Now: now}, filenames, filenames[0:2], nil}, + // repeat for safe file names {"1 hour safe names", PruneOptions{Retention: "1h", Now: now}, safefilenames, safefilenames[0:1], nil}, // 2 hours - file[2] is 2h+30m = 2.5h, so it should be pruned @@ -95,6 +98,8 @@ func TestPrune(t *testing.T) { {"2 days safe names", PruneOptions{Retention: "2d", Now: now}, safefilenames, safefilenames[0:6], nil}, // 3 weeks - file[13] is 504h+30m = 504.5h, so it should be pruned {"3 weeks safe names", PruneOptions{Retention: "3w", Now: now}, safefilenames, safefilenames[0:13], nil}, + // 2 most recent files + {"2 most recent safe names", PruneOptions{Retention: "2c", Now: now}, safefilenames, safefilenames[0:2], nil}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -146,9 +151,10 @@ func TestPrune(t *testing.T) { for _, file := range files { afterFiles = append(afterFiles, file.Name()) } - slices.Sort(afterFiles) - slices.Sort(tt.afterFiles) - assert.ElementsMatch(t, tt.afterFiles, afterFiles) + afterFilesSorted, ttAfterFilesSorted := slices.Clone(afterFiles), slices.Clone(tt.afterFiles) + slices.Sort(afterFilesSorted) + slices.Sort(ttAfterFilesSorted) + assert.ElementsMatch(t, ttAfterFilesSorted, afterFilesSorted) }) } }