mirror of
https://github.com/coredns/coredns.git
synced 2025-12-06 10:25:10 -05:00
Stop importing testing in the main binary (#2479)
* Stop importing testing in the main binary Stop importing "testing" into the main binary: * test/helpers.go imported it; remote that and change function signature * update all tests that use this Signed-off-by: Miek Gieben <miek@miek.nl> * Drop import testing from metrics plugin Signed-off-by: Miek Gieben <miek@miek.nl> * more fiddling Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -2,8 +2,8 @@ package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
@@ -113,29 +113,25 @@ func OPT(bufsize int, do bool) *dns.OPT {
|
||||
}
|
||||
|
||||
// Header test if the header in resp matches the header as defined in tc.
|
||||
func Header(t *testing.T, tc Case, resp *dns.Msg) bool {
|
||||
func Header(tc Case, resp *dns.Msg) error {
|
||||
if resp.Rcode != tc.Rcode {
|
||||
t.Errorf("Rcode is %q, expected %q", dns.RcodeToString[resp.Rcode], dns.RcodeToString[tc.Rcode])
|
||||
return false
|
||||
return fmt.Errorf("Rcode is %q, expected %q", dns.RcodeToString[resp.Rcode], dns.RcodeToString[tc.Rcode])
|
||||
}
|
||||
|
||||
if len(resp.Answer) != len(tc.Answer) {
|
||||
t.Errorf("Answer for %q contained %d results, %d expected", tc.Qname, len(resp.Answer), len(tc.Answer))
|
||||
return false
|
||||
return fmt.Errorf("Answer for %q contained %d results, %d expected", tc.Qname, len(resp.Answer), len(tc.Answer))
|
||||
}
|
||||
if len(resp.Ns) != len(tc.Ns) {
|
||||
t.Errorf("Authority for %q contained %d results, %d expected", tc.Qname, len(resp.Ns), len(tc.Ns))
|
||||
return false
|
||||
return fmt.Errorf("Authority for %q contained %d results, %d expected", tc.Qname, len(resp.Ns), len(tc.Ns))
|
||||
}
|
||||
if len(resp.Extra) != len(tc.Extra) {
|
||||
t.Errorf("Additional for %q contained %d results, %d expected", tc.Qname, len(resp.Extra), len(tc.Extra))
|
||||
return false
|
||||
return fmt.Errorf("Additional for %q contained %d results, %d expected", tc.Qname, len(resp.Extra), len(tc.Extra))
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
// Section tests if the the section in tc matches rr.
|
||||
func Section(t *testing.T, tc Case, sec sect, rr []dns.RR) bool {
|
||||
func Section(tc Case, sec sect, rr []dns.RR) error {
|
||||
section := []dns.RR{}
|
||||
switch sec {
|
||||
case 0:
|
||||
@@ -148,134 +144,112 @@ func Section(t *testing.T, tc Case, sec sect, rr []dns.RR) bool {
|
||||
|
||||
for i, a := range rr {
|
||||
if a.Header().Name != section[i].Header().Name {
|
||||
t.Errorf("RR %d should have a Header Name of %q, but has %q", i, section[i].Header().Name, a.Header().Name)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Header Name of %q, but has %q", i, section[i].Header().Name, a.Header().Name)
|
||||
}
|
||||
// 303 signals: don't care what the ttl is.
|
||||
if section[i].Header().Ttl != 303 && a.Header().Ttl != section[i].Header().Ttl {
|
||||
if _, ok := section[i].(*dns.OPT); !ok {
|
||||
// we check edns0 bufize on this one
|
||||
t.Errorf("RR %d should have a Header TTL of %d, but has %d", i, section[i].Header().Ttl, a.Header().Ttl)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Header TTL of %d, but has %d", i, section[i].Header().Ttl, a.Header().Ttl)
|
||||
}
|
||||
}
|
||||
if a.Header().Rrtype != section[i].Header().Rrtype {
|
||||
t.Errorf("RR %d should have a header rr type of %d, but has %d", i, section[i].Header().Rrtype, a.Header().Rrtype)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a header rr type of %d, but has %d", i, section[i].Header().Rrtype, a.Header().Rrtype)
|
||||
}
|
||||
|
||||
switch x := a.(type) {
|
||||
case *dns.SRV:
|
||||
if x.Priority != section[i].(*dns.SRV).Priority {
|
||||
t.Errorf("RR %d should have a Priority of %d, but has %d", i, section[i].(*dns.SRV).Priority, x.Priority)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Priority of %d, but has %d", i, section[i].(*dns.SRV).Priority, x.Priority)
|
||||
}
|
||||
if x.Weight != section[i].(*dns.SRV).Weight {
|
||||
t.Errorf("RR %d should have a Weight of %d, but has %d", i, section[i].(*dns.SRV).Weight, x.Weight)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Weight of %d, but has %d", i, section[i].(*dns.SRV).Weight, x.Weight)
|
||||
}
|
||||
if x.Port != section[i].(*dns.SRV).Port {
|
||||
t.Errorf("RR %d should have a Port of %d, but has %d", i, section[i].(*dns.SRV).Port, x.Port)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Port of %d, but has %d", i, section[i].(*dns.SRV).Port, x.Port)
|
||||
}
|
||||
if x.Target != section[i].(*dns.SRV).Target {
|
||||
t.Errorf("RR %d should have a Target of %q, but has %q", i, section[i].(*dns.SRV).Target, x.Target)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Target of %q, but has %q", i, section[i].(*dns.SRV).Target, x.Target)
|
||||
}
|
||||
case *dns.RRSIG:
|
||||
if x.TypeCovered != section[i].(*dns.RRSIG).TypeCovered {
|
||||
t.Errorf("RR %d should have a TypeCovered of %d, but has %d", i, section[i].(*dns.RRSIG).TypeCovered, x.TypeCovered)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a TypeCovered of %d, but has %d", i, section[i].(*dns.RRSIG).TypeCovered, x.TypeCovered)
|
||||
}
|
||||
if x.Labels != section[i].(*dns.RRSIG).Labels {
|
||||
t.Errorf("RR %d should have a Labels of %d, but has %d", i, section[i].(*dns.RRSIG).Labels, x.Labels)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Labels of %d, but has %d", i, section[i].(*dns.RRSIG).Labels, x.Labels)
|
||||
}
|
||||
if x.SignerName != section[i].(*dns.RRSIG).SignerName {
|
||||
t.Errorf("RR %d should have a SignerName of %s, but has %s", i, section[i].(*dns.RRSIG).SignerName, x.SignerName)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a SignerName of %s, but has %s", i, section[i].(*dns.RRSIG).SignerName, x.SignerName)
|
||||
}
|
||||
case *dns.NSEC:
|
||||
if x.NextDomain != section[i].(*dns.NSEC).NextDomain {
|
||||
t.Errorf("RR %d should have a NextDomain of %s, but has %s", i, section[i].(*dns.NSEC).NextDomain, x.NextDomain)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a NextDomain of %s, but has %s", i, section[i].(*dns.NSEC).NextDomain, x.NextDomain)
|
||||
}
|
||||
// TypeBitMap
|
||||
case *dns.A:
|
||||
if x.A.String() != section[i].(*dns.A).A.String() {
|
||||
t.Errorf("RR %d should have a Address of %q, but has %q", i, section[i].(*dns.A).A.String(), x.A.String())
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Address of %q, but has %q", i, section[i].(*dns.A).A.String(), x.A.String())
|
||||
}
|
||||
case *dns.AAAA:
|
||||
if x.AAAA.String() != section[i].(*dns.AAAA).AAAA.String() {
|
||||
t.Errorf("RR %d should have a Address of %q, but has %q", i, section[i].(*dns.AAAA).AAAA.String(), x.AAAA.String())
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Address of %q, but has %q", i, section[i].(*dns.AAAA).AAAA.String(), x.AAAA.String())
|
||||
}
|
||||
case *dns.TXT:
|
||||
for j, txt := range x.Txt {
|
||||
if txt != section[i].(*dns.TXT).Txt[j] {
|
||||
t.Errorf("RR %d should have a Txt of %q, but has %q", i, section[i].(*dns.TXT).Txt[j], txt)
|
||||
return false
|
||||
return fmt.Errorf("RR %d should have a Txt of %q, but has %q", i, section[i].(*dns.TXT).Txt[j], txt)
|
||||
}
|
||||
}
|
||||
case *dns.HINFO:
|
||||
if x.Cpu != section[i].(*dns.HINFO).Cpu {
|
||||
t.Errorf("RR %d should have a Cpu of %s, but has %s", i, section[i].(*dns.HINFO).Cpu, x.Cpu)
|
||||
return fmt.Errorf("RR %d should have a Cpu of %s, but has %s", i, section[i].(*dns.HINFO).Cpu, x.Cpu)
|
||||
}
|
||||
if x.Os != section[i].(*dns.HINFO).Os {
|
||||
t.Errorf("RR %d should have a Os of %s, but has %s", i, section[i].(*dns.HINFO).Os, x.Os)
|
||||
return fmt.Errorf("RR %d should have a Os of %s, but has %s", i, section[i].(*dns.HINFO).Os, x.Os)
|
||||
}
|
||||
case *dns.SOA:
|
||||
tt := section[i].(*dns.SOA)
|
||||
if x.Ns != tt.Ns {
|
||||
t.Errorf("SOA nameserver should be %q, but is %q", tt.Ns, x.Ns)
|
||||
return false
|
||||
return fmt.Errorf("SOA nameserver should be %q, but is %q", tt.Ns, x.Ns)
|
||||
}
|
||||
case *dns.PTR:
|
||||
tt := section[i].(*dns.PTR)
|
||||
if x.Ptr != tt.Ptr {
|
||||
t.Errorf("PTR ptr should be %q, but is %q", tt.Ptr, x.Ptr)
|
||||
return false
|
||||
return fmt.Errorf("PTR ptr should be %q, but is %q", tt.Ptr, x.Ptr)
|
||||
}
|
||||
case *dns.CNAME:
|
||||
tt := section[i].(*dns.CNAME)
|
||||
if x.Target != tt.Target {
|
||||
t.Errorf("CNAME target should be %q, but is %q", tt.Target, x.Target)
|
||||
return false
|
||||
return fmt.Errorf("CNAME target should be %q, but is %q", tt.Target, x.Target)
|
||||
}
|
||||
case *dns.MX:
|
||||
tt := section[i].(*dns.MX)
|
||||
if x.Mx != tt.Mx {
|
||||
t.Errorf("MX Mx should be %q, but is %q", tt.Mx, x.Mx)
|
||||
return false
|
||||
return fmt.Errorf("MX Mx should be %q, but is %q", tt.Mx, x.Mx)
|
||||
}
|
||||
if x.Preference != tt.Preference {
|
||||
t.Errorf("MX Preference should be %q, but is %q", tt.Preference, x.Preference)
|
||||
return false
|
||||
return fmt.Errorf("MX Preference should be %q, but is %q", tt.Preference, x.Preference)
|
||||
}
|
||||
case *dns.NS:
|
||||
tt := section[i].(*dns.NS)
|
||||
if x.Ns != tt.Ns {
|
||||
t.Errorf("NS nameserver should be %q, but is %q", tt.Ns, x.Ns)
|
||||
return false
|
||||
return fmt.Errorf("NS nameserver should be %q, but is %q", tt.Ns, x.Ns)
|
||||
}
|
||||
case *dns.OPT:
|
||||
tt := section[i].(*dns.OPT)
|
||||
if x.UDPSize() != tt.UDPSize() {
|
||||
t.Errorf("OPT UDPSize should be %d, but is %d", tt.UDPSize(), x.UDPSize())
|
||||
return false
|
||||
return fmt.Errorf("OPT UDPSize should be %d, but is %d", tt.UDPSize(), x.UDPSize())
|
||||
}
|
||||
if x.Do() != tt.Do() {
|
||||
t.Errorf("OPT DO should be %t, but is %t", tt.Do(), x.Do())
|
||||
return false
|
||||
return fmt.Errorf("OPT DO should be %t, but is %t", tt.Do(), x.Do())
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
// CNAMEOrder makes sure that CNAMES do not appear after their target records
|
||||
func CNAMEOrder(t *testing.T, res *dns.Msg) {
|
||||
func CNAMEOrder(res *dns.Msg) error {
|
||||
for i, c := range res.Answer {
|
||||
if c.Header().Rrtype != dns.TypeCNAME {
|
||||
continue
|
||||
@@ -284,38 +258,32 @@ func CNAMEOrder(t *testing.T, res *dns.Msg) {
|
||||
if a.Header().Name != c.(*dns.CNAME).Target {
|
||||
continue
|
||||
}
|
||||
t.Errorf("CNAME found after target record\n")
|
||||
t.Logf("%v\n", res)
|
||||
|
||||
return fmt.Errorf("CNAME found after target record")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SortAndCheck sorts resp and the checks the header and three sections against the testcase in tc.
|
||||
func SortAndCheck(t *testing.T, resp *dns.Msg, tc Case) {
|
||||
func SortAndCheck(resp *dns.Msg, tc Case) error {
|
||||
sort.Sort(RRSet(resp.Answer))
|
||||
sort.Sort(RRSet(resp.Ns))
|
||||
sort.Sort(RRSet(resp.Extra))
|
||||
|
||||
if !Header(t, tc, resp) {
|
||||
t.Logf("%v\n", resp)
|
||||
return
|
||||
if err := Header(tc, resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !Section(t, tc, Answer, resp.Answer) {
|
||||
t.Logf("%v\n", resp)
|
||||
return
|
||||
if err := Section(tc, Answer, resp.Answer); err != nil {
|
||||
return err
|
||||
}
|
||||
if !Section(t, tc, Ns, resp.Ns) {
|
||||
t.Logf("%v\n", resp)
|
||||
return
|
||||
if err := Section(tc, Ns, resp.Ns); err != nil {
|
||||
return err
|
||||
|
||||
}
|
||||
if !Section(t, tc, Extra, resp.Extra) {
|
||||
t.Logf("%v\n", resp)
|
||||
return
|
||||
if err := Section(tc, Extra, resp.Extra); err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
// ErrorHandler returns a Handler that returns ServerFailure error when called.
|
||||
|
||||
Reference in New Issue
Block a user