coredns/plugin/reverse/reverse_test.go
Miek Gieben 8722336fff
global: move to context (#1699)
* global: move to context

Move from golang.org/x/net/context to std lib's context.

Change done with:

for i in $(grep -l '/context' **/*.go); do sed -e 's|golang.org/x/net/context|context|' -i $i; echo $i; done
for i in **/*.go; do goimports -w $i; done

* drop from dns.pb.go as well
2018-04-20 11:01:06 +01:00

71 lines
1.6 KiB
Go

package reverse
import (
"net"
"regexp"
"testing"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnstest"
"github.com/coredns/coredns/plugin/test"
"context"
"github.com/miekg/dns"
)
func TestReverse(t *testing.T) {
_, net4, _ := net.ParseCIDR("10.1.1.0/24")
regexIP4, _ := regexp.Compile("^.*ip-" + regexMatchV4 + "\\.example\\.org\\.$")
em := Reverse{
Networks: networks{network{
IPnet: net4,
Zone: "example.org.",
Template: "ip-{ip}.example.org.",
RegexMatchIP: regexIP4,
}},
}
tests := []struct {
next plugin.Handler
qname string
qtype uint16
expectedCode int
expectedReply string
expectedErr error
}{
{
next: test.NextHandler(dns.RcodeSuccess, nil),
qname: "test.ip-10.1.1.2.example.org.",
expectedCode: dns.RcodeSuccess,
expectedReply: "10.1.1.2",
expectedErr: nil,
},
}
ctx := context.TODO()
for i, tr := range tests {
req := new(dns.Msg)
tr.qtype = dns.TypeA
req.SetQuestion(tr.qname, tr.qtype)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
code, err := em.ServeDNS(ctx, rec, req)
if err != tr.expectedErr {
t.Errorf("Test %d: Expected error %v, but got %v", i, tr.expectedErr, err)
}
if code != int(tr.expectedCode) {
t.Errorf("Test %d: Expected status code %d, but got %d", i, tr.expectedCode, code)
}
if tr.expectedReply != "" {
answer := rec.Msg.Answer[0].(*dns.A).A.String()
if answer != tr.expectedReply {
t.Errorf("Test %d: Expected answer %s, but got %s", i, tr.expectedReply, answer)
}
}
}
}