1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package errors
import (
"bytes"
golog "log"
"strings"
"testing"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
clog "github.com/coredns/coredns/plugin/pkg/log"
)
func TestErrorsParse(t *testing.T) {
tests := []struct {
inputErrorsRules string
shouldErr bool
optCount int
stacktrace bool
}{
{`errors`, false, 0, false},
{`errors stdout`, false, 0, false},
{`errors errors.txt`, true, 0, false},
{`errors visible`, true, 0, false},
{`errors { log visible }`, true, 0, false},
{`errors
errors `, true, 0, false},
{`errors a b`, true, 0, false},
{`errors {
consolidate
}`, true, 0, false},
{`errors {
consolidate 1m
}`, true, 0, false},
{`errors {
consolidate 1m .* extra
}`, true, 0, false},
{`errors {
consolidate abc .*
}`, true, 0, false},
{`errors {
consolidate 1 .*
}`, true, 0, false},
{`errors {
consolidate 1m ())
}`, true, 0, false},
{`errors {
stacktrace
}`, false, 0, true},
{`errors {
stacktrace
consolidate 1m ^exact$
}`, false, 1, true},
{`errors {
consolidate 1m ^exact$
}`, false, 1, false},
{`errors {
consolidate 1m error
}`, false, 1, false},
{`errors {
consolidate 1m "format error"
}`, false, 1, false},
{`errors {
consolidate 1m error1
consolidate 5s error2
}`, false, 2, false},
}
for i, test := range tests {
c := caddy.NewTestController("dns", test.inputErrorsRules)
h, err := errorsParse(c)
if err == nil && test.shouldErr {
t.Errorf("Test %d didn't error, but it should have", i)
} else if err != nil && !test.shouldErr {
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
} else if h != nil && len(h.patterns) != test.optCount {
t.Errorf("Test %d: pattern count mismatch, expected %d, got %d",
i, test.optCount, len(h.patterns))
}
if dnsserver.GetConfig(c).Stacktrace != test.stacktrace {
t.Errorf("Test %d: stacktrace, expected %t, got %t",
i, test.stacktrace, dnsserver.GetConfig(c).Stacktrace)
}
}
}
func TestProperLogCallbackIsSet(t *testing.T) {
tests := []struct {
name string
inputErrorsRules string
wantLogLevel string
}{
{
name: "warning is parsed properly",
inputErrorsRules: `errors {
consolidate 1m .* warning
}`,
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)
}
})
}
}
|