coredns/test/rewrite_test.go
Miek Gieben f3134da45e
Clean up tests logging (#1979)
* Clean up tests logging

This cleans up the travis logs so you can see the failures better.

Older tests in tests/ would call log.SetOutput(ioutil.Discard) in
a haphazard way. This add log.Discard and put an `init` function in each
package's dir (no way to do this globally). The cleanup in tests/ is
clear.

All plugins also got this init function to have some uniformity and kill
any (future) logging there in the tests as well.

There is a one-off in pkg/healthcheck because that does log.

Signed-off-by: Miek Gieben <miek@miek.nl>

* bring back original log_test.go

Signed-off-by: Miek Gieben <miek@miek.nl>

* suppress logging here as well

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-07-19 16:23:06 +01:00

86 lines
2.1 KiB
Go

package test
import (
"bytes"
"testing"
"github.com/miekg/dns"
)
func TestRewrite(t *testing.T) {
t.Parallel()
corefile := `.:0 {
rewrite type MX a
rewrite edns0 local set 0xffee hello-world
erratic . {
drop 0
}
}`
i, udp, _, err := CoreDNSServerAndPorts(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
defer i.Stop()
testMX(t, udp)
testEdns0(t, udp)
}
func testMX(t *testing.T, server string) {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeMX)
r, err := dns.Exchange(m, server)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
// expect answer section with A record in it
if len(r.Answer) == 0 {
t.Error("Expected to at least one RR in the answer section, got none")
}
if r.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected RR to A, got: %d", r.Answer[0].Header().Rrtype)
}
if r.Answer[0].(*dns.A).A.String() != "192.0.2.53" {
t.Errorf("Expected 192.0.2.53, got: %s", r.Answer[0].(*dns.A).A.String())
}
}
func testEdns0(t *testing.T, server string) {
m := new(dns.Msg)
m.SetQuestion("example.com.", dns.TypeA)
r, err := dns.Exchange(m, server)
if err != nil {
t.Fatalf("Expected to receive reply, but didn't: %s", err)
}
// expect answer section with A record in it
if len(r.Answer) == 0 {
t.Error("Expected to at least one RR in the answer section, got none")
}
if r.Answer[0].Header().Rrtype != dns.TypeA {
t.Errorf("Expected RR to A, got: %d", r.Answer[0].Header().Rrtype)
}
if r.Answer[0].(*dns.A).A.String() != "192.0.2.53" {
t.Errorf("Expected 192.0.2.53, got: %s", r.Answer[0].(*dns.A).A.String())
}
o := r.IsEdns0()
if o == nil || len(o.Option) == 0 {
t.Error("Expected EDNS0 options but got none")
} else {
if e, ok := o.Option[0].(*dns.EDNS0_LOCAL); ok {
if e.Code != 0xffee {
t.Errorf("Expected EDNS_LOCAL code 0xffee but got %x", e.Code)
}
if !bytes.Equal(e.Data, []byte("hello-world")) {
t.Errorf("Expected EDNS_LOCAL data 'hello-world' but got %q", e.Data)
}
} else {
t.Errorf("Expected EDNS0_LOCAL but got %v", o.Option[0])
}
}
}