github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/daemon/logger/journald/read_test.go (about) 1 //go:build linux && cgo && !static_build && journald 2 // +build linux,cgo,!static_build,journald 3 4 package journald // import "github.com/docker/docker/daemon/logger/journald" 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/coreos/go-systemd/v22/journal" 11 "gotest.tools/v3/assert" 12 13 "github.com/docker/docker/daemon/logger" 14 "github.com/docker/docker/daemon/logger/journald/internal/fake" 15 "github.com/docker/docker/daemon/logger/loggertest" 16 ) 17 18 func TestLogRead(t *testing.T) { 19 r := loggertest.Reader{ 20 Factory: func(t *testing.T, info logger.Info) func(*testing.T) logger.Logger { 21 journalDir := t.TempDir() 22 23 // Fill the journal with irrelevant events which the 24 // LogReader needs to filter out. 25 rotatedJournal := fake.NewT(t, journalDir+"/rotated.journal") 26 rotatedJournal.AssignEventTimestampFromSyslogTimestamp = true 27 l, err := new(logger.Info{ 28 ContainerID: "wrongone0001", 29 ContainerName: "fake", 30 }) 31 assert.NilError(t, err) 32 l.sendToJournal = rotatedJournal.Send 33 assert.NilError(t, l.Log(&logger.Message{Source: "stdout", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stdout of a different container in a rotated journal file")})) 34 assert.NilError(t, l.Log(&logger.Message{Source: "stderr", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stderr of a different container in a rotated journal file")})) 35 assert.NilError(t, rotatedJournal.Send("a log message from a totally different process in a rotated journal", journal.PriInfo, nil)) 36 37 activeJournal := fake.NewT(t, journalDir+"/fake.journal") 38 activeJournal.AssignEventTimestampFromSyslogTimestamp = true 39 l, err = new(logger.Info{ 40 ContainerID: "wrongone0002", 41 ContainerName: "fake", 42 }) 43 assert.NilError(t, err) 44 l.sendToJournal = activeJournal.Send 45 assert.NilError(t, l.Log(&logger.Message{Source: "stdout", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stdout of a different container in the active journal file")})) 46 assert.NilError(t, l.Log(&logger.Message{Source: "stderr", Timestamp: time.Now().Add(-1 * 30 * time.Minute), Line: []byte("stderr of a different container in the active journal file")})) 47 assert.NilError(t, rotatedJournal.Send("a log message from a totally different process in the active journal", journal.PriInfo, nil)) 48 49 return func(t *testing.T) logger.Logger { 50 s := make(chan sendit, 100) 51 t.Cleanup(func() { close(s) }) 52 go func() { 53 for m := range s { 54 <-m.after 55 activeJournal.Send(m.message, m.priority, m.vars) 56 if m.sent != nil { 57 close(m.sent) 58 } 59 } 60 }() 61 l, err := new(info) 62 assert.NilError(t, err) 63 l.journalReadDir = journalDir 64 65 sl := &syncLogger{journald: l} 66 l.sendToJournal = func(message string, priority journal.Priority, vars map[string]string) error { 67 sent := make(chan struct{}) 68 s <- sendit{ 69 message: message, 70 priority: priority, 71 vars: vars, 72 after: time.After(150 * time.Millisecond), 73 sent: sent, 74 } 75 sl.waitOn = sent 76 return nil 77 } 78 l.readSyncTimeout = 3 * time.Second 79 return sl 80 } 81 }, 82 } 83 t.Run("Tail", r.TestTail) 84 t.Run("Follow", r.TestFollow) 85 } 86 87 type sendit struct { 88 message string 89 priority journal.Priority 90 vars map[string]string 91 after <-chan time.Time 92 sent chan<- struct{} 93 } 94 95 type syncLogger struct { 96 *journald 97 waitOn <-chan struct{} 98 } 99 100 func (l *syncLogger) Sync() error { 101 if l.waitOn != nil { 102 <-l.waitOn 103 } 104 return nil 105 }