2018-08-27 16:24:19 +02:00
|
|
|
package pruning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-03-22 19:41:12 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-08-31 14:05:32 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-08-27 16:24:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestKeepLastN(t *testing.T) {
|
|
|
|
|
|
|
|
o := func(minutes int) time.Time {
|
|
|
|
return time.Unix(123, 0).Add(time.Duration(minutes) * time.Minute)
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs := map[string][]Snapshot{
|
|
|
|
"s1": []Snapshot{
|
|
|
|
stubSnap{name: "1", date: o(10)},
|
|
|
|
stubSnap{name: "2", date: o(20)},
|
|
|
|
stubSnap{name: "3", date: o(15)},
|
|
|
|
stubSnap{name: "4", date: o(30)},
|
|
|
|
stubSnap{name: "5", date: o(30)},
|
|
|
|
},
|
|
|
|
"s2": []Snapshot{},
|
|
|
|
}
|
|
|
|
|
|
|
|
tcs := map[string]testCase{
|
|
|
|
"keep2": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
2020-08-31 14:05:32 +02:00
|
|
|
MustKeepLastN(2, ""),
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: map[string]bool{
|
|
|
|
"1": true, "2": true, "3": true,
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
"keep1OfTwoWithSameTime": { // Keep one of two with same time
|
2018-08-27 16:24:19 +02:00
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
2020-08-31 14:05:32 +02:00
|
|
|
MustKeepLastN(1, ""),
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
2020-08-29 17:26:31 +02:00
|
|
|
expDestroy: map[string]bool{"1": true, "2": true, "3": true, "4": true},
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
|
|
|
"keepMany": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
2020-08-31 14:05:32 +02:00
|
|
|
MustKeepLastN(100, ""),
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: map[string]bool{},
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
2020-08-31 14:05:32 +02:00
|
|
|
"empty_input": {
|
2018-08-27 16:24:19 +02:00
|
|
|
inputs: inputs["s2"],
|
|
|
|
rules: []KeepRule{
|
2020-08-31 14:05:32 +02:00
|
|
|
MustKeepLastN(100, ""),
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: map[string]bool{},
|
2018-08-27 16:24:19 +02:00
|
|
|
},
|
2020-08-31 14:05:32 +02:00
|
|
|
"empty_regex": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
|
|
|
MustKeepLastN(4, ""),
|
|
|
|
},
|
|
|
|
expDestroy: map[string]bool{
|
|
|
|
"1": true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"multiple_regexes": {
|
|
|
|
inputs: []Snapshot{
|
|
|
|
stubSnap{"a1", false, o(10)},
|
|
|
|
stubSnap{"b1", false, o(11)},
|
|
|
|
stubSnap{"a2", false, o(20)},
|
|
|
|
stubSnap{"b2", false, o(21)},
|
|
|
|
stubSnap{"a3", false, o(30)},
|
|
|
|
stubSnap{"b3", false, o(31)},
|
|
|
|
},
|
|
|
|
rules: []KeepRule{
|
|
|
|
MustKeepLastN(2, "^a"),
|
|
|
|
MustKeepLastN(2, "^b"),
|
|
|
|
},
|
|
|
|
expDestroy: map[string]bool{
|
|
|
|
"a1": true,
|
|
|
|
"b1": true,
|
|
|
|
},
|
|
|
|
},
|
2021-03-25 22:36:01 +01:00
|
|
|
"keep_more_than_matching": {
|
|
|
|
inputs: []Snapshot{
|
|
|
|
stubSnap{"a1", false, o(10)},
|
|
|
|
stubSnap{"b1", false, o(11)},
|
|
|
|
stubSnap{"a2", false, o(12)},
|
|
|
|
},
|
|
|
|
rules: []KeepRule{
|
2023-12-22 13:38:14 +01:00
|
|
|
MustKeepLastN(4, "a"),
|
2021-03-25 22:36:01 +01:00
|
|
|
},
|
|
|
|
expDestroy: map[string]bool{
|
|
|
|
"b1": true,
|
|
|
|
},
|
|
|
|
},
|
2018-08-27 16:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
testTable(tcs, t)
|
|
|
|
|
|
|
|
t.Run("mustBePositive", func(t *testing.T) {
|
|
|
|
var err error
|
2020-08-31 14:05:32 +02:00
|
|
|
_, err = NewKeepLastN(0, "foo")
|
2018-08-27 16:24:19 +02:00
|
|
|
assert.Error(t, err)
|
2020-08-31 14:05:32 +02:00
|
|
|
_, err = NewKeepLastN(-5, "foo")
|
2018-08-27 16:24:19 +02:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2020-08-31 14:05:32 +02:00
|
|
|
t.Run("emptyRegexAllowed", func(t *testing.T) {
|
|
|
|
_, err := NewKeepLastN(23, "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
2018-08-27 16:24:19 +02:00
|
|
|
}
|