1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
package config_test
import (
"strconv"
"testing"
"time"
"github.com/rebuy-de/aws-nuke/pkg/config"
yaml "gopkg.in/yaml.v2"
)
func TestUnmarshalFilter(t *testing.T) {
past := time.Now().UTC().Add(-24 * time.Hour)
future := time.Now().UTC().Add(24 * time.Hour)
cases := []struct {
yaml string
match, mismatch []string
}{
{
yaml: `foo`,
match: []string{"foo"},
mismatch: []string{"fo", "fooo", "o", "fo"},
},
{
yaml: `{"type":"exact","value":"foo"}`,
match: []string{"foo"},
mismatch: []string{"fo", "fooo", "o", "fo"},
},
{
yaml: `{"type":"glob","value":"b*sh"}`,
match: []string{"bish", "bash", "bosh", "bush", "boooooosh", "bsh"},
mismatch: []string{"woooosh", "fooo", "o", "fo"},
},
{
yaml: `{"type":"glob","value":"b?sh"}`,
match: []string{"bish", "bash", "bosh", "bush"},
mismatch: []string{"woooosh", "fooo", "o", "fo", "boooooosh", "bsh"},
},
{
yaml: `{"type":"regex","value":"b[iao]sh"}`,
match: []string{"bish", "bash", "bosh"},
mismatch: []string{"woooosh", "fooo", "o", "fo", "boooooosh", "bsh", "bush"},
},
{
yaml: `{"type":"contains","value":"mba"}`,
match: []string{"bimbaz", "mba", "bi mba z"},
mismatch: []string{"bim-baz"},
},
{
yaml: `{"type":"dateOlderThan","value":"0"}`,
match: []string{strconv.Itoa(int(future.Unix())),
future.Format("2006-01-02"),
future.Format("2006/01/02"),
future.Format("2006-01-02T15:04:05Z"),
future.Format(time.RFC3339Nano),
future.Format(time.RFC3339),
},
mismatch: []string{"",
strconv.Itoa(int(past.Unix())),
past.Format("2006-01-02"),
past.Format("2006/01/02"),
past.Format("2006-01-02T15:04:05Z"),
past.Format(time.RFC3339Nano),
past.Format(time.RFC3339),
},
},
}
for _, tc := range cases {
t.Run(tc.yaml, func(t *testing.T) {
var filter config.Filter
err := yaml.Unmarshal([]byte(tc.yaml), &filter)
if err != nil {
t.Fatal(err)
}
for _, o := range tc.match {
match, err := filter.Match(o)
if err != nil {
t.Fatal(err)
}
if !match {
t.Fatalf("'%v' should match", o)
}
}
for _, o := range tc.mismatch {
match, err := filter.Match(o)
if err != nil {
t.Fatal(err)
}
if match {
t.Fatalf("'%v' should not match", o)
}
}
})
}
}
|