-
Notifications
You must be signed in to change notification settings - Fork 95
/
logging_test.go
90 lines (77 loc) · 2.61 KB
/
logging_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
package main
import (
"fmt"
"regexp"
"testing"
)
type mockLogEntry struct {
Content string `json:"content"`
}
func (entry mockLogEntry) String() string {
return fmt.Sprintf("test %v", entry.Content)
}
func (mockLogEntry) eventType() string {
return "test"
}
func testLogging(t *testing.T, cfg *loggingConfig, log logEntry, expectedLogs *regexp.Regexp) {
t.Helper()
c := &config{
Logging: *cfg,
}
logBuffer := setupLogBuffer(t, c)
connContext{ConnMetadata: mockConnContext{}, cfg: c}.logEvent(log)
logs := logBuffer.String()
// Remove trailing newline
logs = logs[:len(logs)-1]
if !expectedLogs.MatchString(logs) {
t.Errorf("logs=%v, want match for %v", logs, expectedLogs)
}
}
func TestPlainWithTimestamps(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: false,
Timestamps: true,
}, mockLogEntry{"lorem"}, regexp.MustCompile(`^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} \[127\.0\.0\.1:1234\] test lorem$`))
}
func TestJSONWithTimestamps(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: true,
Timestamps: true,
}, mockLogEntry{"ipsum"}, regexp.MustCompile(`^{"time":"[^"]+","source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"ipsum"}}$`))
}
func TestPlainWithoutTimestamps(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: false,
Timestamps: false,
}, mockLogEntry{"dolor"}, regexp.MustCompile(`^\[127\.0\.0\.1:1234\] test dolor$`))
}
func TestJSONWithoutTimestamps(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: true,
Timestamps: false,
}, mockLogEntry{"sit"}, regexp.MustCompile(`^{"source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"sit"}}$`))
}
func TestPlainWithAddressSplitting(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: false,
SplitHostPort: true,
}, mockLogEntry{"amet"}, regexp.MustCompile(`^\[127\.0\.0\.1:1234\] test amet$`))
}
func TestJSONWithAddressSplitting(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: true,
SplitHostPort: true,
}, mockLogEntry{"consectetur"}, regexp.MustCompile(`^{"source":{"host":"127\.0\.0\.1","port":1234},"event_type":"test","event":{"content":"consectetur"}}$`))
}
func TestPlainWithoutAddressSplitting(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: false,
SplitHostPort: false,
}, mockLogEntry{"adipiscing"}, regexp.MustCompile(`^\[127\.0\.0\.1:1234\] test adipiscing$`))
}
func TestJSONWithoutAddressSplitting(t *testing.T) {
testLogging(t, &loggingConfig{
JSON: true,
SplitHostPort: false,
}, mockLogEntry{"elit"}, regexp.MustCompile(`^{"source":"127\.0\.0\.1:1234","event_type":"test","event":{"content":"elit"}}$`))
}