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

@ -22,6 +22,7 @@ autopath [ZONE..] RESOLV-CONF
Currently the following set of middleware has implemented *autopath*:
* *kubernetes*
* *erratic*
## Examples

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" }

View file

@ -158,7 +158,7 @@ func TestInSearchPath(t *testing.T) {
{"a.b.svc.cluster.local.", false},
}
for i, tc := range tests {
got := a.FirstInSearchPath(tc.qname)
got := firstInSearchPath(tc.qname, a.search)
if got != tc.b {
t.Errorf("Test %d, got %v, expected %v", i, got, tc.b)
}

View file

@ -5,6 +5,7 @@ import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/middleware"
"github.com/coredns/coredns/middleware/erratic"
"github.com/coredns/coredns/middleware/kubernetes"
"github.com/mholt/caddy"
@ -32,8 +33,11 @@ func setup(c *caddy.Controller) error {
if m == nil {
return nil
}
if k, ok := m.(kubernetes.Kubernetes); ok {
ap.searchFunc = k.AutoPath
if x, ok := m.(kubernetes.Kubernetes); ok {
ap.searchFunc = x.AutoPath
}
if x, ok := m.(*erratic.Erratic); ok {
ap.searchFunc = x.AutoPath
}
return nil
})
@ -50,6 +54,7 @@ func setup(c *caddy.Controller) error {
// need to register themselves with dnsserver.RegisterHandler.
var allowedMiddleware = map[string]bool{
"@kubernetes": true,
"@erratic": true,
}
func autoPathParse(c *caddy.Controller) (*AutoPath, string, error) {

View file

@ -33,6 +33,7 @@ func TestSetupAutoPath(t *testing.T) {
{`autopath ` + resolv, false, "", "", []string{"bar.com.", "baz.com.", ""}, ""},
// negative
{`autopath kubernetes`, true, "", "", nil, "open kubernetes: no such file or directory"},
{`autopath`, true, "", "", nil, "no resolv-conf"},
}
for i, test := range tests {

View file

@ -7,6 +7,9 @@ The *erratic* middleware will respond to every A or AAAA query. For any other ty
a SERVFAIL response. The reply for A will return 192.0.2.53 (see RFC 5737), for AAAA it returns
2001:DB8::53 (see RFC 3849).
*erratic* can also be used in conjunction with the *autopath* middleware. This is mostly to aid in
testing.
## Syntax
~~~ txt

View file

@ -0,0 +1,8 @@
package erratic
import "github.com/coredns/coredns/request"
// AutoPath implements the AutoPathFunc call from the autopath middleware.
func (e *Erratic) AutoPath(state request.Request) []string {
return []string{"a.example.org.", "b.example.org.", ""}
}

View file

@ -28,6 +28,9 @@ func setupErratic(c *caddy.Controller) error {
return e
})
// Also register erratic for use in autopath.
dnsserver.GetConfig(c).RegisterHandler(e)
return nil
}

View file

@ -0,0 +1,85 @@
package test
import (
"testing"
"github.com/miekg/dns"
)
func TestLookupAutoPathErratic(t *testing.T) {
corefile := `.:0 {
erratic
autopath @erratic
proxy . 8.8.8.8:53
debug
}
`
i, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
udp, _ := CoreDNSServerPorts(i, 0)
if udp == "" {
t.Fatalf("Could not get UDP listening port")
}
defer i.Stop()
tests := []struct {
qname string
expectedAnswer string
expectedType uint16
}{
{"google.com.a.example.org.", "google.com.a.example.org.", dns.TypeCNAME},
{"google.com.", "google.com.", dns.TypeA},
}
for i, tc := range tests {
m := new(dns.Msg)
// erratic always returns this search path: "a.example.org.", "b.example.org.", "".
m.SetQuestion(tc.qname, dns.TypeA)
r, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Test %d, failed to sent query: %q", i, err)
}
if len(r.Answer) == 0 {
t.Fatalf("Test %d, answer section should have RRs", i)
}
if x := r.Answer[0].Header().Name; x != tc.expectedAnswer {
t.Fatalf("Test %d, expected answer %s, got %s", i, tc.expectedAnswer, x)
}
if x := r.Answer[0].Header().Rrtype; x != tc.expectedType {
t.Fatalf("Test %d, expected answer type %d, got %d", i, tc.expectedType, x)
}
}
}
func TestAutoPathErraticNotLoaded(t *testing.T) {
corefile := `.:0 {
autopath @erratic
proxy . 8.8.8.8:53
debug
}
`
i, err := CoreDNSServer(corefile)
if err != nil {
t.Fatalf("Could not get CoreDNS serving instance: %s", err)
}
udp, _ := CoreDNSServerPorts(i, 0)
if udp == "" {
t.Fatalf("Could not get UDP listening port")
}
defer i.Stop()
m := new(dns.Msg)
m.SetQuestion("google.com.a.example.org.", dns.TypeA)
r, err := dns.Exchange(m, udp)
if err != nil {
t.Fatalf("Failed to sent query: %q", err)
}
if r.Rcode != dns.RcodeNameError {
t.Fatalf("Expected NXDOMAIN, got %d", r.Rcode)
}
}