From cc4e4a0626bf8881e2ae8b2c1e52e09c6308e74e Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 18 Aug 2017 12:57:23 +0100 Subject: [PATCH] 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. --- middleware/autopath/README.md | 1 + middleware/autopath/autopath.go | 36 ++++++------ middleware/autopath/autopath_test.go | 2 +- middleware/autopath/setup.go | 9 ++- middleware/autopath/setup_test.go | 1 + middleware/erratic/README.md | 3 + middleware/erratic/autopath.go | 8 +++ middleware/erratic/setup.go | 3 + test/erratic_autopath_test.go | 85 ++++++++++++++++++++++++++++ 9 files changed, 126 insertions(+), 22 deletions(-) create mode 100644 middleware/erratic/autopath.go create mode 100644 test/erratic_autopath_test.go diff --git a/middleware/autopath/README.md b/middleware/autopath/README.md index 37ed789c6..4f0507891 100644 --- a/middleware/autopath/README.md +++ b/middleware/autopath/README.md @@ -22,6 +22,7 @@ autopath [ZONE..] RESOLV-CONF Currently the following set of middleware has implemented *autopath*: * *kubernetes* +* *erratic* ## Examples diff --git a/middleware/autopath/autopath.go b/middleware/autopath/autopath.go index 2c581eadd..d3a8c84d2 100644 --- a/middleware/autopath/autopath.go +++ b/middleware/autopath/autopath.go @@ -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" } diff --git a/middleware/autopath/autopath_test.go b/middleware/autopath/autopath_test.go index ca1290e26..b99744ab8 100644 --- a/middleware/autopath/autopath_test.go +++ b/middleware/autopath/autopath_test.go @@ -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) } diff --git a/middleware/autopath/setup.go b/middleware/autopath/setup.go index 1855dd440..f9b12d98e 100644 --- a/middleware/autopath/setup.go +++ b/middleware/autopath/setup.go @@ -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) { diff --git a/middleware/autopath/setup_test.go b/middleware/autopath/setup_test.go index b31110ddb..0f086c5bb 100644 --- a/middleware/autopath/setup_test.go +++ b/middleware/autopath/setup_test.go @@ -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 { diff --git a/middleware/erratic/README.md b/middleware/erratic/README.md index e2989da96..a84146fbb 100644 --- a/middleware/erratic/README.md +++ b/middleware/erratic/README.md @@ -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 diff --git a/middleware/erratic/autopath.go b/middleware/erratic/autopath.go new file mode 100644 index 000000000..79b0ec847 --- /dev/null +++ b/middleware/erratic/autopath.go @@ -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.", ""} +} diff --git a/middleware/erratic/setup.go b/middleware/erratic/setup.go index 98db02247..1f4d0942d 100644 --- a/middleware/erratic/setup.go +++ b/middleware/erratic/setup.go @@ -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 } diff --git a/test/erratic_autopath_test.go b/test/erratic_autopath_test.go new file mode 100644 index 000000000..cd9a077d1 --- /dev/null +++ b/test/erratic_autopath_test.go @@ -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) + } +}