aboutsummaryrefslogtreecommitdiff
path: root/plugin/errors/setup_test.go
diff options
context:
space:
mode:
authorGravatar Ondřej Benkovský <ondrej.benkovsky@wandera.com> 2021-07-09 16:23:02 +0200
committerGravatar GitHub <noreply@github.com> 2021-07-09 14:23:02 +0000
commit70b51a73d3a0029394dbdbc9f52a4d3d8e89d6f5 (patch)
tree9bd9362cdda53be3d3e56b4a76a59b8331b5ef21 /plugin/errors/setup_test.go
parenta6a7e738136cf09950dcdf880fe12ea9e14e3afc (diff)
downloadcoredns-70b51a73d3a0029394dbdbc9f52a4d3d8e89d6f5.tar.gz
coredns-70b51a73d3a0029394dbdbc9f52a4d3d8e89d6f5.tar.zst
coredns-70b51a73d3a0029394dbdbc9f52a4d3d8e89d6f5.zip
add configurable log level to errors plugin (#4718)
Automatically submitted.
Diffstat (limited to 'plugin/errors/setup_test.go')
-rw-r--r--plugin/errors/setup_test.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/plugin/errors/setup_test.go b/plugin/errors/setup_test.go
index c61cf54c4..3bfb87c8d 100644
--- a/plugin/errors/setup_test.go
+++ b/plugin/errors/setup_test.go
@@ -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)
+ }
+ })
+ }
+}