...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package journal
16
17 import (
18 "fmt"
19 "io/ioutil"
20 "runtime"
21 "strconv"
22 "strings"
23 "testing"
24 )
25
26 func TestJournalEnabled(t *testing.T) {
27 enabled := Enabled()
28
29 if !enabled {
30 t.Fatalf("journald socket not detected")
31 }
32 }
33
34 func TestValidVarName(t *testing.T) {
35 validTestCases := []string{
36 "TEST",
37 "TE_ST",
38 "TEST_",
39 "0TEST0",
40 }
41 invalidTestCases := []string{
42 "test",
43 "_TEST",
44 "",
45 }
46
47 for _, tt := range validTestCases {
48 if err := validVarName(tt); err != nil {
49 t.Fatalf("\"%s\" should be a valid variable", tt)
50 }
51 }
52 for _, tt := range invalidTestCases {
53 if err := validVarName(tt); err == nil {
54 t.Fatalf("\"%s\" should be an invalid variable", tt)
55 }
56 }
57
58 }
59
60 func TestJournalSend(t *testing.T) {
61 if !Enabled() {
62 t.Skip("systemd journal not available locally")
63 }
64
65
66 hugeValue := 1234567890
67
68
69
70 largeValue := hugeValue
71 if wmem, err := ioutil.ReadFile("/proc/sys/net/core/wmem_default"); err == nil {
72 wmemStr := strings.TrimSpace(string(wmem))
73 if v, err := strconv.Atoi(wmemStr); err == nil {
74 largeValue = v + 1
75 }
76 }
77
78
79
80 testValues := []struct {
81 label string
82 len int
83 }{
84 {
85 "empty message",
86 0,
87 },
88 {
89 "small message",
90 5,
91 },
92 {
93 "large message",
94 largeValue,
95 },
96 {
97 "huge message",
98 hugeValue,
99 },
100 }
101
102
103 for i, tt := range testValues {
104 t.Logf("journal send test #%v - %s (len=%d)", i, tt.label, tt.len)
105 runtime.GC()
106 err := SendAlloc(i, tt.label, tt.len)
107 if err != nil {
108 t.Fatalf("#%v: failed sending %s: %s", i, tt.label, err)
109 }
110 runtime.GC()
111 }
112 }
113
114 func SendAlloc(run int, label string, len int) error {
115 largeVars := map[string]string{
116 "KEY": string(make([]byte, len)),
117 }
118
119 msg := fmt.Sprintf("go-systemd test #%v - %s", run, label)
120 return Send(msg, PriCrit, largeVars)
121 }
122
View as plain text