pkg: add dnstest (#1098)

Add a full test server impl in this new package + tests. Move
dnsrecorder into this package as well and finish up the commented out
tests that were left in the old dnsrecorder package.

Update all callers and tests.
This commit is contained in:
Miek Gieben
2017-09-21 15:15:47 +01:00
committed by GitHub
parent 7109c6715c
commit 284061eee7
44 changed files with 223 additions and 131 deletions

View File

@@ -1,28 +0,0 @@
package dnsrecorder
/*
func TestNewResponseRecorder(t *testing.T) {
w := httptest.NewRecorder()
recordRequest := NewResponseRecorder(w)
if !(recordRequest.ResponseWriter == w) {
t.Fatalf("Expected Response writer in the Recording to be same as the one sent\n")
}
if recordRequest.status != http.StatusOK {
t.Fatalf("Expected recorded status to be http.StatusOK (%d) , but found %d\n ", http.StatusOK, recordRequest.status)
}
}
func TestWrite(t *testing.T) {
w := httptest.NewRecorder()
responseTestString := "test"
recordRequest := NewResponseRecorder(w)
buf := []byte(responseTestString)
recordRequest.Write(buf)
if recordRequest.size != len(buf) {
t.Fatalf("Expected the bytes written counter to be %d, but instead found %d\n", len(buf), recordRequest.size)
}
if w.Body.String() != responseTestString {
t.Fatalf("Expected Response Body to be %s , but found %s\n", responseTestString, w.Body.String())
}
}
*/

View File

@@ -1,5 +1,5 @@
// Package dnsrecorder allows you to record a DNS response when it is send to the client.
package dnsrecorder
// Package dnstest allows for easy testing of DNS client against a test server.
package dnstest
import (
"time"
@@ -24,7 +24,7 @@ type Recorder struct {
// New makes and returns a new Recorder,
// which captures the DNS rcode from the ResponseWriter
// and also the length of the response message written through it.
func New(w dns.ResponseWriter) *Recorder {
func NewRecorder(w dns.ResponseWriter) *Recorder {
return &Recorder{
ResponseWriter: w,
Rcode: 0,
@@ -52,7 +52,3 @@ func (r *Recorder) Write(buf []byte) (int, error) {
}
return n, err
}
// Hijack implements dns.Hijacker. It simply wraps the underlying
// ResponseWriter's Hijack method if there is one, or returns an error.
func (r *Recorder) Hijack() { r.ResponseWriter.Hijack(); return }

View File

@@ -0,0 +1,50 @@
package dnstest
import (
"testing"
"github.com/miekg/dns"
)
type responseWriter struct{ dns.ResponseWriter }
func (r *responseWriter) WriteMsg(m *dns.Msg) error { return nil }
func (r *responseWriter) Write(buf []byte) (int, error) { return len(buf), nil }
func TestNewRecorder(t *testing.T) {
w := &responseWriter{}
record := NewRecorder(w)
if record.ResponseWriter != w {
t.Fatalf("Expected Response writer in the Recording to be same as the one sent\n")
}
if record.Rcode != dns.RcodeSuccess {
t.Fatalf("Expected recorded status to be dns.RcodeSuccess (%d) , but found %d\n ", dns.RcodeSuccess, record.Rcode)
}
}
func TestWriteMsg(t *testing.T) {
w := &responseWriter{}
record := NewRecorder(w)
responseTestName := "testmsg.example.org."
responseTestMsg := new(dns.Msg)
responseTestMsg.SetQuestion(responseTestName, dns.TypeA)
record.WriteMsg(responseTestMsg)
if record.Len != responseTestMsg.Len() {
t.Fatalf("Expected the bytes written counter to be %d, but instead found %d\n", responseTestMsg.Len(), record.Len)
}
if x := record.Msg.Question[0].Name; x != responseTestName {
t.Fatalf("Expected Msg Qname to be %s , but found %s\n", responseTestName, x)
}
}
func TestWrite(t *testing.T) {
w := &responseWriter{}
record := NewRecorder(w)
responseTest := []byte("testmsg.example.org.")
record.Write(responseTest)
if record.Len != len(responseTest) {
t.Fatalf("Expected the bytes written counter to be %d, but instead found %d\n", len(responseTest), record.Len)
}
}

View File

@@ -0,0 +1,46 @@
package dnstest
import (
"net"
"github.com/miekg/dns"
)
// A Server is an DNS server listening on a system-chosen port on the local
// loopback interface, for use in end-to-end DNS tests.
type Server struct {
Addr string // Address where the server listening.
s1 *dns.Server // udp
s2 *dns.Server // tcp
}
// NewServer starts and returns a new Server. The caller should call Close when
// finished, to shut it down.
func NewServer(f dns.HandlerFunc) *Server {
dns.HandleFunc(".", f)
ch1 := make(chan bool)
ch2 := make(chan bool)
p, _ := net.ListenPacket("udp", ":0")
l, _ := net.Listen("tcp", p.LocalAddr().String())
s1 := &dns.Server{PacketConn: p}
s2 := &dns.Server{Listener: l}
s1.NotifyStartedFunc = func() { close(ch1) }
s2.NotifyStartedFunc = func() { close(ch2) }
go s1.ActivateAndServe()
go s2.ActivateAndServe()
<-ch1
<-ch2
return &Server{s1: s1, s2: s2, Addr: p.LocalAddr().String()}
}
// Close shuts down the server.
func (s *Server) Close() {
s.s1.Shutdown()
s.s2.Shutdown()
}

View File

@@ -0,0 +1,28 @@
package dnstest
import (
"testing"
"github.com/miekg/dns"
)
func TestNewServer(t *testing.T) {
s := NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
ret := new(dns.Msg)
ret.SetReply(r)
w.WriteMsg(ret)
})
defer s.Close()
c := new(dns.Client)
c.Net = "tcp"
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeSOA)
ret, _, err := c.Exchange(m, s.Addr)
if err != nil {
t.Fatalf("Could not send message to dnstest.Server: %s", err)
}
if ret.Id != m.Id {
t.Fatalf("Msg ID's should match, expected %d, got %d", m.Id, ret.Id)
}
}

View File

@@ -5,7 +5,7 @@ import (
"strings"
"time"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
@@ -31,7 +31,7 @@ type replacer struct {
// values into the replacer. rr may be nil if it is not
// available. emptyValue should be the string that is used
// in place of empty string (can still be empty string).
func New(r *dns.Msg, rr *dnsrecorder.Recorder, emptyValue string) Replacer {
func New(r *dns.Msg, rr *dnstest.Recorder, emptyValue string) Replacer {
req := request.Request{W: rr, Req: r}
rep := replacer{
replacements: map[string]string{

View File

@@ -3,14 +3,14 @@ package replacer
import (
"testing"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
func TestNewReplacer(t *testing.T) {
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeHINFO)
@@ -37,7 +37,7 @@ func TestNewReplacer(t *testing.T) {
}
func TestSet(t *testing.T) {
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeHINFO)