Commit 284061ee authored by Miek Gieben's avatar Miek Gieben Committed by GitHub

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.
parent 7109c671
......@@ -4,7 +4,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -49,7 +49,7 @@ func TestAutoPath(t *testing.T) {
for _, tc := range autopathTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := ap.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......@@ -94,7 +94,7 @@ func TestAutoPathNoAnswer(t *testing.T) {
for _, tc := range autopathNoAnswerTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rcode, err := ap.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -7,7 +7,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/cache"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -29,7 +29,7 @@ func TestPrefetch(t *testing.T) {
req := new(dns.Msg)
req.SetQuestion("lowttl.example.org.", dns.TypeA)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
c.ServeDNS(ctx, rec, req)
p = true // prefetch should be true for the 2nd fetch
......
......@@ -4,7 +4,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -59,7 +59,7 @@ func TestChaos(t *testing.T) {
req.Question[0].Qclass = dns.ClassCHAOS
em.Next = tc.next
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := em.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
......
......@@ -6,7 +6,7 @@ import (
"github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/cache"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -95,7 +95,7 @@ func TestLookupZone(t *testing.T) {
for _, tc := range dnsTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := dh.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......@@ -118,7 +118,7 @@ func TestLookupDNSKEY(t *testing.T) {
for _, tc := range dnssecTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := dh.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -3,7 +3,7 @@ package erratic
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"
......@@ -28,7 +28,7 @@ func TestErraticDrop(t *testing.T) {
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeA)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := e.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
......@@ -62,7 +62,7 @@ func TestErraticTruncate(t *testing.T) {
req := new(dns.Msg)
req.SetQuestion("example.org.", dns.TypeA)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := e.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
......
......@@ -9,7 +9,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -48,7 +48,7 @@ func TestErrors(t *testing.T) {
for i, tc := range tests {
em.Next = tc.next
buf.Reset()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := em.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
......
......@@ -8,7 +8,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -25,7 +25,7 @@ func TestCnameLookup(t *testing.T) {
for _, tc := range dnsTestCasesCname {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := etc.ServeDNS(ctxt, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -6,7 +6,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -22,7 +22,7 @@ func TestGroupLookup(t *testing.T) {
for _, tc := range dnsTestCasesGroup {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := etc.ServeDNS(ctxt, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -9,7 +9,7 @@ import (
"time"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/singleflight"
"github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/proxy"
......@@ -262,7 +262,7 @@ func TestLookup(t *testing.T) {
for _, tc := range dnsTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
etc.ServeDNS(ctxt, rec, m)
resp := rec.Msg
......
......@@ -6,7 +6,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -25,7 +25,7 @@ func TestMultiLookup(t *testing.T) {
for _, tc := range dnsTestCasesMulti {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := etc.ServeDNS(ctxt, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -10,7 +10,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -26,7 +26,7 @@ func TestOtherLookup(t *testing.T) {
for _, tc := range dnsTestCasesOther {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := etc.ServeDNS(ctxt, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -8,7 +8,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -51,7 +51,7 @@ func TestStubLookup(t *testing.T) {
for _, tc := range dnsTestCasesStub {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := etc.ServeDNS(ctxt, rec, m)
if err != nil && m.Question[0].Name == "example.org." {
// This is OK, we expect this backend to *not* work.
......
......@@ -4,7 +4,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin/kubernetes"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -68,7 +68,7 @@ func TestFederationKubernetes(t *testing.T) {
for i, tc := range tests {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fed.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Test %d, expected no error, got %v\n", i, err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"testing"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/proxy"
"github.com/coredns/coredns/plugin/test"
......@@ -25,7 +25,7 @@ func TestLookupCNAMEChain(t *testing.T) {
for _, tc := range cnameTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......@@ -82,7 +82,7 @@ func TestLookupCNAMEExternal(t *testing.T) {
for _, tc := range exernalTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -162,7 +162,7 @@ func testDelegation(t *testing.T, z, origin string, testcases []test.Case) {
for _, tc := range testcases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %q\n", err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -101,7 +101,7 @@ func TestLookupDNAME(t *testing.T) {
for _, tc := range dnameTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......@@ -154,7 +154,7 @@ func TestLookupDNAMEDNSSEC(t *testing.T) {
for _, tc := range dnameDnssecTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -138,7 +138,7 @@ func TestLookupDNSSEC(t *testing.T) {
for _, tc := range dnssecTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......@@ -158,7 +158,7 @@ func BenchmarkFileLookupDNSSEC(b *testing.B) {
fm := File{Next: test.ErrorHandler(), Zones: Zones{Z: map[string]*Zone{testzone: zone}, Names: []string{testzone}}}
ctx := context.TODO()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
tc := test.Case{
Qname: "b.miek.nl.", Qtype: dns.TypeA, Do: true,
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -62,7 +62,7 @@ func TestLookupDS(t *testing.T) {
for _, tc := range dsTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -42,7 +42,7 @@ func TestLookupEnt(t *testing.T) {
for _, tc := range entTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -44,7 +44,7 @@ func TestLookupGlue(t *testing.T) {
for _, tc := range atoomTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -114,7 +114,7 @@ func TestLookup(t *testing.T) {
for _, tc := range dnsTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("expected no error, got %v\n", err)
......@@ -131,7 +131,7 @@ func TestLookupNil(t *testing.T) {
ctx := context.TODO()
m := dnsTestCases[0].Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
fm.ServeDNS(ctx, rec, m)
}
......@@ -143,7 +143,7 @@ func BenchmarkFileLookup(b *testing.B) {
fm := File{Next: test.ErrorHandler(), Zones: Zones{Z: map[string]*Zone{testzone: zone}, Names: []string{testzone}}}
ctx := context.TODO()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
tc := test.Case{
Qname: "www.miek.nl.", Qtype: dns.TypeA,
......
......@@ -4,7 +4,7 @@ import (
"strings"
"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"
......@@ -89,7 +89,7 @@ func TestLookupWildcard(t *testing.T) {
for _, tc := range wildcardTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......@@ -150,7 +150,7 @@ func TestLookupDoubleWildcard(t *testing.T) {
for _, tc := range wildcardDoubleTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......@@ -205,7 +205,7 @@ func TestLookupApexWildcard(t *testing.T) {
for _, tc := range apexWildcardTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......@@ -248,7 +248,7 @@ func TestLookupMultiWildcard(t *testing.T) {
for _, tc := range multiWildcardTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := fm.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......
......@@ -5,7 +5,7 @@ import (
"testing"
"time"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -21,7 +21,7 @@ func TestLookupA(t *testing.T) {
for _, tc := range hostsTestCases {
m := tc.Msg()
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := h.ServeDNS(ctx, rec, m)
if err != nil {
t.Errorf("Expected no error, got %v\n", err)
......
......@@ -3,7 +3,7 @@ package kubernetes
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"
......@@ -40,7 +40,7 @@ func TestServeDNSModeDisabled(t *testing.T) {
for i, tc := range podModeDisabledCases {
r := tc.Msg()
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := k.ServeDNS(ctx, w, r)
if err != tc.Error {
......
......@@ -3,7 +3,7 @@ package kubernetes
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"
......@@ -38,7 +38,7 @@ func TestServeDNSModeInsecure(t *testing.T) {
for i, tc := range podModeInsecureCases {
r := tc.Msg()
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := k.ServeDNS(ctx, w, r)
if err != tc.Error {
......
......@@ -3,7 +3,7 @@ package kubernetes
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"
......@@ -38,7 +38,7 @@ func TestServeDNSModeVerified(t *testing.T) {
for i, tc := range podModeVerifiedCases {
r := tc.Msg()
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := k.ServeDNS(ctx, w, r)
if err != tc.Error {
......
......@@ -3,7 +3,7 @@ package kubernetes
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"
......@@ -165,7 +165,7 @@ func TestServeDNS(t *testing.T) {
for i, tc := range dnsTestCases {
r := tc.Msg()
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := k.ServeDNS(ctx, w, r)
if err != tc.Error {
......
......@@ -3,7 +3,7 @@ package kubernetes
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"
......@@ -47,7 +47,7 @@ func TestServeDNSApex(t *testing.T) {
for i, tc := range kubeApexCases {
r := tc.Msg()
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := k.ServeDNS(ctx, w, r)
if err != tc.Error {
......
......@@ -3,7 +3,7 @@ package kubernetes
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"
......@@ -108,7 +108,7 @@ func TestReverse(t *testing.T) {
for i, tc := range tests {
r := tc.Msg()
w := dnsrecorder.New(&test.ResponseWriter{})
w := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := k.ServeDNS(ctx, w, r)
if err != tc.Error {
......
......@@ -4,7 +4,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -79,7 +79,7 @@ func TestLoadBalance(t *testing.T) {
},
}
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
for i, test := range tests {
req := new(dns.Msg)
......
......@@ -7,7 +7,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics/vars"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/rcode"
"github.com/coredns/coredns/plugin/pkg/replacer"
"github.com/coredns/coredns/plugin/pkg/response"
......@@ -32,7 +32,7 @@ func (l Logger) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
continue
}
rrw := dnsrecorder.New(w)
rrw := dnstest.NewRecorder(w)
rc, err := plugin.NextOrFailure(l.Name(), l.Next, ctx, rrw, r)
if rc > 0 {
......
......@@ -6,7 +6,7 @@ import (
"strings"
"testing"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/response"
"github.com/coredns/coredns/plugin/test"
......@@ -31,7 +31,7 @@ func TestLoggedStatus(t *testing.T) {
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rcode, _ := logger.ServeDNS(ctx, rec, r)
if rcode != 0 {
......@@ -62,7 +62,7 @@ func TestLoggedClassDenial(t *testing.T) {
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
logger.ServeDNS(ctx, rec, r)
......@@ -90,7 +90,7 @@ func TestLoggedClassError(t *testing.T) {
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
logger.ServeDNS(ctx, rec, r)
......
......@@ -3,7 +3,7 @@ package metrics
import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics/vars"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/pkg/rcode"
"github.com/coredns/coredns/request"
......@@ -22,7 +22,7 @@ func (m *Metrics) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}
// Record response to get status code and size of the reply.
rw := dnsrecorder.New(w)
rw := dnstest.NewRecorder(w)
status, err := plugin.NextOrFailure(m.Name(), m.Next, ctx, rw, r)
vars.Report(state, zone, rcode.ToString(rw.Rcode), rw.Len, rw.Start)
......
......@@ -5,7 +5,7 @@ import (
"github.com/coredns/coredns/plugin"
mtest "github.com/coredns/coredns/plugin/metrics/test"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -65,7 +65,7 @@ func TestMetrics(t *testing.T) {
req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
met.Next = tc.next
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
_, err := met.ServeDNS(ctx, rec, req)
if err != nil {
t.Fatalf("Test %d: Expected no error, but got %s", i, err)
......
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())
}
}
*/
// 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 }
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)
}
}
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()
}
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)
}
}
......@@ -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{
......
......@@ -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)
......
......@@ -6,7 +6,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -52,7 +52,7 @@ func TestReverse(t *testing.T) {
tr.qtype = dns.TypeA
req.SetQuestion(dns.Fqdn(tr.qname), tr.qtype)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := em.ServeDNS(ctx, rec, req)
if err != tr.expectedErr {
......
......@@ -6,7 +6,7 @@ import (
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsrecorder"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
......@@ -180,7 +180,7 @@ func TestRewrite(t *testing.T) {
m.SetQuestion(tc.from, tc.fromT)
m.Question[0].Qclass = tc.fromC
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
......@@ -252,7 +252,7 @@ func TestRewriteEDNS0Local(t *testing.T) {
}
rw.Rules = []Rule{r}
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
......@@ -307,7 +307,7 @@ func TestEdns0LocalMultiRule(t *testing.T) {
}
o.Option = append(o.Option, tc.fromOpts...)
}
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
......@@ -443,7 +443,7 @@ func TestRewriteEDNS0LocalVariable(t *testing.T) {
}
rw.Rules = []Rule{r}
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
......@@ -553,7 +553,7 @@ func TestRewriteEDNS0Subnet(t *testing.T) {
continue
}
rw.Rules = []Rule{r}
rec := dnsrecorder.New(tc.writer)
rec := dnstest.NewRecorder(tc.writer)
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
......
......@@ -3,7 +3,7 @@ package whoami
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"
......@@ -35,7 +35,7 @@ func TestWhoami(t *testing.T) {
req := new(dns.Msg)
req.SetQuestion(dns.Fqdn(tc.qname), tc.qtype)
rec := dnsrecorder.New(&test.ResponseWriter{})
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := wh.ServeDNS(ctx, rec, req)
if err != tc.expectedErr {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment