From 70b51a73d3a0029394dbdbc9f52a4d3d8e89d6f5 Mon Sep 17 00:00:00 2001 From: Ondřej Benkovský Date: Fri, 9 Jul 2021 16:23:02 +0200 Subject: add configurable log level to errors plugin (#4718) Automatically submitted. --- plugin/errors/setup_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'plugin/errors/setup_test.go') 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) + } + }) + } +} -- cgit v1.2.3