forked from ooni/probe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasklogger_test.go
106 lines (93 loc) · 3.23 KB
/
tasklogger_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
package oonimkall
//
// This file contains tests for the taskLogger type.
//
import (
"testing"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestTaskLogger(t *testing.T) {
// debugMessage is the debug message we expect to see.
debugMessage := "debug message"
// infoMessage is the info message we expect to see.
infoMessage := "info message"
// warningMessage is the warning message we expect to see.
warningMessage := "warning message"
// emitMessages is an helper function for implementing this test.
emitMessages := func(logger model.Logger) {
logger.Debug(debugMessage)
logger.Debugf("%s", debugMessage)
logger.Info(infoMessage)
logger.Infof("%s", infoMessage)
logger.Warn(warningMessage)
logger.Warnf("%s", warningMessage)
}
// convertEventsToLogEvents converts the generic events to
// logEvents and fails if this operation is not possible.
convertEventsToLogEvents := func(t *testing.T, in []*event) (out []eventLog) {
for _, ev := range in {
if ev.Key != eventTypeLog {
t.Fatalf("expected log event, found %s", ev.Key)
}
out = append(out, ev.Value.(eventLog))
}
return
}
// checkNumberOfEvents ensures we've the right number of events.
checkNumberOfEvents := func(t *testing.T, events []eventLog, expect int) {
if len(events) != expect {
t.Fatalf(
"invalid number of log events %d (expected %d)",
len(events), expect,
)
}
}
// matchEvent ensures the given event has the right level and message.
matchEvent := func(t *testing.T, event eventLog, level, msg string) {
if event.LogLevel != level {
t.Fatalf(
"invalid log level %s (expected %s)",
event.LogLevel, level,
)
}
if event.Message != msg {
t.Fatalf(
"invalid log message '%s' (expected '%s')",
event.Message, msg,
)
}
}
t.Run("debug logger", func(t *testing.T) {
emitter := &CollectorTaskEmitter{}
logger := newTaskLogger(emitter, logLevelDebug)
emitMessages(logger)
logEvents := convertEventsToLogEvents(t, emitter.Collect())
checkNumberOfEvents(t, logEvents, 6)
matchEvent(t, logEvents[0], logLevelDebug, debugMessage)
matchEvent(t, logEvents[1], logLevelDebug, debugMessage)
matchEvent(t, logEvents[2], logLevelInfo, infoMessage)
matchEvent(t, logEvents[3], logLevelInfo, infoMessage)
matchEvent(t, logEvents[4], logLevelWarning, warningMessage)
matchEvent(t, logEvents[5], logLevelWarning, warningMessage)
})
t.Run("info logger", func(t *testing.T) {
emitter := &CollectorTaskEmitter{}
logger := newTaskLogger(emitter, logLevelInfo)
emitMessages(logger)
logEvents := convertEventsToLogEvents(t, emitter.Collect())
checkNumberOfEvents(t, logEvents, 4)
matchEvent(t, logEvents[0], logLevelInfo, infoMessage)
matchEvent(t, logEvents[1], logLevelInfo, infoMessage)
matchEvent(t, logEvents[2], logLevelWarning, warningMessage)
matchEvent(t, logEvents[3], logLevelWarning, warningMessage)
})
t.Run("warn logger", func(t *testing.T) {
emitter := &CollectorTaskEmitter{}
logger := newTaskLogger(emitter, logLevelWarning)
emitMessages(logger)
logEvents := convertEventsToLogEvents(t, emitter.Collect())
checkNumberOfEvents(t, logEvents, 2)
matchEvent(t, logEvents[0], logLevelWarning, warningMessage)
matchEvent(t, logEvents[1], logLevelWarning, warningMessage)
})
}