forked from spiffe/spire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
79 lines (66 loc) · 1.79 KB
/
logs.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
package spiretest
import (
"fmt"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
type LogEntry struct {
Level logrus.Level
Message string
Data logrus.Fields
}
func AssertLogs(t *testing.T, entries []*logrus.Entry, expected []LogEntry) {
for _, entry := range entries {
for key, field := range entry.Data {
entry.Data[key] = fmt.Sprint(field)
}
}
assert.Equal(t, expected, convertLogEntries(entries), "unexpected logs")
}
func AssertLogsAnyOrder(t *testing.T, entries []*logrus.Entry, expected []LogEntry) {
for _, entry := range entries {
for key, field := range entry.Data {
entry.Data[key] = fmt.Sprint(field)
}
}
assert.ElementsMatch(t, expected, convertLogEntries(entries), "unexpected logs")
}
func AssertLastLogs(t *testing.T, entries []*logrus.Entry, expected []LogEntry) {
for _, entry := range entries {
for key, field := range entry.Data {
entry.Data[key] = fmt.Sprint(field)
}
}
removeLen := len(entries) - len(expected)
if removeLen > 0 {
assert.Equal(t, expected, convertLogEntries(entries[removeLen:]), "unexpected logs")
return
}
assert.Equal(t, expected, convertLogEntries(entries), "unexpected logs")
}
func AssertLogsContainEntries(t *testing.T, entries []*logrus.Entry, expectedEntries []LogEntry) {
if len(expectedEntries) == 0 {
return
}
logEntries := convertLogEntries(entries)
for _, entry := range expectedEntries {
assert.Contains(t, logEntries, entry)
}
}
func convertLogEntries(entries []*logrus.Entry) (out []LogEntry) {
for _, entry := range entries {
out = append(out, LogEntry{
Level: entry.Level,
Message: entry.Message,
Data: fieldsOrNil(entry.Data),
})
}
return out
}
func fieldsOrNil(data logrus.Fields) logrus.Fields {
if len(data) > 0 {
return data
}
return nil
}