forked from rasky/journalhook
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjournalhook_test.go
95 lines (72 loc) · 2.59 KB
/
journalhook_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
package journalhook
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
)
var (
uniqueSmallMessageID = "1"
)
func TestMain(m *testing.M) {
uniqueSmallMessageID = time.Now().String()
os.Exit(m.Run())
}
type Test struct {
Field1 string
Field2 int
Field3 map[string]float32
}
func TestCheckSmallMessage(t *testing.T) {
log := logrus.New()
hook, err := NewJournalHook()
require.NoError(t, err)
log.Hooks.Add(hook)
log.WithFields(logrus.Fields{
"test_id": uniqueSmallMessageID,
"struct": Test{"field1", 2, map[string]float32{"3": 0.4, "5": 6.7}},
"bytes": []byte{'\n', 0xde, 0xad, 0xbe, 0xef},
}).Info("SmallMessage")
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'TEST_ID=%s' -o json", uniqueSmallMessageID)).Output()
fmt.Println(string(out))
require.NoError(t, err)
require.True(t, strings.Contains(string(out), "MESSAGE\" : \"SmallMessage"))
require.True(t, strings.Contains(string(out), "PRIORITY\" : \"6"))
require.True(t, strings.Contains(string(out), "STRUCT\" : \"{field1 2 map[3:0.4 5:6.7]}"))
require.True(t, strings.Contains(string(out), "BYTES\" : [ 10, 222, 173, 190, 239 ]"))
}
func TestCheckErrMessage(t *testing.T) {
log := logrus.New()
hook, err := NewJournalHookWithErrToMsg()
require.NoError(t, err)
log.Hooks.Add(hook)
err = errors.New("something something dark side")
log.WithField("test_id", uniqueSmallMessageID).WithError(err).Error()
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'TEST_ID=%s' -o json", uniqueSmallMessageID)).Output()
fmt.Println(string(out))
require.NoError(t, err)
require.True(t, strings.Contains(string(out), "MESSAGE\" : \"something something dark side"))
require.True(t, strings.Contains(string(out), "PRIORITY\" : \"3"))
}
func TestCheckErrMessageWithMessage(t *testing.T) {
log := logrus.New()
hook, err := NewJournalHookWithErrToMsg()
require.NoError(t, err)
log.Hooks.Add(hook)
err = errors.New("something something dark side")
log.WithField("test_id", uniqueSmallMessageID).WithError(err).Error("are we there yet")
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'TEST_ID=%s' -o json", uniqueSmallMessageID)).Output()
fmt.Println(string(out))
require.NoError(t, err)
require.True(t, strings.Contains(string(out), "ERROR\" : \"something something dark side"))
require.True(t, strings.Contains(string(out), "MESSAGE\" : \"are we there yet"))
require.True(t, strings.Contains(string(out), "PRIORITY\" : \"3"))
}