mw/autopath: integration test with erratic (#930)

Add integration test with erratic. For this erratic now also returns an
autopath searchpath. This tests the whole chain; i.e registring a
searchfunction and calling that from autopath.

This tests does a autopathing domain and a non-autopathing one.
This commit is contained in:
Miek Gieben
2017-08-18 12:57:23 +01:00
committed by GitHub
parent 5a1875120c
commit cc4e4a0626
9 changed files with 126 additions and 22 deletions

View File

@@ -33,7 +33,7 @@ func (m Middleware ) AutoPath(state request.Request) []string {
*/
import (
"errors"
"log"
"github.com/coredns/coredns/middleware"
"github.com/coredns/coredns/middleware/pkg/dnsutil"
@@ -61,35 +61,33 @@ type AutoPath struct {
// ServeDNS implements the middleware.Handle interface.
func (a *AutoPath) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := request.Request{W: w, Req: r}
if state.QClass() != dns.ClassINET {
return dns.RcodeServerFailure, middleware.Error(a.Name(), errors.New("can only deal with ClassINET"))
}
zone := middleware.Zones(a.Zones).Matches(state.Name())
if zone == "" {
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
// Check if autopath should be done, searchFunc takes precedence over the local configured
// search path.
// Check if autopath should be done, searchFunc takes precedence over the local configured search path.
var err error
searchpath := a.search
if a.searchFunc != nil {
searchpath = a.searchFunc(state)
if len(searchpath) == 0 {
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
}
match := a.FirstInSearchPath(state.Name())
if !match {
if len(searchpath) == 0 {
log.Printf("[WARNING] No search path available for autopath")
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
if !firstInSearchPath(state.Name(), searchpath) {
return middleware.NextOrFailure(a.Name(), a.Next, ctx, w, r)
}
origQName := state.QName()
// Establish base name of the query. I.e what was originally asked.
base, err := dnsutil.TrimZone(state.QName(), a.search[0]) // TODO(miek): we loose the original case of the query here.
base, err := dnsutil.TrimZone(state.QName(), searchpath[0]) // TODO(miek): we loose the original case of the query here.
if err != nil {
return dns.RcodeServerFailure, err
}
@@ -140,16 +138,16 @@ func (a *AutoPath) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
return firstRcode, firstErr
}
// FirstInSearchPath checks if name is equal to are a sibling of the first element in the search path.
func (a *AutoPath) FirstInSearchPath(name string) bool {
if name == a.search[0] {
// Name implements the Handler interface.
func (a *AutoPath) Name() string { return "autopath" }
// firstInSearchPath checks if name is equal to are a sibling of the first element in the search path.
func firstInSearchPath(name string, searchpath []string) bool {
if name == searchpath[0] {
return true
}
if dns.IsSubDomain(a.search[0], name) {
if dns.IsSubDomain(searchpath[0], name) {
return true
}
return false
}
// Name implements the Handler interface.
func (a *AutoPath) Name() string { return "autopath" }