* plugin/forward: add HealthChecker interface Make the HealthChecker interface and morph the current DNS health checker into that interface. Remove all whole bunch of method on Forward that didn't make sense. This is done in preparation of adding a DoH client to forward - which requires a completely different healthcheck implementation (and more, but lets start here) Signed-off-by: Miek Gieben <miek@miek.nl> * Use protocol Signed-off-by: Miek Gieben <miek@miek.nl> * Dial doesnt need to be method an Forward either Signed-off-by: Miek Gieben <miek@miek.nl> * Address comments Address various comments on the PR. Signed-off-by: Miek Gieben <miek@miek.nl>
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
package forward
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/coredns/coredns/plugin/pkg/dnstest"
|
|
"github.com/coredns/coredns/plugin/test"
|
|
"github.com/coredns/coredns/request"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
func TestForward(t *testing.T) {
|
|
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
|
|
ret := new(dns.Msg)
|
|
ret.SetReply(r)
|
|
ret.Answer = append(ret.Answer, test.A("example.org. IN A 127.0.0.1"))
|
|
w.WriteMsg(ret)
|
|
})
|
|
defer s.Close()
|
|
|
|
p := NewProxy(s.Addr, DNS)
|
|
f := New()
|
|
f.SetProxy(p)
|
|
defer f.Close()
|
|
|
|
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
|
|
state.Req.SetQuestion("example.org.", dns.TypeA)
|
|
resp, err := f.Forward(state)
|
|
if err != nil {
|
|
t.Fatal("Expected to receive reply, but didn't")
|
|
}
|
|
// expect answer section with A record in it
|
|
if len(resp.Answer) == 0 {
|
|
t.Fatalf("Expected to at least one RR in the answer section, got none: %s", resp)
|
|
}
|
|
if resp.Answer[0].Header().Rrtype != dns.TypeA {
|
|
t.Errorf("Expected RR to A, got: %d", resp.Answer[0].Header().Rrtype)
|
|
}
|
|
if resp.Answer[0].(*dns.A).A.String() != "127.0.0.1" {
|
|
t.Errorf("Expected 127.0.0.1, got: %s", resp.Answer[0].(*dns.A).A.String())
|
|
}
|
|
}
|
|
|
|
func TestForwardRefused(t *testing.T) {
|
|
s := dnstest.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
|
|
ret := new(dns.Msg)
|
|
ret.SetReply(r)
|
|
ret.Rcode = dns.RcodeRefused
|
|
w.WriteMsg(ret)
|
|
})
|
|
defer s.Close()
|
|
|
|
p := NewProxy(s.Addr, DNS)
|
|
f := New()
|
|
f.SetProxy(p)
|
|
defer f.Close()
|
|
|
|
state := request.Request{W: &test.ResponseWriter{}, Req: new(dns.Msg)}
|
|
state.Req.SetQuestion("example.org.", dns.TypeA)
|
|
resp, err := f.Forward(state)
|
|
if err != nil {
|
|
t.Fatal("Expected to receive reply, but didn't")
|
|
}
|
|
if resp.Rcode != dns.RcodeRefused {
|
|
t.Errorf("Expected rcode to be %d, got %d", dns.RcodeRefused, resp.Rcode)
|
|
}
|
|
}
|