Repository navigation
Expand file tree
/
Copy pathconfig_test.go
More file actions
343 lines (286 loc) · 8.56 KB
/
Copy pathconfig_test.go
File metadata and controls
343 lines (286 loc) · 8.56 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) Tetrate, Inc 2018 All Rights Reserved.
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"io/ioutil"
"log"
"os"
"regexp"
"strconv"
"strings"
"testing"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc/grpclog"
)
const timePattern = "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9][0-9][0-9][0-9][0-9]Z"
type testDateEncoder struct {
zapcore.PrimitiveArrayEncoder
output string
}
func (tde *testDateEncoder) AppendString(s string) {
tde.output = s
}
func TestTimestampProperYear(t *testing.T) {
testEnc := &testDateEncoder{}
cases := []struct {
name string
input time.Time
want string
}{
{"1", time.Date(1, time.April, 1, 1, 1, 1, 1, time.UTC), "0001"},
{"1989", time.Date(1989, time.February, 1, 1, 1, 1, 1, time.UTC), "1989"},
{"2017", time.Date(2017, time.January, 1, 1, 1, 1, 1, time.UTC), "2017"},
{"2083", time.Date(2083, time.March, 1, 1, 1, 1, 1, time.UTC), "2083"},
{"2573", time.Date(2573, time.June, 1, 1, 1, 1, 1, time.UTC), "2573"},
{"9999", time.Date(9999, time.May, 1, 1, 1, 1, 1, time.UTC), "9999"},
}
for _, v := range cases {
t.Run(v.name, func(t *testing.T) {
formatDate(v.input, testEnc)
if !strings.HasPrefix(testEnc.output, v.want) {
t.Errorf("formatDate(%v) => %s, want year: %s", v.input, testEnc.output, v.want)
}
})
}
}
func TestTimestampProperMicros(t *testing.T) {
testEnc := &testDateEncoder{}
cases := []struct {
name string
input time.Time
want string
}{
{"1", time.Date(2017, time.April, 1, 1, 1, 1, 1000, time.UTC), "1"},
{"99", time.Date(1989, time.February, 1, 1, 1, 1, 99000, time.UTC), "99"},
{"999", time.Date(2017, time.January, 1, 1, 1, 1, 999000, time.UTC), "999"},
{"9999", time.Date(2083, time.March, 1, 1, 1, 1, 9999000, time.UTC), "9999"},
{"99999", time.Date(2083, time.March, 1, 1, 1, 1, 99999000, time.UTC), "99999"},
{"999999", time.Date(2083, time.March, 1, 1, 1, 1, 999999000, time.UTC), "999999"},
}
for _, v := range cases {
t.Run(v.name, func(t *testing.T) {
formatDate(v.input, testEnc)
if !strings.HasSuffix(testEnc.output, v.want+"Z") {
t.Errorf("formatDate(%v) => %s, want micros: %s", v.input, testEnc.output, v.want)
}
})
}
}
func TestOddballs(t *testing.T) {
o := DefaultOptions()
_ = Configure(o)
o = DefaultOptions()
o.outputLevels = "default,,"
err := Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
o.outputLevels = "foobar"
err = Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
o.outputLevels = "foobar:debug"
err = Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
o.stackTraceLevels = "default,,"
err = Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
o.stackTraceLevels = "foobar"
err = Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
o.stackTraceLevels = "foobar:debug"
err = Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
o.logCallers = "foobar"
err = Configure(o)
if err == nil {
t.Error("Got success, expected failure")
}
o = DefaultOptions()
//using invalid filename
o.OutputPaths = []string{"//"}
err = Configure(o)
if err == nil {
t.Errorf("Got success, expecting error")
}
o = DefaultOptions()
o.ErrorOutputPaths = []string{"//"}
err = Configure(o)
if err == nil {
t.Errorf("Got success, expecting error")
}
}
func TestRotateNoStdout(t *testing.T) {
// Ensure that rotation is setup properly
dir, _ := ioutil.TempDir("", "TestRotateNoStdout")
defer func() {
_ = os.RemoveAll(dir)
}()
file := dir + "/rot.log"
o := DefaultOptions()
o.OutputPaths = []string{}
o.RotateOutputPath = file
if err := Configure(o); err != nil {
t.Fatalf("Unable to configure logging: %v", err)
}
defaultScope.Error("HELLO")
Sync() // nolint: errcheck
content, err := ioutil.ReadFile(file)
if err != nil {
t.Errorf("Got failure '%v', expecting success", err)
}
lines := strings.Split(string(content), "\n")
if !strings.Contains(lines[0], "HELLO") {
t.Errorf("Expecting for first line of log to contain HELLO, got %s", lines[0])
}
}
func TestRotateAndStdout(t *testing.T) {
dir, _ := ioutil.TempDir("", "TestRotateAndStdout")
defer func() {
_ = os.RemoveAll(dir)
}()
file := dir + "/rot.log"
stdoutLines, _ := captureStdout(func() {
o := DefaultOptions()
o.RotateOutputPath = file
if err := Configure(o); err != nil {
t.Fatalf("Unable to configure logger: %v", err)
}
defaultScope.Error("HELLO")
Sync() // nolint: errcheck
content, err := ioutil.ReadFile(file)
if err != nil {
t.Errorf("Got failure '%v', expecting success", err)
}
rotLines := strings.Split(string(content), "\n")
if !strings.Contains(rotLines[0], "HELLO") {
t.Errorf("Expecting for first line of log to contain HELLO, got %s", rotLines[0])
}
})
if !strings.Contains(stdoutLines[0], "HELLO") {
t.Errorf("Expecting for first line of log to contain HELLO, got %s", stdoutLines[0])
}
}
func TestCapture(t *testing.T) {
lines, _ := captureStdout(func() {
o := DefaultOptions()
o.SetLogCallers(DefaultScopeName, true)
o.SetOutputLevel(DefaultScopeName, DebugLevel)
_ = Configure(o)
// output to the plain golang "log" package
log.Println("golang")
// output to the gRPC logging package
grpclog.Error("grpc-error")
grpclog.Warning("grpc-warn")
grpclog.Info("grpc-info")
// output directly to zap
zap.L().Error("zap-error")
zap.L().Warn("zap-warn")
zap.L().Info("zap-info")
zap.L().Debug("zap-debug")
l := zap.L().With(zap.String("a", "b"))
l.Error("zap-with")
entry := zapcore.Entry{
Message: "zap-write",
Level: zapcore.ErrorLevel,
}
_ = zap.L().Core().Write(entry, nil)
defaultScope.SetOutputLevel(NoneLevel)
// all these get thrown out since the level is set to none
log.Println("golang-2")
zap.L().Error("zap-error-2")
zap.L().Warn("zap-warn-2")
zap.L().Info("zap-info-2")
zap.L().Debug("zap-debug-2")
})
patterns := []string{
timePattern + "\tinfo\tlog/config_test.go:.*\tgolang",
timePattern + "\tinfo\tlog/config_test.go:.*\tgrpc-error", // gRPC errors and warnings come out as info
timePattern + "\tinfo\tlog/config_test.go:.*\tgrpc-warn",
timePattern + "\tinfo\tlog/config_test.go:.*\tgrpc-info",
timePattern + "\terror\tlog/config_test.go:.*\tzap-error",
timePattern + "\twarn\tlog/config_test.go:.*\tzap-warn",
timePattern + "\tinfo\tlog/config_test.go:.*\tzap-info",
timePattern + "\tdebug\tlog/config_test.go:.*\tzap-debug",
timePattern + "\terror\tlog/config_test.go:.*\tzap-with",
timePattern + "\terror\tzap-write",
"",
}
if len(lines) > len(patterns) {
t.Errorf("Expecting %d lines of output, but got %d", len(patterns), len(lines))
for i := len(patterns); i < len(lines); i++ {
t.Errorf(" Extra line of output: %s", lines[i])
}
}
for i, pat := range patterns {
t.Run(strconv.Itoa(i), func(t *testing.T) {
match, _ := regexp.MatchString(pat, lines[i])
if !match {
t.Errorf("Got '%s', expecting to match '%s'", lines[i], pat)
}
})
}
lines, _ = captureStdout(func() {
o := DefaultOptions()
o.SetStackTraceLevel(DefaultScopeName, DebugLevel)
o.SetOutputLevel(DefaultScopeName, DebugLevel)
_ = Configure(o)
log.Println("golang")
})
for _, line := range lines {
// see if the captured output contains the current file name
if strings.Contains(line, "config_test.go") {
return
}
}
t.Error("Could not find stack trace info in output")
}
// Runs the given function while capturing everything sent to stdout
func captureStdout(f func()) ([]string, error) {
tf, err := ioutil.TempFile("", "log_test")
if err != nil {
return nil, err
}
old := os.Stdout
os.Stdout = tf
f()
os.Stdout = old
path := tf.Name()
_ = tf.Sync()
_ = tf.Close()
content, err := ioutil.ReadFile(path)
_ = os.Remove(path)
if err != nil {
return nil, err
}
return strings.Split(string(content), "\n"), nil
}