coredns/plugin/log/log_test.go
Miek Gieben f3134da45e
Clean up tests logging (#1979)
* Clean up tests logging

This cleans up the travis logs so you can see the failures better.

Older tests in tests/ would call log.SetOutput(ioutil.Discard) in
a haphazard way. This add log.Discard and put an `init` function in each
package's dir (no way to do this globally). The cleanup in tests/ is
clear.

All plugins also got this init function to have some uniformity and kill
any (future) logging there in the tests as well.

There is a one-off in pkg/healthcheck because that does log.

Signed-off-by: Miek Gieben <miek@miek.nl>

* bring back original log_test.go

Signed-off-by: Miek Gieben <miek@miek.nl>

* suppress logging here as well

Signed-off-by: Miek Gieben <miek@miek.nl>
2018-07-19 16:23:06 +01:00

105 lines
2.2 KiB
Go

package log
import (
"bytes"
"context"
"log"
"strings"
"testing"
"github.com/coredns/coredns/plugin/pkg/dnstest"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/plugin/pkg/response"
"github.com/coredns/coredns/plugin/test"
"github.com/miekg/dns"
)
func init() { clog.Discard() }
func TestLoggedStatus(t *testing.T) {
var f bytes.Buffer
rule := Rule{
NameScope: ".",
Format: DefaultLogFormat,
Log: log.New(&f, "", 0),
Class: map[response.Class]bool{response.All: true},
}
logger := Logger{
Rules: []Rule{rule},
Next: test.ErrorHandler(),
}
ctx := context.TODO()
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rcode, _ := logger.ServeDNS(ctx, rec, r)
if rcode != 0 {
t.Errorf("Expected rcode to be 0 - was: %d", rcode)
}
logged := f.String()
if !strings.Contains(logged, "A IN example.org. udp 29 false 512") {
t.Errorf("Expected it to be logged. Logged string: %s", logged)
}
}
func TestLoggedClassDenial(t *testing.T) {
var f bytes.Buffer
rule := Rule{
NameScope: ".",
Format: DefaultLogFormat,
Log: log.New(&f, "", 0),
Class: map[response.Class]bool{response.Denial: true},
}
logger := Logger{
Rules: []Rule{rule},
Next: test.ErrorHandler(),
}
ctx := context.TODO()
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
logger.ServeDNS(ctx, rec, r)
logged := f.String()
if len(logged) != 0 {
t.Errorf("Expected it not to be logged, but got string: %s", logged)
}
}
func TestLoggedClassError(t *testing.T) {
var f bytes.Buffer
rule := Rule{
NameScope: ".",
Format: DefaultLogFormat,
Log: log.New(&f, "", 0),
Class: map[response.Class]bool{response.Error: true},
}
logger := Logger{
Rules: []Rule{rule},
Next: test.ErrorHandler(),
}
ctx := context.TODO()
r := new(dns.Msg)
r.SetQuestion("example.org.", dns.TypeA)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
logger.ServeDNS(ctx, rec, r)
logged := f.String()
if !strings.Contains(logged, "SERVFAIL") {
t.Errorf("Expected it to be logged. Logged string: %s", logged)
}
}