-
Notifications
You must be signed in to change notification settings - Fork 12
/
setup_test.go
79 lines (72 loc) · 2.48 KB
/
setup_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
package dnsredir
import (
"fmt"
"github.com/coredns/caddy"
"strings"
"testing"
)
type testCase struct {
input string
shouldErr bool
expectedErr string
}
func (t testCase) String() string {
return fmt.Sprintf("{%T input=%q shouldErr=%v expectedErr=%q}",
t, t.input, t.shouldErr, t.expectedErr)
}
// Return true if test passed, false otherwise
func (t *testCase) Pass(err error) bool {
// Empty expected error isn't allow
if t.shouldErr == (len(t.expectedErr) == 0) {
panic(fmt.Sprintf("Bad test case %v", t))
}
pass := true
if t.shouldErr == (err != nil) {
if err != nil {
s := strings.ToLower(err.Error())
t := strings.ToLower(t.expectedErr)
if !strings.Contains(s, t) {
pass = false
}
}
} else {
pass = false
}
return pass
}
func TestSetupTo(t *testing.T) {
tests := []testCase{
// Negative
{"dnsredir", true, `missing mandatory property: "to"`},
{"dnsredir .", true, `missing mandatory property: "to"`},
{"dnsredir whitelist.zone {}", true, `missing mandatory property: "to"`},
{"dnsredir root.zone { }", true, `missing mandatory property: "to"`},
{"dnsredir . { to }", true, "not an IP address:"},
{"dnsredir . { to . }", true, "not an IP address:"},
{"dnsredir . { to foo }", true, "not an IP address:"},
{"dnsredir . { to foobar.net }", true, "not an IP address:"},
{"dnsredir . { to foobar.net. }", true, "not an IP address:"},
{"dnsredir . { to 1.2.3 }", true, "not an IP address:"},
{"dnsredir . { to 1.2.3.4 }", true, "not an IP address:"},
{"dnsredir . { to \n }", true, "Wrong argument count or unexpected line ending after"},
{"dnsredir . { to . \n }", true, "not an IP address:"},
{"dnsredir . { to / \n }", true, "not an IP address:"},
{"dnsredir . { to 1.2.3. \n }", true, "not an IP address:"},
{"dnsredir . { to foobar://1.1.1.1 \n }", true, "not an IP address:"},
// Positive
{"dnsredir . { to 1.2.3.4 \n }", false, ""},
{"dnsredir . { to 1.2.3.4 / . \n }", true, "not an IP address:"},
{"dnsredir . { to dns://8.8.4.4 \n }", false, ""},
{"dnsredir . { to dns://192.168.144.10:5353 \n }", false, ""},
{"dnsredir . { to tls://172.16.10.1 \n }", false, ""},
{"dnsredir . { to tls://172.16.10.1:1234 \n }", false, ""},
{"dnsredir . { to 10.1.2.3 dns://192.168.144.100 tls://172.16.10.1:1234 \n }", false, ""},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.input)
_, err := newReloadableUpstream(c)
if !test.Pass(err) {
t.Errorf("Test#%v failed %v vs err: %v", i, test, err)
}
}
}