-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
149 lines (128 loc) · 5.28 KB
/
handler_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
package drovedns
import (
"context"
"encoding/json"
"net"
"testing"
"time"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
type MockDroveClient struct{}
func (*MockDroveClient) FetchApps() (*DroveAppsResponse, error) {
apps := &DroveAppsResponse{}
json.Unmarshal([]byte(`{"status": "ok", "message": "ok", "data":[{"appId": "PS", "vhost": "example.com", "tags": {}, "hosts":[{"host": "host", "port": 1234, "portType": "http"}]}]}`), apps)
return apps, nil
}
func (*MockDroveClient) FetchRecentEvents(sync *CurrSyncPoint) (*DroveEventSummary, error) {
eventCount := make(map[string]interface{})
eventCount["APP_STATE_CHANGE"] = 1
return &DroveEventSummary{eventCount, 1}, nil
}
func (*MockDroveClient) PollEvents(callback func(event *DroveEventSummary)) {
}
type MockResponseWriter struct {
dns.ResponseWriter
validator func(ms *dns.Msg)
callCounter int
}
func (w *MockResponseWriter) WriteMsg(res *dns.Msg) error {
w.callCounter += 1
w.validator(res)
return nil
}
func TestServeDNSNotReady(t *testing.T) {
handler := DroveHandler{DroveEndpoints: newDroveEndpoints(&MockDroveClient{})}
writer := &MockResponseWriter{
validator: func(res *dns.Msg) {
assert.Equal(t, 1, len(res.Answer), "One Answer should be returned")
assert.Equal(t, 0, len(res.Extra), "Additional should be empty")
assert.Equal(t, "host.", res.Answer[0].(*dns.SRV).Target, "'host' should be the target")
assert.Equal(t, uint16(1234), res.Answer[0].(*dns.SRV).Port, "1234 should be the port")
}}
code, err := handler.ServeDNS(context.Background(), writer, &dns.Msg{Question: []dns.Question{dns.Question{Name: "example.com.", Qtype: dns.TypeSRV, Qclass: dns.ClassINET}}})
assert.NotNil(t, err, "Error should be returned")
assert.Equal(t, dns.RcodeServerFailure, code, "Failure error code should be returned")
assert.Equal(t, 0, writer.callCounter, "Message would not be written")
}
func TestServeDNSAnswer(t *testing.T) {
handler := NewDroveHandler(&MockDroveClient{})
for !handler.Ready() {
time.Sleep(1)
}
writer := &MockResponseWriter{
validator: func(res *dns.Msg) {
assert.Equal(t, 1, len(res.Answer), "One Answer should be returned")
assert.Equal(t, 0, len(res.Extra), "Additional should be empty")
assert.Equal(t, "host.", res.Answer[0].(*dns.SRV).Target, "'host' should be the target")
assert.Equal(t, uint16(1234), res.Answer[0].(*dns.SRV).Port, "1234 should be the port")
}}
code, err := handler.ServeDNS(context.Background(), writer, &dns.Msg{Question: []dns.Question{dns.Question{Name: "example.com.", Qtype: dns.TypeSRV, Qclass: dns.ClassINET}}})
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, dns.RcodeSuccess, code, "SuccessCode should be returned")
assert.Less(t, 0, writer.callCounter, "Message should be written")
}
func TestServeDNSAdditional(t *testing.T) {
handler := NewDroveHandler(&MockDroveClient{})
for !handler.Ready() {
time.Sleep(1)
}
writer := &MockResponseWriter{
validator: func(res *dns.Msg) {
assert.Equal(t, 0, len(res.Answer))
assert.Equal(t, 1, len(res.Extra))
assert.Equal(t, "host.", res.Extra[0].(*dns.SRV).Target)
assert.Equal(t, uint16(1234), res.Extra[0].(*dns.SRV).Port)
}}
code, err := handler.ServeDNS(context.Background(), writer, &dns.Msg{Question: []dns.Question{dns.Question{Name: "example.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET}}})
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, dns.RcodeSuccess, code, "SuccessCode should be returned")
assert.Less(t, 0, writer.callCounter, "Message should be written")
}
func TestServeDNSNoMatchingApp(t *testing.T) {
handler := NewDroveHandler(&MockDroveClient{})
for !handler.Ready() {
time.Sleep(1)
}
writer := &MockResponseWriter{
validator: func(res *dns.Msg) {
assert.Equal(t, 0, len(res.Answer))
assert.Equal(t, 1, len(res.Extra))
assert.Equal(t, "host.", res.Extra[0].(*dns.SRV).Target)
assert.Equal(t, uint16(1234), res.Extra[0].(*dns.SRV).Port)
}}
code, err := handler.ServeDNS(context.Background(), writer, &dns.Msg{Question: []dns.Question{dns.Question{Name: "example2.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET}}})
assert.NotNil(t, err, "Error should be returned")
assert.Equal(t, dns.RcodeServerFailure, code, "Failure error code should be returned")
assert.Equal(t, 0, writer.callCounter, "Message should not be Written")
}
type MockHandler struct {
callCounter int
}
func (h *MockHandler) ServeDNS(c context.Context, rw dns.ResponseWriter, r *dns.Msg) (int, error) {
h.callCounter += 1
r.Answer = append(r.Answer, &dns.A{
A: net.IPv4(1, 1, 1, 1),
})
return 0, nil
}
func (h *MockHandler) Name() string {
return "MOCK"
}
func TestServeDNSForwarding(t *testing.T) {
handler := NewDroveHandler(&MockDroveClient{})
mockNextHandler := MockHandler{}
handler.Next = &mockNextHandler
for !handler.Ready() {
time.Sleep(1)
}
writer := &MockResponseWriter{
validator: func(res *dns.Msg) {
assert.Equal(t, 0, len(res.Answer))
assert.Equal(t, 1, len(res.Extra))
assert.Equal(t, "host.", res.Extra[0].(*dns.SRV).Target)
assert.Equal(t, uint16(1234), res.Extra[0].(*dns.SRV).Port)
}}
handler.ServeDNS(context.Background(), writer, &dns.Msg{Question: []dns.Question{dns.Question{Name: "example.com.", Qtype: dns.TypeA, Qclass: dns.ClassINET}}})
assert.Equal(t, 1, mockNextHandler.callCounter, "Next handler should be called")
}