Cleanups and tests (#272)

For some reasons there was a dnsserver/middleware.go that defined
the middleware handlers. This code was a repeat from
middleware/middleware.go. Removed dnsserver/middleware.go and replaced
all uses of dnsserver.Middleware with middleware.Middleware.

Added dnsserver/address_test.go to test the zone normalization (and to
improve the test coverage). The deleted file will also improve the test
coverage :)
This commit is contained in:
Miek Gieben
2016-09-19 11:26:00 +01:00
committed by GitHub
parent 1e706b5f21
commit 8555716046
20 changed files with 58 additions and 81 deletions

View File

@@ -18,6 +18,8 @@ func (z zoneAddr) String() string { return z.Zone + ":" + z.Port }
// normalizeZone parses an zone string into a structured format with separate
// host, and port portions, as well as the original input string.
//
// TODO(miek): possibly move this to middleware/normalize.go
func normalizeZone(str string) (zoneAddr, error) {
var err error

View File

@@ -0,0 +1,28 @@
package dnsserver
import "testing"
func TestNormalizeZone(t *testing.T) {
for i, test := range []struct {
input string
expected string
shouldErr bool
}{
{".", ".:53", false},
{".:54", ".:54", false},
{"..", ":", true},
{"..", ":", true},
} {
addr, err := normalizeZone(test.input)
actual := addr.String()
if test.shouldErr && err == nil {
t.Errorf("Test %d: Expected error, but there wasn't any", i)
}
if !test.shouldErr && err != nil {
t.Errorf("Test %d: Expected no error, but there was one: %v", i, err)
}
if actual != test.expected {
t.Errorf("Test %d: Expected %s but got %s", i, test.expected, actual)
}
}
}

View File

@@ -1,6 +1,10 @@
package dnsserver
import "github.com/mholt/caddy"
import (
"github.com/miekg/coredns/middleware"
"github.com/mholt/caddy"
)
// Config configuration for a single server.
type Config struct {
@@ -13,14 +17,11 @@ type Config struct {
// The port to listen on.
Port string
// The directory from which to parse db files, and store keys.
Root string
// Middleware stack.
Middleware []Middleware
Middleware []middleware.Middleware
// Compiled middleware stack.
middlewareChain Handler
middlewareChain middleware.Handler
}
// GetConfig gets the Config that corresponds to c.
@@ -33,6 +34,6 @@ func GetConfig(c *caddy.Controller) *Config {
// we should only get here during tests because directive
// actions typically skip the server blocks where we make
// the configs.
ctx.saveConfig(c.Key, &Config{Root: Root})
ctx.saveConfig(c.Key, &Config{})
return GetConfig(c)
}

View File

@@ -1,52 +0,0 @@
package dnsserver
import (
"github.com/miekg/dns"
"golang.org/x/net/context"
)
type (
// Middleware is the middle layer which represents the traditional
// idea of middleware: it chains one Handler to the next by being
// passed the next Handler in the chain.
Middleware func(Handler) Handler
// Handler is like dns.Handler except ServeDNS may return an rcode
// and/or error.
//
// If ServeDNS writes to the response body, it should return a status
// code. If the status code is not one of the following:
// * SERVFAIL (dns.RcodeServerFailure)
// * REFUSED (dns.RecodeRefused)
// * FORMERR (dns.RcodeFormatError)
// * NOTIMP (dns.RcodeNotImplemented)
//
// CoreDNS assumes *no* reply has yet been written. All other response
// codes signal other handlers above it that the response message is
// already written, and that they should not write to it also.
//
// If ServeDNS encounters an error, it should return the error value
// so it can be logged by designated error-handling middleware.
//
// If writing a response after calling another ServeDNS method, the
// returned rcode SHOULD be used when writing the response.
//
// If handling errors after calling another ServeDNS method, the
// returned error value SHOULD be logged or handled accordingly.
//
// Otherwise, return values should be propagated down the middleware
// chain by returning them unchanged.
Handler interface {
ServeDNS(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)
}
// HandlerFunc is a convenience type like dns.HandlerFunc, except
// ServeDNS returns an rcode and an error. See Handler
// documentation for more information.
HandlerFunc func(context.Context, dns.ResponseWriter, *dns.Msg) (int, error)
)
// ServeDNS implements the Handler interface.
func (f HandlerFunc) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
return f(ctx, w, r)
}

View File

@@ -6,6 +6,8 @@ import (
"net"
"time"
"github.com/miekg/coredns/middleware"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyfile"
)
@@ -96,7 +98,7 @@ func (h *dnsContext) MakeServers() ([]caddy.Server, error) {
}
// AddMiddleware adds a middleware to a site's middleware stack.
func (c *Config) AddMiddleware(m Middleware) {
func (c *Config) AddMiddleware(m middleware.Middleware) {
c.Middleware = append(c.Middleware, m)
}
@@ -126,17 +128,11 @@ func groupConfigsByListenAddr(configs []*Config) (map[string][]*Config, error) {
const (
// DefaultPort is the default port.
DefaultPort = "2053"
// DefaultRoot is the default root folder.
DefaultRoot = "."
)
// These "soft defaults" are configurable by
// command line flags, etc.
var (
// Root is the site root
// TODO(miek): double check if this is used and if we want to use it.
Root = DefaultRoot
// Port is the site port
Port = DefaultPort

View File

@@ -8,6 +8,7 @@ import (
"sync"
"time"
"github.com/miekg/coredns/middleware"
"github.com/miekg/coredns/middleware/pkg/edns"
"github.com/miekg/coredns/request"
@@ -58,7 +59,7 @@ func NewServer(addr string, group []*Config) (*Server, error) {
// set the config per zone
s.zones[site.Zone] = site
// compile custom middleware for everything
var stack Handler
var stack middleware.Handler
for i := len(site.Middleware) - 1; i >= 0; i-- {
stack = site.Middleware[i](stack)
}