From d9282030ca1ae77eac602ed07d8420055faa7e99 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 26 Jan 2018 18:43:28 +0000 Subject: [PATCH] Add tests and rename middleware to plugin --- README.md | 4 ++-- example.go | 14 +++++++++----- example_test.go | 29 +++++++++++++++++++++++++++++ setup_test.go | 19 +++++++++++++++++++ 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 example_test.go create mode 100644 setup_test.go diff --git a/README.md b/README.md index 18b21e0..891bc7d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # example The example middleware prints "example" on every query received. It can be used as documentation for -writing external middleware and to test if external middleware compiles with CoreDNS. +writing external plugins and to test if external plugins compile with CoreDNS. ## Syntax @@ -11,7 +11,7 @@ example ## Examples -``` +``` corefile example.com { file example.com.db { upstream 8.8.8.8 diff --git a/example.go b/example.go index 5212f41..e910996 100644 --- a/example.go +++ b/example.go @@ -1,23 +1,25 @@ -// The example middleware prints example to stdout on every packet received. +// The example plugin prints example to stdout on every packet received. package example import ( "fmt" + "io" + "os" "github.com/coredns/coredns/plugin" "github.com/miekg/dns" "golang.org/x/net/context" ) -// Example is an example middleware to ... +// Example is an example plugin to show how to write a plugin. type Example struct { Next plugin.Handler } -// ServeDNS implements the middleware.Handler interface. +// ServeDNS implements the plugin.Handler interface. func (e Example) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) { // Somewhat convoluted, as we could have printed "example" here and just call - // the next middleware - but as an example, show how to wrap a ResponseWriter might be + // the next plugin - but as an example, show how to wrap a ResponseWriter might be // educational. pw := NewResponsePrinter(w) return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r) @@ -37,6 +39,8 @@ func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter { // WriteMsg records the status code and calls the underlying ResponseWriter's WriteMsg method. func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error { - fmt.Println("example") + fmt.Fprintf(out, "example") return r.ResponseWriter.WriteMsg(res) } + +var out io.Writer = os.Stdout diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..fa9c834 --- /dev/null +++ b/example_test.go @@ -0,0 +1,29 @@ +package example + +import ( + "bytes" + "testing" + + "github.com/coredns/coredns/plugin/pkg/dnstest" + "github.com/coredns/coredns/plugin/test" + + "github.com/miekg/dns" + "golang.org/x/net/context" +) + +func TestExample(t *testing.T) { + ex := Example{Next: test.ErrorHandler()} + + b := &bytes.Buffer{} + out = b + + ctx := context.TODO() + r := new(dns.Msg) + r.SetQuestion("example.org.", dns.TypeA) + rec := dnstest.NewRecorder(&test.ResponseWriter{}) + + ex.ServeDNS(ctx, rec, r) + if x := b.String(); x != "example" { + t.Errorf("Failed to print 'example', got %s", x) + } +} diff --git a/setup_test.go b/setup_test.go new file mode 100644 index 0000000..4e87130 --- /dev/null +++ b/setup_test.go @@ -0,0 +1,19 @@ +package example + +import ( + "testing" + + "github.com/mholt/caddy" +) + +func TestSetupWhoami(t *testing.T) { + c := caddy.NewTestController("dns", `example`) + if err := setup(c); err != nil { + t.Fatalf("Expected no errors, but got: %v", err) + } + + c = caddy.NewTestController("dns", `example more`) + if err := setup(c); err == nil { + t.Fatalf("Expected errors, but got: %v", err) + } +}