forked from unRob/coredns-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
141 lines (129 loc) · 3.19 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"net"
"testing"
"github.com/coredns/caddy"
)
func TestSetup(t *testing.T) {
defaultTTL := uint32(300)
defaultEndpoint := "consul.service.consul:8500"
defaultTags := []string{"coredns.enabled"}
tests := []struct {
input string
shouldError bool
tags []string
endpoint string
ttl uint32
metaTag string
networks map[string]*net.IPNet
}{
{
input: `consul_catalog`,
shouldError: false,
tags: defaultTags,
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultMeta,
},
{
input: `consul_catalog some.tag`,
shouldError: false,
tags: []string{"some.tag"},
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultMeta,
},
{
input: `consul_catalog some.tag other.tag`,
shouldError: false,
tags: []string{"some.tag", "other.tag"},
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultMeta,
},
{
input: `consul_catalog {
endpoint consul.local:1111
}`,
shouldError: false,
tags: defaultTags,
endpoint: "consul.local:1111",
ttl: defaultTTL,
metaTag: defaultMeta,
},
{
input: `consul_catalog {
ttl 15s
}`,
shouldError: false,
tags: defaultTags,
endpoint: defaultEndpoint,
ttl: 15,
metaTag: defaultMeta,
},
{
input: `consul_catalog {
acl_metadata_tag some-tag
}`,
shouldError: false,
tags: defaultTags,
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: "some-tag",
},
{
input: `consul_catalog {
acl_zone private 10.0.0.1/24
acl_zone public 0.0.0.0/0
}`,
shouldError: false,
tags: defaultTags,
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultMeta,
networks: map[string]*net.IPNet{
"private": {IP: net.ParseIP("10.0.0.0"), Mask: net.IPv4Mask(255, 255, 255, 0)},
"public": {IP: net.ParseIP("0.0.0.0"), Mask: net.IPv4Mask(0, 0, 0, 0)},
},
},
{
input: `consul_catalog {
whatever
}`,
shouldError: true,
},
}
for _, tst := range tests {
t.Run(tst.input, func(t *testing.T) {
c := caddy.NewTestController("dns", tst.input)
catalog, err := parse(c)
if tst.shouldError {
if err == nil {
t.Fatalf("Expected errors, but got none")
}
return
}
if err != nil {
t.Fatalf("Expected no errors, but got: %v", err)
}
if catalog.Tag != tst.tags[0] {
t.Fatalf("Tags don't match: %v != %v", catalog.Tag, tst.tags[0])
}
if catalog.Endpoint != tst.endpoint {
t.Fatalf("Endpoint doesn't match: %v != %v", catalog.Endpoint, tst.endpoint)
}
if catalog.TTL != tst.ttl {
t.Fatalf("TTL doesn't match: %v != %v", catalog.TTL, tst.ttl)
}
for name, cidr := range catalog.Networks {
if expectedCIDR, ok := tst.networks[name]; !ok {
t.Fatalf("Networks missing %s", name)
} else if expectedCIDR.String() != cidr.String() {
t.Fatalf("Wrong CIDR found: %s, expected %s", cidr, expectedCIDR)
}
}
})
}
}