-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup_test.go
161 lines (149 loc) · 3.83 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"net"
"strings"
"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: defaultACLTag,
},
{
input: `consul_catalog some.tag`,
shouldError: false,
tags: []string{"some.tag"},
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultACLTag,
},
{
input: `consul_catalog some.tag other.tag`,
shouldError: false,
tags: []string{"some.tag", "other.tag"},
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultACLTag,
},
{
input: `consul_catalog {
endpoint consul.local:1111
}`,
shouldError: false,
tags: defaultTags,
endpoint: "consul.local:1111",
ttl: defaultTTL,
metaTag: defaultACLTag,
},
{
input: `consul_catalog {
ttl 15s
}`,
shouldError: false,
tags: defaultTags,
endpoint: defaultEndpoint,
ttl: 15,
metaTag: defaultACLTag,
},
{
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 multiple 172.16.0.0/12 192.168.0.0/16
acl_zone public 0.0.0.0/0
}`,
shouldError: false,
tags: defaultTags,
endpoint: defaultEndpoint,
ttl: defaultTTL,
metaTag: defaultACLTag,
networks: map[string][]*net.IPNet{
"private": {
{IP: net.ParseIP("10.0.0.0"), Mask: net.IPv4Mask(255, 255, 255, 0)},
},
"multiple": {
{IP: net.ParseIP("172.16.0.0"), Mask: net.IPv4Mask(255, 240, 0, 0)},
{IP: net.ParseIP("192.168.0.0"), Mask: net.IPv4Mask(255, 255, 0, 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)
}
lastSourceDesc := strings.Split(catalog.Sources[len(catalog.Sources)-1].Name(), " ")
lastSourceName := lastSourceDesc[len(lastSourceDesc)-1]
if lastSourceName != tst.tags[0] {
t.Fatalf("Tags don't match: %v != %v", lastSourceName, 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, cidrRanges := range catalog.Networks {
expectedCIDR, ok := tst.networks[name]
if !ok {
t.Fatalf("Networks missing %s", name)
}
if len(expectedCIDR) != len(cidrRanges) {
t.Fatalf("expected %d ranges, got %d", len(expectedCIDR), len(cidrRanges))
}
for idx, parsed := range cidrRanges {
expected := expectedCIDR[idx]
if parsed.String() != expected.String() {
t.Fatalf("Wrong CIDR found: %s, expected %s", parsed, expected)
}
}
}
})
}
}