-
Notifications
You must be signed in to change notification settings - Fork 0
/
logtrace_test.go
132 lines (109 loc) · 3.34 KB
/
logtrace_test.go
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
package logtrace
import (
"bytes"
"errors"
"io"
"os"
"testing"
"github.com/ansel1/merry/v2"
goerr "github.com/go-errors/errors"
"github.com/shoenig/test"
"github.com/sirupsen/logrus"
)
const TEST_STACKTRACE_KEY = "test_stacktrace"
func testHook(capture bool) LogtraceHook {
return LogtraceHook{
Key: TEST_STACKTRACE_KEY,
LogLevels: logrus.AllLevels,
CaptureStackIfMissing: capture,
}
}
func brokenTestHook() LogtraceHook {
return LogtraceHook{
Key: "",
LogLevels: logrus.AllLevels,
CaptureStackIfMissing: false,
}
}
type voidWriter struct{}
func (voidWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
type validationFormatter struct {
Data *logrus.Fields
}
func (vF validationFormatter) Format(e *logrus.Entry) ([]byte, error) {
*vF.Data = e.Data
return []byte{}, nil
}
func TestDefaultLogtraceHook(t *testing.T) {
hook := DefaultLogtraceHook()
test.Eq(t, "stacktrace", hook.Key)
test.Eq(t, logrus.AllLevels, hook.LogLevels)
test.True(t, hook.CaptureStackIfMissing)
}
func setupErrorTracebackHookTest() (validationFormatter, *logrus.Logger) {
formatter := validationFormatter{
Data: new(logrus.Fields),
}
testLogger := &logrus.Logger{
Out: voidWriter{},
Formatter: formatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
return formatter, testLogger
}
func captureStderr(f func()) (error, string) {
orig := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
f()
var buf bytes.Buffer
w.Close()
os.Stderr = orig
_, err := io.Copy(&buf, r)
return err, buf.String()
}
func TestErrorTracebackHook(t *testing.T) {
testCases := []struct {
name string
hook LogtraceHook
err error
shouldHaveStacktrace bool
expectedStderr string
}{
{"builtin error without CaptureStacktraceIfMissing", testHook(false), errors.New("Testerror"), false, ""},
{"builtin error with CaptureStacktraceIfMissing", testHook(true), errors.New("Testerror"), true, ""},
{"goerror error", testHook(false), goerr.New("Testerror"), true, ""},
{"merry error", testHook(false), merry.New("Testerror"), true, ""},
{"brokenTestHook", brokenTestHook(), merry.New("Testerror"), false, "Failed to fire hook: The key of the hook must be set to a non empty string.\n"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
formatter, testLogger := setupErrorTracebackHookTest()
testLogger.AddHook(tc.hook)
err, stderr := captureStderr(func() { testLogger.WithError(tc.err).Info("") })
if err != nil {
t.Fatal("Failed to capture stderr. Please run test again.")
}
test.Eq(t, tc.expectedStderr, stderr)
if val, ok := (*formatter.Data)[logrus.ErrorKey]; ok {
test.True(t, val == tc.err)
} else {
test.Unreachable(t)
}
_, ok := (*formatter.Data)[TEST_STACKTRACE_KEY]
test.Eq(t, tc.shouldHaveStacktrace, ok)
})
}
}
func TestErrorTracebackHookNonErrorError(t *testing.T) {
_, testLogger := setupErrorTracebackHookTest()
testLogger.AddHook(testHook(false))
err, stderr := captureStderr(func() { testLogger.WithField(logrus.ErrorKey, "foo").Info("") })
if err != nil {
t.Fatal("Failed to capture stderr. Please run test again.")
}
test.Eq(t, "Failed to fire hook: The value in the error field of the given logrus entry is not an error!\n", stderr)
}