add configurable log level to errors plugin (#4718)
Automatically submitted.
This commit is contained in:
parent
a6a7e73813
commit
70b51a73d3
5 changed files with 151 additions and 24 deletions
|
@ -1,9 +1,13 @@
|
|||
package errors
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
golog "log"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/caddy"
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
)
|
||||
|
||||
func TestErrorsParse(t *testing.T) {
|
||||
|
@ -67,3 +71,65 @@ func TestErrorsParse(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProperLogCallbackIsSet(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
inputErrorsRules string
|
||||
wantLogLevel string
|
||||
}{
|
||||
{
|
||||
name: "warn is parsed properly",
|
||||
inputErrorsRules: `errors {
|
||||
consolidate 1m .* warn
|
||||
}`,
|
||||
wantLogLevel: "[WARNING]",
|
||||
},
|
||||
{
|
||||
name: "error is parsed properly",
|
||||
inputErrorsRules: `errors {
|
||||
consolidate 1m .* error
|
||||
}`,
|
||||
wantLogLevel: "[ERROR]",
|
||||
},
|
||||
{
|
||||
name: "info is parsed properly",
|
||||
inputErrorsRules: `errors {
|
||||
consolidate 1m .* info
|
||||
}`,
|
||||
wantLogLevel: "[INFO]",
|
||||
},
|
||||
{
|
||||
name: "debug is parsed properly",
|
||||
inputErrorsRules: `errors {
|
||||
consolidate 1m .* debug
|
||||
}`,
|
||||
wantLogLevel: "[DEBUG]",
|
||||
},
|
||||
{
|
||||
name: "default is error",
|
||||
inputErrorsRules: `errors {
|
||||
consolidate 1m .*
|
||||
}`,
|
||||
wantLogLevel: "[ERROR]",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
buf := bytes.Buffer{}
|
||||
golog.SetOutput(&buf)
|
||||
clog.D.Set()
|
||||
|
||||
c := caddy.NewTestController("dns", tt.inputErrorsRules)
|
||||
h, _ := errorsParse(c)
|
||||
|
||||
l := h.patterns[0].logCallback
|
||||
l("some error happened")
|
||||
|
||||
if log := buf.String(); !strings.Contains(log, tt.wantLogLevel) {
|
||||
t.Errorf("Expected log %q, but got %q", tt.wantLogLevel, log)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue