Fix last_n keep rule (#691) (#750)

From https://github.com/zrepl/zrepl/issues/691

The last_n prune rule keeps everything, regardless of if it matches the
regex or not, if there are less than count snapshot. The expectation
would be to never keep non-regex snapshots, regardless of number.
This commit is contained in:
Denis Shaposhnikov 2023-12-22 13:38:14 +01:00 committed by GitHub
parent 27012e5623
commit ebc46cf1c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 6 deletions

View File

@ -33,11 +33,6 @@ func NewKeepLastN(n int, regex string) (*KeepLastN, error) {
} }
func (k KeepLastN) KeepRule(snaps []Snapshot) (destroyList []Snapshot) { func (k KeepLastN) KeepRule(snaps []Snapshot) (destroyList []Snapshot) {
if k.n > len(snaps) {
return []Snapshot{}
}
matching, notMatching := partitionSnapList(snaps, func(snapshot Snapshot) bool { matching, notMatching := partitionSnapList(snaps, func(snapshot Snapshot) bool {
return k.re.MatchString(snapshot.Name()) return k.re.MatchString(snapshot.Name())
}) })

View File

@ -90,7 +90,7 @@ func TestKeepLastN(t *testing.T) {
stubSnap{"a2", false, o(12)}, stubSnap{"a2", false, o(12)},
}, },
rules: []KeepRule{ rules: []KeepRule{
MustKeepLastN(3, "a"), MustKeepLastN(4, "a"),
}, },
expDestroy: map[string]bool{ expDestroy: map[string]bool{
"b1": true, "b1": true,