-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
119 lines (104 loc) · 4.24 KB
/
config_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package recursor
import (
"github.com/coredns/caddy"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
// Tests the various configs that should be parsed
func TestConfig_should_parse_json(t *testing.T) {
filePath := "examples/config1.json"
fileBytes, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("read config file %s error, %v", filePath, err)
}
rcu, err := readJsonConfig(fileBytes)
checkRcu(t, err, rcu)
}
func TestConfig_should_parse_yaml(t *testing.T) {
filePath := "examples/config1.yaml"
fileBytes, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("read config file %s error, %v", filePath, err)
}
rcu, err := readYamlConfig(fileBytes)
checkRcu(t, err, rcu)
}
func TestConfig_controller_should_read_external_json(t *testing.T) {
rcu, err := loadCaddyControllerConfig(t, "examples/config-with-json-ref1.caddy")
checkRcu(t, err, rcu)
}
func TestConfig_controller_should_read_external_yaml(t *testing.T) {
rcu, err := loadCaddyControllerConfig(t, "examples/config-with-yaml-ref1.caddy")
checkRcu(t, err, rcu)
}
func TestConfig_controller_should_read_inline_caddy(t *testing.T) {
rcu, err := loadCaddyControllerConfig(t, "examples/config1.caddy")
checkRcu(t, err, rcu)
rcu, err = loadCaddyControllerConfig(t, "examples/config2.caddy")
assert.Nil(t, err)
_, found := rcu.Aliases["*"]
assert.True(t, found)
rcu, err = loadCaddyControllerConfig(t, "examples/config3.caddy")
assert.Nil(t, err)
_, found = rcu.Aliases["*"]
assert.True(t, found)
rcu, err = loadCaddyControllerConfig(t, "examples/config3.caddy")
assert.Nil(t, err)
_, found = rcu.Aliases["x"]
assert.False(t, found)
}
func loadCaddyControllerConfig(t *testing.T, filePath string) (recursorCfg, error) {
fileBytes, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("read config file %s error, %v", filePath, err)
}
c := caddy.NewTestController("dns", string(fileBytes))
return readCaddyControllerConfig(c)
}
func checkRcu(t *testing.T, err error, rcu recursorCfg) {
if err != nil {
t.Fatalf("expected no errors, but got: %v", err)
}
assert.Equal(t, 0, rcu.Verbose, "verbose")
assert.Equal(t, "example1.svc", rcu.Zone, "zone")
// resolvers
assert.Equal(t, 2, len(rcu.Resolvers), "resolvers len")
rsl := rcu.Resolvers["resolver_primary"]
assert.Equal(t, 2, len(rsl.Urls), "resolver_primary urls len")
assert.Equal(t, "udp://10.0.0.1:53", rsl.Urls[0], "resolver_primary url[0]")
assert.Equal(t, "udp://10.0.0.2:53", rsl.Urls[1], "resolver_primary url[1]")
assert.Equal(t, 10, rsl.TimeoutMs, "resolver_primary timeout")
rsl = rcu.Resolvers["resolver_secondary"]
assert.Equal(t, 1, len(rsl.Urls), "resolver_secondary urls len")
assert.Equal(t, "udp://10.0.0.3:53", rsl.Urls[0], "resolver_secondary url[0]")
assert.Equal(t, 0, rsl.TimeoutMs, "resolver_secondary timeout")
// aliases
assert.Equal(t, 3, len(rcu.Aliases), "aliases len")
a := rcu.Aliases["alias1"]
assert.Equal(t, 2, len(a.Ips), "alias1 ips len")
assert.Equal(t, "10.1.1.1", a.Ips[0], "alias1 ips[0]")
assert.Equal(t, "10.1.1.2", a.Ips[1], "alias1 ips[1]")
assert.Equal(t, 2, len(a.Hosts), "alias1 hosts len")
assert.Equal(t, "www.example.org", a.Hosts[0], "alias1 hosts[0]")
assert.Equal(t, "www.example.com", a.Hosts[1], "alias1 hosts[1]")
assert.True(t, a.ShuffleIps, "alias1 ips shuffle is on")
assert.Equal(t, uint32(5), a.Ttl, "alias1 ttl")
assert.Equal(t, "resolver_primary", a.ResolverName, "alias1 resolverName")
a = rcu.Aliases["alias2"]
assert.Equal(t, 2, len(a.Ips), "alias2 ips len")
assert.Equal(t, "10.1.1.1", a.Ips[0], "alias2 ips[0]")
assert.Equal(t, "10.1.1.2", a.Ips[1], "alias2 ips[1]")
assert.Equal(t, 0, len(a.Hosts), "alias2 hosts len")
assert.False(t, a.ShuffleIps, "alias2 ips shuffle default false")
assert.Equal(t, uint32(0), a.Ttl, "alias2 ttl")
assert.Equal(t, "default", a.ResolverName, "alias2 resolverName")
a = rcu.Aliases["alias3"]
assert.Equal(t, 0, len(a.Ips), "alias3 ips len")
assert.Equal(t, 2, len(a.Hosts), "alias3 hosts len")
assert.Equal(t, "www.example.org", a.Hosts[0], "alias3 hosts[0]")
assert.Equal(t, "www.example.com", a.Hosts[1], "alias3 hosts[1]")
assert.False(t, a.ShuffleIps, "alias3 ips shuffle default false")
assert.Equal(t, uint32(15), a.Ttl, "alias3 ttl")
assert.Equal(t, "resolver_secondary", a.ResolverName, "alias3 resolverName")
}