-
Notifications
You must be signed in to change notification settings - Fork 39
/
main_test.go
88 lines (82 loc) · 2.29 KB
/
main_test.go
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
package main
import (
"strconv"
"testing"
"github.com/sorenmat/k8s-rds/crd"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestExcluded(t *testing.T) {
tests := []struct {
name string
db *crd.Database
exclNS []string
inclNS []string
excluded bool
}{
{
name: "no excluded or included namespaces",
db: &crd.Database{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}},
excluded: false,
},
{
name: "namespace not in excluded namespaces",
db: &crd.Database{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}},
exclNS: []string{"test"},
excluded: false,
},
{
name: "namespace not in included namespaces",
db: &crd.Database{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}},
inclNS: []string{"test"},
excluded: true,
},
{
name: "namespace in excluded namespaces",
db: &crd.Database{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}},
exclNS: []string{"default"},
excluded: true,
},
{
name: "namespace in included namespaces",
db: &crd.Database{ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}},
inclNS: []string{"default"},
excluded: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
expected := test.excluded
if actual := excluded(test.db, test.exclNS, test.inclNS); actual != expected {
t.Errorf("expected %v, actual %v", expected, actual)
}
})
}
}
func TestStringInSlice(t *testing.T) {
tests := []struct {
str string
slice []string
expected bool
}{
{"", nil, false},
{"test", nil, false},
{"", []string{}, false},
{"test", []string{}, false},
{"test", []string{"hello"}, false},
{"test", []string{"test"}, true},
{"", []string{"test"}, false},
{"", []string{"test", ""}, true},
{"test", []string{"test", "test"}, true},
{"test", []string{"hello", "test"}, true},
{"hello", []string{"hello", "test"}, true},
{"world", []string{"hello", "test"}, false},
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
expected := test.expected
if actual := stringInSlice(test.str, test.slice); actual != expected {
t.Errorf("expected %v, actual %v", expected, actual)
}
})
}
}