middleware/debug: add (#735)

* middleware/debug: add

Add a debug "middleware" that disables the recover() and just lets
CoreDNS crash; very useful for testing.

Fixes ##563

* fix test

* Feedback: check the value of Debug
This commit is contained in:
Miek Gieben 2017-06-13 15:47:17 -07:00 committed by Pat Moroney
parent 46bf7f3106
commit 8e86fa6f23
8 changed files with 137 additions and 28 deletions

View file

@ -23,6 +23,9 @@ type Config struct {
// First consumer is the file middleware to looks for zone files in this place. // First consumer is the file middleware to looks for zone files in this place.
Root string Root string
// Debug controls the panic/recover mechanism that is enabled by default.
Debug bool
// The transport we implement, normally just "dns" over TCP/UDP, but could be // The transport we implement, normally just "dns" over TCP/UDP, but could be
// DNS-over-TLS or DNS-over-gRPC. // DNS-over-TLS or DNS-over-gRPC.
Transport string Transport string

View file

@ -36,6 +36,7 @@ type Server struct {
dnsWg sync.WaitGroup // used to wait on outstanding connections dnsWg sync.WaitGroup // used to wait on outstanding connections
connTimeout time.Duration // the maximum duration of a graceful shutdown connTimeout time.Duration // the maximum duration of a graceful shutdown
trace trace.Trace // the trace middleware for the server trace trace.Trace // the trace middleware for the server
debug bool // disable recover()
} }
// NewServer returns a new CoreDNS server and compiles all middleware in to it. // NewServer returns a new CoreDNS server and compiles all middleware in to it.
@ -56,6 +57,9 @@ func NewServer(addr string, group []*Config) (*Server, error) {
s.dnsWg.Add(1) s.dnsWg.Add(1)
for _, site := range group { for _, site := range group {
if site.Debug {
s.debug = true
}
// set the config per zone // set the config per zone
s.zones[site.Zone] = site s.zones[site.Zone] = site
// compile custom middleware for everything // compile custom middleware for everything
@ -166,13 +170,15 @@ func (s *Server) Address() string { return s.Addr }
// defined in the request so that the correct zone // defined in the request so that the correct zone
// (configuration and middleware stack) will handle the request. // (configuration and middleware stack) will handle the request.
func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) { func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) {
defer func() { if !s.debug {
// In case the user doesn't enable error middleware, we still defer func() {
// need to make sure that we stay alive up here // In case the user doesn't enable error middleware, we still
if rec := recover(); rec != nil { // need to make sure that we stay alive up here
DefaultErrorFunc(w, r, dns.RcodeServerFailure) if rec := recover(); rec != nil {
} DefaultErrorFunc(w, r, dns.RcodeServerFailure)
}() }
}()
}
if m, err := edns.Version(r); err != nil { // Wrong EDNS version, return at once. if m, err := edns.Version(r); err != nil { // Wrong EDNS version, return at once.
w.WriteMsg(m) w.WriteMsg(m)

View file

@ -14,6 +14,7 @@ var directives = []string{
"tls", "tls",
"root", "root",
"bind", "bind",
"debug",
"trace", "trace",
"health", "health",
"pprof", "pprof",

View file

@ -8,6 +8,7 @@ import (
_ "github.com/coredns/coredns/middleware/bind" _ "github.com/coredns/coredns/middleware/bind"
_ "github.com/coredns/coredns/middleware/cache" _ "github.com/coredns/coredns/middleware/cache"
_ "github.com/coredns/coredns/middleware/chaos" _ "github.com/coredns/coredns/middleware/chaos"
_ "github.com/coredns/coredns/middleware/debug"
_ "github.com/coredns/coredns/middleware/dnssec" _ "github.com/coredns/coredns/middleware/dnssec"
_ "github.com/coredns/coredns/middleware/erratic" _ "github.com/coredns/coredns/middleware/erratic"
_ "github.com/coredns/coredns/middleware/errors" _ "github.com/coredns/coredns/middleware/errors"

View file

@ -22,26 +22,27 @@
1:tls:tls 1:tls:tls
10:root:root 10:root:root
20:bind:bind 20:bind:bind
30:trace:trace 30:debug:debug
40:health:health 40:trace:trace
50:pprof:pprof 50:health:health
60:prometheus:metrics 60:pprof:pprof
70:errors:errors 70:prometheus:metrics
80:log:log 80:errors:errors
90:chaos:chaos 90:log:log
100:cache:cache 100:chaos:chaos
110:rewrite:rewrite 110:cache:cache
120:loadbalance:loadbalance 120:rewrite:rewrite
130:dnssec:dnssec 130:loadbalance:loadbalance
140:reverse:reverse 140:dnssec:dnssec
150:hosts:hosts 150:reverse:reverse
160:kubernetes:kubernetes 160:hosts:hosts
170:file:file 170:kubernetes:kubernetes
180:auto:auto 180:file:file
190:secondary:secondary 190:auto:auto
200:etcd:etcd 200:secondary:secondary
210:proxy:proxy 210:etcd:etcd
220:whoami:whoami 220:proxy:proxy
230:erratic:erratic 230:whoami:whoami
240:erratic:erratic
500:startup:github.com/mholt/caddy/startupshutdown 500:startup:github.com/mholt/caddy/startupshutdown
510:shutdown:github.com/mholt/caddy/startupshutdown 510:shutdown:github.com/mholt/caddy/startupshutdown

View file

@ -0,0 +1,20 @@
# debug
*debug* disables the automatic recovery upon a CoreDNS crash so that you'll get a
nice stack trace.
Note that the *errors* middleware (if loaded) will also set a `recover` negating this setting.
The main use of *debug* is to help testing.
## Syntax
~~~ txt
debug
~~~
## Examples
Disable CoreDNS' ability to recover from crashes:
~~~ txt
debug
~~~

28
middleware/debug/debug.go Normal file
View file

@ -0,0 +1,28 @@
package debug
import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/middleware"
"github.com/mholt/caddy"
)
func init() {
caddy.RegisterPlugin("debug", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
config := dnsserver.GetConfig(c)
for c.Next() {
if c.NextArg() {
return middleware.Error("debug", c.ArgErr())
}
config.Debug = true
}
return nil
}

View file

@ -0,0 +1,49 @@
package debug
import (
"io/ioutil"
"log"
"testing"
"github.com/coredns/coredns/core/dnsserver"
"github.com/mholt/caddy"
)
func TestDebug(t *testing.T) {
log.SetOutput(ioutil.Discard)
tests := []struct {
input string
shouldErr bool
expectedDebug bool
}{
// positive
{
`debug`, false, true,
},
// negative
{
`debug off`, true, false,
},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.input)
err := setup(c)
cfg := dnsserver.GetConfig(c)
if test.shouldErr && err == nil {
t.Fatalf("Test %d: Expected error but found %s for input %s", i, err, test.input)
}
if err != nil {
if !test.shouldErr {
t.Fatalf("Test %d: Expected no error but found one for input %s. Error was: %v", i, test.input, err)
}
}
if cfg.Debug != test.expectedDebug {
t.Fatalf("Test %d: Expected debug to be: %t, but got: %t, input: %s", i, test.expectedDebug, test.input)
}
}
}