-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_test.go
134 lines (109 loc) · 4.1 KB
/
send_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package journald
import (
"fmt"
"math/rand"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
uniqueSmallMessageID = "1"
uniqueBigMessageID = "2"
uniquePrintedMessageID = "3"
uniqueLowerMessageID = "4"
)
func TestMain(m *testing.M) {
uniqueSmallMessageID = time.Now().String()
time.Sleep(time.Millisecond)
uniqueBigMessageID = time.Now().String()
time.Sleep(time.Millisecond)
uniquePrintedMessageID = time.Now().String()
time.Sleep(time.Millisecond)
uniqueLowerMessageID = time.Now().String()
os.Exit(m.Run())
}
func TestIsNotExist(t *testing.T) {
require.False(t, IsNotExist())
}
type Test struct {
Field1 string
Field2 int
Field3 map[string]float32
}
func TestSendSmallMessage(t *testing.T) {
s := Test{"field1", 2, map[string]float32{"3": 0.4, "5": 6.7}}
trace := runtime.ReadTrace()
err := Send("AnotherMessage", PriorityInfo, map[string]interface{}{"ONE": 1, "TWO": trace, "THREE": s, "FOUR": RandStringRunes(768)})
require.NoError(t, err)
}
func TestCheckPrint(t *testing.T) {
err := Print(PriorityNotice, "printed message: %s", uniquePrintedMessageID)
require.NoError(t, err)
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'MESSAGE=printed message: %s' -o verbose", uniquePrintedMessageID)).Output()
require.NoError(t, err)
require.True(t, strings.Contains(string(out), fmt.Sprintf("MESSAGE=printed message: %s", uniquePrintedMessageID)))
require.True(t, strings.Contains(string(out), "PRIORITY=5"))
}
func TestCheckSmallMessage(t *testing.T) {
err := Send("SmallMessage", PriorityWarning, map[string]interface{}{"TEST_ID": uniqueSmallMessageID, "WITH_NEWLINES": "b\n\nsd\n"})
require.NoError(t, err)
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'TEST_ID=%s' -o verbose", uniqueSmallMessageID)).Output()
require.NoError(t, err)
require.True(t, len(out) > 500)
require.True(t, strings.Contains(string(out), "WITH_NEWLINES"))
require.True(t, strings.Contains(string(out), "MESSAGE=SmallMessage"))
require.True(t, strings.Contains(string(out), "PRIORITY=4"))
out, err = exec.Command("sh", "-c", fmt.Sprintf("journalctl -o verbose -t 'TEST_ID=%s' -F WITH_NEWLINES", uniqueSmallMessageID)).Output()
require.NoError(t, err)
require.Equal(t, "b\n\nsd\n"+"\n", string(out))
}
func TestCheckBigMessage(t *testing.T) {
err := Send("BigMessage", PriorityErr, map[string]interface{}{"TEST_ID": uniqueBigMessageID, "BIG_MESSAGE": RandStringRunes(1024 * 512)})
require.NoError(t, err)
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'TEST_ID=%s' -o verbose", uniqueBigMessageID)).Output()
require.NoError(t, err)
require.True(t, len(out) > 512*1024)
require.True(t, strings.Contains(string(out), "BIG_MESSAGE"))
require.True(t, strings.Contains(string(out), "MESSAGE=BigMessage"))
require.True(t, strings.Contains(string(out), "PRIORITY=3"))
}
func TestCheckLowerMessage(t *testing.T) {
j := Journal{}
j.NormalizeFieldNameFn = strings.ToUpper
err := j.Send("LowerMessage", PriorityCrit, map[string]interface{}{"test_id": uniqueLowerMessageID})
require.NoError(t, err)
require.NoError(t, j.Close())
time.Sleep(time.Second * 5)
out, err := exec.Command("sh", "-c", fmt.Sprintf("journalctl 'TEST_ID=%s' -o verbose", uniqueLowerMessageID)).Output()
require.NoError(t, err)
require.True(t, strings.Contains(string(out), "MESSAGE=LowerMessage"))
require.True(t, strings.Contains(string(out), "PRIORITY=2"))
}
func TestSendClose(t *testing.T) {
j := Journal{}
err := j.Print(PriorityInfo, "SendClose")
require.NoError(t, err)
require.NoError(t, j.Close())
err = j.Print(PriorityInfo, "SendClose")
assertErr(t, err, " use of closed network connection")
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
func assertErr(t *testing.T, e error, rx interface{}) {
assert.Error(t, e)
assert.Regexp(t, rx, e.Error())
}