-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry_test.go
More file actions
118 lines (96 loc) · 4.65 KB
/
Copy pathentry_test.go
File metadata and controls
118 lines (96 loc) · 4.65 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
package logger
import (
"math/rand"
"strconv"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
)
// The set of these unit tests aim to test the common methods (for both the logger and the entry) but called
// on the entry object. That's why we are looking at the second calls in one call chain.
func TestLogEntryWithTime(t *testing.T) {
ulog := NewUPPInfoLogger("test_service")
hook := test.NewLocal(ulog.Logger)
myExpectedTime := time.Unix(rand.Int63n(time.Now().Unix()), rand.Int63n(1000000000))
ulog.WithMonitoringEvent("an-event", "tid_test", "some-content").WithTime(myExpectedTime).Info("This is a custom time for my event")
assert.Len(t, hook.Entries, 1)
assert.Len(t, hook.LastEntry().Data, 5)
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "This is a custom time for my event", hook.LastEntry().Message)
myActualTime, err := time.Parse(timestampFormat, hook.LastEntry().Data[DefaultKeyTime].(string))
assert.NoError(t, err)
assert.WithinDuration(t, myExpectedTime, myActualTime, 0)
assert.Equal(t, "tid_test", hook.LastEntry().Data[DefaultKeyTransactionID])
}
func TestLogEntryWithTransactionID(t *testing.T) {
conf := KeyNamesConfig{KeyTransactionID: "test-trans-id"}
ulog := NewUPPInfoLogger("test_service", conf)
hook := test.NewLocal(ulog.Logger)
expectedUUID := "50484f2a-a51d-42d8-8deb-11a1d25e6b45"
ulog.WithMonitoringEvent("an-event", "tid_test", "some-content").WithUUID(expectedUUID).Info("a info message")
assert.Len(t, hook.Entries, 1)
assert.Len(t, hook.LastEntry().Data, 5)
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "a info message", hook.LastEntry().Message)
assert.Equal(t, expectedUUID, hook.LastEntry().Data[DefaultKeyUUID])
assert.Equal(t, "tid_test", hook.LastEntry().Data[conf.KeyTransactionID])
}
func TestLogEntryWithValidFlagTrue(t *testing.T) {
ulog := NewUPPInfoLogger("test_service")
hook := test.NewLocal(ulog.Logger)
ulog.WithMonitoringEvent("an-event", "tid_test", "some-content").WithValidFlag(true).Info("a info message")
assert.Len(t, hook.Entries, 1)
assert.Len(t, hook.LastEntry().Data, 5)
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "a info message", hook.LastEntry().Message)
isValid, err := strconv.ParseBool(hook.LastEntry().Data[DefaultKeyIsValid].(string))
assert.NoError(t, err)
assert.True(t, isValid)
assert.Equal(t, "tid_test", hook.LastEntry().Data[DefaultKeyTransactionID])
}
func TestLogEntryWithValidFlagFalse(t *testing.T) {
ulog := NewUPPInfoLogger("test_service")
hook := test.NewLocal(ulog.Logger)
ulog.WithMonitoringEvent("an-event", "tid_test", "some-content").
WithValidFlag(false).Info("a info message")
assert.Len(t, hook.Entries, 1)
assert.Len(t, hook.LastEntry().Data, 5)
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "a info message", hook.LastEntry().Message)
isValid, err := strconv.ParseBool(hook.LastEntry().Data[DefaultKeyIsValid].(string))
assert.NoError(t, err)
assert.False(t, isValid)
assert.Equal(t, "tid_test", hook.LastEntry().Data[DefaultKeyTransactionID])
}
func TestLogEntryWithMonitoringEvent(t *testing.T) {
conf := KeyNamesConfig{KeyEventName: "test-event-name"}
ulog := NewUPPInfoLogger("test_service", conf)
hook := test.NewLocal(ulog.Logger)
ulog.WithUUID("test-uuid-value").
WithMonitoringEvent("an-event", "tid_test", "some-content").
Info("a info message")
assert.Len(t, hook.Entries, 1)
assert.Len(t, hook.LastEntry().Data, 5)
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "an-event", hook.LastEntry().Data[conf.KeyEventName])
assert.Equal(t, "true", hook.LastEntry().Data[DefaultKeyMonitoringEvent])
assert.Equal(t, "some-content", hook.LastEntry().Data[DefaultKeyContentType])
assert.Equal(t, "tid_test", hook.LastEntry().Data[DefaultKeyTransactionID])
}
func TestLogEntryWithCategorisedEvent(t *testing.T) {
conf := KeyNamesConfig{KeyEventCategory: "test-event-category-key"}
ulog := NewUPPInfoLogger("test_service", conf)
hook := test.NewLocal(ulog.Logger)
ulog.WithUUID("test-uuid-value").
WithCategorisedEvent("test-event", "test-category", "test-event-msg", "test-tid").
Info("a info message")
assert.Len(t, hook.Entries, 1)
assert.Len(t, hook.LastEntry().Data, 5)
assert.Equal(t, logrus.InfoLevel, hook.LastEntry().Level)
assert.Equal(t, "test-event", hook.LastEntry().Data[DefaultKeyEventName])
assert.Equal(t, "test-category", hook.LastEntry().Data[conf.KeyEventCategory])
assert.Equal(t, "test-event-msg", hook.LastEntry().Data[DefaultKeyEventMsg])
assert.Equal(t, "test-tid", hook.LastEntry().Data[DefaultKeyTransactionID])
}