2018-08-27 15:09:24 +02:00
|
|
|
package pruning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 {
|
2018-08-30 11:44:43 +02:00
|
|
|
inputs []Snapshot
|
|
|
|
rules []KeepRule
|
|
|
|
expDestroy, effDestroy map[string]bool
|
|
|
|
expDestroyAlternatives []map[string]bool
|
2018-08-27 16:17:39 +02:00
|
|
|
}
|
2018-08-27 15:09:24 +02:00
|
|
|
|
2018-11-16 11:32:24 +01:00
|
|
|
type snapshotList []Snapshot
|
|
|
|
|
|
|
|
func (l snapshotList) ContainsName(n string) bool {
|
|
|
|
for _, s := range l {
|
|
|
|
if s.Name() == n {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l snapshotList) NameList() []string {
|
|
|
|
res := make([]string, len(l))
|
|
|
|
for i, s := range l {
|
|
|
|
res[i] = s.Name()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
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)
|
2018-08-30 11:44:43 +02:00
|
|
|
tc.effDestroy = make(map[string]bool)
|
2018-08-27 16:17:39 +02:00
|
|
|
for _, s := range remove {
|
2018-08-30 11:44:43 +02:00
|
|
|
tc.effDestroy[s.Name()] = true
|
|
|
|
}
|
|
|
|
if tc.expDestroyAlternatives == nil {
|
|
|
|
if tc.expDestroy == nil {
|
|
|
|
panic("must specify either expDestroyAlternatives or expDestroy")
|
|
|
|
}
|
|
|
|
tc.expDestroyAlternatives = []map[string]bool{tc.expDestroy}
|
|
|
|
}
|
|
|
|
var okAlt map[string]bool = nil
|
|
|
|
for _, alt := range tc.expDestroyAlternatives {
|
|
|
|
t.Logf("testing possible result: %v", alt)
|
|
|
|
if mapEqual(alt, tc.effDestroy) {
|
|
|
|
okAlt = alt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if okAlt == nil {
|
|
|
|
t.Errorf("no alternatives matched result: %v", tc.effDestroy)
|
2018-08-27 16:17:39 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2018-11-16 11:32:24 +01:00
|
|
|
MustKeepRegex("foo_", false),
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: map[string]bool{
|
2018-08-27 16:17:39 +02:00
|
|
|
"bar_123": true,
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"multipleRules": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
2018-11-16 11:32:24 +01:00
|
|
|
MustKeepRegex("foo_", false),
|
|
|
|
MustKeepRegex("bar_", false),
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: map[string]bool{},
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
"onlyThoseRemovedByAllAreRemoved": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{
|
2018-11-16 11:32:24 +01:00
|
|
|
MustKeepRegex("notInS1", false), // would remove all
|
|
|
|
MustKeepRegex("bar_", false), // would remove all but bar_, i.e. foo_.*
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: map[string]bool{
|
2018-08-27 16:17:39 +02:00
|
|
|
"foo_123": true,
|
|
|
|
"foo_456": true,
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
"noRulesKeepsAll": {
|
2018-08-30 11:44:43 +02:00
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: []KeepRule{},
|
|
|
|
expDestroy: map[string]bool{},
|
|
|
|
},
|
|
|
|
"nilRulesKeepsAll": {
|
|
|
|
inputs: inputs["s1"],
|
|
|
|
rules: nil,
|
|
|
|
expDestroy: map[string]bool{},
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
|
|
|
"noSnaps": {
|
|
|
|
inputs: []Snapshot{},
|
|
|
|
rules: []KeepRule{
|
2018-11-16 11:32:24 +01:00
|
|
|
MustKeepRegex("foo_", false),
|
2018-08-27 15:09:24 +02:00
|
|
|
},
|
2018-08-30 11:44:43 +02:00
|
|
|
expDestroy: 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
|
|
|
}
|