forked from unRob/coredns-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog_test.go
174 lines (157 loc) · 4.44 KB
/
catalog_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
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright © 2022 Roberto Hidalgo <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package catalog
import (
"context"
"fmt"
"net"
"testing"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
func NewTestCatalog(fetch bool) (*Catalog, ClientCatalog) {
c := New()
c.FQDN = []string{"example.com."}
c.ProxyTag = "traefik.enable=true"
c.ProxyService = "traefik"
c.Networks = map[string]*net.IPNet{}
_, private, _ := net.ParseCIDR("192.168.100.0/24")
c.Networks["private"] = private
_, guest, _ := net.ParseCIDR("192.168.1.0/24")
c.Networks["guest"] = guest
_, public, _ := net.ParseCIDR("0.0.0.0/0")
c.Networks["public"] = public
c.ConfigKey = "some/kv/path"
client := NewTestCatalogClient()
kvClient := NewTestKVClient()
c.SetClients(client, kvClient)
if fetch {
err := c.FetchServices()
if err != nil {
panic(err)
}
}
return c, client
}
func TestServeDNS(t *testing.T) {
allHosts := map[string][]string{
"nomad.service.consul.": {"192.168.100.1"},
"traefik.service.consul.": {"192.168.100.2"},
"git.service.consul.": {"192.168.100.3", "192.168.100.4"},
}
c, _ := NewTestCatalog(true)
for !c.Ready() {
time.Sleep(1 * time.Second)
}
defaultLookup = func(ctx context.Context, req request.Request, target string) (*dns.Msg, error) {
res := new(dns.Msg)
res.Answer = []dns.RR{}
ips, exists := allHosts[target]
if !exists {
res.SetRcode(req.Req, dns.RcodeNameError)
} else {
header := dns.RR_Header{Name: req.QName(), Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300}
for _, ip := range ips {
res.Answer = append(res.Answer, &dns.A{
Hdr: header,
A: net.ParseIP(ip),
})
}
}
return res, nil
}
tests := []struct {
qname string
qtype uint16
expectedCode int
expectedReply []string
expectedErr error
from string
}{
{
qname: "nomad.example.com",
qtype: dns.TypeA,
expectedCode: dns.RcodeSuccess,
expectedReply: []string{"192.168.100.2"},
expectedErr: nil,
from: "192.168.100.42",
},
{
qname: "traefik.example.com",
qtype: dns.TypeA,
expectedCode: dns.RcodeSuccess,
expectedReply: []string{"192.168.100.2"},
expectedErr: nil,
from: "192.168.100.42",
},
{
qname: "git.example.com",
qtype: dns.TypeA,
expectedCode: dns.RcodeSuccess,
expectedReply: []string{"192.168.100.3", "192.168.100.4"},
expectedErr: nil,
from: "192.168.100.42",
},
{
qname: "nomad.example.com",
qtype: dns.TypeA,
expectedCode: dns.RcodeServerFailure,
expectedReply: []string{},
expectedErr: plugin.Error("consul_catalog", fmt.Errorf("no next plugin found")),
from: "192.168.1.1",
},
{
qname: "git.example.com",
qtype: dns.TypeA,
expectedCode: dns.RcodeServerFailure,
expectedReply: []string{},
expectedErr: plugin.Error("consul_catalog", fmt.Errorf("no next plugin found")),
from: "192.168.1.1",
},
{
qname: "git.example.com",
qtype: dns.TypeA,
expectedCode: dns.RcodeSuccess,
expectedReply: []string{"192.168.100.3", "192.168.100.4"},
expectedErr: nil,
from: "10.42.0.1",
},
}
ctx := context.TODO()
for i, tc := range tests {
t.Run(fmt.Sprintf("%s-%s", tc.qname, tc.from), func(it *testing.T) {
req := new(dns.Msg)
req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
rec := dnstest.NewRecorder(&test.ResponseWriter{
RemoteIP: tc.from,
})
code, err := c.ServeDNS(ctx, rec, req)
if err == nil {
if tc.expectedErr != nil {
it.Fatalf("Expected error %v, got nil", tc.expectedErr)
}
} else if tc.expectedErr != nil && tc.expectedErr.Error() != err.Error() {
it.Fatalf("Expected error %v, got %s", tc.expectedErr, err)
}
if code != tc.expectedCode {
it.Fatalf("Test %d: Expected status code %d, but got %d", i, tc.expectedCode, code)
}
if len(tc.expectedReply) != 0 {
for i, expected := range tc.expectedReply {
actual, ok := rec.Msg.Answer[i].(*dns.A)
if !ok {
it.Fatalf("something crapped out: %v", rec.Msg.Answer)
return
}
if !actual.A.Equal(net.ParseIP(expected)) {
it.Errorf("Test %d: Expected answer %s, but got %s", i, expected, actual)
}
}
}
})
}
}