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/errors_test.go | 63 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 13 deletions(-) (limited to 'plugin/errors/errors_test.go') diff --git a/plugin/errors/errors_test.go b/plugin/errors/errors_test.go index 9563b7323..ba9a9b571 100644 --- a/plugin/errors/errors_test.go +++ b/plugin/errors/errors_test.go @@ -14,6 +14,7 @@ import ( "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/pkg/dnstest" + clog "github.com/coredns/coredns/plugin/pkg/log" "github.com/coredns/coredns/plugin/test" "github.com/miekg/dns" @@ -71,21 +72,56 @@ func TestErrors(t *testing.T) { } func TestLogPattern(t *testing.T) { - buf := bytes.Buffer{} - golog.SetOutput(&buf) - - h := &errorHandler{ - patterns: []*pattern{{ - count: 4, - period: 2 * time.Second, - pattern: regexp.MustCompile("^error.*!$"), - }}, + type args struct { + logCallback func(format string, v ...interface{}) + } + tests := []struct { + name string + args args + want string + }{ + { + name: "error log", + args: args{logCallback: log.Errorf}, + want: "[ERROR] plugin/errors: 4 errors like '^error.*!$' occurred in last 2s", + }, + { + name: "warn log", + args: args{logCallback: log.Warningf}, + want: "[WARNING] plugin/errors: 4 errors like '^error.*!$' occurred in last 2s", + }, + { + name: "info log", + args: args{logCallback: log.Infof}, + want: "[INFO] plugin/errors: 4 errors like '^error.*!$' occurred in last 2s", + }, + { + name: "debug log", + args: args{logCallback: log.Debugf}, + want: "[DEBUG] plugin/errors: 4 errors like '^error.*!$' occurred in last 2s", + }, } - h.logPattern(0) - expLog := "4 errors like '^error.*!$' occurred in last 2s" - if log := buf.String(); !strings.Contains(log, expLog) { - t.Errorf("Expected log %q, but got %q", expLog, log) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + buf := bytes.Buffer{} + clog.D.Set() + golog.SetOutput(&buf) + + h := &errorHandler{ + patterns: []*pattern{{ + count: 4, + period: 2 * time.Second, + pattern: regexp.MustCompile("^error.*!$"), + logCallback: tt.args.logCallback, + }}, + } + h.logPattern(0) + + if log := buf.String(); !strings.Contains(log, tt.want) { + t.Errorf("Expected log %q, but got %q", tt.want, log) + } + }) } } @@ -154,6 +190,7 @@ func TestStop(t *testing.T) { patterns: []*pattern{{ period: 2 * time.Second, pattern: regexp.MustCompile("^error.*!$"), + logCallback: log.Errorf, }}, } -- cgit v1.2.3