pruning/keep_last_n: correctly handle the case where count > matching snaps

fixes #446
This commit is contained in:
Christian Schwarz 2021-03-25 22:36:01 +01:00
parent ac4b109872
commit f661d9429f
2 changed files with 19 additions and 1 deletions

View File

@ -57,6 +57,11 @@ func (k KeepLastN) KeepRule(snaps []Snapshot) (destroyList []Snapshot) {
// then lexicographically descending (e.g. b, a)
return strings.Compare(matching[i].Name(), matching[j].Name()) == 1
})
destroyList = append(destroyList, matching[k.n:]...)
n := k.n
if n > len(matching) {
n = len(matching)
}
destroyList = append(destroyList, matching[n:]...)
return destroyList
}

View File

@ -83,6 +83,19 @@ func TestKeepLastN(t *testing.T) {
"b1": true,
},
},
"keep_more_than_matching": {
inputs: []Snapshot{
stubSnap{"a1", false, o(10)},
stubSnap{"b1", false, o(11)},
stubSnap{"a2", false, o(12)},
},
rules: []KeepRule{
MustKeepLastN(3, "a"),
},
expDestroy: map[string]bool{
"b1": true,
},
},
}
testTable(tcs, t)