2018-08-27 15:09:24 +02:00
|
|
|
package pruning
|
|
|
|
|
|
|
|
import (
|
2018-08-27 16:17:39 +02:00
|
|
|
"fmt"
|
2018-08-27 15:09:24 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stubSnap struct {
|
|
|
|
name string
|
|
|
|
replicated bool
|
|
|
|
date time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s stubSnap) Name() string { return s.name }
|
|
|
|
|
|
|
|
func (s stubSnap) Replicated() bool { return s.replicated }
|
|
|
|
|
|
|
|
func (s stubSnap) Date() time.Time { return s.date }
|
|
|
|
|
2018-08-27 16:17:39 +02:00
|
|
|
type testCase struct {
|
|
|
|
inputs []Snapshot
|
|
|
|
rules []KeepRule
|
|
|
|
exp, eff map[string]bool
|
|
|
|
}
|
2018-08-27 15:09:24 +02:00
|
|
|
|
2018-08-27 16:17:39 +02:00
|
|
|
func testTable(tcs map[string]testCase, t *testing.T) {
|
|
|
|
mapEqual := func(a, b map[string]bool) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for k, v := range a {
|
|
|
|
if w, ok := b[k]; !ok || v != w {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2018-08-27 15:09:24 +02:00
|
|
|
}
|
|
|
|
|
2018-08-27 16:17:39 +02:00
|
|
|
for name := range tcs {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tcs[name]
|
|
|
|
remove := PruneSnapshots(tc.inputs, tc.rules)
|
|
|
|
tc.eff = make(map[string]bool)
|
|
|
|
for _, s := range remove {
|
|
|
|
tc.eff[s.Name()] = true
|
|
|
|
}
|
|
|
|
assert.True(t, mapEqual(tc.exp, tc.eff), fmt.Sprintf("is %v but should be %v", tc.eff, tc.exp))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPruneSnapshots(t *testing.T) {
|
|
|
|
|
2018-08-27 15:09:24 +02:00
|
|
|
inputs := map[string][]Snapshot{
|
|
|
|
"s1": []Snapshot{
|
|
|
|
stubSnap{name: "foo_123"},
|
|
|
|
stubSnap{name: "foo_456"},
|
|
|
|
stubSnap{name: "bar_123"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tcs := map[string]testCase{
|
|
|
|
"simple": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
|
|
|
MustKeepRegex("foo_"),
|
|
|
|
},
|
2018-08-27 16:17:39 +02:00
|
|
|
exp: map[string]bool{
|
|
|
|
"bar_123": true,
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"multipleRules": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
|
|
|
MustKeepRegex("foo_"),
|
|
|
|
MustKeepRegex("bar_"),
|
|
|
|
},
|
2018-08-27 16:17:39 +02:00
|
|
|
exp: map[string]bool{},
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
"onlyThoseRemovedByAllAreRemoved": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
|
|
|
MustKeepRegex("notInS1"), // would remove all
|
|
|
|
MustKeepRegex("bar_"), // would remove all but bar_, i.e. foo_.*
|
|
|
|
},
|
2018-08-27 16:17:39 +02:00
|
|
|
exp: map[string]bool{
|
|
|
|
"foo_123": true,
|
|
|
|
"foo_456": true,
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"noRulesKeepsAll": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{},
|
2018-08-27 16:17:39 +02:00
|
|
|
exp: map[string]bool{
|
|
|
|
"foo_123": true,
|
|
|
|
"foo_456": true,
|
|
|
|
"bar_123": true,
|
|
|
|
},
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
"noSnaps": {
|
|
|
|
inputs: []Snapshot{},
|
|
|
|
rules: []KeepRule{
|
|
|
|
MustKeepRegex("foo_"),
|
|
|
|
},
|
2018-08-27 16:17:39 +02:00
|
|
|
exp: map[string]bool{},
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-27 16:17:39 +02:00
|
|
|
testTable(tcs, t)
|
2018-08-27 15:09:24 +02:00
|
|
|
}
|