-
Notifications
You must be signed in to change notification settings - Fork 10
/
hook_test.go
130 lines (119 loc) · 3.32 KB
/
hook_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
package coralogix
import (
"fmt"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestHook_Send(t *testing.T) {
CoralogixHook := NewCoralogixHook(
GetEnv(
"PRIVATE_KEY",
testPrivateKey,
),
"sdk-go",
"test",
)
defer func() { recover() }()
defer CoralogixHook.Close()
log := logrus.New()
log.SetLevel(logrus.TraceLevel)
log.AddHook(CoralogixHook)
fields := logrus.Fields{
"Category": "MyCategory",
"ThreadId": "MyThreadId",
"extra": "additional",
}
testcases := []struct {
name string
logfn func(args ...interface{})
severity uint
}{
{
name: "test trace",
severity: Level.TRACE,
logfn: func(args ...interface{}) {
log.WithFields(fields).Trace(args...)
},
},
{
name: "test debug",
severity: Level.DEBUG,
logfn: func(args ...interface{}) {
log.WithFields(fields).Debug(args...)
},
},
{
name: "test error",
severity: Level.ERROR,
logfn: func(args ...interface{}) {
log.WithFields(fields).Error(args...)
},
},
{
name: "test info",
severity: Level.INFO,
logfn: func(args ...interface{}) {
log.WithFields(fields).Info(args...)
},
},
{
name: "test warn",
severity: Level.WARNING,
logfn: func(args ...interface{}) {
log.WithFields(fields).Warn(args...)
},
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
msg := fmt.Sprintf("%s (%s)", tc.name, t.Name())
tc.logfn(msg)
var bulk *Bulk
assert.Eventually(t, func() (ok bool) {
bulk, ok = mockHTTPServerMap[t.Name()]
return ok
}, 5*time.Second, 1*time.Second, "%s key not found in mockHTTPServerMap", t.Name())
var msgExists bool
for _, entry := range bulk.LogEntries {
if msgExists = strings.Contains(entry.Text, tc.name); msgExists {
assert.Equal(t, tc.severity, entry.Severity)
assert.Equal(t, fields["Category"], entry.Category)
assert.Equal(t, fields["ThreadId"], entry.ThreadID,
"expected %v, got %v", fields["MyThreadId"], entry.ThreadID)
assert.True(t, strings.Contains(entry.Text, fields["extra"].(string)),
"entry Text does not contain extra field", entry.Text, fields["extra"])
break
}
}
assert.True(t, msgExists, "no matching message found", string(bulk.ToJSON()))
})
}
// test with caller
log.SetReportCaller(true)
for _, tc := range testcases {
tc.name = fmt.Sprintf("%s_withReportCaller", tc.name)
t.Run(tc.name, func(t *testing.T) {
msg := fmt.Sprintf("%s (%s)", tc.name, t.Name())
tc.logfn(msg)
time.Sleep(time.Duration(1) * time.Second)
bulk, ok := mockHTTPServerMap[t.Name()]
assert.True(t, ok, "%s key not found in mockHTTPServerMap", t.Name())
var msgExists bool
for _, entry := range bulk.LogEntries {
if msgExists = strings.Contains(entry.Text, tc.name); msgExists {
assert.Equal(t, tc.severity, entry.Severity)
assert.Equal(t, fields["Category"], entry.Category)
assert.Equal(t, fields["ThreadId"], entry.ThreadID,
"expected %v, got %v", fields["ThreadId"], entry.ThreadID)
assert.True(t, strings.Contains(entry.Text, fields["extra"].(string)),
"entry Text does not contain extra field", entry.Text, fields["extra"])
break
}
}
assert.True(t, msgExists, "no matching message found", string(bulk.ToJSON()))
})
}
}