git » dnss » commit 432b60a

dnsserver: Add basic tests for the DNS server

author Alberto Bertogli
2018-04-15 13:27:41 UTC
committer Alberto Bertogli
2018-04-15 13:27:41 UTC
parent c701ad1d11a95ad0b1c064fcb9a43f8e19262d27

dnsserver: Add basic tests for the DNS server

This patch introduces basic DNS server tests, which complement the dnss
end-to-end tests, and can cover some specific scenarios.

internal/dnsserver/server.go +1 -1
internal/dnsserver/server_test.go +49 -0
internal/testutil/testutil.go +13 -0

diff --git a/internal/dnsserver/server.go b/internal/dnsserver/server.go
index d4e84ff..2874f7d 100644
--- a/internal/dnsserver/server.go
+++ b/internal/dnsserver/server.go
@@ -131,7 +131,7 @@ func (s *Server) Handler(w dns.ResponseWriter, r *dns.Msg) {
 
 	fromUp, err := s.resolver.Query(r, tr)
 	if err != nil {
-		log.Infof(err.Error())
+		log.Infof("resolver query error: %v", err)
 		tr.LazyPrintf(err.Error())
 		tr.SetError()
 		return
diff --git a/internal/dnsserver/server_test.go b/internal/dnsserver/server_test.go
new file mode 100644
index 0000000..8612c9c
--- /dev/null
+++ b/internal/dnsserver/server_test.go
@@ -0,0 +1,49 @@
+package dnsserver
+
+import (
+	"testing"
+
+	"github.com/miekg/dns"
+
+	"blitiri.com.ar/go/dnss/internal/testutil"
+)
+
+// Tests for the DNS server.
+
+func TestServe(t *testing.T) {
+	res := testutil.NewTestResolver()
+	res.Response = &dns.Msg{
+		Answer: []dns.RR{testutil.NewRR(t, "response.test. A 1.1.1.1")},
+	}
+
+	unqUpstreamAddr := testutil.GetFreePort()
+	go testutil.ServeTestDNSServer(unqUpstreamAddr,
+		testutil.MakeStaticHandler(t, "unq. A 2.2.2.2"))
+
+	fallbackAddr := testutil.GetFreePort()
+	go testutil.ServeTestDNSServer(fallbackAddr,
+		testutil.MakeStaticHandler(t, "fallback. A 3.3.3.3"))
+
+	srv := New(testutil.GetFreePort(), res, unqUpstreamAddr)
+	srv.SetFallback(fallbackAddr, []string{"one.fallback.", "two.fallback."})
+	go srv.ListenAndServe()
+	testutil.WaitForDNSServer(srv.Addr)
+
+	query(t, srv.Addr, "response.test.", "1.1.1.1")
+	query(t, srv.Addr, "unqualified.", "2.2.2.2")
+	query(t, srv.Addr, "one.fallback.", "3.3.3.3")
+	query(t, srv.Addr, "two.fallback.", "3.3.3.3")
+}
+
+func query(t *testing.T, srv, domain, expected string) {
+	_, rr, err := testutil.DNSQuery(srv, domain, dns.TypeA)
+	if err != nil {
+		t.Errorf("error querying %q: %v", domain, err)
+		return
+	}
+
+	result := rr.(*dns.A).A.String()
+	if result != expected {
+		t.Errorf("query %q: expected %q but got %q", domain, expected, result)
+	}
+}
diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go
index 98e884f..07bada6 100644
--- a/internal/testutil/testutil.go
+++ b/internal/testutil/testutil.go
@@ -143,6 +143,19 @@ func ServeTestDNSServer(addr string, handler func(dns.ResponseWriter, *dns.Msg))
 	panic(err)
 }
 
+// MakeStaticHandler for the DNS server. The given answer must be a valid
+// zone.
+func MakeStaticHandler(tb testing.TB, answer string) func(dns.ResponseWriter, *dns.Msg) {
+	rr := NewRR(tb, answer)
+
+	return func(w dns.ResponseWriter, r *dns.Msg) {
+		m := &dns.Msg{}
+		m.SetReply(r)
+		m.Answer = append(m.Answer, rr)
+		w.WriteMsg(m)
+	}
+}
+
 func NewRR(tb testing.TB, s string) dns.RR {
 	rr, err := dns.NewRR(s)
 	if err != nil {