Repository navigation
Expand file tree
/
Copy pathjournal_test.go
More file actions
115 lines (95 loc) · 3.51 KB
/
Copy pathjournal_test.go
File metadata and controls
115 lines (95 loc) · 3.51 KB
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
//go:build linux
package log
import (
"errors"
"net"
"path/filepath"
"sync"
"testing"
"time"
)
// journalctl -o verbose -f
func TestJournalWriter(t *testing.T) {
w := &JournalWriter{}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
}
_, _ = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
_, _ = wlprintf(w, InfoLevel, "a long long long long message.\n")
w.Close()
}
// TestJournalWriterConnectRetry checks that a bind failure is reported and
// the write after it retries the bind, instead of crashing on the missing
// socket or disabling the writer forever.
func TestJournalWriterConnectRetry(t *testing.T) {
sockname := filepath.Join(t.TempDir(), "journal.sock")
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: sockname, Net: "unixgram"})
if err != nil {
t.Fatalf("listen error: %+v", err)
}
defer conn.Close()
listen := listenUnixgram
defer func() { listenUnixgram = listen }()
w := &JournalWriter{JournalSocket: sockname}
msg := `{"time":"2019-07-10T05:35:54.277Z","level":"info","message":"hello journal writer"}` + "\n"
listenUnixgram = func(network string, laddr *net.UnixAddr) (*net.UnixConn, error) {
return nil, errors.New("bind failed")
}
// A failed bind must be reported, and the write after it must not
// dereference the missing socket.
for i := 0; i < 2; i++ {
if _, err := wlprintf(w, InfoLevel, "%s", msg); err == nil {
t.Fatalf("write %d with a failing bind = nil error, want an error", i)
}
}
listenUnixgram = listen
if _, err := wlprintf(w, InfoLevel, "%s", msg); err != nil {
t.Fatalf("write after the bind recovered = %v, want nil", err)
}
if err := conn.SetReadDeadline(time.Now().Add(5 * time.Second)); err != nil {
t.Fatal(err)
}
if _, _, err := conn.ReadFromUnix(make([]byte, 4096)); err != nil {
t.Fatalf("read the retried entry: %v", err)
}
if err := w.Close(); err != nil {
t.Fatalf("close: %v", err)
}
}
func TestJournalWriterError(t *testing.T) {
sockname := filepath.Join(t.TempDir(), "null.sock")
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: sockname, Net: "unixgram"})
if err != nil {
t.Errorf("listen error: %+v", err)
return
}
// Drain the socket while the writers below run. The goroutine must not call
// t.Logf and must be gone before the test returns: logging from a goroutine
// after its test has completed panics the whole test binary.
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
var data [512]byte
for {
buf := data[:]
if _, _, err := conn.ReadFromUnix(buf); err != nil {
return
}
time.Sleep(10 * time.Millisecond)
}
}()
defer func() {
conn.Close() // unblocks ReadFromUnix
wg.Wait()
}()
w := &JournalWriter{
JournalSocket: sockname,
}
for _, level := range []string{"trace", "debug", "info", "warning", "error", "fatal", "panic", "hahaha"} {
_, _ = wlprintf(w, ParseLevel(level), `{"time":"2019-07-10T05:35:54.277Z","level":"%s","caller":"test.go:42","error":"i am test error","foo":"bar","n":42,"message":"hello journal writer"}`+"\n", level)
}
_, _ = wlprintf(w, InfoLevel, `{"time":"2019-07-10T05:35:54.277Z","level":"error","msg":"a test message\n"}`+"\n")
_, _ = wlprintf(w, InfoLevel, "a long long long long message.\n")
w.Close()
}