-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazaffinity_test.go
107 lines (89 loc) · 2.3 KB
/
azaffinity_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
package azaffinity
import (
"context"
"net"
"testing"
"github.com/coredns/caddy"
"github.com/miekg/dns"
)
func TestSetup(t *testing.T) {
c := caddy.NewTestController("dns", `azaffinity 10.0.0.0/24 ap-southeast-1a`)
err := setup(c)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
c = caddy.NewTestController("dns", `azaffinity invalid-subnet ap-southeast-1a`)
err = setup(c)
if err == nil {
t.Fatalf("Expected error for invalid subnet, got none")
}
c = caddy.NewTestController("dns", `azaffinity 10.0.0.0/24`)
err = setup(c)
if err == nil {
t.Fatalf("Expected error for missing availability zone, got none")
}
}
func TestServeDNS(t *testing.T) {
azaffinity := AZAffinity{
SubnetAZMap: map[string]string{
"10.0.0.0/24": "ap-southeast-1a",
},
}
tests := []struct {
clientIP string
question dns.Question
expectName string
}{
{
clientIP: "10.0.0.1",
question: dns.Question{
Name: "internal-k8s-example-123.ap-southeast-1.elb.amazonaws.com.",
Qtype: dns.TypeCNAME,
},
expectName: "ap-southeast-1a.internal-k8s-example-123.ap-southeast-1.elb.amazonaws.com.",
},
{
clientIP: "192.168.1.1",
question: dns.Question{
Name: "internal-k8s-example-123.ap-southeast-1.elb.amazonaws.com.",
Qtype: dns.TypeCNAME,
},
expectName: "internal-k8s-example-123.ap-southeast-1.elb.amazonaws.com.",
},
}
for _, test := range tests {
w := &testResponseWriter{
remoteAddr: test.clientIP + ":12345",
}
r := new(dns.Msg)
r.SetQuestion(test.question.Name, test.question.Qtype)
ctx := context.TODO()
_, err := azaffinity.ServeDNS(ctx, w, r)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if r.Question[0].Name != test.expectName {
t.Errorf("Expected question name %s, got %s", test.expectName, r.Question[0].Name)
}
}
}
type testResponseWriter struct {
dns.ResponseWriter
remoteAddr string
}
func (t *testResponseWriter) WriteMsg(m *dns.Msg) error {
return nil
}
func (t *testResponseWriter) Write([]byte) (int, error) {
return 0, nil
}
func (t *testResponseWriter) Close() error {
return nil
}
func (t *testResponseWriter) TsigStatus() error {
return nil
}
func (t *testResponseWriter) Hijack() {}
func (t *testResponseWriter) RemoteAddr() net.Addr {
return &net.IPAddr{IP: net.ParseIP(t.remoteAddr)}
}